]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Circle.cpp
Salome HOME
bos#35152 [EDF] (2023-T1) Sketch Circle should allow user to position construction...
[modules/shaper.git] / src / SketchAPI / SketchAPI_Circle.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_Circle.h"
21
22 #include <GeomAPI_Pnt2d.h>
23
24 #include <ModelHighAPI_Double.h>
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
28
29 #include <SketchAPI_Point.h>
30 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
31
32 //==================================================================================================
33 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature> & theFeature)
34 : SketchAPI_SketchEntity(theFeature)
35 {
36   initialize();
37 }
38
39 //==================================================================================================
40 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
41                                    double theCenterX,
42                                    double theCenterY,
43                                    double theRadius, double theAngle)
44 : SketchAPI_SketchEntity(theFeature)
45 {
46   if(initialize()) {
47     setByCenterAndRadius(theCenterX, theCenterY, theRadius, theAngle);
48   }
49 }
50
51 //==================================================================================================
52 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
53                                    const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
54                                    double theRadius, double theAngle)
55 : SketchAPI_SketchEntity(theFeature)
56 {
57   if(initialize()) {
58     setByCenterAndRadius(theCenter, theRadius, theAngle);
59   }
60 }
61
62 //==================================================================================================
63 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
64                                    const ModelHighAPI_Selection& theExternal)
65 : SketchAPI_SketchEntity(theFeature)
66 {
67   if (initialize()) {
68     setByExternal(theExternal);
69   }
70 }
71
72 //==================================================================================================
73 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
74                                    const std::wstring& theExternalName)
75 : SketchAPI_SketchEntity(theFeature)
76 {
77   if (initialize()) {
78     setByExternalName(theExternalName);
79   }
80 }
81
82 //==================================================================================================
83 SketchAPI_Circle::~SketchAPI_Circle()
84 {
85
86 }
87
88 // Create point on circle line
89 void SketchAPI_Circle::createPoint()
90 {
91   // Find sketch
92   CompositeFeaturePtr aSketch;
93   const std::set<AttributePtr>& aRefs = feature()->data()->refsToMe();
94   for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt)
95     if ((*anIt)->id() == SketchPlugin_Sketch::FEATURES_ID())
96     {
97       aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
98       break;
99     }
100   if (!aSketch)
101     return;
102
103   // create point on line
104   FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
105   aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(feature());
106
107   AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108     aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
109
110   GeomPnt2dPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111     feature()->attribute(SketchPlugin_Circle::ROTATE_ID()))->pnt();
112
113   aCoord->setValue(aPnt);
114   aPointFeature->execute();
115
116   FeaturePtr aConstraint = aSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID());
117   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
118     aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
119   aRefAttr->setAttr(feature()->attribute(SketchPlugin_Circle::ROTATE_ID()));
120
121   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
122     aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
123   aRefAttr->setAttr(aCoord);
124
125   feature()->reference(SketchPlugin_Circle::ROTATE_REF_ID())->setValue(aPointFeature);
126 }
127
128 //==================================================================================================
129 void SketchAPI_Circle::setByCenterAndRadius(double theCenterX, double theCenterY,
130                                             double theRadius, double theAngle)
131 {
132   fillAttribute(center(), theCenterX, theCenterY);
133   fillAttribute(theRadius, myradius);
134   fillAttribute(theAngle, myangle);
135
136   bool isNeedPoint =
137     feature()->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0;
138   if (isNeedPoint)
139   {
140     fillAttribute(theAngle, angle());
141
142     execute();
143     createPoint();
144   }
145   else
146   {
147     execute();
148   }
149 }
150
151 //==================================================================================================
152 void SketchAPI_Circle::setByCenterAndRadius(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
153                                             double theRadius, double theAngle)
154 {
155   fillAttribute(theCenter, mycenter);
156   fillAttribute(theRadius, myradius);
157
158   bool isNeedPoint =
159     feature()->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0;
160
161   if (isNeedPoint)
162   {
163     fillAttribute(theAngle, angle());
164
165     execute();
166     createPoint();
167   }
168   else
169   {
170     execute();
171   }
172 }
173
174 //==================================================================================================
175 void SketchAPI_Circle::setByExternal(const ModelHighAPI_Selection & theExternal)
176 {
177   fillAttribute(theExternal, external());
178
179   execute();
180 }
181
182 //==================================================================================================
183 void SketchAPI_Circle::setByExternalName(const std::wstring & theExternalName)
184 {
185   fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
186
187   execute();
188 }
189
190 //==================================================================================================
191 void SketchAPI_Circle::setCenter(double theX, double theY)
192 {
193   fillAttribute(center(), theX, theY);
194
195   execute();
196 }
197
198 //==================================================================================================
199 void SketchAPI_Circle::setCenter(const std::shared_ptr<GeomAPI_Pnt2d> & theCenter)
200 {
201   fillAttribute(theCenter, mycenter);
202
203   execute();
204 }
205
206 //==================================================================================================
207 void SketchAPI_Circle::setRadius(double theRadius)
208 {
209   fillAttribute(ModelHighAPI_Double(theRadius), myradius);
210
211   execute();
212 }
213
214 //==================================================================================================
215 void SketchAPI_Circle::setAngle(double theAngle)
216 {
217   fillAttribute(ModelHighAPI_Double(theAngle), myangle);
218
219   execute();
220 }
221
222 //==================================================================================================
223 // Return created point
224 std::shared_ptr<SketchAPI_SketchEntity> SketchAPI_Circle::createdPoint() const
225 {
226   std::shared_ptr<SketchAPI_SketchEntity> anEnt;
227
228   AttributeReferencePtr anRef = feature()->reference(SketchPlugin_Circle::ROTATE_REF_ID());
229   if (!anRef->isInitialized())
230     return anEnt;
231
232   FeaturePtr aFeature = ModelAPI_Feature::feature(anRef->value());
233   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
234   {
235     anEnt = std::shared_ptr < SketchAPI_SketchEntity>(new SketchAPI_Point(aFeature));
236   }
237   return anEnt;
238 }
239
240 //==================================================================================================
241 void SketchAPI_Circle::dump(ModelHighAPI_Dumper& theDumper) const
242 {
243   if (isCopy())
244     return; // no need to dump copied feature
245
246   FeaturePtr aBase = feature();
247   const std::string& aSketchName = theDumper.parentName(aBase);
248
249   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
250   std::string aComName = aBase->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0 ?
251     "addCircleWithPoint" : "addCircle";
252
253   if (anExternal->context()) {
254     // circle is external
255     theDumper << aBase << " = " << aSketchName << "." << aComName << "(" << anExternal << ")" << std::endl;
256   }
257   else {// circle given by center and radius
258     theDumper << aBase << " = " << aSketchName << "." << aComName << "(" << center() << ", " << radius();
259     if (aBase->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0)
260     {
261       theDumper << ", " << angle() << ")" << std::endl;
262       std::shared_ptr<SketchAPI_SketchEntity> aPoint = createdPoint();
263       if (aPoint)
264         theDumper << aPoint->feature() << " = " << theDumper.name(aBase) << ".createdPoint()" << std::endl;
265     }
266     else
267     {
268       theDumper << ")" << std::endl;
269     }
270   }
271   // dump "auxiliary" flag if necessary
272   SketchAPI_SketchEntity::dump(theDumper);
273 }