1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchAPI_Sketch.h"
21 //--------------------------------------------------------------------------------------
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_ConstraintAngle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintCollinear.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_Fillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMiddle.h>
34 #include <SketchPlugin_ConstraintMirror.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
39 #include <SketchPlugin_Trim.h>
40 #include <SketchPlugin_Split.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketcherPrs_Tools.h>
44 //--------------------------------------------------------------------------------------
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_ResultConstruction.h>
48 #include <ModelHighAPI_Double.h>
49 #include <ModelHighAPI_Dumper.h>
50 #include <ModelHighAPI_RefAttr.h>
51 #include <ModelHighAPI_Selection.h>
52 #include <ModelHighAPI_Services.h>
53 #include <ModelHighAPI_Tools.h>
54 //--------------------------------------------------------------------------------------
55 #include "SketchAPI_Arc.h"
56 #include "SketchAPI_BSpline.h"
57 #include "SketchAPI_Circle.h"
58 #include "SketchAPI_Ellipse.h"
59 #include "SketchAPI_EllipticArc.h"
60 #include "SketchAPI_IntersectionPoint.h"
61 #include "SketchAPI_Line.h"
62 #include "SketchAPI_MacroArc.h"
63 #include "SketchAPI_MacroCircle.h"
64 #include "SketchAPI_MacroEllipse.h"
65 #include "SketchAPI_MacroEllipticArc.h"
66 #include "SketchAPI_Mirror.h"
67 #include "SketchAPI_Point.h"
68 #include "SketchAPI_Projection.h"
69 #include "SketchAPI_Rectangle.h"
70 #include "SketchAPI_Rotation.h"
71 #include "SketchAPI_Translation.h"
72 //--------------------------------------------------------------------------------------
73 #include <GeomAPI_Curve.h>
74 #include <GeomAPI_Dir2d.h>
75 #include <GeomAPI_PlanarEdges.h>
76 #include <GeomAPI_ShapeExplorer.h>
77 #include <GeomAPI_XY.h>
78 #include <GeomAlgoAPI_SketchBuilder.h>
82 //--------------------------------------------------------------------------------------
83 SketchAPI_Sketch::SketchAPI_Sketch(
84 const std::shared_ptr<ModelAPI_Feature> & theFeature)
85 : ModelHighAPI_Interface(theFeature)
90 SketchAPI_Sketch::SketchAPI_Sketch(
91 const std::shared_ptr<ModelAPI_Feature> & theFeature,
92 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
93 : ModelHighAPI_Interface(theFeature)
100 SketchAPI_Sketch::SketchAPI_Sketch(
101 const std::shared_ptr<ModelAPI_Feature> & theFeature,
102 const ModelHighAPI_Selection & theExternal)
103 : ModelHighAPI_Interface(theFeature)
106 setExternal(theExternal);
110 SketchAPI_Sketch::SketchAPI_Sketch(
111 const std::shared_ptr<ModelAPI_Feature> & theFeature,
112 std::shared_ptr<ModelAPI_Object> thePlaneObject)
113 : ModelHighAPI_Interface(theFeature)
116 setExternal(thePlaneObject);
120 SketchAPI_Sketch::~SketchAPI_Sketch()
125 //--------------------------------------------------------------------------------------
126 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
128 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
131 //--------------------------------------------------------------------------------------
132 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
134 fillAttribute(thePlane->origin(), myorigin);
135 fillAttribute(thePlane->dirX(), mydirX);
136 fillAttribute(thePlane->normal(), mynormal);
141 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
142 bool theRemoveExternalDependency)
144 FeaturePtr aSketch = feature();
146 DocumentPtr aDoc = aSketch->document();
147 bool useVisible = false;
148 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
149 aDoc->setCurrentFeature(aSketch, useVisible);
151 if (theRemoveExternalDependency)
152 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
154 setExternal(thePlane);
156 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
159 //--------------------------------------------------------------------------------------
160 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
162 fillAttribute(theExternal, myexternal);
167 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
169 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
170 ModelHighAPI_Selection aSel(aRes);
174 //--------------------------------------------------------------------------------------
175 void SketchAPI_Sketch::setValue(
176 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
177 const ModelHighAPI_Double & theValue)
179 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
181 // theConstraint->execute();
184 //--------------------------------------------------------------------------------------
185 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
187 const_cast<SketchAPI_Sketch*>(this)->execute();
189 std::list<ModelHighAPI_Selection> aSelectionList;
191 ResultConstructionPtr aResultConstruction =
192 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
193 if (aResultConstruction.get() == NULL)
194 return aSelectionList;
196 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
197 aSelectionList.push_back(
198 ModelHighAPI_Selection(aResultConstruction,
199 aResultConstruction->face(anIndex)));
202 return aSelectionList;
205 //--------------------------------------------------------------------------------------
206 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
207 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
209 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
210 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
213 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
214 const ModelHighAPI_Selection & theExternal)
216 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
217 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
220 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
221 const std::string & theExternalName)
223 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
225 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
228 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
229 std::shared_ptr<ModelAPI_Object> thePlaneObject)
231 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
232 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
236 //--------------------------------------------------------------------------------------
237 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
239 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
240 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
241 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
242 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
243 PointPtr aPoint(new SketchAPI_Point(aFeature));
244 aFreePoints.push_back(aPoint);
249 //--------------------------------------------------------------------------------------
250 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
252 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
253 if (aCurve->isTrimmed())
254 aCurve = aCurve->basisCurve();
258 void SketchAPI_Sketch::changeFacesOrder(
259 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
261 // collect faces of the sketch
262 ResultConstructionPtr aSketchResult =
263 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
264 if (!aSketchResult) {
265 // sketch is nested to a boolean operation, thus, it has no result yet.
266 feature()->execute();
268 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
270 std::list<GeomFacePtr> aFaces;
271 int aFacesNum = aSketchResult->facesNum();
272 for (int i = 0; i < aFacesNum; ++i)
273 aFaces.push_back(aSketchResult->face(i));
274 // find new faces order according to the given lists of edges
275 std::list<GeomFacePtr> aNewFacesOrder;
276 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
277 for (; anIt != theFaces.end(); ++anIt) {
278 // find the appropriate face
279 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
280 for (; aFIt != aFaces.end(); ++aFIt) {
281 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
282 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
283 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
284 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
287 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
288 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
289 if (!aCurve1->isEqual(aCurve2))
293 if (aEdgeIt == anIt->end() && !aFExp.more()) {
295 aNewFacesOrder.push_back(*aFIt);
301 // place the rest faces at the end of new faces list
303 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
304 // update the result of the sketch with the new order of faces
305 aSketchResult->setFacesOrder(aNewFacesOrder);
308 //--------------------------------------------------------------------------------------
309 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
310 double theX, double theY)
312 std::shared_ptr<ModelAPI_Feature> aFeature =
313 compositeFeature()->addFeature(SketchPlugin_Point::ID());
314 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
316 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
317 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
319 std::shared_ptr<ModelAPI_Feature> aFeature =
320 compositeFeature()->addFeature(SketchPlugin_Point::ID());
321 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
323 std::shared_ptr<SketchAPI_Point>
324 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
326 std::shared_ptr<ModelAPI_Feature> aFeature =
327 compositeFeature()->addFeature(SketchPlugin_Point::ID());
328 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
330 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
332 std::shared_ptr<ModelAPI_Feature> aFeature =
333 compositeFeature()->addFeature(SketchPlugin_Point::ID());
334 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
337 //--------------------------------------------------------------------------------------
338 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
339 const ModelHighAPI_Selection & theExternal,
342 std::shared_ptr<ModelAPI_Feature> aFeature =
343 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
344 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
345 anIntersection->setIncludeToResult(theKeepResult);
346 return anIntersection;
348 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
349 const std::string & theExternalName,
352 std::shared_ptr<ModelAPI_Feature> aFeature =
353 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
354 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
355 anIntersection->setIncludeToResult(theKeepResult);
356 return anIntersection;
359 //--------------------------------------------------------------------------------------
360 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
361 double theX2, double theY2)
363 std::shared_ptr<ModelAPI_Feature> aFeature =
364 compositeFeature()->addFeature(SketchPlugin_Line::ID());
365 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
367 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
368 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
369 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
371 std::shared_ptr<ModelAPI_Feature> aFeature =
372 compositeFeature()->addFeature(SketchPlugin_Line::ID());
373 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
375 std::shared_ptr<SketchAPI_Line>
376 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
378 std::shared_ptr<ModelAPI_Feature> aFeature =
379 compositeFeature()->addFeature(SketchPlugin_Line::ID());
380 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
382 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
384 std::shared_ptr<ModelAPI_Feature> aFeature =
385 compositeFeature()->addFeature(SketchPlugin_Line::ID());
386 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
389 //--------------------------------------------------------------------------------------
390 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
391 double theX2, double theY2)
393 std::shared_ptr<ModelAPI_Feature> aFeature =
394 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
395 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
397 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
398 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
399 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
401 std::shared_ptr<ModelAPI_Feature> aFeature =
402 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
403 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
406 //--------------------------------------------------------------------------------------
407 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
411 std::shared_ptr<ModelAPI_Feature> aFeature =
412 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
413 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
416 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
417 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
420 std::shared_ptr<ModelAPI_Feature> aFeature =
421 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
422 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
425 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
430 std::shared_ptr<ModelAPI_Feature> aFeature =
431 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
432 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
433 thePassedX, thePassedY));
436 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
437 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
438 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
440 std::shared_ptr<ModelAPI_Feature> aFeature =
441 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
442 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
445 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
446 double theX2, double theY2,
447 double theX3, double theY3)
449 std::shared_ptr<ModelAPI_Feature> aFeature =
450 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
451 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
456 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
457 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
458 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
459 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
461 std::shared_ptr<ModelAPI_Feature> aFeature =
462 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
463 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
466 std::shared_ptr<SketchAPI_Circle>
467 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
469 std::shared_ptr<ModelAPI_Feature> aFeature =
470 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
471 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
474 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
476 std::shared_ptr<ModelAPI_Feature> aFeature =
477 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
478 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
481 //--------------------------------------------------------------------------------------
482 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
483 double theStartX, double theStartY,
484 double theEndX, double theEndY,
487 std::shared_ptr<ModelAPI_Feature> aFeature =
488 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
489 return ArcPtr(new SketchAPI_Arc(aFeature,
490 theCenterX, theCenterY,
491 theStartX, theStartY,
496 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
497 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
498 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
499 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
502 std::shared_ptr<ModelAPI_Feature> aFeature =
503 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
504 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
507 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
508 double theEndX, double theEndY,
509 double thePassedX, double thePassedY)
511 std::shared_ptr<ModelAPI_Feature> aFeature =
512 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
513 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
514 theStartX, theStartY,
516 thePassedX, thePassedY));
519 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
520 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
521 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
522 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
524 std::shared_ptr<ModelAPI_Feature> aFeature =
525 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
526 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
529 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
530 const ModelHighAPI_RefAttr& theTangentPoint,
531 double theEndX, double theEndY,
535 std::shared_ptr<ModelAPI_Feature> aFeature =
536 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
537 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
539 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
541 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
545 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
546 const ModelHighAPI_RefAttr& theTangentPoint,
547 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
551 std::shared_ptr<ModelAPI_Feature> aFeature =
552 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
553 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
555 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
557 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
561 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
563 std::shared_ptr<ModelAPI_Feature> aFeature =
564 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
565 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
568 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
570 std::shared_ptr<ModelAPI_Feature> aFeature =
571 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
572 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
575 //--------------------------------------------------------------------------------------
576 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
577 double theCenterX, double theCenterY,
578 double theFocusX, double theFocusY,
579 double theMinorRadius)
581 std::shared_ptr<ModelAPI_Feature> aFeature =
582 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
583 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
584 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
587 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
588 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
589 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
590 double theMinorRadius)
592 std::shared_ptr<ModelAPI_Feature> aFeature =
593 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
594 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
597 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
598 double thePoint1X, double thePoint1Y,
599 double thePoint2X, double thePoint2Y,
600 double thePassedX, double thePassedY,
603 std::shared_ptr<ModelAPI_Feature> aFeature =
604 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
605 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
606 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
609 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
610 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint1,
611 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint2,
612 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePassedPoint,
615 std::shared_ptr<ModelAPI_Feature> aFeature =
616 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
617 MacroEllipsePtr anEllipse;
618 if (thePoint1.second.isEmpty() &&
619 thePoint2.second.isEmpty() &&
620 thePassedPoint.second.isEmpty()) {
621 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
622 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
625 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
626 thePoint1.first, thePoint1.second,
627 thePoint2.first, thePoint2.second,
628 thePassedPoint.first, thePassedPoint.second,
634 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
635 const ModelHighAPI_Selection & theExternal)
637 std::shared_ptr<ModelAPI_Feature> aFeature =
638 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
639 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
642 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
643 const std::string & theExternalName)
645 std::shared_ptr<ModelAPI_Feature> aFeature =
646 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
647 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
650 //--------------------------------------------------------------------------------------
651 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
652 double theCenterX, double theCenterY,
653 double theFocusX, double theFocusY,
654 double theStartX, double theStartY,
655 double theEndX, double theEndY,
658 std::shared_ptr<ModelAPI_Feature> aFeature =
659 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
660 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
661 theCenterX, theCenterY,
662 theFocusX, theFocusY,
663 theStartX, theStartY,
668 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
669 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theCenter,
670 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theMajorAxisPoint,
671 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theStartPoint,
672 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theEndPoint,
675 std::shared_ptr<ModelAPI_Feature> aFeature =
676 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
677 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
678 theCenter.first, theCenter.second,
679 theMajorAxisPoint.first, theMajorAxisPoint.second,
680 theStartPoint.first, theStartPoint.second,
681 theEndPoint.first, theEndPoint.second,
685 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
686 const ModelHighAPI_Selection & theExternal)
688 std::shared_ptr<ModelAPI_Feature> aFeature =
689 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
690 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
693 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
694 const std::string & theExternalName)
696 std::shared_ptr<ModelAPI_Feature> aFeature =
697 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
698 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
701 //--------------------------------------------------------------------------------------
703 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
704 const ModelHighAPI_Selection & external,
706 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& poles,
707 const std::list<ModelHighAPI_Double>& weights,
708 const std::list<ModelHighAPI_Double>& knots,
709 const std::list<ModelHighAPI_Integer>& multiplicities,
712 FeaturePtr aFeature = compositeFeature()->addFeature(
713 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
715 BSplinePtr aBSpline(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
716 : new SketchAPI_BSpline(aFeature));
717 if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
718 aBSpline->setByExternal(external);
719 else if (knots.empty() || multiplicities.empty())
720 aBSpline->setByDegreePolesAndWeights(degree, poles, weights);
722 aBSpline->setByParameters(degree, poles, weights, knots, multiplicities);
727 //--------------------------------------------------------------------------------------
728 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
729 const ModelHighAPI_Selection & theExternalFeature,
732 std::shared_ptr<ModelAPI_Feature> aFeature =
733 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
734 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
735 aProjection->setIncludeToResult(theKeepResult);
739 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
740 const std::string & theExternalName,
743 std::shared_ptr<ModelAPI_Feature> aFeature =
744 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
745 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
746 aProjection->setIncludeToResult(theKeepResult);
750 //--------------------------------------------------------------------------------------
751 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
752 const ModelHighAPI_RefAttr & theMirrorLine,
753 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
755 std::shared_ptr<ModelAPI_Feature> aFeature =
756 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
757 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
760 //--------------------------------------------------------------------------------------
761 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
762 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
763 const ModelHighAPI_RefAttr & thePoint1,
764 const ModelHighAPI_RefAttr & thePoint2,
765 const ModelHighAPI_Integer & theNumberOfObjects,
768 std::shared_ptr<ModelAPI_Feature> aFeature =
769 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
770 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
771 thePoint2, theNumberOfObjects, theFullValue));
774 //--------------------------------------------------------------------------------------
775 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
776 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
777 const ModelHighAPI_RefAttr & theCenter,
778 const ModelHighAPI_Double & theAngle,
779 const ModelHighAPI_Integer & theNumberOfObjects,
783 std::shared_ptr<ModelAPI_Feature> aFeature =
784 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
786 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
787 theAngle, theNumberOfObjects, theFullValue, theReversed));
790 //--------------------------------------------------------------------------------------
791 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
792 const ModelHighAPI_Reference& theFeature,
793 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
795 std::shared_ptr<ModelAPI_Feature> aFeature =
796 compositeFeature()->addFeature(SketchPlugin_Split::ID());
797 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
799 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
800 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
801 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
802 fillAttribute(thePositionPoint, aPointAttr);
805 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
808 //--------------------------------------------------------------------------------------
809 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
810 const ModelHighAPI_Reference& theFeature,
811 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
813 std::shared_ptr<ModelAPI_Feature> aFeature =
814 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
815 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
817 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
818 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
819 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
820 fillAttribute(thePositionPoint, aPointAttr);
823 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
826 //--------------------------------------------------------------------------------------
827 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
828 const ModelHighAPI_RefAttr & theLine1,
829 const ModelHighAPI_RefAttr & theLine2,
830 const ModelHighAPI_Double & theValue,
831 const std::string& theType)
833 std::shared_ptr<ModelAPI_Feature> aFeature =
834 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
836 const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
837 : SketchPlugin_ConstraintAngle::THE_VERSION_1;
838 fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
840 int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
841 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
842 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
844 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
845 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
847 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
848 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
850 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
851 std::string aTypeLC = theType;
852 std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(), ::tolower);
853 if (aTypeLC == "supplementary")
854 aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
855 else if (aTypeLC == "backward")
856 aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
858 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
859 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
863 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
866 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
867 const ModelHighAPI_RefAttr & theLine1,
868 const ModelHighAPI_RefAttr & theLine2,
869 const ModelHighAPI_Double & theValue)
871 std::shared_ptr<ModelAPI_Feature> aFeature =
872 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
873 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
874 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
875 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
876 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
877 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
878 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
879 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
881 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
884 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
885 const ModelHighAPI_RefAttr & theLine1,
886 const ModelHighAPI_RefAttr & theLine2,
887 const ModelHighAPI_Double & theValue)
889 std::shared_ptr<ModelAPI_Feature> aFeature =
890 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
891 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
892 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
893 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
894 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
895 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
896 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
897 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
899 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
902 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
903 const ModelHighAPI_RefAttr & thePoint1,
904 const ModelHighAPI_RefAttr & thePoint2)
906 std::shared_ptr<ModelAPI_Feature> aFeature =
907 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
908 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
909 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
911 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
914 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
915 const ModelHighAPI_RefAttr & theLine1,
916 const ModelHighAPI_RefAttr & theLine2)
918 std::shared_ptr<ModelAPI_Feature> aFeature =
919 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
920 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
921 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
923 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
926 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
927 const ModelHighAPI_RefAttr & thePoint,
928 const ModelHighAPI_RefAttr & thePointOrLine,
929 const ModelHighAPI_Double & theValue,
932 std::shared_ptr<ModelAPI_Feature> aFeature =
933 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
934 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
935 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
936 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
937 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
939 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
942 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
943 const ModelHighAPI_RefAttr & thePoint,
944 const ModelHighAPI_RefAttr & thePointOrLine,
945 const ModelHighAPI_Double & theValue)
947 return setDistance(thePoint, thePointOrLine, theValue, true);
950 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
951 const ModelHighAPI_RefAttr & thePoint,
952 const ModelHighAPI_RefAttr & thePointOrLine,
953 const ModelHighAPI_Double & theValue)
955 return setDistance(thePoint, thePointOrLine, theValue, false);
958 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
959 const ModelHighAPI_RefAttr & thePoint1,
960 const ModelHighAPI_RefAttr & thePoint2,
961 const ModelHighAPI_Double & theValue)
963 std::shared_ptr<ModelAPI_Feature> aFeature =
964 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
965 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
966 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
967 fillAttribute(theValue,
968 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
970 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
973 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
974 const ModelHighAPI_RefAttr & thePoint1,
975 const ModelHighAPI_RefAttr & thePoint2,
976 const ModelHighAPI_Double & theValue)
978 std::shared_ptr<ModelAPI_Feature> aFeature =
979 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
980 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
981 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
982 fillAttribute(theValue,
983 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
985 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
988 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
989 const ModelHighAPI_RefAttr & theObject1,
990 const ModelHighAPI_RefAttr & theObject2)
992 std::shared_ptr<ModelAPI_Feature> aFeature =
993 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
994 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
995 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
997 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1000 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1001 const ModelHighAPI_RefAttr & thePoint)
1003 std::shared_ptr<ModelAPI_Feature> aFeature =
1004 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1005 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1006 apply(); // finish operation to remove Fillet feature correcly
1007 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1010 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1011 const ModelHighAPI_RefAttr & thePoint,
1012 const ModelHighAPI_Double & theRadius)
1014 CompositeFeaturePtr aSketch = compositeFeature();
1015 int aNbSubs = aSketch->numberOfSubs();
1018 InterfacePtr aFilletFeature = setFillet(thePoint);
1020 // set radius for just created arc
1021 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1022 if (anArc->getKind() == SketchPlugin_Arc::ID())
1023 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1025 return aFilletFeature;
1028 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1029 const ModelHighAPI_RefAttr & theObject)
1031 std::shared_ptr<ModelAPI_Feature> aFeature =
1032 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1033 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1034 aFeature->execute();
1035 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1038 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1039 const ModelHighAPI_RefAttr & theLine)
1041 std::shared_ptr<ModelAPI_Feature> aFeature =
1042 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1043 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1044 aFeature->execute();
1045 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1048 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1049 const ModelHighAPI_RefAttr & theLine,
1050 const ModelHighAPI_Double & theValue)
1052 std::shared_ptr<ModelAPI_Feature> aFeature =
1053 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1054 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1055 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1056 aFeature->execute();
1057 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1060 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1061 const ModelHighAPI_RefAttr & thePoint,
1062 const ModelHighAPI_RefAttr & theLine)
1064 std::shared_ptr<ModelAPI_Feature> aFeature =
1065 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1066 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1067 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1068 aFeature->execute();
1069 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1072 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1073 const ModelHighAPI_RefAttr & theLine1,
1074 const ModelHighAPI_RefAttr & theLine2)
1076 std::shared_ptr<ModelAPI_Feature> aFeature =
1077 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1078 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1079 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1080 aFeature->execute();
1081 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1084 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1085 const ModelHighAPI_RefAttr & theLine1,
1086 const ModelHighAPI_RefAttr & theLine2)
1088 std::shared_ptr<ModelAPI_Feature> aFeature =
1089 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1090 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1091 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1092 aFeature->execute();
1093 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1096 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1097 const ModelHighAPI_RefAttr & theCircleOrArc,
1098 const ModelHighAPI_Double & theValue)
1100 std::shared_ptr<ModelAPI_Feature> aFeature =
1101 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1102 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1103 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1104 aFeature->execute();
1105 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1108 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1109 const ModelHighAPI_RefAttr & theLine,
1110 const ModelHighAPI_RefAttr & theCircle)
1112 std::shared_ptr<ModelAPI_Feature> aFeature =
1113 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1114 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1115 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1116 aFeature->execute();
1117 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1120 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1121 const ModelHighAPI_RefAttr & theLine)
1123 std::shared_ptr<ModelAPI_Feature> aFeature =
1124 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1125 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1126 aFeature->execute();
1127 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1130 //--------------------------------------------------------------------------------------
1132 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1134 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1135 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1138 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1140 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1141 theFeature->attribute(SketchPlugin_Line::START_ID()));
1142 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1143 theFeature->attribute(SketchPlugin_Line::END_ID()));
1145 if (!aStartAttr || !aEndAttr)
1146 return std::shared_ptr<GeomAPI_Pnt2d>();
1148 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1149 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1150 return std::shared_ptr<GeomAPI_Pnt2d>(
1151 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1154 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1156 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1157 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1158 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1160 if (!aCenter || !aRadius)
1161 return std::shared_ptr<GeomAPI_Pnt2d>();
1163 return std::shared_ptr<GeomAPI_Pnt2d>(
1164 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1167 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1169 static const double PI = 3.141592653589793238463;
1171 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1172 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1173 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1174 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1175 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1176 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1178 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1179 return std::shared_ptr<GeomAPI_Pnt2d>();
1181 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1182 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1183 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1184 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1186 double anAngle = aStartDir->angle(aEndDir);
1187 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1188 if (isReversed && anAngle > 0.)
1189 anAngle -= 2.0 * PI;
1190 else if (!isReversed && anAngle <= 0.)
1191 anAngle += 2.0 * PI;
1193 double cosA = cos(anAngle);
1194 double sinA = sin(anAngle);
1196 // rotate start dir to find middle point on arc
1197 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1198 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1199 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1201 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1204 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1205 bool isEllipse = true)
1207 const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1208 SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1209 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1210 theFeature->attribute(anAttrName));
1211 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1214 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1215 const CompositeFeaturePtr& theSketch)
1217 GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1218 GeomPointPtr aMiddle = anEdge.middlePoint();
1220 std::shared_ptr<SketchPlugin_Sketch> aSketch =
1221 std::dynamic_pointer_cast<SketchPlugin_Sketch>(theSketch);
1222 return aSketch->to2D(aMiddle);
1225 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1226 const CompositeFeaturePtr& theSketch)
1228 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1229 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1231 // move only features of the following types
1232 const std::string& aFeatureKind = aFeature->getKind();
1233 if (aFeatureKind == SketchPlugin_Point::ID())
1234 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1235 else if (aFeatureKind == SketchPlugin_Line::ID())
1236 aMiddlePoint = middlePointOnLine(aFeature);
1237 else if (aFeatureKind == SketchPlugin_Circle::ID())
1238 aMiddlePoint = pointOnCircle(aFeature);
1239 else if (aFeatureKind == SketchPlugin_Arc::ID())
1240 aMiddlePoint = middlePointOnArc(aFeature);
1241 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1242 aMiddlePoint = pointOnEllipse(aFeature);
1243 else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1244 aMiddlePoint = pointOnEllipse(aFeature, false);
1245 else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1246 aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1247 aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1249 return aMiddlePoint;
1252 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1253 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1255 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1256 theMovedEntity.fillMessage(aMessage);
1258 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1259 if (aMessage->movedAttribute())
1260 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1262 anOriginalPosition = middlePoint(aMessage->movedObject(), compositeFeature());
1264 if (!anOriginalPosition)
1265 return; // something has gone wrong, do not process movement
1267 aMessage->setOriginalPosition(anOriginalPosition);
1268 aMessage->setCurrentPosition(theTargetPoint);
1269 Events_Loop::loop()->send(aMessage);
1272 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1273 double theTargetX, double theTargetY)
1275 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1276 move(theMovedEntity, aTargetPoint);
1279 //--------------------------------------------------------------------------------------
1281 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1283 FeaturePtr aBase = feature();
1284 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1285 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1286 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1287 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1288 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1289 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1290 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1292 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1295 //--------------------------------------------------------------------------------------
1297 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1299 // collect edges of the first face
1300 std::list<GeomShapePtr> anEdges1;
1301 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1302 anEdges1.push_back(anExp.current());
1303 // compare edges of faces
1304 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1305 GeomShapePtr aCurrent = anExp.current();
1306 bool isFound = false;
1307 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1308 for (; anIt1 != anEdges1.end(); ++anIt1)
1309 if (aCurrent->isSameGeometry(*anIt1)) {
1311 anEdges1.erase(anIt1);
1317 return !anEdges1.empty();
1320 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1322 ResultConstructionPtr aSketchResult =
1323 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1327 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1328 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1332 // collect faces constructed by SketchBuilder algorithm
1333 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1334 aWires->norm(), aWires);
1335 const ListOfShape& aFaces = aSketchBuilder.faces();
1337 // compare faces stored in sketch with faces generated by SketchBuilder
1338 int aNbSketchFaces = aSketchResult->facesNum();
1340 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1341 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1342 ++aFIt, ++aFaceIndex) {
1343 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1344 GeomFacePtr aCurFace = (*aFIt)->face();
1345 if (isDifferent(aSketchFace, aCurFace))
1351 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1352 std::list<std::list<ResultPtr> >& theEdges)
1354 ResultConstructionPtr aSketchResult =
1355 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1359 // collect curves of the sketch
1360 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1361 int aSubNum = theSketch->numberOfSubs();
1362 for (int a = 0; a < aSubNum; ++a) {
1363 FeaturePtr aSub = theSketch->subFeature(a);
1364 const std::list<ResultPtr>& aResults = aSub->results();
1365 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1366 for (; aRes != aResults.cend(); aRes++) {
1367 GeomShapePtr aCurShape = (*aRes)->shape();
1368 if (aCurShape && aCurShape->isEdge())
1369 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1373 // convert each face to the list of results of its edges
1374 int aFacesNum = aSketchResult->facesNum();
1375 for (int a = 0; a < aFacesNum; ++a) {
1376 theEdges.push_back(std::list<ResultPtr>());
1377 std::list<ResultPtr>& aCurEdges = theEdges.back();
1379 GeomFacePtr aFace = aSketchResult->face(a);
1380 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1381 anExp.more(); anExp.next()) {
1382 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1383 aCurEdges.push_back(aCurves[aCurrent]);
1388 //--------------------------------------------------------------------------------------
1390 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1392 FeaturePtr aBase = feature();
1393 const std::string& aDocName = theDumper.name(aBase->document());
1395 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1396 if (anExternal->value()) {
1397 theDumper << aBase << " = model.addSketch(" << aDocName <<
1398 ", " << anExternal << ")" << std::endl;
1400 // Sketch is base on a plane.
1401 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1402 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1403 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1404 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1405 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1406 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1408 // Check the plane is coordinate plane
1409 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1410 if(anExternal->context()) { // checking for selected planes
1411 if (!aPlaneName.empty()
1412 && anExternal->context()->data()
1413 && anExternal->context()->data()->name() == aPlaneName) {
1414 // dump sketch based on coordinate plane
1415 theDumper << aBase << " = model.addSketch(" << aDocName
1416 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1417 } else { // some other plane
1418 theDumper << aBase << " = model.addSketch(" << aDocName <<
1419 ", " << anExternal<< ")" << std::endl;
1422 if (aPlaneName.empty()) {
1423 // needs import additional module
1424 theDumper.importModule("GeomAPI");
1425 // dump plane parameters
1426 const std::string& aSketchName = theDumper.name(aBase);
1427 std::string anOriginName = aSketchName + "_origin";
1428 std::string aNormalName = aSketchName + "_norm";
1429 std::string aDirXName = aSketchName + "_dirx";
1430 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1431 // and then dumplicate dumping it in the next line
1432 theDumper << anOriginName << " = " << anOrigin << "\n"
1433 << aNormalName << " = " << aNormal << "\n"
1434 << aDirXName << " = " << aDirX << "\n";
1435 // dump sketch based on arbitrary plane
1436 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1437 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1439 // dump sketch based on coordinate plane
1440 theDumper << aBase << " = model.addSketch(" << aDocName
1441 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1446 // dump sketch's subfeatures
1447 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1448 theDumper.processSubs(aCompFeat, true);
1450 // if face order differs to the order generated by SketchBuilder,
1451 // dump the list of faces for correct execution of the script
1452 if (isCustomFacesOrder(aCompFeat)) {
1453 std::list<std::list<ResultPtr> > aFaces;
1454 edgesOfSketchFaces(aCompFeat, aFaces);
1456 const std::string& aSketchName = theDumper.name(aBase);
1457 std::string aMethodName(".changeFacesOrder");
1458 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1460 theDumper << aSketchName << aMethodName << "([";
1461 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1462 aFIt != aFaces.end(); ++aFIt) {
1463 if (aFIt != aFaces.begin())
1464 theDumper << ",\n" << aSpaceShift << " ";
1467 theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1468 // call model.do() for correct update of the document's labels related to the changed faces
1469 theDumper << "model.do()" << std::endl;