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