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 // File: SketchPlugin_Ellipse.cpp
21 // Created: 26 April 2017
22 // Author: Artem ZHIDKOV
24 #include <SketchPlugin_Ellipse.h>
25 #include <SketchPlugin_Sketch.h>
27 #include <GeomAlgoAPI_EdgeBuilder.h>
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Ellipse.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_ResultConstruction.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
36 static const double tolerance = 1e-7;
39 SketchPlugin_Ellipse::SketchPlugin_Ellipse()
40 : SketchPlugin_SketchEntity()
44 void SketchPlugin_Ellipse::initDerivedClassAttributes()
46 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
47 data()->addAttribute(FOCUS_ID(), GeomDataAPI_Point2D::typeId());
48 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
55 void SketchPlugin_Ellipse::execute()
57 SketchPlugin_Sketch* aSketch = sketch();
62 // Compute a ellipse in 3D view.
63 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
64 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
65 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
66 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FOCUS_ID()));
67 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
68 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
69 if (!aCenterAttr->isInitialized() ||
70 !aFocusAttr->isInitialized() ||
71 !aMajorRadiusAttr->isInitialized() ||
72 !aMinorRadiusAttr->isInitialized()) {
76 double aMajorRadius = aMajorRadiusAttr->value();
77 double aMinorRadius = aMinorRadiusAttr->value();
78 if(aMajorRadius < tolerance || aMinorRadius < tolerance) {
82 // Make a visible point.
83 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), 0);
85 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86 aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
88 // Make a visible ellipse.
89 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
90 std::shared_ptr<GeomAPI_Pnt> aFocus(aSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
91 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
92 std::shared_ptr<GeomAPI_Dir> aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
93 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
95 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
96 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
98 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
99 aResult->setShape(anEllipseShape);
100 aResult->setIsInHistory(false);
101 setResult(aResult, 1);
104 bool SketchPlugin_Ellipse::isFixed() {
105 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
108 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
109 // the second condition for unability to move external segments anywhere
110 if (theID == EXTERNAL_ID() || isFixed()) {
111 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
113 // empty shape in selection shows that the shape is equal to context
114 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
116 aSelection = anExtRes->shape();
118 // update arguments due to the selection value
119 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
120 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
121 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
123 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
124 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
125 aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
127 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
128 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FOCUS_ID()));
129 aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
131 real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
132 real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());