Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[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,
75                                                                       sketch()->coordinatePlane(),
76                                                                       thePrevious);
77   return anAIS;
78 }
79
80 //*************************************************************************************
81 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
82 {
83   std::shared_ptr<ModelAPI_Data> aData = data();
84   if (!aData->isValid())
85     return;
86
87   // Recalculate a shift of flyout point in terms of local coordinates
88   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
89   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
90   std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
91       data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
92   std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
93       data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
94
95   std::shared_ptr<GeomAPI_XY> aStartPnt;
96   std::shared_ptr<GeomAPI_XY> aEndPnt;
97   if (aPointA && aPointB) {
98     aStartPnt = aPointA->pnt()->xy();
99     aEndPnt = aPointB->pnt()->xy();
100   } else if (aPointA) {
101     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
102         SketchPlugin_Constraint::ENTITY_B());
103     if (!aLine)
104       return;
105     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
106     aStartPnt = aPoint->xy();
107     aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
108   } else if (aPointB) {
109     FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
110         SketchPlugin_Constraint::ENTITY_A());
111     if (!aLine)
112       return;
113     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
114     aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
115     aEndPnt = aPoint->xy();
116   } else
117     return;
118
119   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
120   double dX = aDir->dot(aLineDir->xy());
121   double dY = -aDir->cross(aLineDir->xy());
122
123   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
125   myFlyoutUpdate = true;
126   if (aPoint->isInitialized()) {
127     aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
128   } else {
129     aPoint->setValue(dX, dY);
130   }
131   myFlyoutUpdate = false;
132 }
133
134 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
135 {
136   double aDistance = -1.;
137
138   std::shared_ptr<ModelAPI_Data> aData = data();
139   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
140   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
141     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
142   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
143       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
144
145   if (aPointA.get() && aPointB.get()) {  // both points
146     aDistance = aPointA->pnt()->distance(aPointB->pnt());
147   } else {
148     FeaturePtr aLineFeature;
149     std::shared_ptr<SketchPlugin_Line> aLine;
150     if (!aPointA.get() && aPointB.get()) {  //Line and point
151       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
152       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
153       if (aLine.get()) {
154         aDistance = aLine->distanceToPoint(aPointB->pnt());
155       }
156     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
157       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
158       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
159       if (aLine.get()) {
160         aDistance = aLine->distanceToPoint(aPointA->pnt());
161       }
162     }
163   }
164   return aDistance;
165 }
166
167 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
168 {
169   std::shared_ptr<ModelAPI_Data> aData = data();
170   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
171   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
172       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
173   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
174       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
175
176   if (!aPointA && !aPointB)
177     return false;
178   else if (aPointA || aPointB) {
179     FeaturePtr aLine;
180     if (!aPointA)
181       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
182     else if (!aPointB)
183       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
184     else // both points are initialized
185       return true;
186
187     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
188       return false;
189   }
190   return true;
191 }
192
193 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
194 {
195   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
196       theID == SketchPlugin_Constraint::ENTITY_B())
197   {
198     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
199         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
200     if (!aValueAttr->isInitialized()) {
201       // only if it is not initialized, try to compute the current value
202       double aDistance = calculateCurrentDistance();
203       if (aDistance > 0) { // set as value the length of updated references
204         aValueAttr->setValue(aDistance);
205       }
206     }
207   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
208     myFlyoutUpdate = true;
209     // Recalculate flyout point in local coordinates of the distance constraint:
210     // the X coordinate is a length of projection of the flyout point on the
211     //                  line binding two distanced points
212     //                  or a line of projection of the distanced point onto the distanced segment
213     // the Y coordinate is a distance from the flyout point to the line
214     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
215         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
216         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
217     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
218
219     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
220     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
221         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
222     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
223         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
224
225     std::shared_ptr<GeomAPI_XY> aStartPnt;
226     std::shared_ptr<GeomAPI_XY> aEndPnt;
227     if (aPointA && aPointB) {
228       aStartPnt = aPointA->pnt()->xy();
229       aEndPnt = aPointB->pnt()->xy();
230     } else if (aPointA) {
231       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
232           SketchPlugin_Constraint::ENTITY_B());
233       if (!aLine)
234         return;
235       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
236       aStartPnt = aPoint->xy();
237       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
238     } else if (aPointB) {
239       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
240           SketchPlugin_Constraint::ENTITY_A());
241       if (!aLine)
242         return;
243       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
244       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
245       aEndPnt = aPoint->xy();
246     } else
247       return;
248
249     if (aEndPnt->distance(aStartPnt) < tolerance)
250       return;
251
252     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
253     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
254
255     double X = aFlyoutDir->dot(aLineDir->xy());
256     double Y = -aFlyoutDir->cross(aLineDir->xy());
257     aFlyoutAttr->setValue(X, Y);
258     myFlyoutUpdate = false;
259   }
260 }
261