1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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_Sketch.h"
21 //--------------------------------------------------------------------------------------
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_ConstraintAngle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintCollinear.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_Fillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMiddle.h>
34 #include <SketchPlugin_ConstraintMirror.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
39 #include <SketchPlugin_Trim.h>
40 #include <SketchPlugin_Split.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketcherPrs_Tools.h>
44 //--------------------------------------------------------------------------------------
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_ResultConstruction.h>
48 #include <ModelHighAPI_Double.h>
49 #include <ModelHighAPI_Dumper.h>
50 #include <ModelHighAPI_RefAttr.h>
51 #include <ModelHighAPI_Selection.h>
52 #include <ModelHighAPI_Services.h>
53 #include <ModelHighAPI_Tools.h>
54 //--------------------------------------------------------------------------------------
55 #include "SketchAPI_Arc.h"
56 #include "SketchAPI_Circle.h"
57 #include "SketchAPI_Ellipse.h"
58 #include "SketchAPI_IntersectionPoint.h"
59 #include "SketchAPI_Line.h"
60 #include "SketchAPI_MacroArc.h"
61 #include "SketchAPI_MacroCircle.h"
62 #include "SketchAPI_MacroEllipse.h"
63 #include "SketchAPI_Mirror.h"
64 #include "SketchAPI_Point.h"
65 #include "SketchAPI_Projection.h"
66 #include "SketchAPI_Rectangle.h"
67 #include "SketchAPI_Rotation.h"
68 #include "SketchAPI_Translation.h"
69 //--------------------------------------------------------------------------------------
70 #include <GeomAPI_Curve.h>
71 #include <GeomAPI_Dir2d.h>
72 #include <GeomAPI_PlanarEdges.h>
73 #include <GeomAPI_ShapeExplorer.h>
74 #include <GeomAPI_XY.h>
75 #include <GeomAlgoAPI_SketchBuilder.h>
77 //--------------------------------------------------------------------------------------
78 SketchAPI_Sketch::SketchAPI_Sketch(
79 const std::shared_ptr<ModelAPI_Feature> & theFeature)
80 : ModelHighAPI_Interface(theFeature)
85 SketchAPI_Sketch::SketchAPI_Sketch(
86 const std::shared_ptr<ModelAPI_Feature> & theFeature,
87 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
88 : ModelHighAPI_Interface(theFeature)
95 SketchAPI_Sketch::SketchAPI_Sketch(
96 const std::shared_ptr<ModelAPI_Feature> & theFeature,
97 const ModelHighAPI_Selection & theExternal)
98 : ModelHighAPI_Interface(theFeature)
101 setExternal(theExternal);
105 SketchAPI_Sketch::SketchAPI_Sketch(
106 const std::shared_ptr<ModelAPI_Feature> & theFeature,
107 std::shared_ptr<ModelAPI_Object> thePlaneObject)
108 : ModelHighAPI_Interface(theFeature)
111 setExternal(thePlaneObject);
115 SketchAPI_Sketch::~SketchAPI_Sketch()
120 //--------------------------------------------------------------------------------------
121 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
123 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
126 //--------------------------------------------------------------------------------------
127 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
129 fillAttribute(thePlane->origin(), myorigin);
130 fillAttribute(thePlane->dirX(), mydirX);
131 fillAttribute(thePlane->normal(), mynormal);
136 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
137 bool theRemoveExternalDependency)
139 FeaturePtr aSketch = feature();
141 DocumentPtr aDoc = aSketch->document();
142 bool useVisible = false;
143 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
144 aDoc->setCurrentFeature(aSketch, useVisible);
146 if (theRemoveExternalDependency)
147 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
149 setExternal(thePlane);
151 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
154 //--------------------------------------------------------------------------------------
155 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
157 fillAttribute(theExternal, myexternal);
162 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
164 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
165 ModelHighAPI_Selection aSel(aRes);
169 //--------------------------------------------------------------------------------------
170 void SketchAPI_Sketch::setValue(
171 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
172 const ModelHighAPI_Double & theValue)
174 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
176 // theConstraint->execute();
179 //--------------------------------------------------------------------------------------
180 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
182 const_cast<SketchAPI_Sketch*>(this)->execute();
184 std::list<ModelHighAPI_Selection> aSelectionList;
186 ResultConstructionPtr aResultConstruction =
187 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
188 if (aResultConstruction.get() == NULL)
189 return aSelectionList;
191 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
192 aSelectionList.push_back(
193 ModelHighAPI_Selection(aResultConstruction,
194 aResultConstruction->face(anIndex)));
197 return aSelectionList;
200 //--------------------------------------------------------------------------------------
201 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
202 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
204 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
205 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
208 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
209 const ModelHighAPI_Selection & theExternal)
211 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
212 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
215 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
216 const std::string & theExternalName)
218 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
220 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
223 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
224 std::shared_ptr<ModelAPI_Object> thePlaneObject)
226 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
227 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
231 //--------------------------------------------------------------------------------------
232 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
234 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
235 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
236 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
237 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
238 PointPtr aPoint(new SketchAPI_Point(aFeature));
239 aFreePoints.push_back(aPoint);
244 //--------------------------------------------------------------------------------------
245 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
247 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
248 if (aCurve->isTrimmed())
249 aCurve = aCurve->basisCurve();
253 void SketchAPI_Sketch::changeFacesOrder(
254 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
256 // collect faces of the sketch
257 ResultConstructionPtr aSketchResult =
258 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
259 if (!aSketchResult) {
260 // sketch is nested to a boolean operation, thus, it has no result yet.
261 feature()->execute();
263 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
265 std::list<GeomFacePtr> aFaces;
266 int aFacesNum = aSketchResult->facesNum();
267 for (int i = 0; i < aFacesNum; ++i)
268 aFaces.push_back(aSketchResult->face(i));
269 // find new faces order according to the given lists of edges
270 std::list<GeomFacePtr> aNewFacesOrder;
271 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
272 for (; anIt != theFaces.end(); ++anIt) {
273 // find the appropriate face
274 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
275 for (; aFIt != aFaces.end(); ++aFIt) {
276 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
277 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
278 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
279 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
282 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
283 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
284 if (!aCurve1->isEqual(aCurve2))
288 if (aEdgeIt == anIt->end() && !aFExp.more()) {
290 aNewFacesOrder.push_back(*aFIt);
296 // place the rest faces at the end of new faces list
298 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
299 // update the result of the sketch with the new order of faces
300 aSketchResult->setFacesOrder(aNewFacesOrder);
303 //--------------------------------------------------------------------------------------
304 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
305 double theX, double theY)
307 std::shared_ptr<ModelAPI_Feature> aFeature =
308 compositeFeature()->addFeature(SketchPlugin_Point::ID());
309 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
311 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
312 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
314 std::shared_ptr<ModelAPI_Feature> aFeature =
315 compositeFeature()->addFeature(SketchPlugin_Point::ID());
316 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
318 std::shared_ptr<SketchAPI_Point>
319 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
321 std::shared_ptr<ModelAPI_Feature> aFeature =
322 compositeFeature()->addFeature(SketchPlugin_Point::ID());
323 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
325 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
327 std::shared_ptr<ModelAPI_Feature> aFeature =
328 compositeFeature()->addFeature(SketchPlugin_Point::ID());
329 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
332 //--------------------------------------------------------------------------------------
333 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
334 const ModelHighAPI_Selection & theExternal,
337 std::shared_ptr<ModelAPI_Feature> aFeature =
338 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
339 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
340 anIntersection->setIncludeToResult(theKeepResult);
341 return anIntersection;
343 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
344 const std::string & theExternalName,
347 std::shared_ptr<ModelAPI_Feature> aFeature =
348 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
349 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
350 anIntersection->setIncludeToResult(theKeepResult);
351 return anIntersection;
354 //--------------------------------------------------------------------------------------
355 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
356 double theX2, double theY2)
358 std::shared_ptr<ModelAPI_Feature> aFeature =
359 compositeFeature()->addFeature(SketchPlugin_Line::ID());
360 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
362 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
363 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
364 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
366 std::shared_ptr<ModelAPI_Feature> aFeature =
367 compositeFeature()->addFeature(SketchPlugin_Line::ID());
368 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
370 std::shared_ptr<SketchAPI_Line>
371 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
373 std::shared_ptr<ModelAPI_Feature> aFeature =
374 compositeFeature()->addFeature(SketchPlugin_Line::ID());
375 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
377 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
379 std::shared_ptr<ModelAPI_Feature> aFeature =
380 compositeFeature()->addFeature(SketchPlugin_Line::ID());
381 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
384 //--------------------------------------------------------------------------------------
385 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
386 double theX2, double theY2)
388 std::shared_ptr<ModelAPI_Feature> aFeature =
389 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
390 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
392 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
393 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
394 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
396 std::shared_ptr<ModelAPI_Feature> aFeature =
397 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
398 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
401 //--------------------------------------------------------------------------------------
402 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
406 std::shared_ptr<ModelAPI_Feature> aFeature =
407 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
408 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
411 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
412 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
415 std::shared_ptr<ModelAPI_Feature> aFeature =
416 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
417 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
420 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
425 std::shared_ptr<ModelAPI_Feature> aFeature =
426 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
427 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
428 thePassedX, thePassedY));
431 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
432 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
433 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
435 std::shared_ptr<ModelAPI_Feature> aFeature =
436 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
437 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
440 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
441 double theX2, double theY2,
442 double theX3, double theY3)
444 std::shared_ptr<ModelAPI_Feature> aFeature =
445 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
446 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
451 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
452 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
453 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
454 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
456 std::shared_ptr<ModelAPI_Feature> aFeature =
457 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
458 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
461 std::shared_ptr<SketchAPI_Circle>
462 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
464 std::shared_ptr<ModelAPI_Feature> aFeature =
465 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
466 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
469 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
471 std::shared_ptr<ModelAPI_Feature> aFeature =
472 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
473 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
476 //--------------------------------------------------------------------------------------
477 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
478 double theStartX, double theStartY,
479 double theEndX, double theEndY,
482 std::shared_ptr<ModelAPI_Feature> aFeature =
483 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
484 return ArcPtr(new SketchAPI_Arc(aFeature,
485 theCenterX, theCenterY,
486 theStartX, theStartY,
491 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
492 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
493 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
494 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
497 std::shared_ptr<ModelAPI_Feature> aFeature =
498 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
499 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
502 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
503 double theEndX, double theEndY,
504 double thePassedX, double thePassedY)
506 std::shared_ptr<ModelAPI_Feature> aFeature =
507 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
508 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
509 theStartX, theStartY,
511 thePassedX, thePassedY));
514 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
515 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
516 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
517 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
519 std::shared_ptr<ModelAPI_Feature> aFeature =
520 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
521 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
524 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
525 const ModelHighAPI_RefAttr& theTangentPoint,
526 double theEndX, double theEndY,
530 std::shared_ptr<ModelAPI_Feature> aFeature =
531 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
532 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
534 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
536 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
540 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
541 const ModelHighAPI_RefAttr& theTangentPoint,
542 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
546 std::shared_ptr<ModelAPI_Feature> aFeature =
547 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
548 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
550 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
552 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
556 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
558 std::shared_ptr<ModelAPI_Feature> aFeature =
559 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
560 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
563 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
565 std::shared_ptr<ModelAPI_Feature> aFeature =
566 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
567 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
570 //--------------------------------------------------------------------------------------
571 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
572 double theCenterX, double theCenterY,
573 double theFocusX, double theFocusY,
574 double theMinorRadius)
576 std::shared_ptr<ModelAPI_Feature> aFeature =
577 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
578 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
579 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
582 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
583 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
584 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
585 double theMinorRadius)
587 std::shared_ptr<ModelAPI_Feature> aFeature =
588 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
589 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
592 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
593 double thePoint1X, double thePoint1Y,
594 double thePoint2X, double thePoint2Y,
595 double thePassedX, double thePassedY,
598 std::shared_ptr<ModelAPI_Feature> aFeature =
599 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
600 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
601 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
604 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
605 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint1,
606 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint2,
607 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePassedPoint,
610 std::shared_ptr<ModelAPI_Feature> aFeature =
611 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
612 MacroEllipsePtr anEllipse;
613 if (thePoint1.second.isEmpty() &&
614 thePoint2.second.isEmpty() &&
615 thePassedPoint.second.isEmpty()) {
616 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
617 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
620 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
621 thePoint1.first, thePoint1.second,
622 thePoint2.first, thePoint2.second,
623 thePassedPoint.first, thePassedPoint.second,
629 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
630 const ModelHighAPI_Selection & theExternal)
632 std::shared_ptr<ModelAPI_Feature> aFeature =
633 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
634 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
637 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
638 const std::string & theExternalName)
640 std::shared_ptr<ModelAPI_Feature> aFeature =
641 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
642 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
645 //--------------------------------------------------------------------------------------
646 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
647 const ModelHighAPI_Selection & theExternalFeature,
650 std::shared_ptr<ModelAPI_Feature> aFeature =
651 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
652 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
653 aProjection->setIncludeToResult(theKeepResult);
657 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
658 const std::string & theExternalName,
661 std::shared_ptr<ModelAPI_Feature> aFeature =
662 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
663 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
664 aProjection->setIncludeToResult(theKeepResult);
668 //--------------------------------------------------------------------------------------
669 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
670 const ModelHighAPI_RefAttr & theMirrorLine,
671 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
673 std::shared_ptr<ModelAPI_Feature> aFeature =
674 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
675 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
678 //--------------------------------------------------------------------------------------
679 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
680 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
681 const ModelHighAPI_RefAttr & thePoint1,
682 const ModelHighAPI_RefAttr & thePoint2,
683 const ModelHighAPI_Integer & theNumberOfObjects,
686 std::shared_ptr<ModelAPI_Feature> aFeature =
687 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
688 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
689 thePoint2, theNumberOfObjects, theFullValue));
692 //--------------------------------------------------------------------------------------
693 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
694 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
695 const ModelHighAPI_RefAttr & theCenter,
696 const ModelHighAPI_Double & theAngle,
697 const ModelHighAPI_Integer & theNumberOfObjects,
701 std::shared_ptr<ModelAPI_Feature> aFeature =
702 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
704 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
705 theAngle, theNumberOfObjects, theFullValue, theReversed));
708 //--------------------------------------------------------------------------------------
709 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
710 const ModelHighAPI_Reference& theFeature,
711 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
713 std::shared_ptr<ModelAPI_Feature> aFeature =
714 compositeFeature()->addFeature(SketchPlugin_Split::ID());
715 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
717 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
718 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
719 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
720 fillAttribute(thePositionPoint, aPointAttr);
723 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
726 //--------------------------------------------------------------------------------------
727 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
728 const ModelHighAPI_Reference& theFeature,
729 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
731 std::shared_ptr<ModelAPI_Feature> aFeature =
732 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
733 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
735 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
736 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
737 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
738 fillAttribute(thePositionPoint, aPointAttr);
741 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
744 //--------------------------------------------------------------------------------------
745 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
746 const ModelHighAPI_RefAttr & theLine1,
747 const ModelHighAPI_RefAttr & theLine2,
748 const ModelHighAPI_Double & theValue)
750 std::shared_ptr<ModelAPI_Feature> aFeature =
751 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
752 fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
753 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
754 // fill the value before llines to avoid calculation of angle value by the Angle feature
755 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
756 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
757 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
759 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
762 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
763 const ModelHighAPI_RefAttr & theLine1,
764 const ModelHighAPI_RefAttr & theLine2,
765 const ModelHighAPI_Double & theValue)
767 std::shared_ptr<ModelAPI_Feature> aFeature =
768 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
769 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
770 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
771 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
772 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
773 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
775 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
778 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
779 const ModelHighAPI_RefAttr & theLine1,
780 const ModelHighAPI_RefAttr & theLine2,
781 const ModelHighAPI_Double & theValue)
783 std::shared_ptr<ModelAPI_Feature> aFeature =
784 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
785 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
786 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
787 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
788 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
789 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
791 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
794 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
795 const ModelHighAPI_RefAttr & thePoint1,
796 const ModelHighAPI_RefAttr & thePoint2)
798 std::shared_ptr<ModelAPI_Feature> aFeature =
799 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
800 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
801 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
803 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
806 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
807 const ModelHighAPI_RefAttr & theLine1,
808 const ModelHighAPI_RefAttr & theLine2)
810 std::shared_ptr<ModelAPI_Feature> aFeature =
811 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
812 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
813 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
815 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
818 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
819 const ModelHighAPI_RefAttr & thePoint,
820 const ModelHighAPI_RefAttr & thePointOrLine,
821 const ModelHighAPI_Double & theValue,
824 std::shared_ptr<ModelAPI_Feature> aFeature =
825 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
826 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
827 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
828 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
829 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
831 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
834 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
835 const ModelHighAPI_RefAttr & thePoint,
836 const ModelHighAPI_RefAttr & thePointOrLine,
837 const ModelHighAPI_Double & theValue)
839 return setDistance(thePoint, thePointOrLine, theValue, true);
842 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
843 const ModelHighAPI_RefAttr & thePoint,
844 const ModelHighAPI_RefAttr & thePointOrLine,
845 const ModelHighAPI_Double & theValue)
847 return setDistance(thePoint, thePointOrLine, theValue, false);
850 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
851 const ModelHighAPI_RefAttr & thePoint1,
852 const ModelHighAPI_RefAttr & thePoint2,
853 const ModelHighAPI_Double & theValue)
855 std::shared_ptr<ModelAPI_Feature> aFeature =
856 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
857 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
858 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
859 fillAttribute(theValue,
860 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
862 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
865 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
866 const ModelHighAPI_RefAttr & thePoint1,
867 const ModelHighAPI_RefAttr & thePoint2,
868 const ModelHighAPI_Double & theValue)
870 std::shared_ptr<ModelAPI_Feature> aFeature =
871 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
872 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
873 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
874 fillAttribute(theValue,
875 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
877 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
880 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
881 const ModelHighAPI_RefAttr & theObject1,
882 const ModelHighAPI_RefAttr & theObject2)
884 std::shared_ptr<ModelAPI_Feature> aFeature =
885 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
886 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
887 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
889 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
892 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
893 const ModelHighAPI_RefAttr & thePoint)
895 std::shared_ptr<ModelAPI_Feature> aFeature =
896 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
897 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
898 apply(); // finish operation to remove Fillet feature correcly
899 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
902 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
903 const ModelHighAPI_RefAttr & thePoint,
904 const ModelHighAPI_Double & theRadius)
906 CompositeFeaturePtr aSketch = compositeFeature();
907 int aNbSubs = aSketch->numberOfSubs();
910 InterfacePtr aFilletFeature = setFillet(thePoint);
912 // set radius for just created arc
913 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
914 if (anArc->getKind() == SketchPlugin_Arc::ID())
915 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
917 return aFilletFeature;
920 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
921 const ModelHighAPI_RefAttr & theObject)
923 std::shared_ptr<ModelAPI_Feature> aFeature =
924 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
925 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
927 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
930 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
931 const ModelHighAPI_RefAttr & theLine)
933 std::shared_ptr<ModelAPI_Feature> aFeature =
934 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
935 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
937 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
940 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
941 const ModelHighAPI_RefAttr & theLine,
942 const ModelHighAPI_Double & theValue)
944 std::shared_ptr<ModelAPI_Feature> aFeature =
945 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
946 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
947 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
949 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
952 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
953 const ModelHighAPI_RefAttr & thePoint,
954 const ModelHighAPI_RefAttr & theLine)
956 std::shared_ptr<ModelAPI_Feature> aFeature =
957 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
958 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
959 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
961 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
964 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
965 const ModelHighAPI_RefAttr & theLine1,
966 const ModelHighAPI_RefAttr & theLine2)
968 std::shared_ptr<ModelAPI_Feature> aFeature =
969 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
970 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
971 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
973 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
976 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
977 const ModelHighAPI_RefAttr & theLine1,
978 const ModelHighAPI_RefAttr & theLine2)
980 std::shared_ptr<ModelAPI_Feature> aFeature =
981 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
982 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
983 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
985 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
988 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
989 const ModelHighAPI_RefAttr & theCircleOrArc,
990 const ModelHighAPI_Double & theValue)
992 std::shared_ptr<ModelAPI_Feature> aFeature =
993 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
994 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
995 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
997 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1000 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1001 const ModelHighAPI_RefAttr & theLine,
1002 const ModelHighAPI_RefAttr & theCircle)
1004 std::shared_ptr<ModelAPI_Feature> aFeature =
1005 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1006 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1007 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1008 aFeature->execute();
1009 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1012 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1013 const ModelHighAPI_RefAttr & theLine)
1015 std::shared_ptr<ModelAPI_Feature> aFeature =
1016 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1017 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1018 aFeature->execute();
1019 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1022 //--------------------------------------------------------------------------------------
1024 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1026 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1027 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1030 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1032 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1033 theFeature->attribute(SketchPlugin_Line::START_ID()));
1034 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1035 theFeature->attribute(SketchPlugin_Line::END_ID()));
1037 if (!aStartAttr || !aEndAttr)
1038 return std::shared_ptr<GeomAPI_Pnt2d>();
1040 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1041 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1042 return std::shared_ptr<GeomAPI_Pnt2d>(
1043 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1046 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1048 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1049 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1050 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1052 if (!aCenter || !aRadius)
1053 return std::shared_ptr<GeomAPI_Pnt2d>();
1055 return std::shared_ptr<GeomAPI_Pnt2d>(
1056 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1059 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1061 static const double PI = 3.141592653589793238463;
1063 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1064 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1065 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1066 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1067 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1068 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1070 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1071 return std::shared_ptr<GeomAPI_Pnt2d>();
1073 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1074 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1075 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1076 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1078 double anAngle = aStartDir->angle(aEndDir);
1079 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1080 if (isReversed && anAngle > 0.)
1081 anAngle -= 2.0 * PI;
1082 else if (!isReversed && anAngle <= 0.)
1083 anAngle += 2.0 * PI;
1085 double cosA = cos(anAngle);
1086 double sinA = sin(anAngle);
1088 // rotate start dir to find middle point on arc
1089 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1090 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1091 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1093 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1096 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature)
1098 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1099 theFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()));
1100 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1103 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1105 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1106 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1108 // move only features of the following types
1109 const std::string& aFeatureKind = aFeature->getKind();
1110 if (aFeatureKind == SketchPlugin_Point::ID())
1111 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1112 else if (aFeatureKind == SketchPlugin_Line::ID())
1113 aMiddlePoint = middlePointOnLine(aFeature);
1114 else if (aFeatureKind == SketchPlugin_Circle::ID())
1115 aMiddlePoint = pointOnCircle(aFeature);
1116 else if (aFeatureKind == SketchPlugin_Arc::ID())
1117 aMiddlePoint = middlePointOnArc(aFeature);
1118 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1119 aMiddlePoint = pointOnEllipse(aFeature);
1121 return aMiddlePoint;
1124 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1125 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1127 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1128 theMovedEntity.fillMessage(aMessage);
1130 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1131 if (aMessage->movedAttribute())
1132 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1134 anOriginalPosition = middlePoint(aMessage->movedObject());
1136 if (!anOriginalPosition)
1137 return; // something has gone wrong, do not process movement
1139 aMessage->setOriginalPosition(anOriginalPosition);
1140 aMessage->setCurrentPosition(theTargetPoint);
1141 Events_Loop::loop()->send(aMessage);
1144 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1145 double theTargetX, double theTargetY)
1147 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1148 move(theMovedEntity, aTargetPoint);
1151 //--------------------------------------------------------------------------------------
1153 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1155 FeaturePtr aBase = feature();
1156 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1157 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1158 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1159 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1160 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1161 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1162 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1164 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1167 //--------------------------------------------------------------------------------------
1169 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1171 // collect edges of the first face
1172 std::list<GeomShapePtr> anEdges1;
1173 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1174 anEdges1.push_back(anExp.current());
1175 // compare edges of faces
1176 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1177 GeomShapePtr aCurrent = anExp.current();
1178 bool isFound = false;
1179 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1180 for (; anIt1 != anEdges1.end(); ++anIt1)
1181 if (aCurrent->isSameGeometry(*anIt1)) {
1183 anEdges1.erase(anIt1);
1189 return !anEdges1.empty();
1192 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1194 ResultConstructionPtr aSketchResult =
1195 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1199 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1200 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1204 // collect faces constructed by SketchBuilder algorithm
1205 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1206 aWires->norm(), aWires);
1207 const ListOfShape& aFaces = aSketchBuilder.faces();
1209 // compare faces stored in sketch with faces generated by SketchBuilder
1210 int aNbSketchFaces = aSketchResult->facesNum();
1212 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1213 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1214 ++aFIt, ++aFaceIndex) {
1215 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1216 GeomFacePtr aCurFace = (*aFIt)->face();
1217 if (isDifferent(aSketchFace, aCurFace))
1223 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1224 std::list<std::list<ResultPtr> >& theEdges)
1226 ResultConstructionPtr aSketchResult =
1227 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1231 // collect curves of the sketch
1232 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1233 int aSubNum = theSketch->numberOfSubs();
1234 for (int a = 0; a < aSubNum; ++a) {
1235 FeaturePtr aSub = theSketch->subFeature(a);
1236 const std::list<ResultPtr>& aResults = aSub->results();
1237 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1238 for (; aRes != aResults.cend(); aRes++) {
1239 GeomShapePtr aCurShape = (*aRes)->shape();
1240 if (aCurShape && aCurShape->isEdge())
1241 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1245 // convert each face to the list of results of its edges
1246 int aFacesNum = aSketchResult->facesNum();
1247 for (int a = 0; a < aFacesNum; ++a) {
1248 theEdges.push_back(std::list<ResultPtr>());
1249 std::list<ResultPtr>& aCurEdges = theEdges.back();
1251 GeomFacePtr aFace = aSketchResult->face(a);
1252 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1253 anExp.more(); anExp.next()) {
1254 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1255 aCurEdges.push_back(aCurves[aCurrent]);
1260 //--------------------------------------------------------------------------------------
1262 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1264 FeaturePtr aBase = feature();
1265 const std::string& aDocName = theDumper.name(aBase->document());
1267 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1268 if (anExternal->value()) {
1269 theDumper << aBase << " = model.addSketch(" << aDocName <<
1270 ", " << anExternal << ")" << std::endl;
1272 // Sketch is base on a plane.
1273 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1274 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1275 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1276 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1277 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1278 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1280 // Check the plane is coordinate plane
1281 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1282 if(anExternal->context()) { // checking for selected planes
1283 if (!aPlaneName.empty()
1284 && anExternal->context()->data()
1285 && anExternal->context()->data()->name() == aPlaneName) {
1286 // dump sketch based on coordinate plane
1287 theDumper << aBase << " = model.addSketch(" << aDocName
1288 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1289 } else { // some other plane
1290 theDumper << aBase << " = model.addSketch(" << aDocName <<
1291 ", " << anExternal<< ")" << std::endl;
1294 if (aPlaneName.empty()) {
1295 // needs import additional module
1296 theDumper.importModule("GeomAPI");
1297 // dump plane parameters
1298 const std::string& aSketchName = theDumper.name(aBase);
1299 std::string anOriginName = aSketchName + "_origin";
1300 std::string aNormalName = aSketchName + "_norm";
1301 std::string aDirXName = aSketchName + "_dirx";
1302 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1303 // and then dumplicate dumping it in the next line
1304 theDumper << anOriginName << " = " << anOrigin << "\n"
1305 << aNormalName << " = " << aNormal << "\n"
1306 << aDirXName << " = " << aDirX << "\n";
1307 // dump sketch based on arbitrary plane
1308 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1309 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1311 // dump sketch based on coordinate plane
1312 theDumper << aBase << " = model.addSketch(" << aDocName
1313 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1318 // dump sketch's subfeatures
1319 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1320 theDumper.processSubs(aCompFeat, true);
1322 // if face order differs to the order generated by SketchBuilder,
1323 // dump the list of faces for correct execution of the script
1324 if (isCustomFacesOrder(aCompFeat)) {
1325 std::list<std::list<ResultPtr> > aFaces;
1326 edgesOfSketchFaces(aCompFeat, aFaces);
1328 const std::string& aSketchName = theDumper.name(aBase);
1329 std::string aMethodName(".changeFacesOrder");
1330 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1332 theDumper << aSketchName << aMethodName << "([";
1333 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1334 aFIt != aFaces.end(); ++aFIt) {
1335 if (aFIt != aFaces.begin())
1336 theDumper << ",\n" << aSpaceShift << " ";
1339 theDumper << "\n" << aSpaceShift << " ])" << std::endl;