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_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>
37 static const double tolerance = 1e-7;
40 SketchPlugin_Ellipse::SketchPlugin_Ellipse()
41 : SketchPlugin_SketchEntity()
45 void SketchPlugin_Ellipse::initDerivedClassAttributes()
47 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
48 data()->addAttribute(FIRST_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
49 data()->addAttribute(SECOND_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
50 data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
51 data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
52 data()->addAttribute(MINOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
53 data()->addAttribute(MINOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
54 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
61 void SketchPlugin_Ellipse::execute()
63 SketchPlugin_Sketch* aSketch = sketch();
68 // Calculate all characteristics of the ellipse.
69 fillCharacteristicPoints();
71 // Make visible points related to ellipse characteristics.
73 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), aResultIndex++);
74 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, FIRST_FOCUS_ID(), aResultIndex++);
75 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, SECOND_FOCUS_ID(), aResultIndex++);
76 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MAJOR_AXIS_START_ID(), aResultIndex++);
77 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MAJOR_AXIS_END_ID(), aResultIndex++);
78 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MINOR_AXIS_START_ID(), aResultIndex++);
79 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MINOR_AXIS_END_ID(), aResultIndex++);
81 // Make auxiliary axes
82 SketchPlugin_Sketch::createLine2DResult(this, aSketch,
83 MAJOR_AXIS_START_ID(), MAJOR_AXIS_END_ID(), aResultIndex++);
84 SketchPlugin_Sketch::createLine2DResult(this, aSketch,
85 MINOR_AXIS_START_ID(), MINOR_AXIS_END_ID(), aResultIndex++);
87 // Mark already created results auxiliary
88 myAuxiliaryResults.clear();
89 const std::list<ResultPtr>& aResults = results();
90 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
91 for (int anIndex = 0; anIt != aResults.end() && anIndex < aResultIndex; ++anIt, ++anIndex)
92 myAuxiliaryResults.insert(*anIt);
94 // Make a visible ellipse.
95 createEllipse(aSketch, aResultIndex);
98 bool SketchPlugin_Ellipse::isFixed() {
99 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
102 bool SketchPlugin_Ellipse::isAuxiliary(ResultPtr theResult)
104 return myAuxiliaryResults.find(theResult) != myAuxiliaryResults.end();
107 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
108 // the second condition for unability to move external segments anywhere
109 if (theID == EXTERNAL_ID() || isFixed()) {
110 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
112 // empty shape in selection shows that the shape is equal to context
113 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
115 aSelection = anExtRes->shape();
117 // update arguments due to the selection value
118 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
119 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
120 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
122 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
123 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
124 aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
126 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
127 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
128 aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
130 real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
131 real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
136 bool SketchPlugin_Ellipse::fillCharacteristicPoints()
138 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
139 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
140 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
141 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
143 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
144 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
146 if (!aCenterAttr->isInitialized() ||
147 !aFocusAttr->isInitialized() ||
148 !aMajorRadiusAttr->isInitialized() ||
149 !aMinorRadiusAttr->isInitialized()) {
153 double aMajorRadius = aMajorRadiusAttr->value();
154 double aMinorRadius = aMinorRadiusAttr->value();
155 if (aMajorRadius < tolerance || aMinorRadius < tolerance) {
159 data()->blockSendAttributeUpdated(true);
160 GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
161 GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
162 GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
163 aFocus2d->y() - aCenter2d->y()));
164 GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
165 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
166 ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
167 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
168 ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
169 aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
170 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
171 ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
172 aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
173 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
174 ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
175 aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
176 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
177 ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
178 aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
179 data()->blockSendAttributeUpdated(false);
184 void SketchPlugin_Ellipse::createEllipse(SketchPlugin_Sketch* theSketch, const int theResultIndex)
186 // Compute a ellipse in 3D view.
187 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
188 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
189 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
190 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
192 double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
193 double aMinorRadius = real(MINOR_RADIUS_ID())->value();
195 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
196 theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
198 GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
199 GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
200 GeomDirPtr aNormal = aNDir->dir();
201 std::shared_ptr<GeomAPI_Shape> anEllipseShape;
202 if (aFocus->distance(aCenter) > tolerance) {
203 GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
204 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
207 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
210 // build circle instead of ellipse
211 anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
214 ResultConstructionPtr aResult = document()->createConstruction(data(), theResultIndex);
215 aResult->setShape(anEllipseShape);
216 aResult->setIsInHistory(false);
217 setResult(aResult, theResultIndex);