Salome HOME
3bc24f1ccee81a5aeaf4f8c2db99d444f84c0ee8
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_EllipticArc.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 #include <SketchPlugin_EllipticArc.h>
21 #include <SketchPlugin_Sketch.h>
22
23 #include <GeomAlgoAPI_EdgeBuilder.h>
24
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Ellipse.h>
28 #include <GeomAPI_Pnt2d.h>
29
30 #include <GeomDataAPI_Point2D.h>
31
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36
37 #include <cmath>
38
39 static const double tolerance = 1e-7;
40
41
42 SketchPlugin_EllipticArc::SketchPlugin_EllipticArc()
43 : SketchPlugin_SketchEntity()
44 {
45 }
46
47 void SketchPlugin_EllipticArc::initDerivedClassAttributes()
48 {
49   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
50   data()->addAttribute(FIRST_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
51   data()->addAttribute(SECOND_FOCUS_ID(), GeomDataAPI_Point2D::typeId());
52   data()->addAttribute(MAJOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
53   data()->addAttribute(MAJOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
54   data()->addAttribute(MINOR_AXIS_START_ID(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(MINOR_AXIS_END_ID(), GeomDataAPI_Point2D::typeId());
56   data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
58   data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
59   data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
60
61   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
62
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_FOCUS_ID());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_START_ID());
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_ID());
66   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_START_ID());
67   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_END_ID());
68   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_RADIUS_ID());
69   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_RADIUS_ID());
70
71   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
73 }
74
75 void SketchPlugin_EllipticArc::execute()
76 {
77   SketchPlugin_Sketch* aSketch = sketch();
78   if(!aSketch) {
79     return;
80   }
81
82   // Calculate all characteristics of the ellipse.
83   fillCharacteristicPoints();
84
85   // Make a visible ellipse.
86   createEllipticArc(aSketch);
87 }
88
89 bool SketchPlugin_EllipticArc::isFixed() {
90   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
91 }
92
93 void SketchPlugin_EllipticArc::attributeChanged(const std::string& theID) {
94   // the second condition for unability to move external segments anywhere
95   if (theID == EXTERNAL_ID() || isFixed()) {
96     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
97     if (!aSelection) {
98       // empty shape in selection shows that the shape is equal to context
99       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
100       if (anExtRes)
101         aSelection = anExtRes->shape();
102     }
103     // update arguments due to the selection value
104     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
105       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
106       std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
107
108       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
109           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
110       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
111
112       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
113           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
114       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
115
116       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
117           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_ID()));
118       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
119
120       std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
121           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_ID()));
122       aEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
123
124       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
125       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
126     }
127   }
128 }
129
130 bool SketchPlugin_EllipticArc::fillCharacteristicPoints()
131 {
132   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
133       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
134   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
135       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
136
137   AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
138
139   if (!aCenterAttr->isInitialized() ||
140       !aFocusAttr->isInitialized() ||
141       !aMinorRadiusAttr->isInitialized()) {
142     return false;
143   }
144
145   double aMinorRadius = aMinorRadiusAttr->value();
146   if (aMinorRadius < tolerance) {
147     return false;
148   }
149
150   data()->blockSendAttributeUpdated(true);
151   GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
152   GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
153   GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
154                                              aFocus2d->y() - aCenter2d->y()));
155   GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
156
157   AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
158   double aFocalDist = aCenter2d->distance(aFocus2d);
159   double aMajorRadius = sqrt(aFocalDist * aFocalDist + aMinorRadius * aMinorRadius);
160   aMajorRadiusAttr->setValue(aMajorRadius);
161
162   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
163     ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
164   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
165       ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
166                  aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
167   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
168       ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
169                  aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
170   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
171       ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
172                  aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
173   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
174       ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
175                  aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
176   data()->blockSendAttributeUpdated(false);
177
178   return true;
179 }
180
181 void SketchPlugin_EllipticArc::createEllipticArc(SketchPlugin_Sketch* theSketch)
182 {
183   // Compute a ellipse in 3D view.
184   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
185     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
186   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
187     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
188
189   double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
190   double aMinorRadius = real(MINOR_RADIUS_ID())->value();
191
192   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
193     theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
194
195   GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
196   GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
197   GeomDirPtr aNormal = aNDir->dir();
198   std::shared_ptr<GeomAPI_Shape> anEllipseShape;
199   if (aFocus->distance(aCenter) > tolerance) {
200     GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
201         aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
202
203     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
204         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_POINT_ID()));
205     std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
206         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_POINT_ID()));
207
208     GeomPointPtr aStartPnt(theSketch->to3D(aStartAttr->x(), aStartAttr->y()));
209     GeomPointPtr aEndPnt(theSketch->to3D(aEndAttr->x(), aEndAttr->y()));
210
211     anEllipseShape = GeomAlgoAPI_EdgeBuilder::ellipticArc(aCenter, aNormal, aMajorAxis,
212         aMajorRadius, aMinorRadius, aStartPnt, aEndPnt);
213   }
214   else {
215     // build circle instead of ellipse
216     anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
217   }
218
219   ResultConstructionPtr aResult = document()->createConstruction(data());
220   aResult->setShape(anEllipseShape);
221   aResult->setIsInHistory(false);
222   setResult(aResult);
223 }