1 // Copyright (C) 2017-2023 CEA, EDF
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_Ellipse.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_Pnt2d.h>
30 #include <GeomDataAPI_Point2D.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
39 static const double tolerance = 1e-7;
42 SketchPlugin_Ellipse::SketchPlugin_Ellipse()
43 : SketchPlugin_SketchEntity()
47 void SketchPlugin_Ellipse::initDerivedClassAttributes()
49 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
50 data()->addAttribute(FIRST_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
51 data()->addAttribute(SECOND_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
52 data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
53 data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
54 data()->addAttribute(MINOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
55 data()->addAttribute(MINOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
56 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_FOCUS_ID());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_START_ID());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_ID());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_START_ID());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_END_ID());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_RADIUS_ID());
66 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
70 void SketchPlugin_Ellipse::execute()
72 SketchPlugin_Sketch* aSketch = sketch();
77 // Calculate all characteristics of the ellipse.
78 fillCharacteristicPoints();
80 // Make a visible ellipse.
81 createEllipse(aSketch, 0);
84 bool SketchPlugin_Ellipse::isFixed() {
85 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
88 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
89 // the second condition for unability to move external segments anywhere
90 if (theID == EXTERNAL_ID() || isFixed()) {
91 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
93 // empty shape in selection shows that the shape is equal to context
94 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
96 aSelection = anExtRes->shape();
98 // update arguments due to the selection value
99 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
100 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
101 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
103 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
104 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
105 aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
107 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
108 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
109 aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
111 real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
112 real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
117 bool SketchPlugin_Ellipse::fillCharacteristicPoints()
119 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
120 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
121 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
122 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
124 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
126 if (!aCenterAttr->isInitialized() ||
127 !aFocusAttr->isInitialized() ||
128 !aMinorRadiusAttr->isInitialized()) {
132 double aMinorRadius = aMinorRadiusAttr->value();
133 if (aMinorRadius < tolerance) {
137 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
138 GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
139 GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
140 GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
141 aFocus2d->y() - aCenter2d->y()));
142 GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
144 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
145 double aFocalDist = aCenter2d->distance(aFocus2d);
146 double aMajorRadius = sqrt(aFocalDist * aFocalDist + aMinorRadius * aMinorRadius);
147 if (!aMajorRadiusAttr->isInitialized() ||
148 fabs(aMajorRadiusAttr->value() - aMajorRadius) > tolerance)
149 aMajorRadiusAttr->setValue(aMajorRadius);
151 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
152 ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
153 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
154 ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
155 aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
156 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
157 ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
158 aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
159 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
160 ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
161 aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
162 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
163 ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
164 aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
165 data()->blockSendAttributeUpdated(aWasBlocked);
170 void SketchPlugin_Ellipse::createEllipse(SketchPlugin_Sketch* theSketch, const int theResultIndex)
172 // Compute a ellipse in 3D view.
173 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
174 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
175 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
176 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
178 double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
179 double aMinorRadius = real(MINOR_RADIUS_ID())->value();
181 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
182 theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
184 GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
185 GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
186 GeomDirPtr aNormal = aNDir->dir();
187 std::shared_ptr<GeomAPI_Shape> anEllipseShape;
188 if (aFocus->distance(aCenter) > tolerance) {
189 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
190 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
193 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
196 // build circle instead of ellipse
197 anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
200 ResultConstructionPtr aResult = document()->createConstruction(data(), theResultIndex);
201 aResult->setShape(anEllipseShape);
202 aResult->setIsInHistory(false);
203 setResult(aResult, theResultIndex);