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