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