Salome HOME
2.5. Select the point of a sketch entity must display the editing point panel
[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   // the value should to be computed here, not in the getAISObject in order to change the model value
69   // inside the object transaction. This is important for creating a constraint by preselection.
70   // The display of the presentation in this case happens after the transaction commit
71   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
72       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
73   if(!aFlyOutAttr->isInitialized())
74     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
75 }
76
77 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
78 {
79   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
80     return false;
81
82   if (!sketch())
83     return false;
84
85   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
86                            GeomDataAPI_Point2D>(attribute(theAttributeId));
87   if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
88     return false;
89
90   DataPtr aData = data();
91   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
92   std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
93       aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
94   std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
95       aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
96
97   std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
98   std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
99
100   if (aPoint_A && aPoint_B) {
101     aPnt_A = aPoint_A->pnt();
102     aPnt_B = aPoint_B->pnt();
103   } else if (!aPoint_A && aPoint_B) {
104     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
105         aData, SketchPlugin_Constraint::ENTITY_A());
106     if (aLine) {
107       aPnt_B = aPoint_B->pnt();
108       aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
109     }
110   } else if (aPoint_A && !aPoint_B) {
111     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(
112         aData, SketchPlugin_Constraint::ENTITY_B());
113     if (aLine) {
114       aPnt_A = aPoint_A->pnt();
115       aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
116     }
117   }
118   if (!aPnt_A || !aPnt_B)
119     return false;
120
121   std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
122   std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
123   // it is not possible to create lin2d on the points with equal position
124   if (aPoint1->distance(aPoint2) < tolerance)
125     return false;
126
127   std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
128   double aDist = aPoint1->distance(aPoint2)/5.;
129   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
130   aFlyOutAttr->setValue(aFPnt);
131
132   return true;
133 }
134
135 //*************************************************************************************
136 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
137 {
138   if (!sketch())
139     return thePrevious;
140
141   AISObjectPtr anAIS = thePrevious;
142   if (!anAIS) {
143     anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
144   }
145   return anAIS;
146 }
147
148 //*************************************************************************************
149 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
150 {
151   std::shared_ptr<ModelAPI_Data> aData = data();
152   if (!aData->isValid())
153     return;
154
155   // Recalculate a shift of flyout point in terms of local coordinates
156   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
157   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
158   std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
159       data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
160   std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
161       data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
162
163   std::shared_ptr<GeomAPI_XY> aStartPnt;
164   std::shared_ptr<GeomAPI_XY> aEndPnt;
165   if (aPointA && aPointB) {
166     aStartPnt = aPointA->pnt()->xy();
167     aEndPnt = aPointB->pnt()->xy();
168   } else if (aPointA) {
169     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
170         SketchPlugin_Constraint::ENTITY_B());
171     if (!aLine)
172       return;
173     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
174     aStartPnt = aPoint->xy();
175     aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
176   } else if (aPointB) {
177     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
178         SketchPlugin_Constraint::ENTITY_A());
179     if (!aLine)
180       return;
181     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
182     aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
183     aEndPnt = aPoint->xy();
184   } else
185     return;
186
187   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
188   double dX = aDir->dot(aLineDir->xy());
189   double dY = -aDir->cross(aLineDir->xy());
190
191   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193   myFlyoutUpdate = true;
194   aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
195   myFlyoutUpdate = false;
196 }
197
198 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
199 {
200   double aDistance = -1.;
201
202   std::shared_ptr<ModelAPI_Data> aData = data();
203   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
204   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
205     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
206   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
207       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
208
209   if (aPointA.get() && aPointB.get()) {  // both points
210     aDistance = aPointA->pnt()->distance(aPointB->pnt());
211   } else {
212     FeaturePtr aLineFeature;
213     std::shared_ptr<SketchPlugin_Line> aLine;
214     if (!aPointA.get() && aPointB.get()) {  //Line and point
215       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
216       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
217       if (aLine.get()) {
218         aDistance = aLine->distanceToPoint(aPointB->pnt());
219       }
220     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
221       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
222       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
223       if (aLine.get()) {
224         aDistance = aLine->distanceToPoint(aPointA->pnt());
225       }
226     }
227   }
228   return aDistance;
229 }
230
231 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
232 {
233   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
234       theID == SketchPlugin_Constraint::ENTITY_B()) 
235   {
236     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
237         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
238     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
239       double aDistance = calculateCurrentDistance();
240       if (aDistance > 0) { // set as value the length of updated references
241         aValueAttr->setValue(aDistance);
242       }
243     }
244   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
245     myFlyoutUpdate = true;
246     // Recalculate flyout point in local coordinates of the distance constraint:
247     // the X coordinate is a length of projection of the flyout point on the line binding two distanced points
248     //                  or a line of projection of the distanced point onto the distanced segment
249     // the Y coordinate is a distance from the flyout point to the line
250     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
251         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
253     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
254
255     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
256     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
257         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
258     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
259         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
260
261     std::shared_ptr<GeomAPI_XY> aStartPnt;
262     std::shared_ptr<GeomAPI_XY> aEndPnt;
263     if (aPointA && aPointB) {
264       aStartPnt = aPointA->pnt()->xy();
265       aEndPnt = aPointB->pnt()->xy();
266     } else if (aPointA) {
267       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
268           SketchPlugin_Constraint::ENTITY_B());
269       if (!aLine)
270         return;
271       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
272       aStartPnt = aPoint->xy();
273       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
274     } else if (aPointB) {
275       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
276           SketchPlugin_Constraint::ENTITY_A());
277       if (!aLine)
278         return;
279       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
280       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
281       aEndPnt = aPoint->xy();
282     } else
283       return;
284
285     if (aEndPnt->distance(aStartPnt) < tolerance)
286       return;
287
288     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
289     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
290
291     double X = aFlyoutDir->dot(aLineDir->xy());
292     double Y = -aFlyoutDir->cross(aLineDir->xy());
293     aFlyoutAttr->setValue(X, Y);
294     myFlyoutUpdate = false;
295   }
296 }
297