Salome HOME
Merge remote-tracking branch 'remotes/origin/Filters_Development_2'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Ellipse.cpp
1 // Copyright (C) 2017-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File:        SketchPlugin_Ellipse.cpp
21 // Created:     26 April 2017
22 // Author:      Artem ZHIDKOV
23
24 #include <SketchPlugin_Ellipse.h>
25 #include <SketchPlugin_Sketch.h>
26
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>
35
36 static const double tolerance = 1e-7;
37
38
39 SketchPlugin_Ellipse::SketchPlugin_Ellipse()
40 : SketchPlugin_SketchEntity()
41 {
42 }
43
44 void SketchPlugin_Ellipse::initDerivedClassAttributes()
45 {
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());
50
51   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
53 }
54
55 void SketchPlugin_Ellipse::execute()
56 {
57   SketchPlugin_Sketch* aSketch = sketch();
58   if(!aSketch) {
59     return;
60   }
61
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()) {
73     return;
74   }
75
76   double aMajorRadius = aMajorRadiusAttr->value();
77   double aMinorRadius = aMinorRadiusAttr->value();
78   if(aMajorRadius < tolerance || aMinorRadius < tolerance) {
79     return;
80   }
81
82   // Make a visible point.
83   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), 0);
84
85   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86       aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
87
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()));
94
95   std::shared_ptr<GeomAPI_Shape> anEllipseShape =
96       GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
97
98   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
99   aResult->setShape(anEllipseShape);
100   aResult->setIsInHistory(false);
101   setResult(aResult, 1);
102 }
103
104 bool SketchPlugin_Ellipse::isFixed() {
105   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
106 }
107
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();
112     if (!aSelection) {
113       // empty shape in selection shows that the shape is equal to context
114       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
115       if (anExtRes)
116         aSelection = anExtRes->shape();
117     }
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();
122
123       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
124           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
125       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
126
127       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
128           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FOCUS_ID()));
129       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
130
131       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
132       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
133     }
134   }
135 }