Salome HOME
9722877d1fa8f283bba2651e639c36307c9758c2
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_EllipticArc.cpp
1 // Copyright (C) 2017-2021  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchPlugin_EllipticArc.h>
21 #include <SketchPlugin_Sketch.h>
22
23 #include <GeomAlgoAPI_EdgeBuilder.h>
24
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Ellipse.h>
28 #include <GeomAPI_Ellipse2d.h>
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_XY.h>
31
32 #include <GeomDataAPI_Point2D.h>
33
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_ResultConstruction.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
38
39 #include <cmath>
40
41 static const double tolerance = 1e-7;
42 static const double paramTolerance = 1.e-4;
43 static const double PI = 3.141592653589793238463;
44
45
46 SketchPlugin_EllipticArc::SketchPlugin_EllipticArc()
47   : SketchPlugin_SketchEntity(),
48     myParamDelta(0.0)
49 {
50 }
51
52 void SketchPlugin_EllipticArc::initDerivedClassAttributes()
53 {
54   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(FIRST_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
56   data()->addAttribute(SECOND_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
57   data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
58   data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
59   data()->addAttribute(MINOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
60   data()->addAttribute(MINOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
61   data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
62   data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
63   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
64   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
65
66   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
67
68   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_FOCUS_ID());
69   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_START_ID());
70   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_ID());
71   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_START_ID());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_END_ID());
73   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_RADIUS_ID());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_RADIUS_ID());
75
76   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
77   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
78 }
79
80 void SketchPlugin_EllipticArc::execute()
81 {
82   SketchPlugin_Sketch* aSketch = sketch();
83   if(!aSketch) {
84     return;
85   }
86
87   // Calculate all characteristics of the ellipse.
88   fillCharacteristicPoints();
89
90   // Make a visible ellipse.
91   createEllipticArc(aSketch);
92 }
93
94 bool SketchPlugin_EllipticArc::isFixed() {
95   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
96 }
97
98 void SketchPlugin_EllipticArc::attributeChanged(const std::string& theID) {
99   // the second condition for unability to move external segments anywhere
100   if (theID == EXTERNAL_ID() || isFixed()) {
101     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
102     if (!aSelection) {
103       // empty shape in selection shows that the shape is equal to context
104       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
105       if (anExtRes)
106         aSelection = anExtRes->shape();
107     }
108     // update arguments due to the selection value
109     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
110       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelection));
111       std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
112
113       bool aWasBlocked = data()->blockSendAttributeUpdated(true);
114       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
115         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
116       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
117
118       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
119         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
120       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
121
122       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
123         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
124       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
125
126       std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
127         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
128       aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
129
130       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
131       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
132
133       double aStartParam, aMidParam, aEndParam;
134       anEllipse->parameter(anEdge->firstPoint(), tolerance, aStartParam);
135       anEllipse->parameter(anEdge->middlePoint(), tolerance, aMidParam);
136       anEllipse->parameter(anEdge->lastPoint(), tolerance, aEndParam);
137       if (aEndParam < aStartParam)
138         aEndParam += 2.0 * PI;
139       if (aMidParam < aStartParam)
140         aMidParam += 2.0 * PI;
141       boolean(REVERSED_ID())->setValue(aMidParam > aEndParam);
142
143       data()->blockSendAttributeUpdated(aWasBlocked, false);
144
145       fillCharacteristicPoints();
146     }
147   }
148   else if (theID == CENTER_ID() || theID == FIRST_FOCUS_ID() ||
149            theID == START_POINT_ID() || theID == END_POINT_ID())
150     fillCharacteristicPoints();
151   else if (theID == REVERSED_ID() && myParamDelta == 0.0)
152     myParamDelta = 2.0 * PI;
153 }
154
155 static void calculateRadii(const GeomPnt2dPtr& theCenter,
156                            const GeomPnt2dPtr& theFocus,
157                            const GeomPnt2dPtr& thePassed,
158                            double& theMajorRadius,
159                            double& theMinorRadius)
160 {
161   GeomPnt2dPtr aSecondFocus(new GeomAPI_Pnt2d(
162       theCenter->xy()->multiplied(2.0)->decreased(theFocus->xy())));
163   theMajorRadius = 0.5 * (thePassed->distance(theFocus) + thePassed->distance(aSecondFocus));
164
165   double aFocalDist = theCenter->distance(theFocus);
166   theMinorRadius = sqrt(theMajorRadius * theMajorRadius - aFocalDist * aFocalDist);
167 }
168
169 bool SketchPlugin_EllipticArc::fillCharacteristicPoints()
170 {
171   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
172       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
173   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
174       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
175   std::shared_ptr<GeomDataAPI_Point2D> aStartPointAttr =
176       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
177   std::shared_ptr<GeomDataAPI_Point2D> aEndPointAttr =
178       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
179
180   if (!aCenterAttr->isInitialized() ||
181       !aFocusAttr->isInitialized() ||
182       !aStartPointAttr->isInitialized()) {
183     return false;
184   }
185
186   GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
187   GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
188   GeomPnt2dPtr aStart2d = aStartPointAttr->pnt();
189
190   double aMajorRadius = 0.0, aMinorRadius = 0.0;
191   calculateRadii(aCenter2d, aFocus2d, aStart2d, aMajorRadius, aMinorRadius);
192   if (aMinorRadius < tolerance * aMajorRadius)
193     return false;
194
195   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
196   real(MAJOR_RADIUS_ID())->setValue(aMajorRadius);
197   real(MINOR_RADIUS_ID())->setValue(aMinorRadius);
198
199   GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
200     aFocus2d->y() - aCenter2d->y()));
201   GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
202
203   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
204     ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
205   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
206     ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
207                aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
208   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
209     ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
210                aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
211   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
212     ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
213                aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
214   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
215     ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
216                aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
217
218   if (aEndPointAttr->isInitialized()) {
219     // recalculate REVERSED flag
220     std::shared_ptr<GeomAPI_Ellipse2d> anEllipseForArc(
221         new GeomAPI_Ellipse2d(aCenter2d, aMajorDir2d, aMajorRadius, aMinorRadius));
222     GeomPnt2dPtr anEnd = aEndPointAttr->pnt();
223     std::shared_ptr<GeomAPI_Pnt2d> aProjection = anEllipseForArc->project(anEnd);
224     double aParamStart = 0.0, aParamEnd = 0.0;
225     if (aProjection && anEnd->distance(aProjection) <= tolerance &&
226         anEllipseForArc->parameter(anEnd, paramTolerance, aParamEnd)) {
227       // do not recalculate REVERSED flag if the arc is not consistent
228       anEllipseForArc->parameter(aStart2d, paramTolerance, aParamStart);
229       aParamEnd -= aParamStart;
230
231       if (myParamDelta >= 0.0 && myParamDelta <= PI * 0.5 &&
232           aParamEnd < 0.0 && aParamEnd >= -PI * 0.5) {
233         boolean(REVERSED_ID())->setValue(true);
234       }
235       else if (myParamDelta <= 0.0 && myParamDelta >= -PI * 0.5 &&
236                aParamEnd > 0.0 && aParamEnd <= PI * 0.5) {
237         boolean(REVERSED_ID())->setValue(false);
238       }
239       myParamDelta = aParamEnd;
240     }
241   }
242   data()->blockSendAttributeUpdated(aWasBlocked, false);
243
244   return true;
245 }
246
247 void SketchPlugin_EllipticArc::createEllipticArc(SketchPlugin_Sketch* theSketch)
248 {
249   // Compute a ellipse in 3D view.
250   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
251     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
252   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
253     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
254
255   double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
256   double aMinorRadius = real(MINOR_RADIUS_ID())->value();
257
258   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
259     theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
260
261   GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
262   GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
263   GeomDirPtr aNormal = aNDir->dir();
264   std::shared_ptr<GeomAPI_Shape> anEllipseShape;
265   if (aFocus->distance(aCenter) > tolerance) {
266     GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
267         aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
268
269     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
270         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
271     std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
272         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
273
274     GeomPointPtr aStartPnt(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
275     GeomPointPtr aEndPnt(theSketch->to3D(aEndAttr->x(), aEndAttr->y()));
276     if (boolean(REVERSED_ID())->value())
277       std::swap(aStartPnt, aEndPnt);
278
279     anEllipseShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxis,
280         aMajorRadius, aMinorRadius, aStartPnt, aEndPnt);
281   }
282   else {
283     // build circle instead of ellipse
284     anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
285   }
286
287   ResultConstructionPtr aResult = document()->createConstruction(data());
288   aResult->setShape(anEllipseShape);
289   aResult->setIsInHistory(false);
290   setResult(aResult);
291 }