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