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 const_cast<SketchAPI_Sketch*>(this)->execute();
108 std::list<ModelHighAPI_Selection> aSelectionList;
110 ResultConstructionPtr aResultConstruction =
111 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
112 if (aResultConstruction.get() == NULL)
113 return aSelectionList;
115 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
116 aSelectionList.push_back(
117 ModelHighAPI_Selection(aResultConstruction,
118 aResultConstruction->face(anIndex)));
121 return aSelectionList;
124 //--------------------------------------------------------------------------------------
125 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
126 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
128 // TODO(spo): check that thePart is not empty
129 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
130 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
133 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
134 const ModelHighAPI_Selection & theExternal)
136 // TODO(spo): check that thePart is not empty
137 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
138 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
141 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
142 const std::string & theExternalName)
144 // TODO(spo): check that thePart is not empty
145 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
146 return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
149 //--------------------------------------------------------------------------------------
150 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
152 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
153 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
155 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
156 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
157 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
159 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
160 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
162 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
164 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
165 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
167 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
169 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
170 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
171 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
174 //--------------------------------------------------------------------------------------
175 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
179 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
180 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
183 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
186 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
187 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
190 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
191 double theX2, double theY2,
192 double theX3, double theY3)
194 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
195 return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
198 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
199 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
200 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
202 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
203 return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
206 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
208 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
209 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
212 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
214 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
215 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
216 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
219 //--------------------------------------------------------------------------------------
220 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
221 const ModelHighAPI_RefAttr & thePoint1,
222 const ModelHighAPI_RefAttr & thePoint2)
224 std::shared_ptr<ModelAPI_Feature> aFeature =
225 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
226 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
227 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
232 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
233 const ModelHighAPI_RefAttr & thePoint,
234 const ModelHighAPI_RefAttr & theLine,
235 const ModelHighAPI_Double & theValue)
237 std::shared_ptr<ModelAPI_Feature> aFeature =
238 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
239 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
240 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
241 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
246 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
247 const std::list<ModelHighAPI_RefAttr> & thePoints,
248 const ModelHighAPI_Double & theRadius)
250 std::shared_ptr<ModelAPI_Feature> aFeature =
251 compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
252 fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
253 fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
258 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
259 const ModelHighAPI_RefAttr & theLine)
261 std::shared_ptr<ModelAPI_Feature> aFeature =
262 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
263 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
268 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
269 const ModelHighAPI_RefAttr & theLine,
270 const ModelHighAPI_Double & theValue)
272 std::shared_ptr<ModelAPI_Feature> aFeature =
273 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
274 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
275 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
280 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
281 const ModelHighAPI_RefAttr & theLine1,
282 const ModelHighAPI_RefAttr & theLine2)
284 std::shared_ptr<ModelAPI_Feature> aFeature =
285 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
286 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
287 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
292 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
293 const ModelHighAPI_RefAttr & theLine1,
294 const ModelHighAPI_RefAttr & theLine2)
296 std::shared_ptr<ModelAPI_Feature> aFeature =
297 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
298 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
299 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
304 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRigid(
305 const ModelHighAPI_RefAttr & theObject)
307 std::shared_ptr<ModelAPI_Feature> aFeature =
308 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
309 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
314 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
315 const ModelHighAPI_RefAttr & theLine)
317 std::shared_ptr<ModelAPI_Feature> aFeature =
318 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
319 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
324 //--------------------------------------------------------------------------------------