Salome HOME
6e88c8191102c0c6e4c7fc1b578a6e81dc88a3a7
[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 #include <SketchPlugin_Ellipse.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_Ellipse::SketchPlugin_Ellipse()
43 : SketchPlugin_SketchEntity()
44 {
45 }
46
47 void SketchPlugin_Ellipse::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
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SECOND_FOCUS_ID());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_START_ID());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_AXIS_END_ID());
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_START_ID());
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MINOR_AXIS_END_ID());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), MAJOR_RADIUS_ID());
65
66   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
67   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
68 }
69
70 void SketchPlugin_Ellipse::execute()
71 {
72   SketchPlugin_Sketch* aSketch = sketch();
73   if(!aSketch) {
74     return;
75   }
76
77   // Calculate all characteristics of the ellipse.
78   fillCharacteristicPoints();
79
80   // Make visible points related to ellipse characteristics.
81   int aResultIndex = 0;
82   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, CENTER_ID(), aResultIndex++);
83   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, FIRST_FOCUS_ID(), aResultIndex++);
84   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, SECOND_FOCUS_ID(), aResultIndex++);
85   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MAJOR_AXIS_START_ID(), aResultIndex++);
86   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MAJOR_AXIS_END_ID(), aResultIndex++);
87   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MINOR_AXIS_START_ID(), aResultIndex++);
88   SketchPlugin_Sketch::createPoint2DResult(this, aSketch, MINOR_AXIS_END_ID(), aResultIndex++);
89
90   // Make auxiliary axes
91   SketchPlugin_Sketch::createLine2DResult(this, aSketch,
92       MAJOR_AXIS_START_ID(), MAJOR_AXIS_END_ID(), aResultIndex++);
93   SketchPlugin_Sketch::createLine2DResult(this, aSketch,
94       MINOR_AXIS_START_ID(), MINOR_AXIS_END_ID(), aResultIndex++);
95
96   // Mark already created results auxiliary
97   myAuxiliaryResults.clear();
98   const std::list<ResultPtr>& aResults = results();
99   std::list<ResultPtr>::const_iterator anIt = aResults.begin();
100   for (int anIndex = 0; anIt != aResults.end() && anIndex < aResultIndex; ++anIt, ++anIndex)
101     myAuxiliaryResults.insert(*anIt);
102
103   // Make a visible ellipse.
104   createEllipse(aSketch, aResultIndex);
105 }
106
107 bool SketchPlugin_Ellipse::isFixed() {
108   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
109 }
110
111 bool SketchPlugin_Ellipse::isAuxiliary(ResultPtr theResult)
112 {
113   return myAuxiliaryResults.find(theResult) != myAuxiliaryResults.end();
114 }
115
116 void SketchPlugin_Ellipse::attributeChanged(const std::string& theID) {
117   // the second condition for unability to move external segments anywhere
118   if (theID == EXTERNAL_ID() || isFixed()) {
119     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
120     if (!aSelection) {
121       // empty shape in selection shows that the shape is equal to context
122       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
123       if (anExtRes)
124         aSelection = anExtRes->shape();
125     }
126     // update arguments due to the selection value
127     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
128       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
129       std::shared_ptr<GeomAPI_Ellipse> anEllipse = anEdge->ellipse();
130
131       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
132           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
133       aCenterAttr->setValue(sketch()->to2D(anEllipse->center()));
134
135       std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
136           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_FOCUS_ID()));
137       aFocusAttr->setValue(sketch()->to2D(anEllipse->firstFocus()));
138
139       real(MAJOR_RADIUS_ID())->setValue(anEllipse->majorRadius());
140       real(MINOR_RADIUS_ID())->setValue(anEllipse->minorRadius());
141     }
142   }
143 }
144
145 bool SketchPlugin_Ellipse::fillCharacteristicPoints()
146 {
147   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
148       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
149   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
150       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
151
152   AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
153
154   if (!aCenterAttr->isInitialized() ||
155       !aFocusAttr->isInitialized() ||
156       !aMinorRadiusAttr->isInitialized()) {
157     return false;
158   }
159
160   double aMinorRadius = aMinorRadiusAttr->value();
161   if (aMinorRadius < tolerance) {
162     return false;
163   }
164
165   data()->blockSendAttributeUpdated(true);
166   GeomPnt2dPtr aCenter2d = aCenterAttr->pnt();
167   GeomPnt2dPtr aFocus2d = aFocusAttr->pnt();
168   GeomDir2dPtr aMajorDir2d(new GeomAPI_Dir2d(aFocus2d->x() - aCenter2d->x(),
169     aFocus2d->y() - aCenter2d->y()));
170   GeomDir2dPtr aMinorDir2d(new GeomAPI_Dir2d(-aMajorDir2d->y(), aMajorDir2d->x()));
171
172   AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
173   double aFocalDist = aCenter2d->distance(aFocus2d);
174   double aMajorRadius = sqrt(aFocalDist * aFocalDist + aMinorRadius * aMinorRadius);
175   aMajorRadiusAttr->setValue(aMajorRadius);
176
177   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_FOCUS_ID()))
178     ->setValue(2.0 * aCenter2d->x() - aFocus2d->x(), 2.0 * aCenter2d->y() - aFocus2d->y());
179   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_START_ID()))
180       ->setValue(aCenter2d->x() - aMajorDir2d->x() * aMajorRadius,
181                  aCenter2d->y() - aMajorDir2d->y() * aMajorRadius);
182   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MAJOR_AXIS_END_ID()))
183       ->setValue(aCenter2d->x() + aMajorDir2d->x() * aMajorRadius,
184                  aCenter2d->y() + aMajorDir2d->y() * aMajorRadius);
185   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_START_ID()))
186       ->setValue(aCenter2d->x() - aMinorDir2d->x() * aMinorRadius,
187                  aCenter2d->y() - aMinorDir2d->y() * aMinorRadius);
188   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(MINOR_AXIS_END_ID()))
189       ->setValue(aCenter2d->x() + aMinorDir2d->x() * aMinorRadius,
190                  aCenter2d->y() + aMinorDir2d->y() * aMinorRadius);
191   data()->blockSendAttributeUpdated(false);
192
193   return true;
194 }
195
196 void SketchPlugin_Ellipse::createEllipse(SketchPlugin_Sketch* theSketch, const int theResultIndex)
197 {
198   // Compute a ellipse in 3D view.
199   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
200     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
201   std::shared_ptr<GeomDataAPI_Point2D> aFocusAttr =
202     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(FIRST_FOCUS_ID()));
203
204   double aMajorRadius = real(MAJOR_RADIUS_ID())->value();
205   double aMinorRadius = real(MINOR_RADIUS_ID())->value();
206
207   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
208     theSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
209
210   GeomPointPtr aCenter(theSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
211   GeomPointPtr aFocus(theSketch->to3D(aFocusAttr->x(), aFocusAttr->y()));
212   GeomDirPtr aNormal = aNDir->dir();
213   std::shared_ptr<GeomAPI_Shape> anEllipseShape;
214   if (aFocus->distance(aCenter) > tolerance) {
215     GeomDirPtr aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
216         aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
217
218     anEllipseShape =
219         GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, aMajorRadius, aMinorRadius);
220   }
221   else {
222     // build circle instead of ellipse
223     anEllipseShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aMajorRadius);
224   }
225
226   ResultConstructionPtr aResult = document()->createConstruction(data(), theResultIndex);
227   aResult->setShape(anEllipseShape);
228   aResult->setIsInHistory(false);
229   setResult(aResult, theResultIndex);
230 }