1 // Copyright (C) 2014-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchAPI_Circle.h"
22 #include <GeomAPI_Pnt2d.h>
24 #include <ModelHighAPI_Double.h>
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
29 #include <SketchAPI_Point.h>
30 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
32 //==================================================================================================
33 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature> & theFeature)
34 : SketchAPI_SketchEntity(theFeature)
39 //==================================================================================================
40 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
43 double theRadius, double theAngle)
44 : SketchAPI_SketchEntity(theFeature)
47 setByCenterAndRadius(theCenterX, theCenterY, theRadius, theAngle);
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)
58 setByCenterAndRadius(theCenter, theRadius, theAngle);
62 //==================================================================================================
63 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
64 const ModelHighAPI_Selection& theExternal)
65 : SketchAPI_SketchEntity(theFeature)
68 setByExternal(theExternal);
72 //==================================================================================================
73 SketchAPI_Circle::SketchAPI_Circle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
74 const std::wstring& theExternalName)
75 : SketchAPI_SketchEntity(theFeature)
78 setByExternalName(theExternalName);
82 //==================================================================================================
83 SketchAPI_Circle::~SketchAPI_Circle()
88 // Create point on circle line
89 void SketchAPI_Circle::createPoint()
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())
97 aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
103 // create point on line
104 FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
105 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(feature());
107 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
110 GeomPnt2dPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 feature()->attribute(SketchPlugin_Circle::ROTATE_ID()))->pnt();
113 aCoord->setValue(aPnt);
114 aPointFeature->execute();
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()));
121 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
122 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
123 aRefAttr->setAttr(aCoord);
125 feature()->reference(SketchPlugin_Circle::ROTATE_REF_ID())->setValue(aPointFeature);
128 //==================================================================================================
129 void SketchAPI_Circle::setByCenterAndRadius(double theCenterX, double theCenterY,
130 double theRadius, double theAngle)
132 fillAttribute(center(), theCenterX, theCenterY);
133 fillAttribute(theRadius, myradius);
134 fillAttribute(theAngle, myangle);
137 feature()->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0;
140 fillAttribute(theAngle, angle());
151 //==================================================================================================
152 void SketchAPI_Circle::setByCenterAndRadius(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
153 double theRadius, double theAngle)
155 fillAttribute(theCenter, mycenter);
156 fillAttribute(theRadius, myradius);
159 feature()->integer(SketchPlugin_Circle::VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0;
163 fillAttribute(theAngle, angle());
174 //==================================================================================================
175 void SketchAPI_Circle::setByExternal(const ModelHighAPI_Selection & theExternal)
177 fillAttribute(theExternal, external());
182 //==================================================================================================
183 void SketchAPI_Circle::setByExternalName(const std::wstring & theExternalName)
185 fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
190 //==================================================================================================
191 void SketchAPI_Circle::setCenter(double theX, double theY)
193 fillAttribute(center(), theX, theY);
198 //==================================================================================================
199 void SketchAPI_Circle::setCenter(const std::shared_ptr<GeomAPI_Pnt2d> & theCenter)
201 fillAttribute(theCenter, mycenter);
206 //==================================================================================================
207 void SketchAPI_Circle::setRadius(double theRadius)
209 fillAttribute(ModelHighAPI_Double(theRadius), myradius);
214 //==================================================================================================
215 void SketchAPI_Circle::setAngle(double theAngle)
217 fillAttribute(ModelHighAPI_Double(theAngle), myangle);
222 //==================================================================================================
223 // Return created point
224 std::shared_ptr<SketchAPI_SketchEntity> SketchAPI_Circle::createdPoint() const
226 std::shared_ptr<SketchAPI_SketchEntity> anEnt;
228 AttributeReferencePtr anRef = feature()->reference(SketchPlugin_Circle::ROTATE_REF_ID());
229 if (!anRef->isInitialized())
232 FeaturePtr aFeature = ModelAPI_Feature::feature(anRef->value());
233 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
235 anEnt = std::shared_ptr < SketchAPI_SketchEntity>(new SketchAPI_Point(aFeature));
240 //==================================================================================================
241 void SketchAPI_Circle::dump(ModelHighAPI_Dumper& theDumper) const
244 return; // no need to dump copied feature
246 FeaturePtr aBase = feature();
247 const std::string& aSketchName = theDumper.parentName(aBase);
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";
253 if (anExternal->context()) {
254 // circle is external
255 theDumper << aBase << " = " << aSketchName << "." << aComName << "(" << anExternal << ")" << std::endl;
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)
261 theDumper << ", " << angle() << ")" << std::endl;
262 std::shared_ptr<SketchAPI_SketchEntity> aPoint = createdPoint();
264 theDumper << aPoint->feature() << " = " << theDumper.name(aBase) << ".createdPoint()" << std::endl;
268 theDumper << ")" << std::endl;
271 // dump "auxiliary" flag if necessary
272 SketchAPI_SketchEntity::dump(theDumper);