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_Arc.h"
35 #include "SketchAPI_Circle.h"
36 #include "SketchAPI_Line.h"
37 #include "SketchAPI_Point.h"
38 //--------------------------------------------------------------------------------------
39 SketchAPI_Sketch::SketchAPI_Sketch(
40 const std::shared_ptr<ModelAPI_Feature> & theFeature)
41 : ModelHighAPI_Interface(theFeature)
46 SketchAPI_Sketch::SketchAPI_Sketch(
47 const std::shared_ptr<ModelAPI_Feature> & theFeature,
48 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
49 : ModelHighAPI_Interface(theFeature)
56 SketchAPI_Sketch::SketchAPI_Sketch(
57 const std::shared_ptr<ModelAPI_Feature> & theFeature,
58 const ModelHighAPI_Selection & theExternal)
59 : ModelHighAPI_Interface(theFeature)
62 setExternal(theExternal);
66 SketchAPI_Sketch::~SketchAPI_Sketch()
71 //--------------------------------------------------------------------------------------
72 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
74 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
77 //--------------------------------------------------------------------------------------
78 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
80 fillAttribute(thePlane->origin(), myorigin);
81 fillAttribute(thePlane->dirX(), mydirX);
82 fillAttribute(thePlane->normal(), mynormal);
87 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
89 fillAttribute(theExternal, myexternal);
94 //--------------------------------------------------------------------------------------
95 void SketchAPI_Sketch::setValue(
96 const std::shared_ptr<ModelAPI_Feature> & theConstraint,
97 const ModelHighAPI_Double & theValue)
99 // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
100 fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
102 // theConstraint->execute();
105 //--------------------------------------------------------------------------------------
106 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
108 const_cast<SketchAPI_Sketch*>(this)->execute();
110 std::list<ModelHighAPI_Selection> aSelectionList;
112 ResultConstructionPtr aResultConstruction =
113 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
114 if (aResultConstruction.get() == NULL)
115 return aSelectionList;
117 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
118 aSelectionList.push_back(
119 ModelHighAPI_Selection(aResultConstruction,
120 aResultConstruction->face(anIndex)));
123 return aSelectionList;
126 //--------------------------------------------------------------------------------------
127 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
128 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
130 // TODO(spo): check that thePart is not empty
131 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
132 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
135 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
136 const ModelHighAPI_Selection & theExternal)
138 // TODO(spo): check that thePart is not empty
139 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
140 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
143 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
144 const std::string & theExternalName)
146 // TODO(spo): check that thePart is not empty
147 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
148 return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
151 //--------------------------------------------------------------------------------------
152 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
153 double theX, double theY)
155 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
156 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
158 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
159 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
161 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
162 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
164 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
166 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
167 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
169 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
171 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
172 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
175 //--------------------------------------------------------------------------------------
176 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
178 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
179 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
181 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
182 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
183 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
185 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
186 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
188 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
190 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
191 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
193 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
195 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
196 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
197 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
200 //--------------------------------------------------------------------------------------
201 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
205 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
206 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
209 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
212 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
213 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
216 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
217 double theX2, double theY2,
218 double theX3, double theY3)
220 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
221 return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
224 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
225 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
226 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
228 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
229 return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
232 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
234 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
235 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
238 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
240 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
241 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
242 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
245 //--------------------------------------------------------------------------------------
246 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
247 double theStartX, double theStartY,
248 double theEndX, double theEndY,
251 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
252 return ArcPtr(new SketchAPI_Arc(aFeature,
253 theCenterX, theCenterY,
254 theStartX, theStartY,
259 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
260 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
261 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
264 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
265 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
268 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
269 double theEndX, double theEndY,
270 double thePassedX, double thePassedY)
272 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
273 return ArcPtr(new SketchAPI_Arc(aFeature,
274 theStartX, theStartY,
276 thePassedX, thePassedY));
279 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
280 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
281 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
283 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
284 return ArcPtr(new SketchAPI_Arc(aFeature, theStart, theEnd, thePassed));
287 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
288 double theEndX, double theEndY,
291 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
292 return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEndX, theEndY, theInversed));
295 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
296 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
299 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
300 return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEnd, theInversed));
303 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
305 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
306 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
309 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
311 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
312 std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
313 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
316 //--------------------------------------------------------------------------------------
317 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
318 const ModelHighAPI_RefAttr & thePoint1,
319 const ModelHighAPI_RefAttr & thePoint2)
321 std::shared_ptr<ModelAPI_Feature> aFeature =
322 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
323 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
324 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
329 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
330 const ModelHighAPI_RefAttr & thePoint,
331 const ModelHighAPI_RefAttr & theLine,
332 const ModelHighAPI_Double & theValue)
334 std::shared_ptr<ModelAPI_Feature> aFeature =
335 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
336 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
337 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
338 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
343 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
344 const std::list<ModelHighAPI_RefAttr> & thePoints,
345 const ModelHighAPI_Double & theRadius)
347 std::shared_ptr<ModelAPI_Feature> aFeature =
348 compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
349 fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
350 fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
355 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
356 const ModelHighAPI_RefAttr & theLine)
358 std::shared_ptr<ModelAPI_Feature> aFeature =
359 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
360 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
365 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
366 const ModelHighAPI_RefAttr & theLine,
367 const ModelHighAPI_Double & theValue)
369 std::shared_ptr<ModelAPI_Feature> aFeature =
370 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
371 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
372 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
377 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
378 const ModelHighAPI_RefAttr & theLine1,
379 const ModelHighAPI_RefAttr & theLine2)
381 std::shared_ptr<ModelAPI_Feature> aFeature =
382 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
383 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
384 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
389 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
390 const ModelHighAPI_RefAttr & theLine1,
391 const ModelHighAPI_RefAttr & theLine2)
393 std::shared_ptr<ModelAPI_Feature> aFeature =
394 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
395 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
396 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
401 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRigid(
402 const ModelHighAPI_RefAttr & theObject)
404 std::shared_ptr<ModelAPI_Feature> aFeature =
405 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
406 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
411 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
412 const ModelHighAPI_RefAttr & theLine)
414 std::shared_ptr<ModelAPI_Feature> aFeature =
415 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
416 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
421 //--------------------------------------------------------------------------------------