Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Ellipse.cpp
1 // Copyright (C) 2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 // File:        SketchPlugin_Ellipse.cpp
22 // Created:     26 April 2017
23 // Author:      Artem ZHIDKOV
24
25 #include <SketchPlugin_Ellipse.h>
26 #include <SketchPlugin_Sketch.h>
27
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>
36
37 static const double tolerance = 1e-7;
38
39
40 SketchPlugin_Ellipse::SketchPlugin_Ellipse()
41 : SketchPlugin_SketchEntity()
42 {
43 }
44
45 void SketchPlugin_Ellipse::initDerivedClassAttributes()
46 {
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());
51
52   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
54 }
55
56 void SketchPlugin_Ellipse::execute()
57 {
58   SketchPlugin_Sketch* aSketch = sketch();
59   if(!aSketch) {
60     return;
61   }
62
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()) {
74     return;
75   }
76
77   double aMajorRadius = aMajorRadiusAttr->value();
78   double aMinorRadius = aMinorRadiusAttr->value();
79   if(aMajorRadius < tolerance || aMinorRadius < tolerance) {
80     return;
81   }
82
83   // Make a visible point.
84   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), 0);
85
86   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
87       aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
88
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()));
95
96   std::shared_ptr<GeomAPI_Shape> anEllipseShape =
97       GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
98
99   std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
100   aResult->setShape(anEllipseShape);
101   aResult->setIsInHistory(false);
102   setResult(aResult, 1);
103 }
104
105 void SketchPlugin_Ellipse::move(double theDeltaX, double theDeltaY)
106 {
107   std::shared_ptr<ModelAPI_Data> aData = data();
108   if(!aData->isValid()) {
109     return;
110   }
111
112   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
113       aData->attribute(CENTER_ID()));
114   if(aPoint->isInitialized()) {
115     aPoint->move(theDeltaX, theDeltaY);
116   }
117 }
118
119 bool SketchPlugin_Ellipse::isFixed() {
120   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
121 }
122
123 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
124   // the second condition for unability to move external segments anywhere
125   if (theID == EXTERNAL_ID() || isFixed()) {
126     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
127     if (!aSelection) {
128       // empty shape in selection shows that the shape is equal to context
129       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
130       if (anExtRes)
131         aSelection = anExtRes->shape();
132     }
133     // update arguments due to the selection value
134     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
135       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
136       std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
137
138       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
139           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
140       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
141
142       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
143           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FOCUS_ID()));
144       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
145
146       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
147       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
148     }
149   }
150 }