Salome HOME
Hide scroll bar in SALOME
[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(), ModelAPI_AttributeDouble::typeId());
46   data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
47
48   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
49   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(), ModelAPI_AttributeBoolean::typeId());
50 }
51
52 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
53                                                    std::string& theDefault)
54 {
55   theSection = "Visualization";
56   theName = "sketch_dimension_color";
57   theDefault = SKETCH_DIMENSION_COLOR;
58 }
59
60 void SketchPlugin_ConstraintAngle::execute()
61 {
62   std::shared_ptr<ModelAPI_Data> aData = data();
63
64   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
65   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
66   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
67     return;
68
69   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
70       aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
71
72   if (!anAttrValue->isInitialized()) {
73     double anAngle = calculateAngle();
74     anAttrValue->setValue(anAngle);
75     updateConstraintValueByAngleValue();
76   }
77   // the value should to be computed here, not in the getAISObject in order to change the model value
78   // inside the object transaction. This is important for creating a constraint by preselection.
79   // The display of the presentation in this case happens after the transaction commit
80   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
81       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
82   if(!aFlyOutAttr->isInitialized())
83     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
84 }
85
86 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
87 {
88   if (!sketch())
89     return thePrevious;
90
91   AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
92                                                             thePrevious);
93   return anAIS;
94 }
95
96 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
97 {
98   std::shared_ptr<ModelAPI_Data> aData = data();
99   if (!aData)
100     return;
101   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
102   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
103   if (!aLineA || !aLineB)
104     return;
105
106   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
107       theID == SketchPlugin_Constraint::ENTITY_B()) {
108     AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
109     AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
110     // only if one of attributes is not initialized, try to compute the current value
111     if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
112       if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized()) // initialize base value of constraint
113         updateConstraintValueByAngleValue();
114       double anAngle = calculateAngle();
115       aValueAttr->setValue(anAngle);
116       updateConstraintValueByAngleValue();
117     }
118   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
119     // Recalculate flyout point in local coordinates
120     // coordinates are calculated according to the center of shapes intersection
121     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
122         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
123
124     std::shared_ptr<ModelAPI_Data> aData = data();
125     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
126     FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
127     FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
128
129     // Intersection of lines
130     std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
131     if (!anInter)
132       return;
133
134     myFlyoutUpdate = true;
135     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
136     if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
137       aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
138     myFlyoutUpdate = false;
139   }
140   else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
141     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
142       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
143     double anAngle = calculateAngle();
144     aValueAttr->setValue(anAngle);
145   }
146   else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
147     updateConstraintValueByAngleValue();
148   }
149 }
150
151 double SketchPlugin_ConstraintAngle::calculateAngle()
152 {
153   std::shared_ptr<ModelAPI_Data> aData = data();
154   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
155   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
156   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
157
158   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
159       aLineA->attribute(SketchPlugin_Line::START_ID()));
160   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161       aLineA->attribute(SketchPlugin_Line::END_ID()));
162   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
163       aLineB->attribute(SketchPlugin_Line::START_ID()));
164   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165       aLineB->attribute(SketchPlugin_Line::END_ID()));
166
167   std::shared_ptr<GeomAPI_Angle2d> anAng;
168   if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
169       !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
170     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
171         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
172   else {
173     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
174     bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
175     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
176     bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
177     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
178   }
179   double anAngle = anAng->angleDegree();
180   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
181       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
182   if (!aValueAttr->isInitialized())
183     aValueAttr->setValue(anAngle);
184   /// an angle value should be corrected by the current angle type
185   anAngle = getAngleForType(anAngle);
186   boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
187   boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
188   return anAngle;
189 }
190
191 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
192 {
193   double anAngle = theAngle;
194
195   std::shared_ptr<ModelAPI_Data> aData = data();
196   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
197       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
198   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
199   switch (anAngleType) {
200     case SketcherPrs_Tools::ANGLE_DIRECT:
201       anAngle = theAngle;
202     break;
203     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
204       if (theAngle > 180 || isPreviousValueObtuse)
205         anAngle = theAngle - 180.0;
206       else
207         anAngle = 180.0 - theAngle;
208       
209       if (anAngle < 0.0)
210         anAngle += 360.0;
211     }
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::updateConstraintValueByAngleValue()
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   aValueAttr = std::dynamic_pointer_cast<
230                   ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
231   anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
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 (aFlyOutAttr->isInitialized() &&
259       (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
260     return false;
261
262   DataPtr aData = data();
263   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
264   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
265   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
266
267   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
268     return false;
269
270   // Start and end points of lines
271   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272       aLineA->attribute(SketchPlugin_Line::START_ID()));
273   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
274       aLineA->attribute(SketchPlugin_Line::END_ID()));
275
276   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
277   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
278   if (aStartA->distance(aEndA) < tolerance)
279     return false;
280
281   myFlyoutUpdate = true;
282   double aX = (aStartA->x() + aEndA->x()) / 2.;
283   double aY = (aStartA->y() + aEndA->y()) / 2.;
284
285   aFlyOutAttr->setValue(aX, aY);
286   myFlyoutUpdate = false;
287
288   return true;
289 }
290
291
292 // ===============   Auxiliary functions   ==================================
293 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
294 {
295   // Start and end points of lines
296   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297       theLine1->attribute(SketchPlugin_Line::START_ID()));
298   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
299       theLine1->attribute(SketchPlugin_Line::END_ID()));
300
301   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
302       theLine2->attribute(SketchPlugin_Line::START_ID()));
303   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
304       theLine2->attribute(SketchPlugin_Line::END_ID()));
305
306   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
307   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
308   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
309   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
310   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
311     std::shared_ptr<GeomAPI_Pnt2d>();
312
313   // Lines and their intersection point
314   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
315   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
316   return aLA->intersect(aLB);
317 }