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