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