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