]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp
Salome HOME
Fixes for crashes and bad behavior on delete of features and Parts. For now delete...
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintAngle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintAngle.cpp
4 // Created: 19 August 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintAngle.h"
8 #include <SketchPlugin_Line.h>
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_AttributeInteger.h>
12
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_XY.h>
19
20 #include <SketcherPrs_Factory.h>
21 #include <SketcherPrs_Tools.h>
22
23 #include <math.h>
24
25 const double tolerance = 1.e-7;
26 #define PI 3.1415926535897932
27
28 /// \brief Calculate intersection point of two lines
29 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
30
31
32 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
33 {
34   myFlyoutUpdate = false;
35 }
36
37 void SketchPlugin_ConstraintAngle::initAttributes()
38 {
39   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
40   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
41   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
42   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
43
44   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(), ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
46 }
47
48 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
49                                                    std::string& theDefault)
50 {
51   theSection = "Visualization";
52   theName = "sketch_dimension_color";
53   theDefault = SKETCH_DIMENSION_COLOR;
54 }
55
56 void SketchPlugin_ConstraintAngle::execute()
57 {
58   std::shared_ptr<ModelAPI_Data> aData = data();
59
60   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
61   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
62   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
63     return;
64
65   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
66       aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
67
68   if (!anAttrValue->isInitialized()) {
69     double anAngle = calculateAngle();
70     anAttrValue->setValue(anAngle);
71     updateAngleValue();
72   }
73   // the value should to be computed here, not in the getAISObject in order to change the model value
74   // inside the object transaction. This is important for creating a constraint by preselection.
75   // The display of the presentation in this case happens after the transaction commit
76   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
77       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
78   if(!aFlyOutAttr->isInitialized())
79     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
80 }
81
82 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
83 {
84   if (!sketch())
85     return thePrevious;
86
87   AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
88                                                             thePrevious);
89   return anAIS;
90 }
91
92 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
93 {
94   std::shared_ptr<ModelAPI_Data> aData = data();
95   if (!aData)
96     return;
97   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
98   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
99   if (!aLineA || !aLineB)
100     return;
101
102   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
103       theID == SketchPlugin_Constraint::ENTITY_B()) {
104     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
105         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
106     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
107       double anAngle = calculateAngle();
108       aValueAttr->setValue(anAngle);
109       updateAngleValue();
110     }
111   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
112     // Recalculate flyout point in local coordinates
113     // coordinates are calculated according to the center of shapes intersection
114     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
115         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
116
117     std::shared_ptr<ModelAPI_Data> aData = data();
118     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
119     FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
120     FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
121
122     // Intersection of lines
123     std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
124     if (!anInter)
125       return;
126
127     myFlyoutUpdate = true;
128     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
129     if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
130       aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
131     myFlyoutUpdate = false;
132   }
133   else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
134     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
135       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
136     double anAngle = calculateAngle();
137     aValueAttr->setValue(anAngle);
138     updateAngleValue();
139   }
140   else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
141     updateAngleValue();
142   }
143 }
144
145 double SketchPlugin_ConstraintAngle::calculateAngle()
146 {
147   double anAngle = 0.0;
148
149   std::shared_ptr<ModelAPI_Data> aData = data();
150   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
151   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
152   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
153
154   // Intersection of lines
155   std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
156   if (!anInter)
157     return anAngle;
158
159   // Start and end points of lines
160   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161       aLineA->attribute(SketchPlugin_Line::START_ID()));
162   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
163       aLineA->attribute(SketchPlugin_Line::END_ID()));
164
165   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
166       aLineB->attribute(SketchPlugin_Line::START_ID()));
167   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168       aLineB->attribute(SketchPlugin_Line::END_ID()));
169
170   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
171   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
172   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
173   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
174
175   double aDist[2][2] = {
176       { anInter->distance(aStartA), anInter->distance(aEndA) },
177       { anInter->distance(aStartB), anInter->distance(aEndB) }
178   };
179
180   // Directions of lines
181   if (aDist[0][0] > aDist[0][1])
182     aEndA = aStartA;
183   if (aDist[1][0] > aDist[1][1])
184     aEndB = aStartB;
185
186   std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
187   std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
188
189   double aDirAngle = aDirA->angle(aDirB);
190   if (aDirAngle < 0)
191     aDirAngle += 2.0 * PI;
192   anAngle = fabs(aDirAngle) * 180.0 / PI;
193   /// an angle value should be corrected by the current angle type
194   anAngle = getAngleForType(anAngle);
195   return anAngle;
196 }
197
198 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle)
199 {
200   double anAngle = theAngle;
201
202   std::shared_ptr<ModelAPI_Data> aData = data();
203   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
204       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
205   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
206   switch (anAngleType) {
207     case SketcherPrs_Tools::ANGLE_DIRECT:
208       anAngle = theAngle;
209     break;
210     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
211       anAngle = fabs(180.0 - theAngle);
212     break;
213     case SketcherPrs_Tools::ANGLE_BACKWARD:
214       anAngle = 360.0 - theAngle;
215     break;
216     default:
217       break;
218   }
219   return anAngle;
220 }
221
222 void SketchPlugin_ConstraintAngle::updateAngleValue()
223 {
224   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
225     ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
226   double anAngle = aValueAttr->value();
227
228   /// an angle value should be corrected by the current angle type
229   anAngle = getAngleForType(anAngle);
230   aValueAttr = std::dynamic_pointer_cast<
231                   ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
232   aValueAttr->setValue(anAngle);
233 }
234
235 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
236 {
237   std::shared_ptr<ModelAPI_Data> aData = data();
238   if (!aData->isValid())
239     return;
240
241   myFlyoutUpdate = true;
242   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
243       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
244   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
245   myFlyoutUpdate = false;
246 }
247
248
249 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
250 {
251   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
252     return false;
253   if (!sketch())
254     return false;
255
256   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
257                            GeomDataAPI_Point2D>(attribute(theAttributeId));
258   if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
259     return false;
260
261   DataPtr aData = data();
262   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
263   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
264   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
265
266   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
267     return false;
268
269   // Start and end points of lines
270   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
271       aLineA->attribute(SketchPlugin_Line::START_ID()));
272   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273       aLineA->attribute(SketchPlugin_Line::END_ID()));
274
275   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
276   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
277   if (aStartA->distance(aEndA) < tolerance)
278     return false;
279
280   myFlyoutUpdate = true;
281   double aX = (aStartA->x() + aEndA->x()) / 2.;
282   double aY = (aStartA->y() + aEndA->y()) / 2.;
283
284   aFlyOutAttr->setValue(aX, aY);
285   myFlyoutUpdate = false;
286
287   return true;
288 }
289
290
291 // ===============   Auxiliary functions   ==================================
292 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
293 {
294   // Start and end points of lines
295   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
296       theLine1->attribute(SketchPlugin_Line::START_ID()));
297   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
298       theLine1->attribute(SketchPlugin_Line::END_ID()));
299
300   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
301       theLine2->attribute(SketchPlugin_Line::START_ID()));
302   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
303       theLine2->attribute(SketchPlugin_Line::END_ID()));
304
305   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
306   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
307   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
308   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
309   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
310     std::shared_ptr<GeomAPI_Pnt2d>();
311
312   // Lines and their intersection point
313   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
314   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
315   return aLA->intersect(aLB);
316 }