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 GeomDir2dPtr aXAxis(new GeomAPI_Dir2d(theFocus->x() - theCenter->x(),
149 theFocus->y() - theCenter->y()));
150 std::shared_ptr<GeomAPI_XY> aPassedVec = thePassed->xy()->decreased(theCenter->xy());
152 double x = aPassedVec->dot(aXAxis->xy()) / theMajorRadius;
153 double y = abs(aPassedVec->cross(aXAxis->xy()));
154 theMinorRadius = y / sqrt(1.0 - x * x);
157 bool SketchPlugin_EllipticArc::fillCharacteristicPoints()
159 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
160 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
161 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
162 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
163 std::shared_ptr<GeomDataAPI_Point2D> aStartPointAttr =
164 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
166 if (!aCenterAttr->isInitialized() ||
167 !aFocusAttr->isInitialized() ||
168 !aStartPointAttr->isInitialized()) {
172 data()->blockSendAttributeUpdated(true);
173 GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
174 GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
175 GeomPnt2dPtr aStart2d = aStartPointAttr->pnt();
177 double aMajorRadius = 0.0, aMinorRadius = 0.0;
178 calculateRadii(aCenter2d, aFocus2d, aStart2d, aMajorRadius, aMinorRadius);
179 if (aMinorRadius < tolerance)
181 real(MAJOR_RADIUS_ID())->setValue(aMajorRadius);
182 real(MINOR_RADIUS_ID())->setValue(aMinorRadius);
184 GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
185 aFocus2d->y() - aCenter2d->y()));
186 GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
188 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
189 ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
190 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
191 ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
192 aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
193 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
194 ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
195 aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
196 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
197 ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
198 aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
199 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
200 ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
201 aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
203 std::shared_ptr<GeomDataAPI_Point2D> aEndPointAttr =
204 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
205 if (aEndPointAttr->isInitialized()) {
206 // recalculate REVERSED flag
207 std::shared_ptr<GeomAPI_Ellipse2d> anEllipseForArc(
208 new GeomAPI_Ellipse2d(aCenter2d, aMajorDir2d, aMajorRadius, aMinorRadius));
209 GeomPnt2dPtr anEnd = aEndPointAttr->pnt();
210 std::shared_ptr<GeomAPI_Pnt2d> aProjection = anEllipseForArc->project(anEnd);
211 double aParamStart = 0.0, aParamEnd = 0.0;
212 if (aProjection && anEnd->distance(aProjection) <= tolerance &&
213 anEllipseForArc->parameter(anEnd, paramTolerance, aParamEnd)) {
214 // do not recalculate REVERSED flag if the arc is not consistent
215 anEllipseForArc->parameter(aStart2d, paramTolerance, aParamStart);
216 aParamEnd -= aParamStart;
218 if (myParamDelta >= 0.0 && myParamDelta <= PI * 0.5 &&
219 aParamEnd < 0 && aParamEnd >= -PI * 0.5) {
220 boolean(REVERSED_ID())->setValue(true);
222 else if (myParamDelta <= 0.0 && myParamDelta >= -PI * 0.5 &&
223 aParamEnd > 0.0 && aParamEnd <= PI * 0.5) {
224 boolean(REVERSED_ID())->setValue(false);
227 myParamDelta = aParamEnd;
229 data()->blockSendAttributeUpdated(false);
234 void SketchPlugin_EllipticArc::createEllipticArc(SketchPlugin_Sketch* theSketch)
236 // Compute a ellipse in 3D view.
237 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
238 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
239 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
240 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
242 double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
243 double aMinorRadius = real(MINOR_RADIUS_ID())->value();
245 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
246 theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
248 GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
249 GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
250 GeomDirPtr aNormal = aNDir->dir();
251 std::shared_ptr<GeomAPI_Shape> anEllipseShape;
252 if (aFocus->distance(aCenter) > tolerance) {
253 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
254 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
256 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
257 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
258 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
259 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
261 GeomPointPtr aStartPnt(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
262 GeomPointPtr aEndPnt(theSketch->to3D(aEndAttr->x(), aEndAttr->y()));
263 if (boolean(REVERSED_ID())->value())
264 std::swap(aStartPnt, aEndPnt);
266 anEllipseShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxis,
267 aMajorRadius, aMinorRadius, aStartPnt, aEndPnt);
270 // build circle instead of ellipse
271 anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
274 ResultConstructionPtr aResult = document()->createConstruction(data());
275 aResult->setShape(anEllipseShape);
276 aResult->setIsInHistory(false);