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 // Old way for create circle
593 //--------------------------------------------------------------------------------------
594 [[deprecated("Use addCircleWithPoint method instead.")]]
595 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
599 std::shared_ptr<ModelAPI_Feature> aFeature =
600 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
601 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_Circle::VERSION_ID()));
602 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius, 0));
605 [[deprecated("Use addCircleWithPoint method instead.")]]
606 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
607 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
610 std::shared_ptr<ModelAPI_Feature> aFeature =
611 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
612 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_Circle::VERSION_ID()));
613 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius, 0));
616 [[deprecated("Use addCircleWithPoint method instead.")]]
617 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
622 std::shared_ptr<ModelAPI_Feature> aFeature =
623 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
624 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_MacroCircle::VERSION_ID()));
625 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
626 thePassedX, thePassedY, 0));
629 [[deprecated("Use addCircleWithPoint method instead.")]]
630 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
631 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
632 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
634 std::shared_ptr<ModelAPI_Feature> aFeature =
635 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
636 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_MacroCircle::VERSION_ID()));
637 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint, 0));
640 [[deprecated("Use addCircleWithPoint method instead.")]]
641 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
642 double theX2, double theY2,
643 double theX3, double theY3)
645 std::shared_ptr<ModelAPI_Feature> aFeature =
646 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
647 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_MacroCircle::VERSION_ID()));
649 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
654 [[deprecated("Use addCircleWithPoint method instead.")]]
655 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
656 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
657 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
658 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
660 std::shared_ptr<ModelAPI_Feature> aFeature =
661 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
662 fillAttribute(SketchPlugin_Circle::THE_VERSION_0, aFeature->integer(SketchPlugin_Circle::VERSION_ID()));
663 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3, 0));
666 [[deprecated("Use addCircleWithPoint method instead.")]]
667 std::shared_ptr<SketchAPI_Circle>
668 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
670 std::shared_ptr<ModelAPI_Feature> aFeature =
671 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
672 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
675 [[deprecated("Use addCircleWithPoint method instead.")]]
676 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::wstring & theExternalName)
678 std::shared_ptr<ModelAPI_Feature> aFeature =
679 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
680 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
683 // New way for create Circle
684 //--------------------------------------------------------------------------------------
685 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircleWithPoint(double theCenterX,
690 std::shared_ptr<ModelAPI_Feature> aFeature =
691 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
692 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius, theAngle));
695 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircleWithPoint(
696 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
700 std::shared_ptr<ModelAPI_Feature> aFeature =
701 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
702 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius, theAngle));
705 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircleWithPoint(double theCenterX,
711 std::shared_ptr<ModelAPI_Feature> aFeature =
712 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
713 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
714 thePassedX, thePassedY, theAngle));
717 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircleWithPoint(
718 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
719 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
722 std::shared_ptr<ModelAPI_Feature> aFeature =
723 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
724 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint, theAngle));
727 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircleWithPoint(double theX1, double theY1,
728 double theX2, double theY2,
729 double theX3, double theY3,
732 std::shared_ptr<ModelAPI_Feature> aFeature =
733 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
734 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
736 theX3, theY3, theAngle));
739 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircleWithPoint(
740 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
741 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
742 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3,
745 std::shared_ptr<ModelAPI_Feature> aFeature =
746 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
747 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3, theAngle));
750 std::shared_ptr<SketchAPI_Circle>
751 SketchAPI_Sketch::addCircleWithPoint(const ModelHighAPI_Selection& theExternal)
753 std::shared_ptr<ModelAPI_Feature> aFeature =
754 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
755 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
758 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircleWithPoint(const std::wstring& theExternalName)
760 std::shared_ptr<ModelAPI_Feature> aFeature =
761 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
762 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
765 //--------------------------------------------------------------------------------------
766 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
767 double theStartX, double theStartY,
768 double theEndX, double theEndY,
771 std::shared_ptr<ModelAPI_Feature> aFeature =
772 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
773 return ArcPtr(new SketchAPI_Arc(aFeature,
774 theCenterX, theCenterY,
775 theStartX, theStartY,
780 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
781 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
782 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
783 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
786 std::shared_ptr<ModelAPI_Feature> aFeature =
787 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
788 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
791 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
792 double theEndX, double theEndY,
793 double thePassedX, double thePassedY)
795 std::shared_ptr<ModelAPI_Feature> aFeature =
796 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
797 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
798 theStartX, theStartY,
800 thePassedX, thePassedY));
803 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
804 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
805 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
806 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
808 std::shared_ptr<ModelAPI_Feature> aFeature =
809 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
810 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
813 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
814 const ModelHighAPI_RefAttr& theTangentPoint,
815 double theEndX, double theEndY,
819 std::shared_ptr<ModelAPI_Feature> aFeature =
820 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
821 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
823 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
825 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
829 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
830 const ModelHighAPI_RefAttr& theTangentPoint,
831 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
835 std::shared_ptr<ModelAPI_Feature> aFeature =
836 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
837 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
839 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
841 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
845 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
847 std::shared_ptr<ModelAPI_Feature> aFeature =
848 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
849 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
852 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::wstring & theExternalName)
854 std::shared_ptr<ModelAPI_Feature> aFeature =
855 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
856 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
859 //--------------------------------------------------------------------------------------
860 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
861 double theCenterX, double theCenterY,
862 double theFocusX, double theFocusY,
863 double theMinorRadius)
865 std::shared_ptr<ModelAPI_Feature> aFeature =
866 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
867 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
868 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
871 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
872 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
873 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
874 double theMinorRadius)
876 std::shared_ptr<ModelAPI_Feature> aFeature =
877 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
878 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
881 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
882 double thePoint1X, double thePoint1Y,
883 double thePoint2X, double thePoint2Y,
884 double thePassedX, double thePassedY,
887 std::shared_ptr<ModelAPI_Feature> aFeature =
888 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
889 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
890 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
893 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
894 const PointOrReference& thePoint1,
895 const PointOrReference& thePoint2,
896 const PointOrReference& thePassedPoint,
899 std::shared_ptr<ModelAPI_Feature> aFeature =
900 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
901 MacroEllipsePtr anEllipse;
902 if (thePoint1.second.isEmpty() &&
903 thePoint2.second.isEmpty() &&
904 thePassedPoint.second.isEmpty()) {
905 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
906 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
909 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
910 thePoint1.first, thePoint1.second,
911 thePoint2.first, thePoint2.second,
912 thePassedPoint.first, thePassedPoint.second,
918 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
919 const ModelHighAPI_Selection & theExternal)
921 std::shared_ptr<ModelAPI_Feature> aFeature =
922 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
923 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
926 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
927 const std::wstring & theExternalName)
929 std::shared_ptr<ModelAPI_Feature> aFeature =
930 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
931 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
934 //--------------------------------------------------------------------------------------
935 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
936 double theCenterX, double theCenterY,
937 double theFocusX, double theFocusY,
938 double theStartX, double theStartY,
939 double theEndX, double theEndY,
942 std::shared_ptr<ModelAPI_Feature> aFeature =
943 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
944 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
945 theCenterX, theCenterY,
946 theFocusX, theFocusY,
947 theStartX, theStartY,
952 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
953 const PointOrReference& theCenter,
954 const PointOrReference& theMajorAxisPoint,
955 const PointOrReference& theStartPoint,
956 const PointOrReference& theEndPoint,
959 std::shared_ptr<ModelAPI_Feature> aFeature =
960 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
961 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
962 theCenter.first, theCenter.second,
963 theMajorAxisPoint.first, theMajorAxisPoint.second,
964 theStartPoint.first, theStartPoint.second,
965 theEndPoint.first, theEndPoint.second,
969 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
970 const ModelHighAPI_Selection & theExternal)
972 std::shared_ptr<ModelAPI_Feature> aFeature =
973 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
974 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
977 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
978 const std::wstring & theExternalName)
980 std::shared_ptr<ModelAPI_Feature> aFeature =
981 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
982 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
985 //--------------------------------------------------------------------------------------
987 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
988 const ModelHighAPI_Selection & external,
990 const std::list<PointOrReference>& poles,
991 const std::list<ModelHighAPI_Double>& weights,
992 const std::list<ModelHighAPI_Double>& knots,
993 const std::list<ModelHighAPI_Integer>& multiplicities,
996 // split poles and references to other shapes
997 bool hasReference = false;
998 std::list<GeomPnt2dPtr> aPoints;
999 std::list<ModelHighAPI_RefAttr> aReferences;
1000 for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
1001 aPoints.push_back(it->first);
1002 aReferences.push_back(it->second);
1003 if (!it->second.isEmpty())
1004 hasReference = true;
1007 BSplinePtr aBSpline;
1008 CompositeFeaturePtr aSketch = compositeFeature();
1010 // use macro-feature to create coincidences to referred features
1011 FeaturePtr aMacroFeature = aSketch->addFeature(
1012 periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
1013 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
1014 aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
1015 AttributeDoubleArrayPtr aWeightsAttr =
1016 aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
1017 AttributeRefAttrListPtr aPolesRefAttr =
1018 aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
1019 // always generate a control polygon to apply coincidences correctly
1020 aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
1021 // initialize B-spline attributes
1022 fillAttribute(aPoints, aPolesAttr);
1023 if (weights.empty())
1024 fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
1026 fillAttribute(weights, aWeightsAttr);
1027 fillAttribute(aReferences, aPolesRefAttr);
1028 apply(); // to kill macro-feature
1030 // find created B-spline feature
1031 const std::string& aKindToFind =
1032 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
1033 int aNbSubs = aSketch->numberOfSubs();
1034 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
1035 FeaturePtr aFeature = aSketch->subFeature(anIndex);
1036 if (aFeature->getKind() == aKindToFind) {
1037 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
1038 : new SketchAPI_BSpline(aFeature));
1039 aBSpline->execute();
1045 // compute B-spline by parameters
1046 FeaturePtr aFeature = aSketch->addFeature(
1047 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
1049 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
1050 : new SketchAPI_BSpline(aFeature));
1051 if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
1052 aBSpline->setByExternal(external);
1053 else if (knots.empty() || multiplicities.empty())
1054 aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
1056 aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
1061 //--------------------------------------------------------------------------------------
1062 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
1063 const CompositeFeaturePtr& theSketch,
1064 const FeaturePtr& theCurveFittingFeature,
1065 const std::list<ModelHighAPI_RefAttr>& points,
1066 const bool periodic,
1069 AttributeBooleanPtr aPeriodicAttr =
1070 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
1071 fillAttribute(periodic, aPeriodicAttr);
1072 AttributeBooleanPtr aClosedAttr =
1073 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
1074 fillAttribute(closed, aClosedAttr);
1075 AttributeRefAttrListPtr aPointsAttr =
1076 theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
1077 fillAttribute(points, aPointsAttr);
1078 apply(); // to execute and kill the macro-feature
1080 // find created B-spline feature
1081 BSplinePtr aBSpline;
1082 const std::string& aKindToFind =
1083 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
1084 int aNbSubs = theSketch->numberOfSubs();
1085 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
1086 FeaturePtr aFeature = theSketch->subFeature(anIndex);
1087 if (aFeature->getKind() == aKindToFind) {
1088 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
1089 : new SketchAPI_BSpline(aFeature));
1090 aBSpline->execute();
1097 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
1098 const std::list<ModelHighAPI_RefAttr>& points,
1099 const bool periodic,
1102 CompositeFeaturePtr aSketch = compositeFeature();
1103 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1104 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1105 ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
1106 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1109 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
1110 const std::list<ModelHighAPI_RefAttr>& points,
1111 const ModelHighAPI_Double& precision,
1112 const bool periodic,
1115 CompositeFeaturePtr aSketch = compositeFeature();
1116 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1117 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1118 ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
1119 fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
1120 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1123 //--------------------------------------------------------------------------------------
1124 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
1125 const ModelHighAPI_Selection & theExternalFeature,
1127 bool keepRefToOriginal)
1129 std::shared_ptr<ModelAPI_Feature> aFeature =
1130 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
1131 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature));
1132 aProjection->setIncludeToResult(keepResult);
1133 aProjection->setKeepReferenceToOriginal(keepRefToOriginal);
1134 aProjection->setExternalFeature(theExternalFeature);
1138 //--------------------------------------------------------------------------------------
1139 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
1140 const ModelHighAPI_RefAttr & theMirrorLine,
1141 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
1143 std::shared_ptr<ModelAPI_Feature> aFeature =
1144 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
1145 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
1148 //--------------------------------------------------------------------------------------
1149 std::shared_ptr<SketchAPI_Offset> SketchAPI_Sketch::addOffset(
1150 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1151 const ModelHighAPI_Double & theValue,
1152 const bool theReversed,
1153 const std::string & theJointType,
1154 const bool theApprox)
1156 std::shared_ptr<ModelAPI_Feature> aFeature =
1157 compositeFeature()->addFeature(SketchPlugin_Offset::ID());
1158 return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed, theJointType, theApprox));
1161 //--------------------------------------------------------------------------------------
1162 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
1163 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1164 const ModelHighAPI_RefAttr & thePoint1,
1165 const ModelHighAPI_RefAttr & thePoint2,
1166 const ModelHighAPI_Integer & theNumberOfObjects,
1169 std::shared_ptr<ModelAPI_Feature> aFeature =
1170 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
1171 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
1172 thePoint2, theNumberOfObjects, theFullValue));
1175 //--------------------------------------------------------------------------------------
1176 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
1177 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1178 const ModelHighAPI_RefAttr & theCenter,
1179 const ModelHighAPI_Double & theAngle,
1180 const ModelHighAPI_Integer & theNumberOfObjects,
1184 std::shared_ptr<ModelAPI_Feature> aFeature =
1185 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
1187 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
1188 theAngle, theNumberOfObjects, theFullValue, theReversed));
1191 //--------------------------------------------------------------------------------------
1192 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
1193 const ModelHighAPI_Reference& theFeature,
1194 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1196 std::shared_ptr<ModelAPI_Feature> aFeature =
1197 compositeFeature()->addFeature(SketchPlugin_Split::ID());
1198 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
1200 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
1201 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1202 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1203 fillAttribute(thePositionPoint, aPointAttr);
1206 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1209 //--------------------------------------------------------------------------------------
1210 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
1211 const ModelHighAPI_Reference& theFeature,
1212 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1214 std::shared_ptr<ModelAPI_Feature> aFeature =
1215 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
1216 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
1218 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
1219 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1220 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1221 fillAttribute(thePositionPoint, aPointAttr);
1224 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1227 //--------------------------------------------------------------------------------------
1228 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
1229 const ModelHighAPI_RefAttr & theLine1,
1230 const ModelHighAPI_RefAttr & theLine2,
1231 const ModelHighAPI_Double & theValue,
1232 const std::string& theType)
1234 std::shared_ptr<ModelAPI_Feature> aFeature =
1235 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1237 const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
1238 : SketchPlugin_ConstraintAngle::THE_VERSION_1;
1239 fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1241 int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
1242 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
1243 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1245 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
1246 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1248 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1249 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1251 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
1252 std::string aTypeLC = theType;
1253 std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
1254 [](char c) { return static_cast<char>(::tolower(c)); });
1255 if (aTypeLC == "supplementary")
1256 aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
1257 else if (aTypeLC == "backward")
1258 aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
1260 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1261 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1264 aFeature->execute();
1265 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1268 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1269 const ModelHighAPI_RefAttr & theLine1,
1270 const ModelHighAPI_RefAttr & theLine2,
1271 const ModelHighAPI_Double & theValue)
1273 std::shared_ptr<ModelAPI_Feature> aFeature =
1274 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1275 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1276 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1277 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1278 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1279 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1280 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1281 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1282 aFeature->execute();
1283 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1286 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1287 const ModelHighAPI_RefAttr & theLine1,
1288 const ModelHighAPI_RefAttr & theLine2,
1289 const ModelHighAPI_Double & theValue)
1291 std::shared_ptr<ModelAPI_Feature> aFeature =
1292 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1293 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1294 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1295 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1296 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1297 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1298 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1299 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1300 aFeature->execute();
1301 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1304 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1305 const ModelHighAPI_RefAttr & thePoint1,
1306 const ModelHighAPI_RefAttr & thePoint2)
1308 std::shared_ptr<ModelAPI_Feature> aFeature =
1309 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1310 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1311 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1312 aFeature->execute();
1313 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1316 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1317 const ModelHighAPI_RefAttr & theLine1,
1318 const ModelHighAPI_RefAttr & theLine2)
1320 std::shared_ptr<ModelAPI_Feature> aFeature =
1321 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1322 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1323 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1324 aFeature->execute();
1325 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1328 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1329 const ModelHighAPI_RefAttr & thePoint,
1330 const ModelHighAPI_RefAttr & thePointOrLine,
1331 const ModelHighAPI_Double & theValue,
1334 std::shared_ptr<ModelAPI_Feature> aFeature =
1335 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1336 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1337 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1338 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1339 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1340 aFeature->execute();
1341 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1344 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1345 const ModelHighAPI_RefAttr & thePoint,
1346 const ModelHighAPI_RefAttr & thePointOrLine,
1347 const ModelHighAPI_Double & theValue)
1349 return setDistance(thePoint, thePointOrLine, theValue, true);
1352 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1353 const ModelHighAPI_RefAttr & thePoint,
1354 const ModelHighAPI_RefAttr & thePointOrLine,
1355 const ModelHighAPI_Double & theValue)
1357 return setDistance(thePoint, thePointOrLine, theValue, false);
1360 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1361 const ModelHighAPI_RefAttr & thePoint1,
1362 const ModelHighAPI_RefAttr & thePoint2,
1363 const ModelHighAPI_Double & theValue)
1365 std::shared_ptr<ModelAPI_Feature> aFeature =
1366 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1367 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1368 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1369 fillAttribute(theValue,
1370 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1371 aFeature->execute();
1372 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1375 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1376 const ModelHighAPI_RefAttr & thePoint1,
1377 const ModelHighAPI_RefAttr & thePoint2,
1378 const ModelHighAPI_Double & theValue)
1380 std::shared_ptr<ModelAPI_Feature> aFeature =
1381 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1382 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1383 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1384 fillAttribute(theValue,
1385 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1386 aFeature->execute();
1387 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1390 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1391 const ModelHighAPI_RefAttr & theObject1,
1392 const ModelHighAPI_RefAttr & theObject2)
1394 std::shared_ptr<ModelAPI_Feature> aFeature =
1395 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1396 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1397 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1398 aFeature->execute();
1399 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1402 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1403 const ModelHighAPI_RefAttr & thePoint)
1405 std::shared_ptr<ModelAPI_Feature> aFeature =
1406 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1407 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1408 apply(); // finish operation to remove Fillet feature correcly
1409 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1412 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1413 const ModelHighAPI_RefAttr & thePoint,
1414 const ModelHighAPI_Double & theRadius)
1416 CompositeFeaturePtr aSketch = compositeFeature();
1417 int aNbSubs = aSketch->numberOfSubs();
1420 InterfacePtr aFilletFeature = setFillet(thePoint);
1422 // set radius for just created arc
1423 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1424 if (anArc->getKind() == SketchPlugin_Arc::ID())
1425 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1427 return aFilletFeature;
1430 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1431 const ModelHighAPI_RefAttr & theObject)
1433 std::shared_ptr<ModelAPI_Feature> aFeature =
1434 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1435 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1436 aFeature->execute();
1437 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1440 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1441 const ModelHighAPI_RefAttr & theLine)
1443 std::shared_ptr<ModelAPI_Feature> aFeature =
1444 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1445 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1446 aFeature->execute();
1447 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1450 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1451 const ModelHighAPI_RefAttr & theLine,
1452 const ModelHighAPI_Double & theValue)
1454 std::shared_ptr<ModelAPI_Feature> aFeature =
1455 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1456 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1457 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1458 aFeature->execute();
1459 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1462 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1463 const ModelHighAPI_RefAttr & thePoint,
1464 const ModelHighAPI_RefAttr & theLine)
1466 std::shared_ptr<ModelAPI_Feature> aFeature =
1467 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1468 auto aType = aFeature->data()->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE());
1469 fillAttribute(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE_AND_POINT(), aType);
1471 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1472 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1474 aFeature->execute();
1475 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1478 std::shared_ptr<SketchAPI_MacroMiddlePoint> SketchAPI_Sketch::setMiddlePoint(
1479 const ModelHighAPI_RefAttr& theLine)
1481 std::shared_ptr<ModelAPI_Feature> aFeature =
1482 compositeFeature()->addFeature(SketchPlugin_Point::ID());
1484 ObjectPtr anObj = theLine.object();
1485 auto aPoint = middlePoint(anObj, this);
1487 return std::shared_ptr<SketchAPI_MacroMiddlePoint>
1488 (new SketchAPI_MacroMiddlePoint(aFeature, theLine, aPoint));
1491 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1492 const ModelHighAPI_RefAttr & theLine1,
1493 const ModelHighAPI_RefAttr & theLine2)
1495 std::shared_ptr<ModelAPI_Feature> aFeature =
1496 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1497 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1498 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1499 aFeature->execute();
1500 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1503 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1504 const ModelHighAPI_RefAttr & theLine1,
1505 const ModelHighAPI_RefAttr & theLine2)
1507 std::shared_ptr<ModelAPI_Feature> aFeature =
1508 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1509 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1510 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1511 aFeature->execute();
1512 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1515 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1516 const ModelHighAPI_RefAttr & theCircleOrArc,
1517 const ModelHighAPI_Double & theValue)
1519 std::shared_ptr<ModelAPI_Feature> aFeature =
1520 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1521 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1522 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1523 aFeature->execute();
1524 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1527 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1528 const ModelHighAPI_RefAttr & theLine,
1529 const ModelHighAPI_RefAttr & theCircle)
1531 std::shared_ptr<ModelAPI_Feature> aFeature =
1532 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1533 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1534 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1535 aFeature->execute();
1536 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1539 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1540 const ModelHighAPI_RefAttr & theLine)
1542 std::shared_ptr<ModelAPI_Feature> aFeature =
1543 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1544 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1545 aFeature->execute();
1546 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1549 //--------------------------------------------------------------------------------------
1551 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1552 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1554 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1555 theMovedEntity.fillMessage(aMessage);
1557 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1558 if (aMessage->movedAttribute())
1559 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1561 anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1563 if (!anOriginalPosition)
1564 return; // something has gone wrong, do not process movement
1566 aMessage->setOriginalPosition(anOriginalPosition);
1567 aMessage->setCurrentPosition(theTargetPoint);
1568 Events_Loop::loop()->send(aMessage);
1571 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1572 double theTargetX, double theTargetY)
1574 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1575 move(theMovedEntity, aTargetPoint);
1578 //--------------------------------------------------------------------------------------
1580 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1582 FeaturePtr aBase = feature();
1583 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1584 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1585 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1586 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1587 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1588 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1589 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1591 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1594 //--------------------------------------------------------------------------------------
1596 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1598 // collect edges of the first face
1599 std::list<GeomShapePtr> anEdges1;
1600 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1601 anEdges1.push_back(anExp.current());
1602 // compare edges of faces
1603 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1604 GeomShapePtr aCurrent = anExp.current();
1605 bool isFound = false;
1606 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1607 for (; anIt1 != anEdges1.end(); ++anIt1)
1608 if (aCurrent->isSameGeometry(*anIt1)) {
1610 anEdges1.erase(anIt1);
1616 return !anEdges1.empty();
1619 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1621 ResultConstructionPtr aSketchResult =
1622 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1626 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1627 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1631 // collect faces constructed by SketchBuilder algorithm
1632 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1633 aWires->norm(), aWires);
1634 const ListOfShape& aFaces = aSketchBuilder.faces();
1636 // compare faces stored in sketch with faces generated by SketchBuilder
1637 int aNbSketchFaces = aSketchResult->facesNum();
1639 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1640 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1641 ++aFIt, ++aFaceIndex) {
1642 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1643 GeomFacePtr aCurFace = (*aFIt)->face();
1644 if (isDifferent(aSketchFace, aCurFace))
1650 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1651 std::list<std::list<ResultPtr> >& theEdges)
1653 ResultConstructionPtr aSketchResult =
1654 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1658 // collect curves of the sketch
1659 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1660 int aSubNum = theSketch->numberOfSubs();
1661 for (int a = 0; a < aSubNum; ++a) {
1662 FeaturePtr aSub = theSketch->subFeature(a);
1663 const std::list<ResultPtr>& aResults = aSub->results();
1664 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1665 for (; aRes != aResults.cend(); aRes++) {
1666 GeomShapePtr aCurShape = (*aRes)->shape();
1667 if (aCurShape && aCurShape->isEdge())
1668 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1672 // convert each face to the list of results of its edges
1673 int aFacesNum = aSketchResult->facesNum();
1674 for (int a = 0; a < aFacesNum; ++a) {
1675 theEdges.push_back(std::list<ResultPtr>());
1676 std::list<ResultPtr>& aCurEdges = theEdges.back();
1678 GeomFacePtr aFace = aSketchResult->face(a);
1679 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1680 anExp.more(); anExp.next()) {
1681 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1682 aCurEdges.push_back(aCurves[aCurrent]);
1687 //--------------------------------------------------------------------------------------
1689 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1691 FeaturePtr aBase = feature();
1692 const std::string& aDocName = theDumper.name(aBase->document());
1694 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1695 if (anExternal->value()) {
1696 theDumper << aBase << " = model.addSketch(" << aDocName <<
1697 ", " << anExternal << ")" << std::endl;
1699 // Sketch is base on a plane.
1700 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1701 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1702 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1703 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1704 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1705 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1707 // Check the plane is coordinate plane
1708 std::wstring aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1709 if(anExternal->context()) { // checking for selected planes
1710 if (!aPlaneName.empty()
1711 && anExternal->context()->data()
1712 && anExternal->context()->data()->name() == aPlaneName) {
1713 // dump sketch based on coordinate plane
1714 theDumper << aBase << " = model.addSketch(" << aDocName
1715 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1716 } else { // some other plane
1717 theDumper << aBase << " = model.addSketch(" << aDocName <<
1718 ", " << anExternal<< ")" << std::endl;
1721 if (aPlaneName.empty()) {
1722 // needs import additional module
1723 theDumper.importModule("GeomAPI");
1724 // dump plane parameters
1725 const std::string& aSketchName = theDumper.name(aBase);
1726 std::string anOriginName = aSketchName + "_origin";
1727 std::string aNormalName = aSketchName + "_norm";
1728 std::string aDirXName = aSketchName + "_dirx";
1729 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1730 // and then dumplicate dumping it in the next line
1731 theDumper << anOriginName << " = " << anOrigin << "\n"
1732 << aNormalName << " = " << aNormal << "\n"
1733 << aDirXName << " = " << aDirX << "\n";
1734 // dump sketch based on arbitrary plane
1735 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1736 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1738 // dump sketch based on coordinate plane
1739 theDumper << aBase << " = model.addSketch(" << aDocName
1740 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1745 // dump sketch's subfeatures
1746 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1747 theDumper.processSubs(aCompFeat, true);
1749 // if face order differs to the order generated by SketchBuilder,
1750 // dump the list of faces for correct execution of the script
1751 if (isCustomFacesOrder(aCompFeat)) {
1752 std::list<std::list<ResultPtr> > aFaces;
1753 edgesOfSketchFaces(aCompFeat, aFaces);
1755 const std::string& aSketchName = theDumper.name(aBase);
1756 std::string aMethodName(".changeFacesOrder");
1757 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1759 theDumper << aSketchName << aMethodName << "([";
1760 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1761 aFIt != aFaces.end(); ++aFIt) {
1762 if (aFIt != aFaces.begin())
1763 theDumper << ",\n" << aSpaceShift << " ";
1766 theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1767 // call model.do() for correct update of the document's labels related to the changed faces
1768 theDumper << "model.do()" << std::endl;