Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchAPI / SketchAPI_EllipticArc.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_EllipticArc.h"
21 #include "SketchAPI_Ellipse.h"
22
23 #include <GeomAPI_Pnt2d.h>
24
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
28
29 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
32
33
34 SketchAPI_EllipticArc::SketchAPI_EllipticArc(const std::shared_ptr<ModelAPI_Feature> & theFeature)
35   : SketchAPI_SketchEntity(theFeature)
36 {
37   initialize();
38 }
39
40 SketchAPI_EllipticArc::SketchAPI_EllipticArc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
41                                              double theCenterX, double theCenterY,
42                                              double theFocusX, double theFocusY,
43                                              double theStartX, double theStartY,
44                                              double theEndX, double theEndY,
45                                              bool theInversed)
46   : SketchAPI_SketchEntity(theFeature)
47 {
48   if(initialize()) {
49     setByCenterFocusAndPoints(theCenterX, theCenterY, theFocusX, theFocusY,
50                               theStartX, theStartY, theEndX, theEndY, theInversed);
51   }
52 }
53
54 SketchAPI_EllipticArc::SketchAPI_EllipticArc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
55                                              const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
56                                              const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
57                                              const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
58                                              const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
59                                              bool theInversed)
60   : SketchAPI_SketchEntity(theFeature)
61 {
62   if(initialize()) {
63     setByCenterFocusAndPoints(theCenter, theFocus, theStart, theEnd, theInversed);
64   }
65 }
66
67 SketchAPI_EllipticArc::SketchAPI_EllipticArc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
68                                      const ModelHighAPI_Selection& theExternal)
69   : SketchAPI_SketchEntity(theFeature)
70 {
71   if (initialize()) {
72     setByExternal(theExternal);
73   }
74 }
75
76 SketchAPI_EllipticArc::SketchAPI_EllipticArc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
77                                      const std::string& theExternalName)
78   : SketchAPI_SketchEntity(theFeature)
79 {
80   if (initialize()) {
81     setByExternalName(theExternalName);
82   }
83 }
84
85 SketchAPI_EllipticArc::~SketchAPI_EllipticArc()
86 {
87 }
88
89 void SketchAPI_EllipticArc::setByCenterFocusAndPoints(double theCenterX, double theCenterY,
90                                                       double theFocusX, double theFocusY,
91                                                       double theStartX, double theStartY,
92                                                       double theEndX, double theEndY,
93                                                       bool theInversed)
94 {
95   fillAttribute(center(), theCenterX, theCenterY);
96   fillAttribute(firstFocus(), theFocusX, theFocusY);
97   fillAttribute(startPoint(), theStartX, theStartY);
98   fillAttribute(endPoint(), theEndX, theEndY);
99   fillAttribute(theInversed, reversed());
100
101   execute();
102 }
103
104 void SketchAPI_EllipticArc::setByCenterFocusAndPoints(
105     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
106     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
107     const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
108     const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
109     bool theInversed)
110 {
111   fillAttribute(theCenter, center());
112   fillAttribute(theFocus, firstFocus());
113   fillAttribute(theStart, startPoint());
114   fillAttribute(theEnd, endPoint());
115   fillAttribute(theInversed, reversed());
116
117   execute();
118 }
119
120 void SketchAPI_EllipticArc::setByExternal(const ModelHighAPI_Selection & theExternal)
121 {
122   fillAttribute(theExternal, external());
123   execute();
124 }
125
126 void SketchAPI_EllipticArc::setByExternalName(const std::string & theExternalName)
127 {
128   fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
129   execute();
130 }
131
132 void SketchAPI_EllipticArc::setCenter(double theX, double theY)
133 {
134   fillAttribute(center(), theX, theY);
135   execute();
136 }
137
138 void SketchAPI_EllipticArc::setCenter(const std::shared_ptr<GeomAPI_Pnt2d> & theCenter)
139 {
140   fillAttribute(theCenter, mycenter);
141   execute();
142 }
143
144 void SketchAPI_EllipticArc::setFocus(double theX, double theY)
145 {
146   fillAttribute(firstFocus(), theX, theY);
147   execute();
148 }
149
150 void SketchAPI_EllipticArc::setFocus(const std::shared_ptr<GeomAPI_Pnt2d> & theFocus)
151 {
152   fillAttribute(theFocus, myfirstFocus);
153   execute();
154 }
155
156 static const std::list<PairOfStrings>& ellipticArcAttrAndDumpNames()
157 {
158   static std::list<PairOfStrings> anAttributes;
159   if (anAttributes.empty()) {
160     anAttributes.push_back(
161         PairOfStrings(SketchPlugin_EllipticArc::CENTER_ID(), "center"));
162     anAttributes.push_back(
163         PairOfStrings(SketchPlugin_EllipticArc::FIRST_FOCUS_ID(), "firstFocus"));
164     anAttributes.push_back(
165         PairOfStrings(SketchPlugin_EllipticArc::SECOND_FOCUS_ID(), "secondFocus"));
166     anAttributes.push_back(
167         PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(), "majorAxisStart"));
168     anAttributes.push_back(
169         PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID(), "majorAxisEnd"));
170     anAttributes.push_back(
171         PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(), "minorAxisStart"));
172     anAttributes.push_back(
173         PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_END_ID(), "minorAxisEnd"));
174   }
175   return anAttributes;
176 }
177
178 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_EllipticArc::construction(
179     const std::string& center,
180     const std::string& firstFocus,
181     const std::string& secondFocus,
182     const std::string& majorAxisStart,
183     const std::string& majorAxisEnd,
184     const std::string& minorAxisStart,
185     const std::string& minorAxisEnd,
186     const std::string& majorAxis,
187     const std::string& minorAxis) const
188 {
189   FeaturePtr anEllipse = feature();
190
191   std::list<PairOfStrings> anAttributes = ellipticArcAttrAndDumpNames();
192   // append start and end attributes for axes
193   anAttributes.push_back(PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
194                                        SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID()));
195   anAttributes.push_back(PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
196                                        SketchPlugin_EllipticArc::MINOR_AXIS_END_ID()));
197
198   return SketchAPI_Ellipse::buildConstructionEntities(anEllipse, anAttributes,
199                                                       center, firstFocus, secondFocus,
200                                                       majorAxisStart, majorAxisEnd,
201                                                       minorAxisStart, minorAxisEnd,
202                                                       majorAxis, minorAxis);
203 }
204
205 void SketchAPI_EllipticArc::dump(ModelHighAPI_Dumper& theDumper) const
206 {
207   if (isCopy())
208     return; // no need to dump copied feature
209
210   FeaturePtr aBase = feature();
211   const std::string& aSketchName = theDumper.parentName(aBase);
212
213   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
214   if (anExternal->context()) {
215     // circle is external
216     theDumper << aBase << " = " << aSketchName << ".addEllipticArc(" << anExternal << ")" << std::endl;
217   } else {
218     // ellipse given by center, focus and radius
219     theDumper << aBase << " = " << aSketchName << ".addEllipticArc("
220               << center() << ", " << firstFocus() << ", "
221               << startPoint() << ", " << endPoint() << ", "
222               << reversed() << ")" << std::endl;
223   }
224   // dump "auxiliary" flag if necessary
225   SketchAPI_SketchEntity::dump(theDumper);
226
227   // dump auxiliary features produced by elliptic arc
228   std::map<std::string, FeaturePtr> anAuxFeatures;
229   static const std::pair<std::string, std::string> aMajorAxis(
230       SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
231       SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
232   static const std::pair<std::string, std::string> aMinorAxis(
233       SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
234       SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
235   SketchAPI_Ellipse::collectAuxiliaryFeatures(aBase, aMajorAxis, aMinorAxis, anAuxFeatures);
236
237   if (!anAuxFeatures.empty()) {
238     // a list of attributes to write features in special order
239     static std::list<PairOfStrings> anAttributes = ellipticArcAttrAndDumpNames();
240     SketchAPI_Ellipse::dumpConstructionEntities(theDumper, aBase, anAttributes, anAuxFeatures);
241   }
242 }