Salome HOME
Merge branch 'master' into Dev_1.1.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 <GeomAPI_Lin2d.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Data.h>
18
19 #include <Config_PropManager.h>
20
21 const double tolerance = 1e-7;
22
23 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
24 {
25 }
26
27 //*************************************************************************************
28 void SketchPlugin_ConstraintDistance::initAttributes()
29 {
30   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
31   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
34 }
35
36 //*************************************************************************************
37 void SketchPlugin_ConstraintDistance::execute()
38 {
39   std::shared_ptr<ModelAPI_Data> aData = data();
40   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
41       aData->attribute(SketchPlugin_Constraint::VALUE()));
42
43   if(anAttrValue->isInitialized())
44     return;
45
46   double aDistance = calculateCurrentDistance();
47   if(aDistance >= 0) {
48     anAttrValue->setValue(aDistance);
49   }
50
51   // the value should to be computed here, not in the getAISObject in order to change the model value
52   // inside the object transaction. This is important for creating a constraint by preselection.
53   // The display of the presentation in this case happens after the transaction commit
54   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
55       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
56   if(!aFlyOutAttr->isInitialized())
57     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
58 }
59
60 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
61 {
62   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
63     return false;
64
65   if (!sketch())
66     return false;
67
68   DataPtr aData = data();
69   std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
70       aData, SketchPlugin_Constraint::ENTITY_A());
71   std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
72       aData, SketchPlugin_Constraint::ENTITY_B());
73
74   std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
75   std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
76
77   if (aPoint_A && aPoint_B) {
78     aPnt_A = aPoint_A->pnt();
79     aPnt_B = aPoint_B->pnt();
80   } else if (!aPoint_A && aPoint_B) {
81     std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
82         aData, SketchPlugin_Constraint::ENTITY_A());
83     if (aLine) {
84       aPnt_B = aPoint_B->pnt();
85       aPnt_A = getProjectionPoint(aLine, aPnt_B);
86     }
87   } else if (aPoint_A && !aPoint_B) {
88     std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
89         aData, SketchPlugin_Constraint::ENTITY_B());
90     if (aLine) {
91       aPnt_A = aPoint_A->pnt();
92       aPnt_B = getProjectionPoint(aLine, aPnt_A);
93     }
94   }
95   if (!aPnt_A || !aPnt_B)
96     return false;
97
98   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
99                            GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
100
101   std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
102   std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
103   // it is not possible to create lin2d on the points with equal position
104   if (aPoint1->distance(aPoint1) < tolerance)
105     return false;
106
107   std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
108   double aDist = aPoint1->distance(aPoint2)/5.;
109   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
110   aFlyOutAttr->setValue(aFPnt);
111
112   return true;
113 }
114
115 //*************************************************************************************
116 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
117 {
118   if (!sketch())
119     return thePrevious;
120
121   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
122
123   DataPtr aData = data();
124   std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
125       aData, SketchPlugin_Constraint::ENTITY_A());
126   std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
127       aData, SketchPlugin_Constraint::ENTITY_B());
128
129   std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
130   std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
131
132   if (aPoint_A && aPoint_B) {
133     aPnt_A = aPoint_A->pnt();
134     aPnt_B = aPoint_B->pnt();
135   } else if (!aPoint_A && aPoint_B) {
136     std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
137         aData, SketchPlugin_Constraint::ENTITY_A());
138     if (aLine) {
139       aPnt_B = aPoint_B->pnt();
140       aPnt_A = getProjectionPoint(aLine, aPnt_B);
141     }
142   } else if (aPoint_A && !aPoint_B) {
143     std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
144         aData, SketchPlugin_Constraint::ENTITY_B());
145     if (aLine) {
146       aPnt_A = aPoint_A->pnt();
147       aPnt_B = getProjectionPoint(aLine, aPnt_A);
148     }
149   }
150   if (!aPnt_A || !aPnt_B)
151     return AISObjectPtr();
152
153   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
154       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
155
156   std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
157   std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
158   if(!aFlyOutAttr->isInitialized())
159     return AISObjectPtr();
160   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()/*aFPnt->x(), aFPnt->y()*/);
161
162   // value calculation
163   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
164       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
165   double aValue = 0;
166   // Issue #196: checking the positivity of the distance constraint
167   // there is a validator for a distance constraint, that the value should be positive
168   // in case if an invalid value is set, the current distance value is shown
169   if (aValueAttr->isInitialized())
170     aValue = aValueAttr->value();
171
172   AISObjectPtr anAIS = thePrevious;
173   if (!anAIS)
174     anAIS = AISObjectPtr(new GeomAPI_AISObject);
175   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
176
177   // Set color from preferences
178   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
179                                                     SKETCH_DIMENSION_COLOR);
180   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
181   return anAIS;
182 }
183
184 //*************************************************************************************
185 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
186 {
187   std::shared_ptr<ModelAPI_Data> aData = data();
188   if (!aData->isValid())
189     return;
190
191   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193   aPoint->move(theDeltaX, theDeltaY);
194 }
195
196 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
197 {
198   double aDistance = -1.;
199
200   std::shared_ptr<ModelAPI_Data> aData = data();
201   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
202     getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
203   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
204       getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
205
206   if (aPointA && aPointB) {  // both points
207     aDistance = aPointA->pnt()->distance(aPointB->pnt());
208   } else {
209     if (!aPointA && aPointB) {  //Line and point
210       std::shared_ptr<SketchPlugin_Line> aLine =
211           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
212       if (aLine) {
213         aDistance = aLine->distanceToPoint(aPointB->pnt());
214       }
215     } else if (aPointA && !aPointB) {  // Point and line
216       std::shared_ptr<SketchPlugin_Line> aLine =
217           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
218       if (aLine) {
219         aDistance = aLine->distanceToPoint(aPointA->pnt());
220       }
221     }
222   }
223   return aDistance;
224 }
225
226 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
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   }
239 }
240
241 //*************************************************************************************
242 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
243                                                        const std::string& theAttribute)
244 {
245   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
246
247   if (!theData)
248     return aPointAttr;
249
250   FeaturePtr aFeature;
251   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
252       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
253   if (anAttr)
254     aFeature = ModelAPI_Feature::feature(anAttr->object());
255
256   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
257     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
258         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
259   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
260     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
261         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
262   else if (anAttr->attr()) {
263     aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
264   }
265   return aPointAttr;
266 }
267
268 //*************************************************************************************
269 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
270                                                     const std::string& theAttribute)
271 {
272   std::shared_ptr<SketchPlugin_Line> aLine;
273   if (!theData)
274     return aLine;
275
276   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
277       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
278   if (anAttr) {
279     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
280     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
281       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
282     }
283   }
284   return aLine;
285 }
286
287 //*************************************************************************************
288 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
289     const std::shared_ptr<SketchPlugin_Line>& theLine,
290     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
291 {
292   std::shared_ptr<ModelAPI_Data> aData = theLine->data();
293   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
294       aData->attribute(SketchPlugin_Line::START_ID()));
295   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296       aData->attribute(SketchPlugin_Line::END_ID()));
297
298   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
299   return aLin2d.project(thePoint);
300 }