Salome HOME
Copyright update 2020
[modules/shaper.git] / src / SketchAPI / SketchAPI_EllipticArc.cpp
1 // Copyright (C) 2014-2020  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   // the order of attribute initialization is reversed to avoid odd recalculation of an elliptic arc
96   fillAttribute(theInversed, reversed());
97   fillAttribute(endPoint(), theEndX, theEndY);
98   fillAttribute(startPoint(), theStartX, theStartY);
99   fillAttribute(firstFocus(), theFocusX, theFocusY);
100   fillAttribute(center(), theCenterX, theCenterY);
101
102   execute();
103 }
104
105 void SketchAPI_EllipticArc::setByCenterFocusAndPoints(
106     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
107     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
108     const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
109     const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
110     bool theInversed)
111 {
112   // the order of attribute initialization is reversed to avoid odd recalculation of an elliptic arc
113   fillAttribute(theInversed, reversed());
114   fillAttribute(theEnd, endPoint());
115   fillAttribute(theStart, startPoint());
116   fillAttribute(theFocus, firstFocus());
117   fillAttribute(theCenter, center());
118
119   execute();
120 }
121
122 void SketchAPI_EllipticArc::setByExternal(const ModelHighAPI_Selection & theExternal)
123 {
124   fillAttribute(theExternal, external());
125   execute();
126 }
127
128 void SketchAPI_EllipticArc::setByExternalName(const std::string & theExternalName)
129 {
130   fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
131   execute();
132 }
133
134 void SketchAPI_EllipticArc::setCenter(double theX, double theY)
135 {
136   fillAttribute(center(), theX, theY);
137   execute();
138 }
139
140 void SketchAPI_EllipticArc::setCenter(const std::shared_ptr<GeomAPI_Pnt2d> & theCenter)
141 {
142   fillAttribute(theCenter, mycenter);
143   execute();
144 }
145
146 void SketchAPI_EllipticArc::setFocus(double theX, double theY)
147 {
148   fillAttribute(firstFocus(), theX, theY);
149   execute();
150 }
151
152 void SketchAPI_EllipticArc::setFocus(const std::shared_ptr<GeomAPI_Pnt2d> & theFocus)
153 {
154   fillAttribute(theFocus, myfirstFocus);
155   execute();
156 }
157
158 static const std::list<PairOfStrings>& ellipticArcAttrAndDumpNames()
159 {
160   static std::list<PairOfStrings> anAttributes;
161   if (anAttributes.empty()) {
162     anAttributes.push_back(
163         PairOfStrings(SketchPlugin_EllipticArc::CENTER_ID(), "center"));
164     anAttributes.push_back(
165         PairOfStrings(SketchPlugin_EllipticArc::FIRST_FOCUS_ID(), "firstFocus"));
166     anAttributes.push_back(
167         PairOfStrings(SketchPlugin_EllipticArc::SECOND_FOCUS_ID(), "secondFocus"));
168     anAttributes.push_back(
169         PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(), "majorAxisStart"));
170     anAttributes.push_back(
171         PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID(), "majorAxisEnd"));
172     anAttributes.push_back(
173         PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(), "minorAxisStart"));
174     anAttributes.push_back(
175         PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_END_ID(), "minorAxisEnd"));
176   }
177   return anAttributes;
178 }
179
180 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_EllipticArc::construction(
181     const std::string& center,
182     const std::string& firstFocus,
183     const std::string& secondFocus,
184     const std::string& majorAxisStart,
185     const std::string& majorAxisEnd,
186     const std::string& minorAxisStart,
187     const std::string& minorAxisEnd,
188     const std::string& majorAxis,
189     const std::string& minorAxis) const
190 {
191   FeaturePtr anEllipse = feature();
192
193   std::list<PairOfStrings> anAttributes = ellipticArcAttrAndDumpNames();
194   // append start and end attributes for axes
195   anAttributes.push_back(PairOfStrings(SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
196                                        SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID()));
197   anAttributes.push_back(PairOfStrings(SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
198                                        SketchPlugin_EllipticArc::MINOR_AXIS_END_ID()));
199
200   return SketchAPI_Ellipse::buildConstructionEntities(anEllipse, anAttributes,
201                                                       center, firstFocus, secondFocus,
202                                                       majorAxisStart, majorAxisEnd,
203                                                       minorAxisStart, minorAxisEnd,
204                                                       majorAxis, minorAxis);
205 }
206
207 void SketchAPI_EllipticArc::dump(ModelHighAPI_Dumper& theDumper) const
208 {
209   if (isCopy())
210     return; // no need to dump copied feature
211
212   FeaturePtr aBase = feature();
213   const std::string& aSketchName = theDumper.parentName(aBase);
214
215   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
216   if (anExternal->context()) {
217     // circle is external
218     theDumper << aBase << " = " << aSketchName << ".addEllipticArc("
219               << anExternal << ")" << std::endl;
220   } else {
221     // ellipse given by center, focus and radius
222     theDumper << aBase << " = " << aSketchName << ".addEllipticArc("
223               << center() << ", " << firstFocus() << ", "
224               << startPoint() << ", " << endPoint() << ", "
225               << reversed() << ")" << std::endl;
226   }
227   // dump "auxiliary" flag if necessary
228   SketchAPI_SketchEntity::dump(theDumper);
229
230   // dump auxiliary features produced by elliptic arc
231   std::map<std::string, FeaturePtr> anAuxFeatures;
232   static const std::pair<std::string, std::string> aMajorAxis(
233       SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID(),
234       SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID());
235   static const std::pair<std::string, std::string> aMinorAxis(
236       SketchPlugin_EllipticArc::MINOR_AXIS_START_ID(),
237       SketchPlugin_EllipticArc::MINOR_AXIS_END_ID());
238   SketchAPI_Ellipse::collectAuxiliaryFeatures(aBase, aMajorAxis, aMinorAxis, anAuxFeatures);
239
240   if (!anAuxFeatures.empty()) {
241     // a list of attributes to write features in special order
242     static std::list<PairOfStrings> anAttributes = ellipticArcAttrAndDumpNames();
243     SketchAPI_Ellipse::dumpConstructionEntities(theDumper, aBase, anAttributes, anAuxFeatures);
244   }
245 }