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