1 // Copyright (C) 2017-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <SketchPlugin_EllipticArc.h>
21 #include <SketchPlugin_Sketch.h>
23 #include <GeomAlgoAPI_EdgeBuilder.h>
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>
32 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_ResultConstruction.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
41 static const double tolerance = 1e-7;
42 static const double paramTolerance = 1.e-4;
43 static const double PI = 3.141592653589793238463;
46 SketchPlugin_EllipticArc::SketchPlugin_EllipticArc()
47 : SketchPlugin_SketchEntity(),
52 void SketchPlugin_EllipticArc::initDerivedClassAttributes()
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());
66 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
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());
76 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
77 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
80 void SketchPlugin_EllipticArc::execute()
82 SketchPlugin_Sketch* aSketch = sketch();
87 // Calculate all characteristics of the ellipse.
88 fillCharacteristicPoints();
90 // Make a visible ellipse.
91 createEllipticArc(aSketch);
94 bool SketchPlugin_EllipticArc::isFixed() {
95 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
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();
103 // empty shape in selection shows that the shape is equal to context
104 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
106 aSelection = anExtRes->shape();
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();
113 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
114 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
115 aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
117 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
119 aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
121 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
122 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
123 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
125 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
127 aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
129 real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
130 real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
133 else if (theID == CENTER_ID() || theID == FIRST_FOCUS_ID() ||
134 theID == START_POINT_ID() || theID == END_POINT_ID())
135 fillCharacteristicPoints();
138 static void calculateRadii(const GeomPnt2dPtr& theCenter,
139 const GeomPnt2dPtr& theFocus,
140 const GeomPnt2dPtr& thePassed,
141 double& theMajorRadius,
142 double& theMinorRadius)
144 GeomPnt2dPtr aSecondFocus(new GeomAPI_Pnt2d(
145 theCenter->xy()->multiplied(2.0)->decreased(theFocus->xy())));
146 theMajorRadius = 0.5 * (thePassed->distance(theFocus) + thePassed->distance(aSecondFocus));
148 double aFocalDist = theCenter->distance(theFocus);
149 theMinorRadius = sqrt(theMajorRadius * theMajorRadius - aFocalDist * aFocalDist);
152 bool SketchPlugin_EllipticArc::fillCharacteristicPoints()
154 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
155 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
156 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
157 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
158 std::shared_ptr<GeomDataAPI_Point2D> aStartPointAttr =
159 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
160 std::shared_ptr<GeomDataAPI_Point2D> aEndPointAttr =
161 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
163 if (!aCenterAttr->isInitialized() ||
164 !aFocusAttr->isInitialized() ||
165 !aStartPointAttr->isInitialized()) {
169 data()->blockSendAttributeUpdated(true);
170 GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
171 GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
172 GeomPnt2dPtr aStart2d = aStartPointAttr->pnt();
174 double aMajorRadius = 0.0, aMinorRadius = 0.0;
175 calculateRadii(aCenter2d, aFocus2d, aStart2d, aMajorRadius, aMinorRadius);
176 if (aMinorRadius < tolerance *aMajorRadius)
178 real(MAJOR_RADIUS_ID())->setValue(aMajorRadius);
179 real(MINOR_RADIUS_ID())->setValue(aMinorRadius);
181 GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
182 aFocus2d->y() - aCenter2d->y()));
183 GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
185 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
186 ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
187 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
188 ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
189 aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
190 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
191 ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
192 aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
193 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
194 ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
195 aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
196 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
197 ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
198 aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
200 if (aEndPointAttr->isInitialized()) {
201 // recalculate REVERSED flag
202 std::shared_ptr<GeomAPI_Ellipse2d> anEllipseForArc(
203 new GeomAPI_Ellipse2d(aCenter2d, aMajorDir2d, aMajorRadius, aMinorRadius));
204 GeomPnt2dPtr anEnd = aEndPointAttr->pnt();
205 std::shared_ptr<GeomAPI_Pnt2d> aProjection = anEllipseForArc->project(anEnd);
206 double aParamStart = 0.0, aParamEnd = 0.0;
207 if (aProjection && anEnd->distance(aProjection) <= tolerance &&
208 anEllipseForArc->parameter(anEnd, paramTolerance, aParamEnd)) {
209 // do not recalculate REVERSED flag if the arc is not consistent
210 anEllipseForArc->parameter(aStart2d, paramTolerance, aParamStart);
211 aParamEnd -= aParamStart;
213 if (myParamDelta >= 0.0 && myParamDelta <= PI * 0.5 &&
214 aParamEnd < 0 && aParamEnd >= -PI * 0.5) {
215 boolean(REVERSED_ID())->setValue(true);
217 else if (myParamDelta <= 0.0 && myParamDelta >= -PI * 0.5 &&
218 aParamEnd > 0.0 && aParamEnd <= PI * 0.5) {
219 boolean(REVERSED_ID())->setValue(false);
222 myParamDelta = aParamEnd;
224 data()->blockSendAttributeUpdated(false);
229 void SketchPlugin_EllipticArc::createEllipticArc(SketchPlugin_Sketch* theSketch)
231 // Compute a ellipse in 3D view.
232 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
233 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
234 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
235 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
237 double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
238 double aMinorRadius = real(MINOR_RADIUS_ID())->value();
240 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
241 theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
243 GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
244 GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
245 GeomDirPtr aNormal = aNDir->dir();
246 std::shared_ptr<GeomAPI_Shape> anEllipseShape;
247 if (aFocus->distance(aCenter) > tolerance) {
248 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
249 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
251 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
252 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
253 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
254 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
256 GeomPointPtr aStartPnt(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
257 GeomPointPtr aEndPnt(theSketch->to3D(aEndAttr->x(), aEndAttr->y()));
258 if (boolean(REVERSED_ID())->value())
259 std::swap(aStartPnt, aEndPnt);
261 anEllipseShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxis,
262 aMajorRadius, aMinorRadius, aStartPnt, aEndPnt);
265 // build circle instead of ellipse
266 anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
269 ResultConstructionPtr aResult = document()->createConstruction(data());
270 aResult->setShape(anEllipseShape);
271 aResult->setIsInHistory(false);