]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_MacroEllipse.cpp
Salome HOME
e833775e204e281e037e7eb4a0e1486f0439d681
[modules/shaper.git] / src / SketchAPI / SketchAPI_MacroEllipse.cpp
1 // Copyright (C) 2014-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 "SketchAPI_MacroEllipse.h"
21 #include "SketchAPI_Line.h"
22 #include "SketchAPI_Point.h"
23
24 #include <GeomAPI_Pnt2d.h>
25
26 #include <ModelHighAPI_RefAttr.h>
27 #include <ModelHighAPI_Tools.h>
28
29 #include <SketchPlugin_Sketch.h>
30
31 #define MAJOR_AXIS_NEGATIVE (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
32                              feature()->attribute(SketchPlugin_MacroEllipse::FIRST_POINT_ID())))
33 #define MAJOR_AXIS_POSITIVE (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
34                              feature()->attribute(SketchPlugin_MacroEllipse::SECOND_POINT_ID())))
35 #define PASSED_POINT (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
36                       feature()->attribute(SketchPlugin_MacroEllipse::PASSED_POINT_ID())))
37
38 #define MAJOR_AXIS_NEGATIVE_REF (feature()->refattr( \
39                                  SketchPlugin_MacroEllipse::FIRST_POINT_REF_ID()))
40 #define MAJOR_AXIS_POSITIVE_REF (feature()->refattr( \
41                                  SketchPlugin_MacroEllipse::SECOND_POINT_REF_ID()))
42 #define PASSED_POINT_REF (feature()->refattr(SketchPlugin_MacroEllipse::PASSED_POINT_REF_ID()))
43
44
45 // find a parent sketch
46 static CompositeFeaturePtr sketch(FeaturePtr theFeature)
47 {
48   CompositeFeaturePtr aSketch;
49   const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
50   for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt)
51     if ((*anIt)->id() == SketchPlugin_Sketch::FEATURES_ID()) {
52       aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
53       break;
54     }
55   return aSketch;
56 }
57
58
59 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
60                                                bool callInitialize)
61 : SketchAPI_SketchEntity(theFeature)
62 {
63   if (callInitialize && initialize())
64     mySketch = sketch(theFeature);
65 }
66
67 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
68                                                double theX1, double theY1,
69                                                double theX2, double theY2,
70                                                double theX3, double theY3,
71                                                bool byCenter)
72   : SketchAPI_SketchEntity(theFeature)
73 {
74   if (initialize()) {
75     if (byCenter)
76       setByCenterAndPassedPoints();
77     else
78       setByMajorAxisAndPassedPoint();
79
80     initializePoints(theX1, theY1, theX2, theY2, theX3, theY3);
81   }
82 }
83
84 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
85                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
86                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
87                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3,
88                                                bool byCenter)
89   : SketchAPI_SketchEntity(theFeature)
90 {
91   if (initialize()) {
92     if (byCenter)
93       setByCenterAndPassedPoints();
94     else
95       setByMajorAxisAndPassedPoint();
96
97     initializePoints(thePoint1, thePoint2, thePoint3);
98   }
99 }
100
101 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
102                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
103                                                const ModelHighAPI_RefAttr&           thePoint1Ref,
104                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
105                                                const ModelHighAPI_RefAttr&           thePoint2Ref,
106                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3,
107                                                const ModelHighAPI_RefAttr&           thePoint3Ref,
108                                                bool byCenter)
109   : SketchAPI_SketchEntity(theFeature)
110 {
111   if (initialize()) {
112     if (byCenter)
113       setByCenterAndPassedPoints();
114     else
115       setByMajorAxisAndPassedPoint();
116
117     initializePoints(thePoint1, thePoint1Ref, thePoint2, thePoint2Ref, thePoint3, thePoint3Ref);
118   }
119 }
120
121 SketchAPI_MacroEllipse::~SketchAPI_MacroEllipse()
122 {
123 }
124
125 void SketchAPI_MacroEllipse::setByCenterAndPassedPoints()
126 {
127   fillAttribute(SketchPlugin_MacroEllipse::ELLIPSE_TYPE_BY_CENTER_AXIS_POINT(), myellipseType);
128 }
129
130 void SketchAPI_MacroEllipse::setByMajorAxisAndPassedPoint()
131 {
132   fillAttribute(SketchPlugin_MacroEllipse::ELLIPSE_TYPE_BY_AXIS_AND_POINT(), myellipseType);
133 }
134
135 void SketchAPI_MacroEllipse::initializePoints(double theX1, double theY1,
136                                               double theX2, double theY2,
137                                               double theX3, double theY3)
138 {
139   fillAttribute(MAJOR_AXIS_NEGATIVE, theX1, theY1);
140   fillAttribute(MAJOR_AXIS_POSITIVE, theX2, theY2);
141   fillAttribute(PASSED_POINT, theX3, theY3);
142
143   mySketch = sketch(feature());
144   execute();
145 }
146
147 void SketchAPI_MacroEllipse::initializePoints(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
148                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
149                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
150 {
151   fillAttribute(thePoint1, MAJOR_AXIS_NEGATIVE);
152   fillAttribute(thePoint2, MAJOR_AXIS_POSITIVE);
153   fillAttribute(thePoint3, PASSED_POINT);
154
155   mySketch = sketch(feature());
156   execute();
157 }
158
159 static void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
160                           const ModelHighAPI_RefAttr& thePointRef,
161                           std::shared_ptr<GeomDataAPI_Point2D> thePointAttr,
162                           AttributeRefAttrPtr thePointRefAttr)
163 {
164   GeomPnt2dPtr aPoint = thePoint;
165   if (!thePointRef.isEmpty()) {
166     fillAttribute(thePointRef, thePointRefAttr);
167     std::shared_ptr<GeomDataAPI_Point2D> anAttrPnt =
168         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePointRefAttr->attr());
169     if (anAttrPnt)
170       aPoint = anAttrPnt->pnt();
171   }
172   fillAttribute(aPoint, thePointAttr);
173 }
174
175 void SketchAPI_MacroEllipse::initializePoints(
176     const std::shared_ptr<GeomAPI_Pnt2d>& theMajorAxisPoint1,
177     const ModelHighAPI_RefAttr&           theMajorAxisPoint1Ref,
178     const std::shared_ptr<GeomAPI_Pnt2d>& theMajorAxisPoint2,
179     const ModelHighAPI_RefAttr&           theMajorAxisPoint2Ref,
180     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
181     const ModelHighAPI_RefAttr&           thePassedPointRef)
182 {
183   fillAttribute(theMajorAxisPoint1, theMajorAxisPoint1Ref,
184                 MAJOR_AXIS_NEGATIVE, MAJOR_AXIS_NEGATIVE_REF);
185   fillAttribute(theMajorAxisPoint2, theMajorAxisPoint2Ref,
186                 MAJOR_AXIS_POSITIVE, MAJOR_AXIS_POSITIVE_REF);
187   fillAttribute(thePassedPoint, thePassedPointRef,
188                 PASSED_POINT, PASSED_POINT_REF);
189
190   mySketch = sketch(feature());
191   execute();
192 }
193
194 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::center()
195 {
196   if (!myCenter)
197     collectAuxiliary();
198   return myCenter;
199 }
200
201 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::focus1()
202 {
203   if (!myFocus1)
204     collectAuxiliary();
205   return myFocus1;
206 }
207
208 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::focus2()
209 {
210   if (!myFocus2)
211     collectAuxiliary();
212   return myFocus2;
213 }
214
215 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::majorAxisStart()
216 {
217   if (!myMajorAxisStart)
218     collectAuxiliary();
219   return myMajorAxisStart;
220 }
221
222 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::majorAxisEnd()
223 {
224   if (!myMajorAxisEnd)
225     collectAuxiliary();
226   return myMajorAxisEnd;
227 }
228
229 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::minorAxisStart()
230 {
231   if (!myMinorAxisStart)
232     collectAuxiliary();
233   return myMinorAxisStart;
234 }
235
236 std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::minorAxisEnd()
237 {
238   if (!myMinorAxisEnd)
239     collectAuxiliary();
240   return myMinorAxisEnd;
241 }
242
243 std::shared_ptr<SketchAPI_Line> SketchAPI_MacroEllipse::majorAxis()
244 {
245   if (!myMajorAxis)
246     collectAuxiliary();
247   return myMajorAxis;
248 }
249
250 std::shared_ptr<SketchAPI_Line> SketchAPI_MacroEllipse::minorAxis()
251 {
252   if (!myMinorAxis)
253     collectAuxiliary();
254   return myMinorAxis;
255 }
256
257 void SketchAPI_MacroEllipse::collectAuxiliary()
258 {
259   // collect auxiliary features
260   int aNbSubs = mySketch->numberOfSubs();
261   std::shared_ptr<SketchAPI_Point>* anAuxPoint[] = {
262     &myCenter, &myFocus1, &myFocus2,
263     &myMajorAxisStart, &myMajorAxisEnd,
264     &myMinorAxisStart, &myMinorAxisEnd
265   };
266   std::shared_ptr<SketchAPI_Line>* anAuxLine[] = {&myMajorAxis, &myMinorAxis};
267   for (int aPtInd = 7, aLinInd = 2; (aPtInd > 0 || aLinInd > 0) && aNbSubs >= 0; ) {
268     FeaturePtr aCurFeature = mySketch->subFeature(--aNbSubs);
269     if (aPtInd > 0 && aCurFeature->getKind() == SketchPlugin_Point::ID())
270       anAuxPoint[--aPtInd]->reset(new SketchAPI_Point(aCurFeature));
271     else if (aLinInd > 0 && aCurFeature->getKind() == SketchPlugin_Line::ID())
272       anAuxLine[--aLinInd]->reset(new SketchAPI_Line(aCurFeature));
273   }
274 }