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::setAngle(
318 const ModelHighAPI_RefAttr & theLine1,
319 const ModelHighAPI_RefAttr & theLine2,
320 const ModelHighAPI_Double & theValue)
322 // TODO(spo): is support of angle type necessary?
323 std::shared_ptr<ModelAPI_Feature> aFeature =
324 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
325 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
326 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
327 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
332 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
333 const ModelHighAPI_RefAttr & thePoint1,
334 const ModelHighAPI_RefAttr & thePoint2)
336 std::shared_ptr<ModelAPI_Feature> aFeature =
337 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
338 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
339 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
344 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCollinear(
345 const ModelHighAPI_RefAttr & theLine1,
346 const ModelHighAPI_RefAttr & theLine2)
348 std::shared_ptr<ModelAPI_Feature> aFeature =
349 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
350 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
351 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
356 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
357 const ModelHighAPI_RefAttr & thePoint,
358 const ModelHighAPI_RefAttr & thePointOrLine,
359 const ModelHighAPI_Double & theValue)
361 std::shared_ptr<ModelAPI_Feature> aFeature =
362 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
363 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
364 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
365 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
370 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setEqual(
371 const ModelHighAPI_RefAttr & theObject1,
372 const ModelHighAPI_RefAttr & theObject2)
374 std::shared_ptr<ModelAPI_Feature> aFeature =
375 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
376 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
377 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
382 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
383 const std::list<ModelHighAPI_RefAttr> & thePoints,
384 const ModelHighAPI_Double & theRadius)
386 std::shared_ptr<ModelAPI_Feature> aFeature =
387 compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
388 fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
389 fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
394 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
395 const ModelHighAPI_RefAttr & theLine)
397 std::shared_ptr<ModelAPI_Feature> aFeature =
398 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
399 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
404 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
405 const ModelHighAPI_RefAttr & theLine,
406 const ModelHighAPI_Double & theValue)
408 std::shared_ptr<ModelAPI_Feature> aFeature =
409 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
410 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
411 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
416 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setMiddlePoint(
417 const ModelHighAPI_RefAttr & thePoint,
418 const ModelHighAPI_RefAttr & theLine)
420 std::shared_ptr<ModelAPI_Feature> aFeature =
421 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
422 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
423 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
428 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
429 const ModelHighAPI_RefAttr & theLine1,
430 const ModelHighAPI_RefAttr & theLine2)
432 std::shared_ptr<ModelAPI_Feature> aFeature =
433 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
434 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
435 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
440 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
441 const ModelHighAPI_RefAttr & theLine1,
442 const ModelHighAPI_RefAttr & theLine2)
444 std::shared_ptr<ModelAPI_Feature> aFeature =
445 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
446 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
447 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
452 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRadius(
453 const ModelHighAPI_RefAttr & theCircleOrArc,
454 const ModelHighAPI_Double & theValue)
456 std::shared_ptr<ModelAPI_Feature> aFeature =
457 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
458 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
459 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
464 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRigid(
465 const ModelHighAPI_RefAttr & theObject)
467 // TODO(spo): should it be renamed to Fixed?
468 std::shared_ptr<ModelAPI_Feature> aFeature =
469 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
470 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
475 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setTangent(
476 const ModelHighAPI_RefAttr & theLine,
477 const ModelHighAPI_RefAttr & theCircle)
479 std::shared_ptr<ModelAPI_Feature> aFeature =
480 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
481 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
482 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
487 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
488 const ModelHighAPI_RefAttr & theLine)
490 std::shared_ptr<ModelAPI_Feature> aFeature =
491 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
492 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
497 //--------------------------------------------------------------------------------------