1 // Copyright (C) 2014-2023 CEA, EDF
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_CurveFitting.h>
40 #include <SketchPlugin_Trim.h>
41 #include <SketchPlugin_Split.h>
42 #include <SketchPlugin_ConstraintTangent.h>
43 #include <SketchPlugin_ConstraintVertical.h>
44 #include <SketchPlugin_MacroBSpline.h>
45 #include <SketchPlugin_SketchCopy.h>
46 #include <SketchPlugin_Offset.h>
47 #include <SketcherPrs_Tools.h>
48 //--------------------------------------------------------------------------------------
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_CompositeFeature.h>
51 #include <ModelAPI_ResultConstruction.h>
52 #include <ModelHighAPI_Double.h>
53 #include <ModelHighAPI_Dumper.h>
54 #include <ModelHighAPI_RefAttr.h>
55 #include <ModelHighAPI_Selection.h>
56 #include <ModelHighAPI_Services.h>
57 #include <ModelHighAPI_Tools.h>
58 //--------------------------------------------------------------------------------------
59 #include "SketchAPI_Arc.h"
60 #include "SketchAPI_BSpline.h"
61 #include "SketchAPI_Circle.h"
62 #include "SketchAPI_Ellipse.h"
63 #include "SketchAPI_EllipticArc.h"
64 #include "SketchAPI_IntersectionPoint.h"
65 #include "SketchAPI_Line.h"
66 #include "SketchAPI_MacroArc.h"
67 #include "SketchAPI_MacroCircle.h"
68 #include "SketchAPI_MacroEllipse.h"
69 #include "SketchAPI_MacroEllipticArc.h"
70 #include "SketchAPI_MacroMiddlePoint.h"
71 #include "SketchAPI_Mirror.h"
72 #include "SketchAPI_Offset.h"
73 #include "SketchAPI_Point.h"
74 #include "SketchAPI_Projection.h"
75 #include "SketchAPI_Rectangle.h"
76 #include "SketchAPI_Rotation.h"
77 #include "SketchAPI_Translation.h"
78 #include "SketchAPI_Constraint.h"
79 //--------------------------------------------------------------------------------------
80 #include <GeomAPI_Curve.h>
81 #include <GeomAPI_Dir2d.h>
82 #include <GeomAPI_PlanarEdges.h>
83 #include <GeomAPI_ShapeExplorer.h>
84 #include <GeomAPI_XY.h>
85 #include <GeomAlgoAPI_SketchBuilder.h>
89 //--------------------------------------------------------------------------------------
92 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
94 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
95 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
98 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
100 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101 theFeature->attribute(SketchPlugin_Line::START_ID()));
102 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103 theFeature->attribute(SketchPlugin_Line::END_ID()));
105 if (!aStartAttr || !aEndAttr)
106 return std::shared_ptr<GeomAPI_Pnt2d>();
108 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
109 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
110 return std::shared_ptr<GeomAPI_Pnt2d>(
111 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
114 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
116 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
118 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
120 if (!aCenter || !aRadius)
121 return std::shared_ptr<GeomAPI_Pnt2d>();
123 return std::shared_ptr<GeomAPI_Pnt2d>(
124 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
127 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
129 static const double PI = 3.141592653589793238463;
131 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
133 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
134 theFeature->attribute(SketchPlugin_Arc::START_ID()));
135 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
136 theFeature->attribute(SketchPlugin_Arc::END_ID()));
138 if (!aCenterAttr || !aStartAttr || !aEndAttr)
139 return std::shared_ptr<GeomAPI_Pnt2d>();
141 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
142 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
143 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
144 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
146 double anAngle = aStartDir->angle(aEndDir);
147 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
148 if (isReversed && anAngle > 0.)
150 else if (!isReversed && anAngle <= 0.)
153 double cosA = cos(anAngle);
154 double sinA = sin(anAngle);
156 // rotate start dir to find middle point on arc
157 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
158 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
159 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
161 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
164 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
165 bool isEllipse = true)
167 const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
168 SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
169 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
170 theFeature->attribute(anAttrName));
171 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
174 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
175 SketchAPI_Sketch* theSketch)
177 GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
178 GeomPointPtr aMiddle = anEdge.middlePoint();
179 return theSketch->to2D(aMiddle);
182 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
183 SketchAPI_Sketch* theSketch)
185 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
186 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
188 // move only features of the following types
189 const std::string& aFeatureKind = aFeature->getKind();
190 if (aFeatureKind == SketchPlugin_Point::ID())
191 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
192 else if (aFeatureKind == SketchPlugin_Line::ID())
193 aMiddlePoint = middlePointOnLine(aFeature);
194 else if (aFeatureKind == SketchPlugin_Circle::ID())
195 aMiddlePoint = pointOnCircle(aFeature);
196 else if (aFeatureKind == SketchPlugin_Arc::ID())
197 aMiddlePoint = middlePointOnArc(aFeature);
198 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
199 aMiddlePoint = pointOnEllipse(aFeature);
200 else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
201 aMiddlePoint = pointOnEllipse(aFeature, false);
202 else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
203 aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
204 aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
209 SketchAPI_Sketch::SketchAPI_Sketch(
210 const std::shared_ptr<ModelAPI_Feature> & theFeature)
211 : ModelHighAPI_Interface(theFeature)
216 SketchAPI_Sketch::SketchAPI_Sketch(
217 const std::shared_ptr<ModelAPI_Feature> & theFeature,
218 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
219 : ModelHighAPI_Interface(theFeature)
226 SketchAPI_Sketch::SketchAPI_Sketch(
227 const std::shared_ptr<ModelAPI_Feature> & theFeature,
228 const ModelHighAPI_Selection & theExternal)
229 : ModelHighAPI_Interface(theFeature)
232 setExternal(theExternal);
236 SketchAPI_Sketch::SketchAPI_Sketch(
237 const std::shared_ptr<ModelAPI_Feature> & theFeature,
238 std::shared_ptr<ModelAPI_Object> thePlaneObject)
239 : ModelHighAPI_Interface(theFeature)
242 setExternal(thePlaneObject);
246 SketchAPI_Sketch::~SketchAPI_Sketch()
251 //--------------------------------------------------------------------------------------
252 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
254 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
257 //--------------------------------------------------------------------------------------
258 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
260 fillAttribute(thePlane->origin(), myorigin);
261 fillAttribute(thePlane->dirX(), mydirX);
262 fillAttribute(thePlane->normal(), mynormal);
267 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
268 bool theRemoveExternalDependency)
270 FeaturePtr aSketch = feature();
272 DocumentPtr aDoc = aSketch->document();
273 bool useVisible = false;
274 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
275 aDoc->setCurrentFeature(aSketch, useVisible);
277 if (theRemoveExternalDependency)
278 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
280 setExternal(thePlane);
282 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
285 //--------------------------------------------------------------------------------------
286 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
288 fillAttribute(theExternal, myexternal);
293 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
295 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
296 ModelHighAPI_Selection aSel(aRes);
300 //--------------------------------------------------------------------------------------
301 void SketchAPI_Sketch::setValue(
302 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
303 const ModelHighAPI_Double & theValue)
305 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
307 // theConstraint->execute();
310 //--------------------------------------------------------------------------------------
311 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
313 const_cast<SketchAPI_Sketch*>(this)->execute();
315 std::list<ModelHighAPI_Selection> aSelectionList;
317 ResultConstructionPtr aResultConstruction =
318 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
319 if (aResultConstruction.get() == NULL)
320 return aSelectionList;
322 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
323 aSelectionList.push_back(
324 ModelHighAPI_Selection(aResultConstruction,
325 aResultConstruction->face(anIndex)));
328 return aSelectionList;
331 //--------------------------------------------------------------------------------------
332 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
333 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
335 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
336 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
339 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
340 const ModelHighAPI_Selection & theExternal)
342 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
343 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
346 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
347 const std::wstring & theExternalName)
349 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
351 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
354 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
355 std::shared_ptr<ModelAPI_Object> thePlaneObject)
357 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
358 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
361 //--------------------------------------------------------------------------------------
362 SketchPtr copySketch(const std::shared_ptr<ModelAPI_Document> & thePart,
363 const SketchPtr & theSketch)
365 FeaturePtr aCopyer = thePart->addFeature(SketchPlugin_SketchCopy::ID());
366 aCopyer->reference(SketchPlugin_SketchCopy::BASE_ID())->setValue(theSketch->feature());
369 FeaturePtr aNewSketch = thePart->nextFeature(aCopyer);
371 // perform removing the macro-feature
372 thePart->removeFeature(aCopyer);
375 return SketchPtr(new SketchAPI_Sketch(aNewSketch));
379 //--------------------------------------------------------------------------------------
380 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
382 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
383 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
384 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
385 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
386 PointPtr aPoint(new SketchAPI_Point(aFeature));
387 aFreePoints.push_back(aPoint);
392 //--------------------------------------------------------------------------------------
393 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
395 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
396 if (aCurve->isTrimmed())
397 aCurve = aCurve->basisCurve();
401 void SketchAPI_Sketch::changeFacesOrder(
402 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
404 // collect faces of the sketch
405 ResultConstructionPtr aSketchResult =
406 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
407 if (!aSketchResult) {
408 // sketch is nested to a boolean operation, thus, it has no result yet.
409 feature()->execute();
411 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
413 std::list<GeomFacePtr> aFaces;
414 int aFacesNum = aSketchResult->facesNum();
415 for (int i = 0; i < aFacesNum; ++i)
416 aFaces.push_back(aSketchResult->face(i));
417 // find new faces order according to the given lists of edges
418 std::list<GeomFacePtr> aNewFacesOrder;
419 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
420 for (; anIt != theFaces.end(); ++anIt) {
421 // find the appropriate face
422 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
423 for (; aFIt != aFaces.end(); ++aFIt) {
424 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
425 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
426 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
427 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
430 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
431 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
432 if (!aCurve1->isEqual(aCurve2))
436 if (aEdgeIt == anIt->end() && !aFExp.more()) {
438 aNewFacesOrder.push_back(*aFIt);
444 // place the rest faces at the end of new faces list
446 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
447 // update the result of the sketch with the new order of faces
448 aSketchResult->setFacesOrder(aNewFacesOrder);
451 //--------------------------------------------------------------------------------------
452 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
453 double theX, double theY)
455 std::shared_ptr<ModelAPI_Feature> aFeature =
456 compositeFeature()->addFeature(SketchPlugin_Point::ID());
457 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
459 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
460 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
462 std::shared_ptr<ModelAPI_Feature> aFeature =
463 compositeFeature()->addFeature(SketchPlugin_Point::ID());
464 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
466 std::shared_ptr<SketchAPI_Point>
467 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
469 std::shared_ptr<ModelAPI_Feature> aFeature =
470 compositeFeature()->addFeature(SketchPlugin_Point::ID());
471 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
473 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::wstring & theExternalName)
475 std::shared_ptr<ModelAPI_Feature> aFeature =
476 compositeFeature()->addFeature(SketchPlugin_Point::ID());
477 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
480 //--------------------------------------------------------------------------------------
481 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
482 const ModelHighAPI_Selection & theExternal,
485 std::shared_ptr<ModelAPI_Feature> aFeature =
486 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
487 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
488 anIntersection->setIncludeToResult(theKeepResult);
489 return anIntersection;
491 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
492 const std::wstring & theExternalName,
495 std::shared_ptr<ModelAPI_Feature> aFeature =
496 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
497 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
498 anIntersection->setIncludeToResult(theKeepResult);
499 return anIntersection;
502 //--------------------------------------------------------------------------------------
503 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
504 double theX2, double theY2)
506 std::shared_ptr<ModelAPI_Feature> aFeature =
507 compositeFeature()->addFeature(SketchPlugin_Line::ID());
508 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
510 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
511 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
512 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
514 std::shared_ptr<ModelAPI_Feature> aFeature =
515 compositeFeature()->addFeature(SketchPlugin_Line::ID());
516 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
518 std::shared_ptr<SketchAPI_Line>
519 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
521 std::shared_ptr<ModelAPI_Feature> aFeature =
522 compositeFeature()->addFeature(SketchPlugin_Line::ID());
523 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
525 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::wstring & theExternalName)
527 std::shared_ptr<ModelAPI_Feature> aFeature =
528 compositeFeature()->addFeature(SketchPlugin_Line::ID());
529 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
532 //--------------------------------------------------------------------------------------
533 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
534 double theX2, double theY2)
536 std::shared_ptr<ModelAPI_Feature> aFeature =
537 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
538 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
540 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
541 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
542 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
544 std::shared_ptr<ModelAPI_Feature> aFeature =
545 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
546 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
549 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(
550 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & thePoint)
553 return thePoint.first;
555 AttributePtr anAttr = thePoint.second.attr();
556 if (thePoint.second.object()) {
557 FeaturePtr aFeature = ModelAPI_Feature::feature(thePoint.second.object());
559 anAttr = aFeature->attribute(SketchPlugin_Point::COORD_ID());
562 std::shared_ptr<GeomDataAPI_Point2D> aPntAttr =
563 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
565 return aPntAttr->pnt();
566 return std::shared_ptr<GeomAPI_Pnt2d>();
569 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangleCentered(
570 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & theCenter,
571 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & theCorner)
573 std::shared_ptr<ModelAPI_Feature> aFeature =
574 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
575 RectanglePtr aRect(new SketchAPI_Rectangle(aFeature));
576 fillAttribute("RectangleTypeCentered", aRect->type());
577 if (!theCenter.second.isEmpty())
578 fillAttribute(theCenter.second, aRect->centerPointRef());
579 fillAttribute(pointCoordinates(theCenter), aRect->centerPoint());
580 fillAttribute(pointCoordinates(theCorner), aRect->cornerPoint());
583 if (!theCorner.second.isEmpty() && aRect->linesList()->size() > 1) {
584 // get start point of the last line in rectangle and apply coindidence constraint
585 FeaturePtr aLine = ModelAPI_Feature::feature(aRect->linesList()->object(3));
586 AttributePtr aEndPnt = aLine->attribute(SketchPlugin_Line::START_ID());
587 setCoincident(ModelHighAPI_RefAttr(aEndPnt), theCorner.second);
592 //--------------------------------------------------------------------------------------
593 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
597 std::shared_ptr<ModelAPI_Feature> aFeature =
598 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
599 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
602 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
603 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
606 std::shared_ptr<ModelAPI_Feature> aFeature =
607 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
608 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
611 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
616 std::shared_ptr<ModelAPI_Feature> aFeature =
617 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
618 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
619 thePassedX, thePassedY));
622 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
623 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
624 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
626 std::shared_ptr<ModelAPI_Feature> aFeature =
627 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
628 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
631 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
632 double theX2, double theY2,
633 double theX3, double theY3)
635 std::shared_ptr<ModelAPI_Feature> aFeature =
636 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
637 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
642 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
643 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
644 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
645 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
647 std::shared_ptr<ModelAPI_Feature> aFeature =
648 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
649 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
652 std::shared_ptr<SketchAPI_Circle>
653 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
655 std::shared_ptr<ModelAPI_Feature> aFeature =
656 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
657 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
660 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::wstring & theExternalName)
662 std::shared_ptr<ModelAPI_Feature> aFeature =
663 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
664 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
667 //--------------------------------------------------------------------------------------
668 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
669 double theStartX, double theStartY,
670 double theEndX, double theEndY,
673 std::shared_ptr<ModelAPI_Feature> aFeature =
674 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
675 return ArcPtr(new SketchAPI_Arc(aFeature,
676 theCenterX, theCenterY,
677 theStartX, theStartY,
682 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
683 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
684 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
685 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
688 std::shared_ptr<ModelAPI_Feature> aFeature =
689 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
690 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
693 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
694 double theEndX, double theEndY,
695 double thePassedX, double thePassedY)
697 std::shared_ptr<ModelAPI_Feature> aFeature =
698 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
699 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
700 theStartX, theStartY,
702 thePassedX, thePassedY));
705 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
706 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
707 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
708 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
710 std::shared_ptr<ModelAPI_Feature> aFeature =
711 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
712 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
715 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
716 const ModelHighAPI_RefAttr& theTangentPoint,
717 double theEndX, double theEndY,
721 std::shared_ptr<ModelAPI_Feature> aFeature =
722 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
723 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
725 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
727 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
731 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
732 const ModelHighAPI_RefAttr& theTangentPoint,
733 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
737 std::shared_ptr<ModelAPI_Feature> aFeature =
738 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
739 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
741 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
743 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
747 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
749 std::shared_ptr<ModelAPI_Feature> aFeature =
750 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
751 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
754 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::wstring & theExternalName)
756 std::shared_ptr<ModelAPI_Feature> aFeature =
757 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
758 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
761 //--------------------------------------------------------------------------------------
762 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
763 double theCenterX, double theCenterY,
764 double theFocusX, double theFocusY,
765 double theMinorRadius)
767 std::shared_ptr<ModelAPI_Feature> aFeature =
768 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
769 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
770 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
773 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
774 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
775 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
776 double theMinorRadius)
778 std::shared_ptr<ModelAPI_Feature> aFeature =
779 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
780 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
783 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
784 double thePoint1X, double thePoint1Y,
785 double thePoint2X, double thePoint2Y,
786 double thePassedX, double thePassedY,
789 std::shared_ptr<ModelAPI_Feature> aFeature =
790 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
791 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
792 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
795 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
796 const PointOrReference& thePoint1,
797 const PointOrReference& thePoint2,
798 const PointOrReference& thePassedPoint,
801 std::shared_ptr<ModelAPI_Feature> aFeature =
802 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
803 MacroEllipsePtr anEllipse;
804 if (thePoint1.second.isEmpty() &&
805 thePoint2.second.isEmpty() &&
806 thePassedPoint.second.isEmpty()) {
807 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
808 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
811 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
812 thePoint1.first, thePoint1.second,
813 thePoint2.first, thePoint2.second,
814 thePassedPoint.first, thePassedPoint.second,
820 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
821 const ModelHighAPI_Selection & theExternal)
823 std::shared_ptr<ModelAPI_Feature> aFeature =
824 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
825 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
828 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
829 const std::wstring & theExternalName)
831 std::shared_ptr<ModelAPI_Feature> aFeature =
832 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
833 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
836 //--------------------------------------------------------------------------------------
837 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
838 double theCenterX, double theCenterY,
839 double theFocusX, double theFocusY,
840 double theStartX, double theStartY,
841 double theEndX, double theEndY,
844 std::shared_ptr<ModelAPI_Feature> aFeature =
845 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
846 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
847 theCenterX, theCenterY,
848 theFocusX, theFocusY,
849 theStartX, theStartY,
854 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
855 const PointOrReference& theCenter,
856 const PointOrReference& theMajorAxisPoint,
857 const PointOrReference& theStartPoint,
858 const PointOrReference& theEndPoint,
861 std::shared_ptr<ModelAPI_Feature> aFeature =
862 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
863 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
864 theCenter.first, theCenter.second,
865 theMajorAxisPoint.first, theMajorAxisPoint.second,
866 theStartPoint.first, theStartPoint.second,
867 theEndPoint.first, theEndPoint.second,
871 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
872 const ModelHighAPI_Selection & theExternal)
874 std::shared_ptr<ModelAPI_Feature> aFeature =
875 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
876 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
879 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
880 const std::wstring & theExternalName)
882 std::shared_ptr<ModelAPI_Feature> aFeature =
883 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
884 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
887 //--------------------------------------------------------------------------------------
889 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
890 const ModelHighAPI_Selection & external,
892 const std::list<PointOrReference>& poles,
893 const std::list<ModelHighAPI_Double>& weights,
894 const std::list<ModelHighAPI_Double>& knots,
895 const std::list<ModelHighAPI_Integer>& multiplicities,
898 // split poles and references to other shapes
899 bool hasReference = false;
900 std::list<GeomPnt2dPtr> aPoints;
901 std::list<ModelHighAPI_RefAttr> aReferences;
902 for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
903 aPoints.push_back(it->first);
904 aReferences.push_back(it->second);
905 if (!it->second.isEmpty())
910 CompositeFeaturePtr aSketch = compositeFeature();
912 // use macro-feature to create coincidences to referred features
913 FeaturePtr aMacroFeature = aSketch->addFeature(
914 periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
915 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
916 aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
917 AttributeDoubleArrayPtr aWeightsAttr =
918 aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
919 AttributeRefAttrListPtr aPolesRefAttr =
920 aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
921 // always generate a control polygon to apply coincidences correctly
922 aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
923 // initialize B-spline attributes
924 fillAttribute(aPoints, aPolesAttr);
926 fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
928 fillAttribute(weights, aWeightsAttr);
929 fillAttribute(aReferences, aPolesRefAttr);
930 apply(); // to kill macro-feature
932 // find created B-spline feature
933 const std::string& aKindToFind =
934 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
935 int aNbSubs = aSketch->numberOfSubs();
936 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
937 FeaturePtr aFeature = aSketch->subFeature(anIndex);
938 if (aFeature->getKind() == aKindToFind) {
939 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
940 : new SketchAPI_BSpline(aFeature));
947 // compute B-spline by parameters
948 FeaturePtr aFeature = aSketch->addFeature(
949 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
951 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
952 : new SketchAPI_BSpline(aFeature));
953 if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
954 aBSpline->setByExternal(external);
955 else if (knots.empty() || multiplicities.empty())
956 aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
958 aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
963 //--------------------------------------------------------------------------------------
964 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
965 const CompositeFeaturePtr& theSketch,
966 const FeaturePtr& theCurveFittingFeature,
967 const std::list<ModelHighAPI_RefAttr>& points,
971 AttributeBooleanPtr aPeriodicAttr =
972 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
973 fillAttribute(periodic, aPeriodicAttr);
974 AttributeBooleanPtr aClosedAttr =
975 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
976 fillAttribute(closed, aClosedAttr);
977 AttributeRefAttrListPtr aPointsAttr =
978 theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
979 fillAttribute(points, aPointsAttr);
980 apply(); // to execute and kill the macro-feature
982 // find created B-spline feature
984 const std::string& aKindToFind =
985 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
986 int aNbSubs = theSketch->numberOfSubs();
987 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
988 FeaturePtr aFeature = theSketch->subFeature(anIndex);
989 if (aFeature->getKind() == aKindToFind) {
990 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
991 : new SketchAPI_BSpline(aFeature));
999 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
1000 const std::list<ModelHighAPI_RefAttr>& points,
1001 const bool periodic,
1004 CompositeFeaturePtr aSketch = compositeFeature();
1005 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1006 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1007 ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
1008 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1011 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
1012 const std::list<ModelHighAPI_RefAttr>& points,
1013 const ModelHighAPI_Double& precision,
1014 const bool periodic,
1017 CompositeFeaturePtr aSketch = compositeFeature();
1018 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1019 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1020 ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
1021 fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
1022 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1025 //--------------------------------------------------------------------------------------
1026 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
1027 const ModelHighAPI_Selection & theExternalFeature,
1029 bool keepRefToOriginal)
1031 std::shared_ptr<ModelAPI_Feature> aFeature =
1032 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
1033 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature));
1034 aProjection->setIncludeToResult(keepResult);
1035 aProjection->setKeepReferenceToOriginal(keepRefToOriginal);
1036 aProjection->setExternalFeature(theExternalFeature);
1040 //--------------------------------------------------------------------------------------
1041 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
1042 const ModelHighAPI_RefAttr & theMirrorLine,
1043 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
1045 std::shared_ptr<ModelAPI_Feature> aFeature =
1046 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
1047 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
1050 //--------------------------------------------------------------------------------------
1051 std::shared_ptr<SketchAPI_Offset> SketchAPI_Sketch::addOffset(
1052 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1053 const ModelHighAPI_Double & theValue,
1054 const bool theReversed,
1055 const std::string & theJointType,
1056 const bool theApprox)
1058 std::shared_ptr<ModelAPI_Feature> aFeature =
1059 compositeFeature()->addFeature(SketchPlugin_Offset::ID());
1060 return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed, theJointType, theApprox));
1063 //--------------------------------------------------------------------------------------
1064 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
1065 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1066 const ModelHighAPI_RefAttr & thePoint1,
1067 const ModelHighAPI_RefAttr & thePoint2,
1068 const ModelHighAPI_Integer & theNumberOfObjects,
1071 std::shared_ptr<ModelAPI_Feature> aFeature =
1072 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
1073 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
1074 thePoint2, theNumberOfObjects, theFullValue));
1077 //--------------------------------------------------------------------------------------
1078 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
1079 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1080 const ModelHighAPI_RefAttr & theCenter,
1081 const ModelHighAPI_Double & theAngle,
1082 const ModelHighAPI_Integer & theNumberOfObjects,
1086 std::shared_ptr<ModelAPI_Feature> aFeature =
1087 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
1089 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
1090 theAngle, theNumberOfObjects, theFullValue, theReversed));
1093 //--------------------------------------------------------------------------------------
1094 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
1095 const ModelHighAPI_Reference& theFeature,
1096 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1098 std::shared_ptr<ModelAPI_Feature> aFeature =
1099 compositeFeature()->addFeature(SketchPlugin_Split::ID());
1100 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
1102 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
1103 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1104 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1105 fillAttribute(thePositionPoint, aPointAttr);
1108 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1111 //--------------------------------------------------------------------------------------
1112 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
1113 const ModelHighAPI_Reference& theFeature,
1114 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1116 std::shared_ptr<ModelAPI_Feature> aFeature =
1117 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
1118 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
1120 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
1121 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1122 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1123 fillAttribute(thePositionPoint, aPointAttr);
1126 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1129 //--------------------------------------------------------------------------------------
1130 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
1131 const ModelHighAPI_RefAttr & theLine1,
1132 const ModelHighAPI_RefAttr & theLine2,
1133 const ModelHighAPI_Double & theValue,
1134 const std::string& theType)
1136 std::shared_ptr<ModelAPI_Feature> aFeature =
1137 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1139 const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
1140 : SketchPlugin_ConstraintAngle::THE_VERSION_1;
1141 fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1143 int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
1144 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
1145 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1147 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
1148 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1150 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1151 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1153 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
1154 std::string aTypeLC = theType;
1155 std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
1156 [](char c) { return static_cast<char>(::tolower(c)); });
1157 if (aTypeLC == "supplementary")
1158 aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
1159 else if (aTypeLC == "backward")
1160 aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
1162 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1163 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1166 aFeature->execute();
1167 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1170 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1171 const ModelHighAPI_RefAttr & theLine1,
1172 const ModelHighAPI_RefAttr & theLine2,
1173 const ModelHighAPI_Double & theValue)
1175 std::shared_ptr<ModelAPI_Feature> aFeature =
1176 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1177 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1178 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1179 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1180 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1181 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1182 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1183 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1184 aFeature->execute();
1185 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1188 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1189 const ModelHighAPI_RefAttr & theLine1,
1190 const ModelHighAPI_RefAttr & theLine2,
1191 const ModelHighAPI_Double & theValue)
1193 std::shared_ptr<ModelAPI_Feature> aFeature =
1194 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1195 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1196 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1197 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1198 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1199 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1200 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1201 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1202 aFeature->execute();
1203 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1206 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1207 const ModelHighAPI_RefAttr & thePoint1,
1208 const ModelHighAPI_RefAttr & thePoint2)
1210 std::shared_ptr<ModelAPI_Feature> aFeature =
1211 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1212 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1213 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1214 aFeature->execute();
1215 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1218 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1219 const ModelHighAPI_RefAttr & theLine1,
1220 const ModelHighAPI_RefAttr & theLine2)
1222 std::shared_ptr<ModelAPI_Feature> aFeature =
1223 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1224 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1225 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1226 aFeature->execute();
1227 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1230 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1231 const ModelHighAPI_RefAttr & thePoint,
1232 const ModelHighAPI_RefAttr & thePointOrLine,
1233 const ModelHighAPI_Double & theValue,
1236 std::shared_ptr<ModelAPI_Feature> aFeature =
1237 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1238 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1239 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1240 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1241 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1242 aFeature->execute();
1243 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1246 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1247 const ModelHighAPI_RefAttr & thePoint,
1248 const ModelHighAPI_RefAttr & thePointOrLine,
1249 const ModelHighAPI_Double & theValue)
1251 return setDistance(thePoint, thePointOrLine, theValue, true);
1254 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1255 const ModelHighAPI_RefAttr & thePoint,
1256 const ModelHighAPI_RefAttr & thePointOrLine,
1257 const ModelHighAPI_Double & theValue)
1259 return setDistance(thePoint, thePointOrLine, theValue, false);
1262 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1263 const ModelHighAPI_RefAttr & thePoint1,
1264 const ModelHighAPI_RefAttr & thePoint2,
1265 const ModelHighAPI_Double & theValue)
1267 std::shared_ptr<ModelAPI_Feature> aFeature =
1268 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1269 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1270 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1271 fillAttribute(theValue,
1272 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1273 aFeature->execute();
1274 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1277 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1278 const ModelHighAPI_RefAttr & thePoint1,
1279 const ModelHighAPI_RefAttr & thePoint2,
1280 const ModelHighAPI_Double & theValue)
1282 std::shared_ptr<ModelAPI_Feature> aFeature =
1283 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1284 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1285 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1286 fillAttribute(theValue,
1287 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1288 aFeature->execute();
1289 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1292 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1293 const ModelHighAPI_RefAttr & theObject1,
1294 const ModelHighAPI_RefAttr & theObject2)
1296 std::shared_ptr<ModelAPI_Feature> aFeature =
1297 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1298 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1299 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1300 aFeature->execute();
1301 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1304 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1305 const ModelHighAPI_RefAttr & thePoint)
1307 std::shared_ptr<ModelAPI_Feature> aFeature =
1308 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1309 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1310 apply(); // finish operation to remove Fillet feature correcly
1311 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1314 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1315 const ModelHighAPI_RefAttr & thePoint,
1316 const ModelHighAPI_Double & theRadius)
1318 CompositeFeaturePtr aSketch = compositeFeature();
1319 int aNbSubs = aSketch->numberOfSubs();
1322 InterfacePtr aFilletFeature = setFillet(thePoint);
1324 // set radius for just created arc
1325 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1326 if (anArc->getKind() == SketchPlugin_Arc::ID())
1327 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1329 return aFilletFeature;
1332 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1333 const ModelHighAPI_RefAttr & theObject)
1335 std::shared_ptr<ModelAPI_Feature> aFeature =
1336 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1337 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1338 aFeature->execute();
1339 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1342 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1343 const ModelHighAPI_RefAttr & theLine)
1345 std::shared_ptr<ModelAPI_Feature> aFeature =
1346 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1347 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1348 aFeature->execute();
1349 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1352 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1353 const ModelHighAPI_RefAttr & theLine,
1354 const ModelHighAPI_Double & theValue)
1356 std::shared_ptr<ModelAPI_Feature> aFeature =
1357 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1358 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1359 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1360 aFeature->execute();
1361 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1364 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1365 const ModelHighAPI_RefAttr & thePoint,
1366 const ModelHighAPI_RefAttr & theLine)
1368 std::shared_ptr<ModelAPI_Feature> aFeature =
1369 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1370 auto aType = aFeature->data()->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE());
1371 fillAttribute(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE_AND_POINT(), aType);
1373 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1374 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1376 aFeature->execute();
1377 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1380 std::shared_ptr<SketchAPI_MacroMiddlePoint> SketchAPI_Sketch::setMiddlePoint(
1381 const ModelHighAPI_RefAttr& theLine)
1383 std::shared_ptr<ModelAPI_Feature> aFeature =
1384 compositeFeature()->addFeature(SketchPlugin_Point::ID());
1386 ObjectPtr anObj = theLine.object();
1387 auto aPoint = middlePoint(anObj, this);
1389 return std::shared_ptr<SketchAPI_MacroMiddlePoint>
1390 (new SketchAPI_MacroMiddlePoint(aFeature, theLine, aPoint));
1393 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1394 const ModelHighAPI_RefAttr & theLine1,
1395 const ModelHighAPI_RefAttr & theLine2)
1397 std::shared_ptr<ModelAPI_Feature> aFeature =
1398 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1399 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1400 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1401 aFeature->execute();
1402 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1405 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1406 const ModelHighAPI_RefAttr & theLine1,
1407 const ModelHighAPI_RefAttr & theLine2)
1409 std::shared_ptr<ModelAPI_Feature> aFeature =
1410 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1411 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1412 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1413 aFeature->execute();
1414 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1417 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1418 const ModelHighAPI_RefAttr & theCircleOrArc,
1419 const ModelHighAPI_Double & theValue)
1421 std::shared_ptr<ModelAPI_Feature> aFeature =
1422 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1423 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1424 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1425 aFeature->execute();
1426 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1429 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1430 const ModelHighAPI_RefAttr & theLine,
1431 const ModelHighAPI_RefAttr & theCircle)
1433 std::shared_ptr<ModelAPI_Feature> aFeature =
1434 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1435 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1436 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1437 aFeature->execute();
1438 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1441 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1442 const ModelHighAPI_RefAttr & theLine)
1444 std::shared_ptr<ModelAPI_Feature> aFeature =
1445 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1446 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1447 aFeature->execute();
1448 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1451 //--------------------------------------------------------------------------------------
1453 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1454 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1456 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1457 theMovedEntity.fillMessage(aMessage);
1459 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1460 if (aMessage->movedAttribute())
1461 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1463 anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1465 if (!anOriginalPosition)
1466 return; // something has gone wrong, do not process movement
1468 aMessage->setOriginalPosition(anOriginalPosition);
1469 aMessage->setCurrentPosition(theTargetPoint);
1470 Events_Loop::loop()->send(aMessage);
1473 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1474 double theTargetX, double theTargetY)
1476 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1477 move(theMovedEntity, aTargetPoint);
1480 //--------------------------------------------------------------------------------------
1482 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1484 FeaturePtr aBase = feature();
1485 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1486 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1487 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1488 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1489 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1490 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1491 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1493 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1496 //--------------------------------------------------------------------------------------
1498 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1500 // collect edges of the first face
1501 std::list<GeomShapePtr> anEdges1;
1502 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1503 anEdges1.push_back(anExp.current());
1504 // compare edges of faces
1505 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1506 GeomShapePtr aCurrent = anExp.current();
1507 bool isFound = false;
1508 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1509 for (; anIt1 != anEdges1.end(); ++anIt1)
1510 if (aCurrent->isSameGeometry(*anIt1)) {
1512 anEdges1.erase(anIt1);
1518 return !anEdges1.empty();
1521 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1523 ResultConstructionPtr aSketchResult =
1524 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1528 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1529 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1533 // collect faces constructed by SketchBuilder algorithm
1534 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1535 aWires->norm(), aWires);
1536 const ListOfShape& aFaces = aSketchBuilder.faces();
1538 // compare faces stored in sketch with faces generated by SketchBuilder
1539 int aNbSketchFaces = aSketchResult->facesNum();
1541 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1542 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1543 ++aFIt, ++aFaceIndex) {
1544 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1545 GeomFacePtr aCurFace = (*aFIt)->face();
1546 if (isDifferent(aSketchFace, aCurFace))
1552 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1553 std::list<std::list<ResultPtr> >& theEdges)
1555 ResultConstructionPtr aSketchResult =
1556 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1560 // collect curves of the sketch
1561 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1562 int aSubNum = theSketch->numberOfSubs();
1563 for (int a = 0; a < aSubNum; ++a) {
1564 FeaturePtr aSub = theSketch->subFeature(a);
1565 const std::list<ResultPtr>& aResults = aSub->results();
1566 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1567 for (; aRes != aResults.cend(); aRes++) {
1568 GeomShapePtr aCurShape = (*aRes)->shape();
1569 if (aCurShape && aCurShape->isEdge())
1570 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1574 // convert each face to the list of results of its edges
1575 int aFacesNum = aSketchResult->facesNum();
1576 for (int a = 0; a < aFacesNum; ++a) {
1577 theEdges.push_back(std::list<ResultPtr>());
1578 std::list<ResultPtr>& aCurEdges = theEdges.back();
1580 GeomFacePtr aFace = aSketchResult->face(a);
1581 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1582 anExp.more(); anExp.next()) {
1583 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1584 aCurEdges.push_back(aCurves[aCurrent]);
1589 //--------------------------------------------------------------------------------------
1591 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1593 FeaturePtr aBase = feature();
1594 const std::string& aDocName = theDumper.name(aBase->document());
1596 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1597 if (anExternal->value()) {
1598 theDumper << aBase << " = model.addSketch(" << aDocName <<
1599 ", " << anExternal << ")" << std::endl;
1601 // Sketch is base on a plane.
1602 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1603 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1604 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1605 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1606 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1607 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1609 // Check the plane is coordinate plane
1610 std::wstring aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1611 if(anExternal->context()) { // checking for selected planes
1612 if (!aPlaneName.empty()
1613 && anExternal->context()->data()
1614 && anExternal->context()->data()->name() == aPlaneName) {
1615 // dump sketch based on coordinate plane
1616 theDumper << aBase << " = model.addSketch(" << aDocName
1617 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1618 } else { // some other plane
1619 theDumper << aBase << " = model.addSketch(" << aDocName <<
1620 ", " << anExternal<< ")" << std::endl;
1623 if (aPlaneName.empty()) {
1624 // needs import additional module
1625 theDumper.importModule("GeomAPI");
1626 // dump plane parameters
1627 const std::string& aSketchName = theDumper.name(aBase);
1628 std::string anOriginName = aSketchName + "_origin";
1629 std::string aNormalName = aSketchName + "_norm";
1630 std::string aDirXName = aSketchName + "_dirx";
1631 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1632 // and then dumplicate dumping it in the next line
1633 theDumper << anOriginName << " = " << anOrigin << "\n"
1634 << aNormalName << " = " << aNormal << "\n"
1635 << aDirXName << " = " << aDirX << "\n";
1636 // dump sketch based on arbitrary plane
1637 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1638 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1640 // dump sketch based on coordinate plane
1641 theDumper << aBase << " = model.addSketch(" << aDocName
1642 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1647 // dump sketch's subfeatures
1648 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1649 theDumper.processSubs(aCompFeat, true);
1651 // if face order differs to the order generated by SketchBuilder,
1652 // dump the list of faces for correct execution of the script
1653 if (isCustomFacesOrder(aCompFeat)) {
1654 std::list<std::list<ResultPtr> > aFaces;
1655 edgesOfSketchFaces(aCompFeat, aFaces);
1657 const std::string& aSketchName = theDumper.name(aBase);
1658 std::string aMethodName(".changeFacesOrder");
1659 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1661 theDumper << aSketchName << aMethodName << "([";
1662 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1663 aFIt != aFaces.end(); ++aFIt) {
1664 if (aFIt != aFaces.begin())
1665 theDumper << ",\n" << aSpaceShift << " ";
1668 theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1669 // call model.do() for correct update of the document's labels related to the changed faces
1670 theDumper << "model.do()" << std::endl;