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