Salome HOME
Issue #2390: Revolution become invalid after changing parameter
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.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_Arc.h"
22 #include "SketchPlugin_Sketch.h"
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_ConstraintTangent.h>
25
26 #include <Events_Loop.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Tools.h>
37
38 #include <GeomAPI_Ax2.h>
39 #include <GeomAPI_Circ2d.h>
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_Dir2d.h>
42 #include <GeomAPI_Dir.h>
43 #include <GeomAPI_Lin2d.h>
44 #include <GeomAPI_Lin.h>
45 #include <GeomAPI_Pnt2d.h>
46 #include <GeomAPI_Vertex.h>
47 #include <GeomAPI_XY.h>
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Dir.h>
50 #include <GeomAlgoAPI_PointBuilder.h>
51 #include <GeomAlgoAPI_EdgeBuilder.h>
52 #include <GeomAlgoAPI_CompoundBuilder.h>
53 // for sqrt on Linux
54 #include <cmath>
55
56 const double tolerance = 1e-7;
57 const double paramTolerance = 1.e-4;
58 const double PI = 3.141592653589793238463;
59
60
61 SketchPlugin_Arc::SketchPlugin_Arc()
62 : SketchPlugin_SketchEntity()
63 {
64   myParamBefore = 0.0;
65 }
66
67 void SketchPlugin_Arc::initDerivedClassAttributes()
68 {
69   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
70   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
71   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
72
73   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
75
76   AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
77     data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
78
79   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
80   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
81
82   // set after all to avoid in attributeChanged reference to not existing attributes
83   if (!isReversed->isInitialized()) {
84     isReversed->setValue(false);
85   }
86 }
87
88 void SketchPlugin_Arc::execute()
89 {
90   SketchPlugin_Sketch* aSketch = sketch();
91   if(!aSketch) {
92     return;
93   }
94
95   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
96       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
97   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
98       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
99   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
100       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
101   if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
102     return;
103   }
104
105   // Make a visible point.
106   SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
107
108   // Make a visible arc.
109   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
110   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
111   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
112   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
113       aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
114   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
115
116   if (myParamBefore == 0) { // parameter has not been calculate yet
117     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
118         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
119     aCircleForArc->parameter(anEndAttr->pnt(), paramTolerance, myParamBefore);
120   }
121
122   bool isReversed = boolean(REVERSED_ID())->value();
123
124   GeomEdgePtr anArcShape;
125   if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
126     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
127     myParamBefore = 0;
128   } else {
129     anArcShape = isReversed ?
130       GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
131     : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
132   }
133
134   // calculate tolerances for start and end points of the arc and set them to the result shape
135   // (this is done to fix gaps which appear because of inaccurate computation of arcs in PlaneGCS,
136   // which leads to difference in SketchPlugin_Arc attributes and boundary points of result shape)
137   if (anArcShape) {
138     for (int ind = 0; ind < 2; ++ind) {
139       bool isFirst = ind == 0;
140       GeomPointPtr anArcBndPoint = isFirst == isReversed ? anEnd : aStart;
141       GeomPointPtr aShapePoint = isFirst ? anArcShape->firstPoint() : anArcShape->lastPoint();
142       double aDistance = anArcBndPoint->distance(aShapePoint);
143       // avoid setting too high tolerance because it may be caused by incomplete update of an arc
144       if (aDistance > tolerance && aDistance < 100. * tolerance) {
145         if (isFirst)
146           anArcShape->setFirstPointTolerance(aDistance);
147         else
148           anArcShape->setLastPointTolerance(aDistance);
149       }
150     }
151   }
152
153   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
154   aResult->setShape(anArcShape);
155   aResult->setIsInHistory(false);
156   setResult(aResult, 1);
157 }
158
159 bool SketchPlugin_Arc::isFixed()
160 {
161   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
162 }
163
164 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
165 {
166   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
167       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
168   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
169       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
170   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
171       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
172
173   // The second condition for unability to move external segments anywhere.
174   if(theID == EXTERNAL_ID() || isFixed()) {
175     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
176     if(!aSelection) {
177       // empty shape in selection shows that the shape is equal to context
178       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
179       if(anExtRes) {
180         aSelection = anExtRes->shape();
181       }
182     }
183     // update arguments due to the selection value
184     if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
185       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
186       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
187       if(aCirc.get()) {
188         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
189         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
190         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
191         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
192         data()->blockSendAttributeUpdated(aWasBlocked, false);
193
194         std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
195           std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
196                                                              aStartAttr->pnt()));
197
198         double anEndParam = 0.0;
199         aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
200         myParamBefore = anEndParam;
201
202         double aMidParam  = anEndParam / 2.0;
203         std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
204         aCircle2d->D0(aMidParam, aMidPnt2d);
205         std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
206         double aStartParam = 0.0;
207         aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
208         aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
209         aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
210
211         // adjust period
212         anEndParam -= aStartParam;
213         aMidParam -= aStartParam;
214         if (anEndParam < 0.0)
215           anEndParam += 2.0 * PI;
216         if (aMidParam < 0.0)
217           aMidParam += 2.0 * PI;
218
219         aWasBlocked = data()->blockSendAttributeUpdated(true);
220         if(aMidParam < anEndParam) {
221           setReversed(false);
222         } else {
223           setReversed(true);
224         }
225         data()->blockSendAttributeUpdated(aWasBlocked, false);
226       }
227     }
228   } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
229     if(!aCenterAttr->isInitialized()
230       || !aStartAttr->isInitialized()
231       || !anEndAttr->isInitialized()) {
232       return;
233     }
234     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
235     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
236     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
237     double aRadius = aCenter->distance(aStart);
238     if (aRadius < tolerance)
239       return;
240     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
241
242     // Do not recalculate REVERSED flag if the arc is not consistent
243     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
244     if (aProjection && anEnd->distance(aProjection) <= tolerance) {
245       double aParameterNew = 0.0;
246       if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
247         bool aWasBlocked = data()->blockSendAttributeUpdated(true);
248         if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
249           if(!boolean(REVERSED_ID())->value()) {
250             boolean(REVERSED_ID())->setValue(true);
251           }
252         } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
253           if(boolean(REVERSED_ID())->value()) {
254             boolean(REVERSED_ID())->setValue(false);
255           }
256         }
257         data()->blockSendAttributeUpdated(aWasBlocked, false);
258       }
259       if (fabs(aParameterNew) < paramTolerance ||
260           fabs(aParameterNew - 2.0 * PI) < paramTolerance)
261         aParameterNew = 2.0 * PI;
262       myParamBefore = aParameterNew;
263     }
264   }
265
266   double aRadius = 0;
267   double anAngle = 0;
268   if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
269     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
270     if(anEndAttr->isInitialized()) {
271       if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
272         anAngle = 360;
273       } else {
274         GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
275         double aStartParam, anEndParam;
276         aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
277         aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
278         anAngle = (anEndParam - aStartParam) / PI * 180.0;
279         if(isReversed()) anAngle = 360.0 - anAngle;
280       }
281     }
282   }
283
284   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
285   real(RADIUS_ID())->setValue(aRadius);
286   real(ANGLE_ID())->setValue(anAngle);
287   data()->blockSendAttributeUpdated(aWasBlocked, false);
288 }
289
290 void SketchPlugin_Arc::setReversed(bool isReversed)
291 {
292   boolean(REVERSED_ID())->setValue(isReversed);
293 }
294
295 bool SketchPlugin_Arc::isReversed()
296 {
297   return boolean(REVERSED_ID())->value();
298 }