Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[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
22 #include <GeomAPI_Pnt2d.h>
23
24 #include <ModelHighAPI_RefAttr.h>
25 #include <ModelHighAPI_Tools.h>
26
27 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature)
28 : SketchAPI_SketchEntity(theFeature)
29 {
30   initialize();
31 }
32
33 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
34                                                double theX1, double theY1,
35                                                double theX2, double theY2,
36                                                double theX3, double theY3,
37                                                bool byCenter)
38   : SketchAPI_SketchEntity(theFeature)
39 {
40   if (initialize()) {
41     if (byCenter)
42       setByCenterAndPassedPoints();
43     else
44       setByMajorAxisAndPassedPoint();
45
46     initializePoints(theX1, theY1, theX2, theY2, theX3, theY3);
47   }
48 }
49
50 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
51                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
52                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
53                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3,
54                                                bool byCenter)
55   : SketchAPI_SketchEntity(theFeature)
56 {
57   if (initialize()) {
58     if (byCenter)
59       setByCenterAndPassedPoints();
60     else
61       setByMajorAxisAndPassedPoint();
62
63     initializePoints(thePoint1, thePoint2, thePoint3);
64   }
65 }
66
67 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature,
68                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
69                                                const ModelHighAPI_RefAttr&           thePoint1Ref,
70                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
71                                                const ModelHighAPI_RefAttr&           thePoint2Ref,
72                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3,
73                                                const ModelHighAPI_RefAttr&           thePoint3Ref,
74                                                bool byCenter)
75   : SketchAPI_SketchEntity(theFeature)
76 {
77   if (initialize()) {
78     if (byCenter)
79       setByCenterAndPassedPoints();
80     else
81       setByMajorAxisAndPassedPoint();
82
83     initializePoints(thePoint1, thePoint1Ref, thePoint2, thePoint2Ref, thePoint3, thePoint3Ref);
84   }
85 }
86
87 SketchAPI_MacroEllipse::~SketchAPI_MacroEllipse()
88 {
89 }
90
91 void SketchAPI_MacroEllipse::setByCenterAndPassedPoints()
92 {
93   fillAttribute(SketchPlugin_MacroEllipse::ELLIPSE_TYPE_BY_CENTER_AXIS_POINT(), myellipseType);
94 }
95
96 void SketchAPI_MacroEllipse::setByMajorAxisAndPassedPoint()
97 {
98   fillAttribute(SketchPlugin_MacroEllipse::ELLIPSE_TYPE_BY_AXIS_AND_POINT(), myellipseType);
99 }
100
101 void SketchAPI_MacroEllipse::initializePoints(double theX1, double theY1,
102                                               double theX2, double theY2,
103                                               double theX3, double theY3)
104 {
105   fillAttribute(majorAxisNegativePoint(), theX1, theY1);
106   fillAttribute(majorAxisPositivePoint(), theX2, theY2);
107   fillAttribute(passedPoint(), theX3, theY3);
108
109   execute();
110 }
111
112 void SketchAPI_MacroEllipse::initializePoints(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
113                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
114                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
115 {
116   fillAttribute(thePoint1, mymajorAxisNegativePoint);
117   fillAttribute(thePoint2, mymajorAxisPositivePoint);
118   fillAttribute(thePoint3, mypassedPoint);
119
120   execute();
121 }
122
123 static void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
124                           const ModelHighAPI_RefAttr& thePointRef,
125                           std::shared_ptr<GeomDataAPI_Point2D> thePointAttr,
126                           AttributeRefAttrPtr thePointRefAttr)
127 {
128   GeomPnt2dPtr aPoint = thePoint;
129   if (!thePointRef.isEmpty()) {
130     fillAttribute(thePointRef, thePointRefAttr);
131     std::shared_ptr<GeomDataAPI_Point2D> anAttrPnt =
132         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePointRefAttr->attr());
133     if (anAttrPnt)
134       aPoint = anAttrPnt->pnt();
135   }
136   fillAttribute(aPoint, thePointAttr);
137 }
138
139 void SketchAPI_MacroEllipse::initializePoints(
140     const std::shared_ptr<GeomAPI_Pnt2d>& theMajorAxisPoint1,
141     const ModelHighAPI_RefAttr&           theMajorAxisPoint1Ref,
142     const std::shared_ptr<GeomAPI_Pnt2d>& theMajorAxisPoint2,
143     const ModelHighAPI_RefAttr&           theMajorAxisPoint2Ref,
144     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
145     const ModelHighAPI_RefAttr&           thePassedPointRef)
146 {
147   fillAttribute(theMajorAxisPoint1, theMajorAxisPoint1Ref,
148                 mymajorAxisNegativePoint, mymajorAxisNegativePointRef);
149   fillAttribute(theMajorAxisPoint2, theMajorAxisPoint2Ref,
150                 mymajorAxisPositivePoint, mymajorAxisPositivePointRef);
151   fillAttribute(thePassedPoint, thePassedPointRef, mypassedPoint, mypassedPointRef);
152
153   execute();
154 }