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