Salome HOME
Issue 1299 Angle presentation: complementary angle.
[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   //anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI;
194
195   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
196       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
197   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
198   switch (anAngleType) {
199     case SketcherPrs_Tools::ANGLE_DIRECT:
200     break;
201     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY:
202       anAngle = 180 - anAngle;
203     break;
204     case SketcherPrs_Tools::ANGLE_BACKWARD:
205       anAngle = 360 - anAngle;
206     break;
207     default:
208       break;
209   }
210   return anAngle;
211 }
212
213 void SketchPlugin_ConstraintAngle::updateAngleValue()
214 {
215   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
216     ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
217   double anAngle = aValueAttr->value();
218
219   std::shared_ptr<ModelAPI_Data> aData = data();
220   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
221       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
222   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
223   switch (anAngleType) {
224     case SketcherPrs_Tools::ANGLE_DIRECT:
225     break;
226     case SketcherPrs_Tools::ANGLE_SUPPLEMENTARY:
227       anAngle = 180 - anAngle;
228     break;
229     case SketcherPrs_Tools::ANGLE_BACKWARD:
230       anAngle = 360 - anAngle;
231     break;
232     default:
233       break;
234   }
235   aValueAttr = std::dynamic_pointer_cast<
236                   ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
237   aValueAttr->setValue(anAngle);
238 }
239
240 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
241 {
242   std::shared_ptr<ModelAPI_Data> aData = data();
243   if (!aData->isValid())
244     return;
245
246   myFlyoutUpdate = true;
247   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
248       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
249   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
250   myFlyoutUpdate = false;
251 }
252
253
254 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
255 {
256   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
257     return false;
258   if (!sketch())
259     return false;
260
261   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
262                            GeomDataAPI_Point2D>(attribute(theAttributeId));
263   if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
264     return false;
265
266   DataPtr aData = data();
267   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
268   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
269   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
270
271   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
272     return false;
273
274   // Start and end points of lines
275   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
276       aLineA->attribute(SketchPlugin_Line::START_ID()));
277   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
278       aLineA->attribute(SketchPlugin_Line::END_ID()));
279
280   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
281   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
282   if (aStartA->distance(aEndA) < tolerance)
283     return false;
284
285   myFlyoutUpdate = true;
286   double aX = (aStartA->x() + aEndA->x()) / 2.;
287   double aY = (aStartA->y() + aEndA->y()) / 2.;
288
289   aFlyOutAttr->setValue(aX, aY);
290   myFlyoutUpdate = false;
291
292   return true;
293 }
294
295
296 // ===============   Auxiliary functions   ==================================
297 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
298 {
299   // Start and end points of lines
300   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
301       theLine1->attribute(SketchPlugin_Line::START_ID()));
302   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
303       theLine1->attribute(SketchPlugin_Line::END_ID()));
304
305   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
306       theLine2->attribute(SketchPlugin_Line::START_ID()));
307   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
308       theLine2->attribute(SketchPlugin_Line::END_ID()));
309
310   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
311   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
312   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
313   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
314   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
315     std::shared_ptr<GeomAPI_Pnt2d>();
316
317   // Lines and their intersection point
318   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
319   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
320   return aLA->intersect(aLB);
321 }