Salome HOME
Issue #2618: PlaneGCS errors processing
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintAngle.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_ConstraintAngle.h"
22 #include <SketchPlugin_Line.h>
23 #include <SketcherPrs_Tools.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomDataAPI_Point2D.h>
31
32 #include <GeomAPI_Angle2d.h>
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
37
38 #include <SketcherPrs_Factory.h>
39 #include <SketcherPrs_Tools.h>
40
41 #include <math.h>
42
43 const double tolerance = 1.e-7;
44 #define PI 3.1415926535897932
45
46 /// \brief Calculate intersection point of two lines
47 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
48
49
50 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
51 {
52   myFlyoutUpdate = false;
53 }
54
55 void SketchPlugin_ConstraintAngle::initAttributes()
56 {
57   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
60   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
61
62   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(),
63                        ModelAPI_AttributeDouble::typeId());
64   data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(),
65                        ModelAPI_AttributeInteger::typeId());
66
67   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(),
68                        ModelAPI_AttributeBoolean::typeId());
69   data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(),
70                        ModelAPI_AttributeBoolean::typeId());
71
72   data()->addAttribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID(),
73                        ModelAPI_AttributeInteger::typeId());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
75 }
76
77 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
78                                                    std::string& theDefault)
79 {
80   theSection = "Visualization";
81   theName = "sketch_dimension_color";
82   theDefault = SKETCH_DIMENSION_COLOR;
83 }
84
85 void SketchPlugin_ConstraintAngle::execute()
86 {
87   std::shared_ptr<ModelAPI_Data> aData = data();
88
89   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA =
90     aData->refattr(SketchPlugin_Constraint::ENTITY_A());
91   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB =
92     aData->refattr(SketchPlugin_Constraint::ENTITY_B());
93   if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
94     return;
95
96   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
97       aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
98
99   if (!anAttrValue->isInitialized()) {
100     double anAngle = calculateAngle();
101     anAttrValue->setValue(anAngle);
102     updateConstraintValueByAngleValue();
103   }
104   // the value should to be computed here, not in the
105   // getAISObject in order to change the model value
106   // inside the object transaction. This is important for creating a constraint by preselection.
107   // The display of the presentation in this case happens after the transaction commit
108   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
109       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
110   if(!aFlyOutAttr->isInitialized())
111     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
112 }
113
114 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
115 {
116   if (!sketch())
117     return thePrevious;
118
119   AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
120                                                             thePrevious);
121   return anAIS;
122 }
123
124 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
125 {
126   std::shared_ptr<ModelAPI_Data> aData = data();
127   if (!aData)
128     return;
129   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
130   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
131   if (!aLineA || !aLineB)
132     return;
133
134   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
135       theID == SketchPlugin_Constraint::ENTITY_B()) {
136     AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
137     AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
138     // only if one of attributes is not initialized, try to compute the current value
139     if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
140       if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized())
141         // initialize base value of constraint
142         updateConstraintValueByAngleValue();
143       double anAngle = calculateAngle();
144       aValueAttr->setValue(anAngle);
145       updateConstraintValueByAngleValue();
146     }
147   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
148     // Recalculate flyout point in local coordinates
149     // coordinates are calculated according to the center of shapes intersection
150     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
151       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
152       attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
153
154     std::shared_ptr<ModelAPI_Data> aData = data();
155     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
156     FeaturePtr aLineA =
157       SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
158     FeaturePtr aLineB =
159       SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
160
161     // Intersection of lines
162     std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
163     if (!anInter)
164       return;
165
166     myFlyoutUpdate = true;
167     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
168     if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
169       aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
170     myFlyoutUpdate = false;
171   }
172   else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
173     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
174       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
175     double anAngle = calculateAngle();
176     if (aValueAttr->text().empty())
177       aValueAttr->setValue(anAngle);
178     else {
179       aValueAttr = std::dynamic_pointer_cast<
180         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
181       aValueAttr->setValue(anAngle);
182     }
183   }
184   else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
185     updateConstraintValueByAngleValue();
186   }
187 }
188
189 double SketchPlugin_ConstraintAngle::calculateAngle()
190 {
191   std::shared_ptr<ModelAPI_Data> aData = data();
192   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
193   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
194   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
195
196   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197       aLineA->attribute(SketchPlugin_Line::START_ID()));
198   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199       aLineA->attribute(SketchPlugin_Line::END_ID()));
200   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201       aLineB->attribute(SketchPlugin_Line::START_ID()));
202   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203       aLineB->attribute(SketchPlugin_Line::END_ID()));
204
205   std::shared_ptr<GeomAPI_Angle2d> anAng;
206   if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
207       !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
208     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
209         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
210   else {
211     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
212     bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
213     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
214     bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
215     anAng = std::shared_ptr<GeomAPI_Angle2d>(
216       new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
217   }
218   double anAngle = anAng->angleDegree();
219   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
220       ModelAPI_AttributeDouble>(data()->attribute(VALUE()));
221   std::shared_ptr<ModelAPI_AttributeDouble> anAngleValueAttr = std::dynamic_pointer_cast<
222       ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID()));
223   if (!aValueAttr->isInitialized())
224     aValueAttr->setValue(anAngle);
225   /// an angle value should be corrected by the current angle type
226   anAngle = getAngleForType(anAngleValueAttr->text().empty() ?
227                             anAngle : anAngleValueAttr->value());
228   boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
229   boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
230   return anAngle;
231 }
232
233 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
234 {
235   double anAngle = theAngle;
236
237   std::shared_ptr<ModelAPI_Data> aData = data();
238   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
239       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
240   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
241   switch (anAngleType) {
242     case SketcherPrs_Tools::ANGLE_DIRECT:
243       anAngle = theAngle;
244     break;
245     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
246       if (theAngle > 180 || isPreviousValueObtuse)
247         anAngle = theAngle - 180.0;
248       else
249         anAngle = 180.0 - theAngle;
250
251       if (anAngle < 0.0)
252         anAngle += 360.0;
253     }
254     break;
255     case SketcherPrs_Tools::ANGLE_BACKWARD:
256       anAngle = 360.0 - theAngle;
257     break;
258     default:
259       break;
260   }
261   return anAngle;
262 }
263
264 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
265 {
266   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
267     ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
268   double anAngle = aValueAttr->value();
269
270   /// an angle value should be corrected by the current angle type
271   aValueAttr = std::dynamic_pointer_cast<
272                   ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
273   if (!aValueAttr->isInitialized())
274     calculateAngle();
275   anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
276   aValueAttr->setValue(anAngle);
277 }
278
279 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
280 {
281   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
282     return false;
283   if (!sketch())
284     return false;
285
286   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
287                            GeomDataAPI_Point2D>(attribute(theAttributeId));
288   if (aFlyOutAttr->isInitialized() &&
289       (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
290     return false;
291
292   DataPtr aData = data();
293   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
294   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
295   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
296
297   if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
298     return false;
299
300   // Start and end points of lines
301   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
302       aLineA->attribute(SketchPlugin_Line::START_ID()));
303   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
304       aLineA->attribute(SketchPlugin_Line::END_ID()));
305
306   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
307   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
308   if (aStartA->distance(aEndA) < tolerance)
309     return false;
310
311   myFlyoutUpdate = true;
312   double aX = (aStartA->x() + aEndA->x()) / 2.;
313   double aY = (aStartA->y() + aEndA->y()) / 2.;
314
315   aFlyOutAttr->setValue(aX, aY);
316   myFlyoutUpdate = false;
317
318   return true;
319 }
320
321
322 // ===============   Auxiliary functions   ==================================
323 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
324 {
325   // Start and end points of lines
326   std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
327       theLine1->attribute(SketchPlugin_Line::START_ID()));
328   std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
329       theLine1->attribute(SketchPlugin_Line::END_ID()));
330
331   std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
332       theLine2->attribute(SketchPlugin_Line::START_ID()));
333   std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
334       theLine2->attribute(SketchPlugin_Line::END_ID()));
335
336   std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
337   std::shared_ptr<GeomAPI_Pnt2d> aEndA   = aPointA2->pnt();
338   std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
339   std::shared_ptr<GeomAPI_Pnt2d> aEndB   = aPointB2->pnt();
340   if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
341     std::shared_ptr<GeomAPI_Pnt2d>();
342
343   // Lines and their intersection point
344   std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
345   std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
346   return aLA->intersect(aLB);
347 }