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