1 // Copyright (C) 2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 // File: SketchPlugin_Ellipse.cpp
22 // Created: 26 April 2017
23 // Author: Artem ZHIDKOV
25 #include <SketchPlugin_Ellipse.h>
26 #include <SketchPlugin_Sketch.h>
28 #include <GeomAlgoAPI_EdgeBuilder.h>
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Ellipse.h>
31 #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(FOCUS_ID(), GeomDataAPI_Point2D::typeId());
49 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
50 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
52 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
56 void SketchPlugin_Ellipse::execute()
58 SketchPlugin_Sketch* aSketch = sketch();
63 // Compute a ellipse in 3D view.
64 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
65 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
66 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
67 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FOCUS_ID()));
68 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
69 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
70 if (!aCenterAttr->isInitialized() ||
71 !aFocusAttr->isInitialized() ||
72 !aMajorRadiusAttr->isInitialized() ||
73 !aMinorRadiusAttr->isInitialized()) {
77 double aMajorRadius = aMajorRadiusAttr->value();
78 double aMinorRadius = aMinorRadiusAttr->value();
79 if(aMajorRadius < tolerance || aMinorRadius < tolerance) {
83 // Make a visible point.
84 SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), 0);
86 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
87 aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
89 // Make a visible ellipse.
90 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
91 std::shared_ptr<GeomAPI_Pnt> aFocus(aSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
92 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
93 std::shared_ptr<GeomAPI_Dir> aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
94 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
96 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
97 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
99 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
100 aResult->setShape(anEllipseShape);
101 aResult->setIsInHistory(false);
102 setResult(aResult, 1);
105 bool SketchPlugin_Ellipse::isFixed() {
106 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
109 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
110 // the second condition for unability to move external segments anywhere
111 if (theID == EXTERNAL_ID() || isFixed()) {
112 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
114 // empty shape in selection shows that the shape is equal to context
115 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
117 aSelection = anExtRes->shape();
119 // update arguments due to the selection value
120 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
121 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
122 std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
124 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
125 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
126 aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
128 std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
129 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FOCUS_ID()));
130 aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
132 real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
133 real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());