]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
Fixed validators for Boolean and Intersection
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintDistance.cpp
4 // Created: 23 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintDistance.h"
8 #include <SketchPlugin_Point.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
11
12 #include <SketcherPrs_Tools.h>
13 #include <SketcherPrs_Factory.h>
14
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_XY.h>
19 #include <GeomDataAPI_Point2D.h>
20
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_Data.h>
23
24 #include <Config_PropManager.h>
25
26 #include <math.h>
27
28 const double tolerance = 1e-7;
29
30
31 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
32 {
33   myFlyoutUpdate = false;
34 }
35
36 //*************************************************************************************
37 void SketchPlugin_ConstraintDistance::initAttributes()
38 {
39   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
40   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
41   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
42   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
43 }
44
45 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
46                                                       std::string& theDefault)
47 {
48   theSection = "Visualization";
49   theName = "sketch_dimension_color";
50   theDefault = SKETCH_DIMENSION_COLOR;
51 }
52
53 //*************************************************************************************
54 void SketchPlugin_ConstraintDistance::execute()
55 {
56   std::shared_ptr<ModelAPI_Data> aData = data();
57   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
58       aData->attribute(SketchPlugin_Constraint::VALUE()));
59
60   if(anAttrValue->isInitialized())
61     return;
62
63   double aDistance = calculateCurrentDistance();
64   if(aDistance >= 0)
65     anAttrValue->setValue(aDistance);
66 }
67
68 //*************************************************************************************
69 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
70 {
71   if (!sketch())
72     return thePrevious;
73
74   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane(),
75                                                                       thePrevious);
76   return anAIS;
77 }
78
79 //*************************************************************************************
80 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
81 {
82   std::shared_ptr<ModelAPI_Data> aData = data();
83   if (!aData->isValid())
84     return;
85
86   // Recalculate a shift of flyout point in terms of local coordinates
87   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
88   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
89   std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
90       data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
91   std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
92       data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
93
94   std::shared_ptr<GeomAPI_XY> aStartPnt;
95   std::shared_ptr<GeomAPI_XY> aEndPnt;
96   if (aPointA && aPointB) {
97     aStartPnt = aPointA->pnt()->xy();
98     aEndPnt = aPointB->pnt()->xy();
99   } else if (aPointA) {
100     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
101         SketchPlugin_Constraint::ENTITY_B());
102     if (!aLine)
103       return;
104     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
105     aStartPnt = aPoint->xy();
106     aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
107   } else if (aPointB) {
108     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
109         SketchPlugin_Constraint::ENTITY_A());
110     if (!aLine)
111       return;
112     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
113     aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
114     aEndPnt = aPoint->xy();
115   } else
116     return;
117
118   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
119   double dX = aDir->dot(aLineDir->xy());
120   double dY = -aDir->cross(aLineDir->xy());
121
122   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
124   myFlyoutUpdate = true;
125   aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
126   myFlyoutUpdate = false;
127 }
128
129 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
130 {
131   double aDistance = -1.;
132
133   std::shared_ptr<ModelAPI_Data> aData = data();
134   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
135   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
136     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
137   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
138       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
139
140   if (aPointA.get() && aPointB.get()) {  // both points
141     aDistance = aPointA->pnt()->distance(aPointB->pnt());
142   } else {
143     FeaturePtr aLineFeature;
144     std::shared_ptr<SketchPlugin_Line> aLine;
145     if (!aPointA.get() && aPointB.get()) {  //Line and point
146       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
147       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
148       if (aLine.get()) {
149         aDistance = aLine->distanceToPoint(aPointB->pnt());
150       }
151     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
152       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
153       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
154       if (aLine.get()) {
155         aDistance = aLine->distanceToPoint(aPointA->pnt());
156       }
157     }
158   }
159   return aDistance;
160 }
161
162 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
163 {
164   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
165       theID == SketchPlugin_Constraint::ENTITY_B()) 
166   {
167     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
168         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
169     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
170       double aDistance = calculateCurrentDistance();
171       if (aDistance > 0) { // set as value the length of updated references
172         aValueAttr->setValue(aDistance);
173       }
174     }
175   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
176     myFlyoutUpdate = true;
177     // Recalculate flyout point in local coordinates of the distance constraint:
178     // the X coordinate is a length of projection of the flyout point on the line binding two distanced points
179     //                  or a line of projection of the distanced point onto the distanced segment
180     // the Y coordinate is a distance from the flyout point to the line
181     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
182         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
183         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
184     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
185
186     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
187     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
188         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
189     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
190         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
191
192     std::shared_ptr<GeomAPI_XY> aStartPnt;
193     std::shared_ptr<GeomAPI_XY> aEndPnt;
194     if (aPointA && aPointB) {
195       aStartPnt = aPointA->pnt()->xy();
196       aEndPnt = aPointB->pnt()->xy();
197     } else if (aPointA) {
198       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
199           SketchPlugin_Constraint::ENTITY_B());
200       if (!aLine)
201         return;
202       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
203       aStartPnt = aPoint->xy();
204       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
205     } else if (aPointB) {
206       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
207           SketchPlugin_Constraint::ENTITY_A());
208       if (!aLine)
209         return;
210       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
211       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
212       aEndPnt = aPoint->xy();
213     } else
214       return;
215
216     if (aEndPnt->distance(aStartPnt) < tolerance)
217       return;
218
219     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
220     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
221
222     double X = aFlyoutDir->dot(aLineDir->xy());
223     double Y = -aFlyoutDir->cross(aLineDir->xy());
224     aFlyoutAttr->setValue(X, Y);
225     myFlyoutUpdate = false;
226   }
227 }
228