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