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