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