1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchAPI_Sketch.h"
21 //--------------------------------------------------------------------------------------
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_ConstraintAngle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintCollinear.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_Fillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMiddle.h>
34 #include <SketchPlugin_ConstraintMirror.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
39 #include <SketchPlugin_Trim.h>
40 #include <SketchPlugin_Split.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketcherPrs_Tools.h>
44 //--------------------------------------------------------------------------------------
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_ResultConstruction.h>
48 #include <ModelHighAPI_Double.h>
49 #include <ModelHighAPI_Dumper.h>
50 #include <ModelHighAPI_RefAttr.h>
51 #include <ModelHighAPI_Selection.h>
52 #include <ModelHighAPI_Services.h>
53 #include <ModelHighAPI_Tools.h>
54 //--------------------------------------------------------------------------------------
55 #include "SketchAPI_Arc.h"
56 #include "SketchAPI_Circle.h"
57 #include "SketchAPI_Ellipse.h"
58 #include "SketchAPI_EllipticArc.h"
59 #include "SketchAPI_IntersectionPoint.h"
60 #include "SketchAPI_Line.h"
61 #include "SketchAPI_MacroArc.h"
62 #include "SketchAPI_MacroCircle.h"
63 #include "SketchAPI_MacroEllipse.h"
64 #include "SketchAPI_MacroEllipticArc.h"
65 #include "SketchAPI_Mirror.h"
66 #include "SketchAPI_Point.h"
67 #include "SketchAPI_Projection.h"
68 #include "SketchAPI_Rectangle.h"
69 #include "SketchAPI_Rotation.h"
70 #include "SketchAPI_Translation.h"
71 //--------------------------------------------------------------------------------------
72 #include <GeomAPI_Curve.h>
73 #include <GeomAPI_Dir2d.h>
74 #include <GeomAPI_PlanarEdges.h>
75 #include <GeomAPI_ShapeExplorer.h>
76 #include <GeomAPI_XY.h>
77 #include <GeomAlgoAPI_SketchBuilder.h>
79 //--------------------------------------------------------------------------------------
80 SketchAPI_Sketch::SketchAPI_Sketch(
81 const std::shared_ptr<ModelAPI_Feature> & theFeature)
82 : ModelHighAPI_Interface(theFeature)
87 SketchAPI_Sketch::SketchAPI_Sketch(
88 const std::shared_ptr<ModelAPI_Feature> & theFeature,
89 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
90 : ModelHighAPI_Interface(theFeature)
97 SketchAPI_Sketch::SketchAPI_Sketch(
98 const std::shared_ptr<ModelAPI_Feature> & theFeature,
99 const ModelHighAPI_Selection & theExternal)
100 : ModelHighAPI_Interface(theFeature)
103 setExternal(theExternal);
107 SketchAPI_Sketch::SketchAPI_Sketch(
108 const std::shared_ptr<ModelAPI_Feature> & theFeature,
109 std::shared_ptr<ModelAPI_Object> thePlaneObject)
110 : ModelHighAPI_Interface(theFeature)
113 setExternal(thePlaneObject);
117 SketchAPI_Sketch::~SketchAPI_Sketch()
122 //--------------------------------------------------------------------------------------
123 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
125 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
128 //--------------------------------------------------------------------------------------
129 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
131 fillAttribute(thePlane->origin(), myorigin);
132 fillAttribute(thePlane->dirX(), mydirX);
133 fillAttribute(thePlane->normal(), mynormal);
138 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
139 bool theRemoveExternalDependency)
141 FeaturePtr aSketch = feature();
143 DocumentPtr aDoc = aSketch->document();
144 bool useVisible = false;
145 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
146 aDoc->setCurrentFeature(aSketch, useVisible);
148 if (theRemoveExternalDependency)
149 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
151 setExternal(thePlane);
153 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
156 //--------------------------------------------------------------------------------------
157 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
159 fillAttribute(theExternal, myexternal);
164 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
166 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
167 ModelHighAPI_Selection aSel(aRes);
171 //--------------------------------------------------------------------------------------
172 void SketchAPI_Sketch::setValue(
173 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
174 const ModelHighAPI_Double & theValue)
176 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
178 // theConstraint->execute();
181 //--------------------------------------------------------------------------------------
182 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
184 const_cast<SketchAPI_Sketch*>(this)->execute();
186 std::list<ModelHighAPI_Selection> aSelectionList;
188 ResultConstructionPtr aResultConstruction =
189 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
190 if (aResultConstruction.get() == NULL)
191 return aSelectionList;
193 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
194 aSelectionList.push_back(
195 ModelHighAPI_Selection(aResultConstruction,
196 aResultConstruction->face(anIndex)));
199 return aSelectionList;
202 //--------------------------------------------------------------------------------------
203 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
204 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
206 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
207 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
210 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
211 const ModelHighAPI_Selection & theExternal)
213 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
214 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
217 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
218 const std::string & theExternalName)
220 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
222 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
225 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
226 std::shared_ptr<ModelAPI_Object> thePlaneObject)
228 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
229 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
233 //--------------------------------------------------------------------------------------
234 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
236 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
237 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
238 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
239 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
240 PointPtr aPoint(new SketchAPI_Point(aFeature));
241 aFreePoints.push_back(aPoint);
246 //--------------------------------------------------------------------------------------
247 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
249 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
250 if (aCurve->isTrimmed())
251 aCurve = aCurve->basisCurve();
255 void SketchAPI_Sketch::changeFacesOrder(
256 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
258 // collect faces of the sketch
259 ResultConstructionPtr aSketchResult =
260 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
261 if (!aSketchResult) {
262 // sketch is nested to a boolean operation, thus, it has no result yet.
263 feature()->execute();
265 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
267 std::list<GeomFacePtr> aFaces;
268 int aFacesNum = aSketchResult->facesNum();
269 for (int i = 0; i < aFacesNum; ++i)
270 aFaces.push_back(aSketchResult->face(i));
271 // find new faces order according to the given lists of edges
272 std::list<GeomFacePtr> aNewFacesOrder;
273 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
274 for (; anIt != theFaces.end(); ++anIt) {
275 // find the appropriate face
276 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
277 for (; aFIt != aFaces.end(); ++aFIt) {
278 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
279 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
280 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
281 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
284 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
285 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
286 if (!aCurve1->isEqual(aCurve2))
290 if (aEdgeIt == anIt->end() && !aFExp.more()) {
292 aNewFacesOrder.push_back(*aFIt);
298 // place the rest faces at the end of new faces list
300 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
301 // update the result of the sketch with the new order of faces
302 aSketchResult->setFacesOrder(aNewFacesOrder);
305 //--------------------------------------------------------------------------------------
306 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
307 double theX, double theY)
309 std::shared_ptr<ModelAPI_Feature> aFeature =
310 compositeFeature()->addFeature(SketchPlugin_Point::ID());
311 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
313 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
314 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
316 std::shared_ptr<ModelAPI_Feature> aFeature =
317 compositeFeature()->addFeature(SketchPlugin_Point::ID());
318 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
320 std::shared_ptr<SketchAPI_Point>
321 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
323 std::shared_ptr<ModelAPI_Feature> aFeature =
324 compositeFeature()->addFeature(SketchPlugin_Point::ID());
325 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
327 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
329 std::shared_ptr<ModelAPI_Feature> aFeature =
330 compositeFeature()->addFeature(SketchPlugin_Point::ID());
331 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
334 //--------------------------------------------------------------------------------------
335 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
336 const ModelHighAPI_Selection & theExternal,
339 std::shared_ptr<ModelAPI_Feature> aFeature =
340 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
341 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
342 anIntersection->setIncludeToResult(theKeepResult);
343 return anIntersection;
345 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
346 const std::string & theExternalName,
349 std::shared_ptr<ModelAPI_Feature> aFeature =
350 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
351 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
352 anIntersection->setIncludeToResult(theKeepResult);
353 return anIntersection;
356 //--------------------------------------------------------------------------------------
357 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
358 double theX2, double theY2)
360 std::shared_ptr<ModelAPI_Feature> aFeature =
361 compositeFeature()->addFeature(SketchPlugin_Line::ID());
362 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
364 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
365 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
366 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
368 std::shared_ptr<ModelAPI_Feature> aFeature =
369 compositeFeature()->addFeature(SketchPlugin_Line::ID());
370 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
372 std::shared_ptr<SketchAPI_Line>
373 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
375 std::shared_ptr<ModelAPI_Feature> aFeature =
376 compositeFeature()->addFeature(SketchPlugin_Line::ID());
377 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
379 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
381 std::shared_ptr<ModelAPI_Feature> aFeature =
382 compositeFeature()->addFeature(SketchPlugin_Line::ID());
383 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
386 //--------------------------------------------------------------------------------------
387 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
388 double theX2, double theY2)
390 std::shared_ptr<ModelAPI_Feature> aFeature =
391 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
392 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
394 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
395 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
396 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
398 std::shared_ptr<ModelAPI_Feature> aFeature =
399 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
400 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
403 //--------------------------------------------------------------------------------------
404 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
408 std::shared_ptr<ModelAPI_Feature> aFeature =
409 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
410 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
413 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
414 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
417 std::shared_ptr<ModelAPI_Feature> aFeature =
418 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
419 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
422 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
427 std::shared_ptr<ModelAPI_Feature> aFeature =
428 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
429 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
430 thePassedX, thePassedY));
433 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
434 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
435 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
437 std::shared_ptr<ModelAPI_Feature> aFeature =
438 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
439 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
442 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
443 double theX2, double theY2,
444 double theX3, double theY3)
446 std::shared_ptr<ModelAPI_Feature> aFeature =
447 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
448 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
453 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
454 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
455 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
456 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
458 std::shared_ptr<ModelAPI_Feature> aFeature =
459 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
460 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
463 std::shared_ptr<SketchAPI_Circle>
464 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
466 std::shared_ptr<ModelAPI_Feature> aFeature =
467 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
468 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
471 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
473 std::shared_ptr<ModelAPI_Feature> aFeature =
474 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
475 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
478 //--------------------------------------------------------------------------------------
479 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
480 double theStartX, double theStartY,
481 double theEndX, double theEndY,
484 std::shared_ptr<ModelAPI_Feature> aFeature =
485 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
486 return ArcPtr(new SketchAPI_Arc(aFeature,
487 theCenterX, theCenterY,
488 theStartX, theStartY,
493 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
494 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
495 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
496 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
499 std::shared_ptr<ModelAPI_Feature> aFeature =
500 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
501 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
504 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
505 double theEndX, double theEndY,
506 double thePassedX, double thePassedY)
508 std::shared_ptr<ModelAPI_Feature> aFeature =
509 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
510 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
511 theStartX, theStartY,
513 thePassedX, thePassedY));
516 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
517 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
518 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
519 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
521 std::shared_ptr<ModelAPI_Feature> aFeature =
522 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
523 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
526 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
527 const ModelHighAPI_RefAttr& theTangentPoint,
528 double theEndX, double theEndY,
532 std::shared_ptr<ModelAPI_Feature> aFeature =
533 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
534 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
536 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
538 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
542 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
543 const ModelHighAPI_RefAttr& theTangentPoint,
544 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
548 std::shared_ptr<ModelAPI_Feature> aFeature =
549 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
550 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
552 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
554 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
558 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
560 std::shared_ptr<ModelAPI_Feature> aFeature =
561 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
562 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
565 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
567 std::shared_ptr<ModelAPI_Feature> aFeature =
568 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
569 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
572 //--------------------------------------------------------------------------------------
573 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
574 double theCenterX, double theCenterY,
575 double theFocusX, double theFocusY,
576 double theMinorRadius)
578 std::shared_ptr<ModelAPI_Feature> aFeature =
579 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
580 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
581 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
584 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
585 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
586 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
587 double theMinorRadius)
589 std::shared_ptr<ModelAPI_Feature> aFeature =
590 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
591 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
594 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
595 double thePoint1X, double thePoint1Y,
596 double thePoint2X, double thePoint2Y,
597 double thePassedX, double thePassedY,
600 std::shared_ptr<ModelAPI_Feature> aFeature =
601 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
602 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
603 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
606 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
607 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint1,
608 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint2,
609 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePassedPoint,
612 std::shared_ptr<ModelAPI_Feature> aFeature =
613 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
614 MacroEllipsePtr anEllipse;
615 if (thePoint1.second.isEmpty() &&
616 thePoint2.second.isEmpty() &&
617 thePassedPoint.second.isEmpty()) {
618 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
619 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
622 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
623 thePoint1.first, thePoint1.second,
624 thePoint2.first, thePoint2.second,
625 thePassedPoint.first, thePassedPoint.second,
631 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
632 const ModelHighAPI_Selection & theExternal)
634 std::shared_ptr<ModelAPI_Feature> aFeature =
635 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
636 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
639 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
640 const std::string & theExternalName)
642 std::shared_ptr<ModelAPI_Feature> aFeature =
643 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
644 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
647 //--------------------------------------------------------------------------------------
648 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
649 double theCenterX, double theCenterY,
650 double theFocusX, double theFocusY,
651 double theStartX, double theStartY,
652 double theEndX, double theEndY,
655 std::shared_ptr<ModelAPI_Feature> aFeature =
656 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
657 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
658 theCenterX, theCenterY,
659 theFocusX, theFocusY,
660 theStartX, theStartY,
665 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
666 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theCenter,
667 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theMajorAxisPoint,
668 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theStartPoint,
669 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theEndPoint,
672 std::shared_ptr<ModelAPI_Feature> aFeature =
673 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
674 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
675 theCenter.first, theCenter.second,
676 theMajorAxisPoint.first, theMajorAxisPoint.second,
677 theStartPoint.first, theStartPoint.second,
678 theEndPoint.first, theEndPoint.second,
682 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
683 const ModelHighAPI_Selection & theExternal)
685 std::shared_ptr<ModelAPI_Feature> aFeature =
686 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
687 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
690 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
691 const std::string & theExternalName)
693 std::shared_ptr<ModelAPI_Feature> aFeature =
694 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
695 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
698 //--------------------------------------------------------------------------------------
699 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
700 const ModelHighAPI_Selection & theExternalFeature,
703 std::shared_ptr<ModelAPI_Feature> aFeature =
704 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
705 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
706 aProjection->setIncludeToResult(theKeepResult);
710 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
711 const std::string & theExternalName,
714 std::shared_ptr<ModelAPI_Feature> aFeature =
715 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
716 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
717 aProjection->setIncludeToResult(theKeepResult);
721 //--------------------------------------------------------------------------------------
722 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
723 const ModelHighAPI_RefAttr & theMirrorLine,
724 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
726 std::shared_ptr<ModelAPI_Feature> aFeature =
727 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
728 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
731 //--------------------------------------------------------------------------------------
732 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
733 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
734 const ModelHighAPI_RefAttr & thePoint1,
735 const ModelHighAPI_RefAttr & thePoint2,
736 const ModelHighAPI_Integer & theNumberOfObjects,
739 std::shared_ptr<ModelAPI_Feature> aFeature =
740 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
741 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
742 thePoint2, theNumberOfObjects, theFullValue));
745 //--------------------------------------------------------------------------------------
746 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
747 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
748 const ModelHighAPI_RefAttr & theCenter,
749 const ModelHighAPI_Double & theAngle,
750 const ModelHighAPI_Integer & theNumberOfObjects,
754 std::shared_ptr<ModelAPI_Feature> aFeature =
755 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
757 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
758 theAngle, theNumberOfObjects, theFullValue, theReversed));
761 //--------------------------------------------------------------------------------------
762 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
763 const ModelHighAPI_Reference& theFeature,
764 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
766 std::shared_ptr<ModelAPI_Feature> aFeature =
767 compositeFeature()->addFeature(SketchPlugin_Split::ID());
768 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
770 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
771 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
772 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
773 fillAttribute(thePositionPoint, aPointAttr);
776 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
779 //--------------------------------------------------------------------------------------
780 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
781 const ModelHighAPI_Reference& theFeature,
782 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
784 std::shared_ptr<ModelAPI_Feature> aFeature =
785 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
786 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
788 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
789 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
790 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
791 fillAttribute(thePositionPoint, aPointAttr);
794 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
797 //--------------------------------------------------------------------------------------
798 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
799 const ModelHighAPI_RefAttr & theLine1,
800 const ModelHighAPI_RefAttr & theLine2,
801 const ModelHighAPI_Double & theValue)
803 std::shared_ptr<ModelAPI_Feature> aFeature =
804 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
805 fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
806 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
807 // fill the value before llines to avoid calculation of angle value by the Angle feature
808 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
809 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
810 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
812 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
815 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
816 const ModelHighAPI_RefAttr & theLine1,
817 const ModelHighAPI_RefAttr & theLine2,
818 const ModelHighAPI_Double & theValue)
820 std::shared_ptr<ModelAPI_Feature> aFeature =
821 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
822 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
823 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
824 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
825 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
826 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
828 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
831 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
832 const ModelHighAPI_RefAttr & theLine1,
833 const ModelHighAPI_RefAttr & theLine2,
834 const ModelHighAPI_Double & theValue)
836 std::shared_ptr<ModelAPI_Feature> aFeature =
837 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
838 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
839 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
840 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
841 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
842 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
844 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
847 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
848 const ModelHighAPI_RefAttr & thePoint1,
849 const ModelHighAPI_RefAttr & thePoint2)
851 std::shared_ptr<ModelAPI_Feature> aFeature =
852 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
853 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
854 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
856 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
859 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
860 const ModelHighAPI_RefAttr & theLine1,
861 const ModelHighAPI_RefAttr & theLine2)
863 std::shared_ptr<ModelAPI_Feature> aFeature =
864 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
865 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
866 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
868 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
871 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
872 const ModelHighAPI_RefAttr & thePoint,
873 const ModelHighAPI_RefAttr & thePointOrLine,
874 const ModelHighAPI_Double & theValue,
877 std::shared_ptr<ModelAPI_Feature> aFeature =
878 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
879 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
880 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
881 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
882 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
884 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
887 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
888 const ModelHighAPI_RefAttr & thePoint,
889 const ModelHighAPI_RefAttr & thePointOrLine,
890 const ModelHighAPI_Double & theValue)
892 return setDistance(thePoint, thePointOrLine, theValue, true);
895 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
896 const ModelHighAPI_RefAttr & thePoint,
897 const ModelHighAPI_RefAttr & thePointOrLine,
898 const ModelHighAPI_Double & theValue)
900 return setDistance(thePoint, thePointOrLine, theValue, false);
903 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
904 const ModelHighAPI_RefAttr & thePoint1,
905 const ModelHighAPI_RefAttr & thePoint2,
906 const ModelHighAPI_Double & theValue)
908 std::shared_ptr<ModelAPI_Feature> aFeature =
909 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
910 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
911 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
912 fillAttribute(theValue,
913 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
915 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
918 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
919 const ModelHighAPI_RefAttr & thePoint1,
920 const ModelHighAPI_RefAttr & thePoint2,
921 const ModelHighAPI_Double & theValue)
923 std::shared_ptr<ModelAPI_Feature> aFeature =
924 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
925 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
926 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
927 fillAttribute(theValue,
928 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
930 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
933 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
934 const ModelHighAPI_RefAttr & theObject1,
935 const ModelHighAPI_RefAttr & theObject2)
937 std::shared_ptr<ModelAPI_Feature> aFeature =
938 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
939 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
940 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
942 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
945 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
946 const ModelHighAPI_RefAttr & thePoint)
948 std::shared_ptr<ModelAPI_Feature> aFeature =
949 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
950 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
951 apply(); // finish operation to remove Fillet feature correcly
952 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
955 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
956 const ModelHighAPI_RefAttr & thePoint,
957 const ModelHighAPI_Double & theRadius)
959 CompositeFeaturePtr aSketch = compositeFeature();
960 int aNbSubs = aSketch->numberOfSubs();
963 InterfacePtr aFilletFeature = setFillet(thePoint);
965 // set radius for just created arc
966 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
967 if (anArc->getKind() == SketchPlugin_Arc::ID())
968 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
970 return aFilletFeature;
973 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
974 const ModelHighAPI_RefAttr & theObject)
976 std::shared_ptr<ModelAPI_Feature> aFeature =
977 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
978 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
980 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
983 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
984 const ModelHighAPI_RefAttr & theLine)
986 std::shared_ptr<ModelAPI_Feature> aFeature =
987 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
988 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
990 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
993 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
994 const ModelHighAPI_RefAttr & theLine,
995 const ModelHighAPI_Double & theValue)
997 std::shared_ptr<ModelAPI_Feature> aFeature =
998 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
999 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1000 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1001 aFeature->execute();
1002 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1005 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1006 const ModelHighAPI_RefAttr & thePoint,
1007 const ModelHighAPI_RefAttr & theLine)
1009 std::shared_ptr<ModelAPI_Feature> aFeature =
1010 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1011 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1012 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1013 aFeature->execute();
1014 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1017 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1018 const ModelHighAPI_RefAttr & theLine1,
1019 const ModelHighAPI_RefAttr & theLine2)
1021 std::shared_ptr<ModelAPI_Feature> aFeature =
1022 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1023 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1024 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1025 aFeature->execute();
1026 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1029 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1030 const ModelHighAPI_RefAttr & theLine1,
1031 const ModelHighAPI_RefAttr & theLine2)
1033 std::shared_ptr<ModelAPI_Feature> aFeature =
1034 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1035 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1036 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1037 aFeature->execute();
1038 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1041 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1042 const ModelHighAPI_RefAttr & theCircleOrArc,
1043 const ModelHighAPI_Double & theValue)
1045 std::shared_ptr<ModelAPI_Feature> aFeature =
1046 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1047 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1048 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1049 aFeature->execute();
1050 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1053 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1054 const ModelHighAPI_RefAttr & theLine,
1055 const ModelHighAPI_RefAttr & theCircle)
1057 std::shared_ptr<ModelAPI_Feature> aFeature =
1058 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1059 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1060 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1061 aFeature->execute();
1062 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1065 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1066 const ModelHighAPI_RefAttr & theLine)
1068 std::shared_ptr<ModelAPI_Feature> aFeature =
1069 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1070 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1071 aFeature->execute();
1072 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1075 //--------------------------------------------------------------------------------------
1077 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1079 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1080 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1083 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1085 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1086 theFeature->attribute(SketchPlugin_Line::START_ID()));
1087 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1088 theFeature->attribute(SketchPlugin_Line::END_ID()));
1090 if (!aStartAttr || !aEndAttr)
1091 return std::shared_ptr<GeomAPI_Pnt2d>();
1093 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1094 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1095 return std::shared_ptr<GeomAPI_Pnt2d>(
1096 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1099 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1101 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1102 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1103 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1105 if (!aCenter || !aRadius)
1106 return std::shared_ptr<GeomAPI_Pnt2d>();
1108 return std::shared_ptr<GeomAPI_Pnt2d>(
1109 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1112 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1114 static const double PI = 3.141592653589793238463;
1116 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1117 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1118 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1119 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1120 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1121 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1123 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1124 return std::shared_ptr<GeomAPI_Pnt2d>();
1126 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1127 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1128 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1129 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1131 double anAngle = aStartDir->angle(aEndDir);
1132 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1133 if (isReversed && anAngle > 0.)
1134 anAngle -= 2.0 * PI;
1135 else if (!isReversed && anAngle <= 0.)
1136 anAngle += 2.0 * PI;
1138 double cosA = cos(anAngle);
1139 double sinA = sin(anAngle);
1141 // rotate start dir to find middle point on arc
1142 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1143 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1144 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1146 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1149 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature)
1151 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1152 theFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()));
1153 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1156 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1158 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1159 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1161 // move only features of the following types
1162 const std::string& aFeatureKind = aFeature->getKind();
1163 if (aFeatureKind == SketchPlugin_Point::ID())
1164 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1165 else if (aFeatureKind == SketchPlugin_Line::ID())
1166 aMiddlePoint = middlePointOnLine(aFeature);
1167 else if (aFeatureKind == SketchPlugin_Circle::ID())
1168 aMiddlePoint = pointOnCircle(aFeature);
1169 else if (aFeatureKind == SketchPlugin_Arc::ID())
1170 aMiddlePoint = middlePointOnArc(aFeature);
1171 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1172 aMiddlePoint = pointOnEllipse(aFeature);
1174 return aMiddlePoint;
1177 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1178 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1180 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1181 theMovedEntity.fillMessage(aMessage);
1183 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1184 if (aMessage->movedAttribute())
1185 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1187 anOriginalPosition = middlePoint(aMessage->movedObject());
1189 if (!anOriginalPosition)
1190 return; // something has gone wrong, do not process movement
1192 aMessage->setOriginalPosition(anOriginalPosition);
1193 aMessage->setCurrentPosition(theTargetPoint);
1194 Events_Loop::loop()->send(aMessage);
1197 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1198 double theTargetX, double theTargetY)
1200 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1201 move(theMovedEntity, aTargetPoint);
1204 //--------------------------------------------------------------------------------------
1206 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1208 FeaturePtr aBase = feature();
1209 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1210 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1211 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1212 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1213 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1214 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1215 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1217 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1220 //--------------------------------------------------------------------------------------
1222 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1224 // collect edges of the first face
1225 std::list<GeomShapePtr> anEdges1;
1226 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1227 anEdges1.push_back(anExp.current());
1228 // compare edges of faces
1229 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1230 GeomShapePtr aCurrent = anExp.current();
1231 bool isFound = false;
1232 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1233 for (; anIt1 != anEdges1.end(); ++anIt1)
1234 if (aCurrent->isSameGeometry(*anIt1)) {
1236 anEdges1.erase(anIt1);
1242 return !anEdges1.empty();
1245 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1247 ResultConstructionPtr aSketchResult =
1248 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1252 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1253 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1257 // collect faces constructed by SketchBuilder algorithm
1258 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1259 aWires->norm(), aWires);
1260 const ListOfShape& aFaces = aSketchBuilder.faces();
1262 // compare faces stored in sketch with faces generated by SketchBuilder
1263 int aNbSketchFaces = aSketchResult->facesNum();
1265 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1266 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1267 ++aFIt, ++aFaceIndex) {
1268 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1269 GeomFacePtr aCurFace = (*aFIt)->face();
1270 if (isDifferent(aSketchFace, aCurFace))
1276 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1277 std::list<std::list<ResultPtr> >& theEdges)
1279 ResultConstructionPtr aSketchResult =
1280 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1284 // collect curves of the sketch
1285 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1286 int aSubNum = theSketch->numberOfSubs();
1287 for (int a = 0; a < aSubNum; ++a) {
1288 FeaturePtr aSub = theSketch->subFeature(a);
1289 const std::list<ResultPtr>& aResults = aSub->results();
1290 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1291 for (; aRes != aResults.cend(); aRes++) {
1292 GeomShapePtr aCurShape = (*aRes)->shape();
1293 if (aCurShape && aCurShape->isEdge())
1294 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1298 // convert each face to the list of results of its edges
1299 int aFacesNum = aSketchResult->facesNum();
1300 for (int a = 0; a < aFacesNum; ++a) {
1301 theEdges.push_back(std::list<ResultPtr>());
1302 std::list<ResultPtr>& aCurEdges = theEdges.back();
1304 GeomFacePtr aFace = aSketchResult->face(a);
1305 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1306 anExp.more(); anExp.next()) {
1307 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1308 aCurEdges.push_back(aCurves[aCurrent]);
1313 //--------------------------------------------------------------------------------------
1315 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1317 FeaturePtr aBase = feature();
1318 const std::string& aDocName = theDumper.name(aBase->document());
1320 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1321 if (anExternal->value()) {
1322 theDumper << aBase << " = model.addSketch(" << aDocName <<
1323 ", " << anExternal << ")" << std::endl;
1325 // Sketch is base on a plane.
1326 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1327 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1328 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1329 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1330 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1331 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1333 // Check the plane is coordinate plane
1334 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1335 if(anExternal->context()) { // checking for selected planes
1336 if (!aPlaneName.empty()
1337 && anExternal->context()->data()
1338 && anExternal->context()->data()->name() == aPlaneName) {
1339 // dump sketch based on coordinate plane
1340 theDumper << aBase << " = model.addSketch(" << aDocName
1341 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1342 } else { // some other plane
1343 theDumper << aBase << " = model.addSketch(" << aDocName <<
1344 ", " << anExternal<< ")" << std::endl;
1347 if (aPlaneName.empty()) {
1348 // needs import additional module
1349 theDumper.importModule("GeomAPI");
1350 // dump plane parameters
1351 const std::string& aSketchName = theDumper.name(aBase);
1352 std::string anOriginName = aSketchName + "_origin";
1353 std::string aNormalName = aSketchName + "_norm";
1354 std::string aDirXName = aSketchName + "_dirx";
1355 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1356 // and then dumplicate dumping it in the next line
1357 theDumper << anOriginName << " = " << anOrigin << "\n"
1358 << aNormalName << " = " << aNormal << "\n"
1359 << aDirXName << " = " << aDirX << "\n";
1360 // dump sketch based on arbitrary plane
1361 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1362 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1364 // dump sketch based on coordinate plane
1365 theDumper << aBase << " = model.addSketch(" << aDocName
1366 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1371 // dump sketch's subfeatures
1372 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1373 theDumper.processSubs(aCompFeat, true);
1375 // if face order differs to the order generated by SketchBuilder,
1376 // dump the list of faces for correct execution of the script
1377 if (isCustomFacesOrder(aCompFeat)) {
1378 std::list<std::list<ResultPtr> > aFaces;
1379 edgesOfSketchFaces(aCompFeat, aFaces);
1381 const std::string& aSketchName = theDumper.name(aBase);
1382 std::string aMethodName(".changeFacesOrder");
1383 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1385 theDumper << aSketchName << aMethodName << "([";
1386 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1387 aFIt != aFaces.end(); ++aFIt) {
1388 if (aFIt != aFaces.begin())
1389 theDumper << ",\n" << aSpaceShift << " ";
1392 theDumper << "\n" << aSpaceShift << " ])" << std::endl;