1 // Name : SketchAPI_Sketch.cpp
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7 //--------------------------------------------------------------------------------------
8 #include "SketchAPI_Sketch.h"
9 //--------------------------------------------------------------------------------------
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintFillet.h>
17 #include <SketchPlugin_ConstraintHorizontal.h>
18 #include <SketchPlugin_ConstraintLength.h>
19 #include <SketchPlugin_ConstraintMiddle.h>
20 #include <SketchPlugin_ConstraintMirror.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
27 //--------------------------------------------------------------------------------------
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelHighAPI_RefAttr.h>
31 #include <ModelHighAPI_Selection.h>
32 #include <ModelHighAPI_Tools.h>
33 //--------------------------------------------------------------------------------------
34 #include "SketchAPI_Circle.h"
35 #include "SketchAPI_Line.h"
36 //--------------------------------------------------------------------------------------
37 SketchAPI_Sketch::SketchAPI_Sketch(
38 const std::shared_ptr<ModelAPI_Feature> & theFeature)
39 : ModelHighAPI_Interface(theFeature)
44 SketchAPI_Sketch::SketchAPI_Sketch(
45 const std::shared_ptr<ModelAPI_Feature> & theFeature,
46 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
47 : ModelHighAPI_Interface(theFeature)
54 SketchAPI_Sketch::SketchAPI_Sketch(
55 const std::shared_ptr<ModelAPI_Feature> & theFeature,
56 const ModelHighAPI_Selection & theExternal)
57 : ModelHighAPI_Interface(theFeature)
60 setExternal(theExternal);
64 SketchAPI_Sketch::~SketchAPI_Sketch()
69 //--------------------------------------------------------------------------------------
70 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
72 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
75 //--------------------------------------------------------------------------------------
76 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
78 fillAttribute(thePlane->origin(), myorigin);
79 fillAttribute(thePlane->dirX(), mydirX);
80 fillAttribute(thePlane->normal(), mynormal);
85 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
87 fillAttribute(theExternal, myexternal);
92 //--------------------------------------------------------------------------------------
93 void SketchAPI_Sketch::setValue(
94 const std::shared_ptr<ModelAPI_Feature> & theConstraint,
95 const ModelHighAPI_Double & theValue)
97 // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
98 fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
100 theConstraint->execute();
103 //--------------------------------------------------------------------------------------
104 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
106 std::list<ModelHighAPI_Selection> aSelectionList;
108 ResultConstructionPtr aResultConstruction =
109 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
110 if (aResultConstruction.get() == NULL)
111 return aSelectionList;
113 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
114 aSelectionList.push_back(
115 ModelHighAPI_Selection(aResultConstruction,
116 aResultConstruction->face(anIndex)));
119 return aSelectionList;
122 //--------------------------------------------------------------------------------------
123 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
124 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
126 // TODO(spo): check that thePart is not empty
127 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
128 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
131 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
132 const ModelHighAPI_Selection & theExternal)
134 // TODO(spo): check that thePart is not empty
135 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
136 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
139 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
140 const std::string & theExternalName)
142 // TODO(spo): check that thePart is not empty
143 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
144 return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
147 //--------------------------------------------------------------------------------------
148 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
150 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
151 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
153 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
154 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
155 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
157 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
158 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
160 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
162 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
163 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
165 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
167 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
168 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
169 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
172 //--------------------------------------------------------------------------------------
173 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
177 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
178 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
181 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
184 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
185 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
188 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
189 double theX2, double theY2,
190 double theX3, double theY3)
192 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
193 return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
196 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
197 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
198 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
200 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
201 return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
204 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
206 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
207 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
210 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
212 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
213 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
214 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
217 //--------------------------------------------------------------------------------------
218 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
219 const ModelHighAPI_RefAttr & thePoint1,
220 const ModelHighAPI_RefAttr & thePoint2)
222 std::shared_ptr<ModelAPI_Feature> aFeature =
223 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
224 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
225 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
230 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
231 const ModelHighAPI_RefAttr & thePoint,
232 const ModelHighAPI_RefAttr & theLine,
233 const ModelHighAPI_Double & theValue)
235 std::shared_ptr<ModelAPI_Feature> aFeature =
236 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
237 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
238 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
239 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
244 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
245 const ModelHighAPI_RefAttr & theLine,
246 const ModelHighAPI_Double & theValue)
248 std::shared_ptr<ModelAPI_Feature> aFeature =
249 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
250 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
251 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
256 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
257 const ModelHighAPI_RefAttr & theLine1,
258 const ModelHighAPI_RefAttr & theLine2)
260 std::shared_ptr<ModelAPI_Feature> aFeature =
261 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
262 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
263 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
268 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
269 const ModelHighAPI_RefAttr & theLine1,
270 const ModelHighAPI_RefAttr & theLine2)
272 std::shared_ptr<ModelAPI_Feature> aFeature =
273 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
274 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
275 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
279 //--------------------------------------------------------------------------------------