Salome HOME
Simplified calculation of fly out distance and creation of constraints limited only...
[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 <GeomDataAPI_Point2D.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Lin2d.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_XY.h>
17
18 #include <SketcherPrs_Factory.h>
19 #include <SketcherPrs_Tools.h>
20
21 #include <math.h>
22
23 const double tolerance = 1.e-7;
24 #define PI 3.1415926535897932
25
26 /// \brief Calculate intersection point of two lines
27 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
28
29
30 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
31 {
32   myFlyoutUpdate = false;
33 }
34
35 void SketchPlugin_ConstraintAngle::initAttributes()
36 {
37   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
38   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
39   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
40   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
41 }
42
43 void SketchPlugin_ConstraintAngle::execute()
44 {
45   std::shared_ptr<ModelAPI_Data> aData = data();
46
47   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
48   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
49   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
50     return;
51
52   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
53       aData->attribute(SketchPlugin_Constraint::VALUE()));
54
55   if (!anAttrValue->isInitialized()) {
56     double anAngle = calculateAngle();
57     anAttrValue->setValue(anAngle);
58   }
59   // the value should to be computed here, not in the getAISObject in order to change the model value
60   // inside the object transaction. This is important for creating a constraint by preselection.
61   // The display of the presentation in this case happens after the transaction commit
62   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
63       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
64   if(!aFlyOutAttr->isInitialized())
65     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
66 }
67
68 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
69 {
70   if (!sketch())
71     return thePrevious;
72
73   AISObjectPtr anAIS = thePrevious;
74   if (!anAIS) {
75     anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
76   }
77
78   // Set color from preferences
79   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
80                                                     SKETCH_DIMENSION_COLOR);
81   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
82   return anAIS;
83 }
84
85 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
86 {
87   std::shared_ptr<ModelAPI_Data> aData = data();
88   if (!aData)
89     return;
90   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
91   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
92   if (!aLineA || !aLineB)
93     return;
94
95   if (theID == SketchPlugin_Constraint::ENTITY_A() || 
96       theID == SketchPlugin_Constraint::ENTITY_B()) {
97     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
98         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
99     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
100       double anAngle = calculateAngle();
101       aValueAttr->setValue(anAngle);
102     }
103   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
104     // Recalculate flyout point in local coordinates
105     // coordinates are calculated according to the center of shapes intersection
106     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
107         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
108
109     std::shared_ptr<ModelAPI_Data> aData = data();
110     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
111     FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
112     FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
113
114     // Intersection of lines
115     std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
116     if (!anInter)
117       return;
118
119     myFlyoutUpdate = true;
120     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
121     if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
122       aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
123     myFlyoutUpdate = false;
124   }
125 }
126
127 double SketchPlugin_ConstraintAngle::calculateAngle()
128 {
129   double anAngle = 0.0;
130
131   std::shared_ptr<ModelAPI_Data> aData = data();
132   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
133   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
134   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
135
136   // Intersection of lines
137   std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
138   if (!anInter)
139     return anAngle;
140
141   // Start and end points of lines
142   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143       aLineA->attribute(SketchPlugin_Line::START_ID()));
144   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145       aLineA->attribute(SketchPlugin_Line::END_ID()));
146
147   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148       aLineB->attribute(SketchPlugin_Line::START_ID()));
149   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150       aLineB->attribute(SketchPlugin_Line::END_ID()));
151
152   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
153   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
154   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
155   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
156
157   double aDist[2][2] = {
158       { anInter->distance(aStartA), anInter->distance(aEndA) },
159       { anInter->distance(aStartB), anInter->distance(aEndB) }
160   };
161
162   // Directions of lines
163   if (aDist[0][1] < tolerance)
164     aEndA = aStartA;
165   if (aDist[1][1] < tolerance)
166     aEndB = aStartB;
167   std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
168   std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
169
170   anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI;
171
172   // If the lines intersected inside one of them, the angle selected is less than 90 degrees
173   if ((aDist[0][0] >= tolerance && aDist[0][1] >= tolerance &&
174       aDist[0][0] + aDist[0][1] < aStartA->distance(aEndA) + 2.0 * tolerance) ||
175       (aDist[1][0] >= tolerance && aDist[1][1] >= tolerance &&
176       aDist[1][0] + aDist[1][1] < aStartB->distance(aEndB) + 2.0 * tolerance)) {
177     if (anAngle > 90.0)
178       anAngle = 180.0 - anAngle;
179   }
180
181   return anAngle;
182 }
183
184 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
185 {
186   std::shared_ptr<ModelAPI_Data> aData = data();
187   if (!aData->isValid())
188     return;
189
190   myFlyoutUpdate = true;
191   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
192       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193   aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
194   myFlyoutUpdate = false;
195 }
196
197
198 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
199 {
200   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
201     return false;
202   if (!sketch())
203     return false;
204
205   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
206                            GeomDataAPI_Point2D>(attribute(theAttributeId));
207   if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
208     return false;
209
210   DataPtr aData = data();
211   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
212   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
213   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
214
215   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
216     return false;
217
218   // Start and end points of lines
219   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
220       aLineA->attribute(SketchPlugin_Line::START_ID()));
221   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
222       aLineA->attribute(SketchPlugin_Line::END_ID()));
223
224   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
225   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
226   if (aStartA->distance(aEndA) < tolerance)
227     return false;
228
229   myFlyoutUpdate = true;
230   double aX = (aStartA->x() + aEndA->x()) / 2.;
231   double aY = (aStartA->y() + aEndA->y()) / 2.;
232
233   aFlyOutAttr->setValue(aX, aY);
234   myFlyoutUpdate = false;
235
236   return true;
237 }
238
239
240 // ===============   Auxiliary functions   ==================================
241 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
242 {
243   // Start and end points of lines
244   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
245       theLine1->attribute(SketchPlugin_Line::START_ID()));
246   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247       theLine1->attribute(SketchPlugin_Line::END_ID()));
248
249   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
250       theLine2->attribute(SketchPlugin_Line::START_ID()));
251   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
252       theLine2->attribute(SketchPlugin_Line::END_ID()));
253
254   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
255   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
256   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
257   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
258   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
259     std::shared_ptr<GeomAPI_Pnt2d>();
260
261   // Lines and their intersection point
262   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
263   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
264   return aLA->intersect(aLB);
265 }