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_EllipticArc> SketchAPI_Sketch::addEllipticArc(
666 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
667 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
668 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
669 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
672 std::shared_ptr<ModelAPI_Feature> aFeature =
673 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
674 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
675 theCenter, theFocus, theStart, theEnd, theInversed));
678 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
679 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theCenter,
680 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theMajorAxisPoint,
681 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theStartPoint,
682 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theEndPoint,
685 std::shared_ptr<ModelAPI_Feature> aFeature =
686 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
687 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
688 theCenter.first, theCenter.second,
689 theMajorAxisPoint.first, theMajorAxisPoint.second,
690 theStartPoint.first, theStartPoint.second,
691 theEndPoint.first, theEndPoint.second,
695 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
696 const ModelHighAPI_Selection & theExternal)
698 std::shared_ptr<ModelAPI_Feature> aFeature =
699 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
700 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
703 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
704 const std::string & theExternalName)
706 std::shared_ptr<ModelAPI_Feature> aFeature =
707 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
708 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
711 //--------------------------------------------------------------------------------------
712 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
713 const ModelHighAPI_Selection & theExternalFeature,
716 std::shared_ptr<ModelAPI_Feature> aFeature =
717 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
718 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
719 aProjection->setIncludeToResult(theKeepResult);
723 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
724 const std::string & theExternalName,
727 std::shared_ptr<ModelAPI_Feature> aFeature =
728 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
729 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
730 aProjection->setIncludeToResult(theKeepResult);
734 //--------------------------------------------------------------------------------------
735 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
736 const ModelHighAPI_RefAttr & theMirrorLine,
737 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
739 std::shared_ptr<ModelAPI_Feature> aFeature =
740 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
741 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
744 //--------------------------------------------------------------------------------------
745 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
746 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
747 const ModelHighAPI_RefAttr & thePoint1,
748 const ModelHighAPI_RefAttr & thePoint2,
749 const ModelHighAPI_Integer & theNumberOfObjects,
752 std::shared_ptr<ModelAPI_Feature> aFeature =
753 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
754 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
755 thePoint2, theNumberOfObjects, theFullValue));
758 //--------------------------------------------------------------------------------------
759 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
760 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
761 const ModelHighAPI_RefAttr & theCenter,
762 const ModelHighAPI_Double & theAngle,
763 const ModelHighAPI_Integer & theNumberOfObjects,
767 std::shared_ptr<ModelAPI_Feature> aFeature =
768 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
770 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
771 theAngle, theNumberOfObjects, theFullValue, theReversed));
774 //--------------------------------------------------------------------------------------
775 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
776 const ModelHighAPI_Reference& theFeature,
777 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
779 std::shared_ptr<ModelAPI_Feature> aFeature =
780 compositeFeature()->addFeature(SketchPlugin_Split::ID());
781 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
783 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
784 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
785 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
786 fillAttribute(thePositionPoint, aPointAttr);
789 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
792 //--------------------------------------------------------------------------------------
793 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
794 const ModelHighAPI_Reference& theFeature,
795 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
797 std::shared_ptr<ModelAPI_Feature> aFeature =
798 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
799 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
801 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
802 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
803 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
804 fillAttribute(thePositionPoint, aPointAttr);
807 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
810 //--------------------------------------------------------------------------------------
811 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
812 const ModelHighAPI_RefAttr & theLine1,
813 const ModelHighAPI_RefAttr & theLine2,
814 const ModelHighAPI_Double & theValue)
816 std::shared_ptr<ModelAPI_Feature> aFeature =
817 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
818 fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
819 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
820 // fill the value before llines to avoid calculation of angle value by the Angle feature
821 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
822 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
823 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
825 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
828 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
829 const ModelHighAPI_RefAttr & theLine1,
830 const ModelHighAPI_RefAttr & theLine2,
831 const ModelHighAPI_Double & theValue)
833 std::shared_ptr<ModelAPI_Feature> aFeature =
834 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
835 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
836 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
837 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
838 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
839 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
841 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
844 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
845 const ModelHighAPI_RefAttr & theLine1,
846 const ModelHighAPI_RefAttr & theLine2,
847 const ModelHighAPI_Double & theValue)
849 std::shared_ptr<ModelAPI_Feature> aFeature =
850 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
851 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
852 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
853 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
854 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
855 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
857 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
860 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
861 const ModelHighAPI_RefAttr & thePoint1,
862 const ModelHighAPI_RefAttr & thePoint2)
864 std::shared_ptr<ModelAPI_Feature> aFeature =
865 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
866 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
867 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
869 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
872 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
873 const ModelHighAPI_RefAttr & theLine1,
874 const ModelHighAPI_RefAttr & theLine2)
876 std::shared_ptr<ModelAPI_Feature> aFeature =
877 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::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::setDistance(
885 const ModelHighAPI_RefAttr & thePoint,
886 const ModelHighAPI_RefAttr & thePointOrLine,
887 const ModelHighAPI_Double & theValue,
890 std::shared_ptr<ModelAPI_Feature> aFeature =
891 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
892 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
893 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
894 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
895 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
897 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
900 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
901 const ModelHighAPI_RefAttr & thePoint,
902 const ModelHighAPI_RefAttr & thePointOrLine,
903 const ModelHighAPI_Double & theValue)
905 return setDistance(thePoint, thePointOrLine, theValue, true);
908 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
909 const ModelHighAPI_RefAttr & thePoint,
910 const ModelHighAPI_RefAttr & thePointOrLine,
911 const ModelHighAPI_Double & theValue)
913 return setDistance(thePoint, thePointOrLine, theValue, false);
916 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
917 const ModelHighAPI_RefAttr & thePoint1,
918 const ModelHighAPI_RefAttr & thePoint2,
919 const ModelHighAPI_Double & theValue)
921 std::shared_ptr<ModelAPI_Feature> aFeature =
922 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
923 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
924 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
925 fillAttribute(theValue,
926 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
928 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
931 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
932 const ModelHighAPI_RefAttr & thePoint1,
933 const ModelHighAPI_RefAttr & thePoint2,
934 const ModelHighAPI_Double & theValue)
936 std::shared_ptr<ModelAPI_Feature> aFeature =
937 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
938 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
939 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
940 fillAttribute(theValue,
941 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
943 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
946 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
947 const ModelHighAPI_RefAttr & theObject1,
948 const ModelHighAPI_RefAttr & theObject2)
950 std::shared_ptr<ModelAPI_Feature> aFeature =
951 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
952 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
953 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
955 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
958 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
959 const ModelHighAPI_RefAttr & thePoint)
961 std::shared_ptr<ModelAPI_Feature> aFeature =
962 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
963 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
964 apply(); // finish operation to remove Fillet feature correcly
965 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
968 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
969 const ModelHighAPI_RefAttr & thePoint,
970 const ModelHighAPI_Double & theRadius)
972 CompositeFeaturePtr aSketch = compositeFeature();
973 int aNbSubs = aSketch->numberOfSubs();
976 InterfacePtr aFilletFeature = setFillet(thePoint);
978 // set radius for just created arc
979 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
980 if (anArc->getKind() == SketchPlugin_Arc::ID())
981 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
983 return aFilletFeature;
986 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
987 const ModelHighAPI_RefAttr & theObject)
989 std::shared_ptr<ModelAPI_Feature> aFeature =
990 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
991 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
993 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
996 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
997 const ModelHighAPI_RefAttr & theLine)
999 std::shared_ptr<ModelAPI_Feature> aFeature =
1000 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1001 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1002 aFeature->execute();
1003 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1006 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1007 const ModelHighAPI_RefAttr & theLine,
1008 const ModelHighAPI_Double & theValue)
1010 std::shared_ptr<ModelAPI_Feature> aFeature =
1011 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1012 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1013 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1014 aFeature->execute();
1015 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1018 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1019 const ModelHighAPI_RefAttr & thePoint,
1020 const ModelHighAPI_RefAttr & theLine)
1022 std::shared_ptr<ModelAPI_Feature> aFeature =
1023 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1024 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1025 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1026 aFeature->execute();
1027 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1030 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1031 const ModelHighAPI_RefAttr & theLine1,
1032 const ModelHighAPI_RefAttr & theLine2)
1034 std::shared_ptr<ModelAPI_Feature> aFeature =
1035 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1036 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1037 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1038 aFeature->execute();
1039 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1042 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1043 const ModelHighAPI_RefAttr & theLine1,
1044 const ModelHighAPI_RefAttr & theLine2)
1046 std::shared_ptr<ModelAPI_Feature> aFeature =
1047 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1048 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1049 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1050 aFeature->execute();
1051 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1054 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1055 const ModelHighAPI_RefAttr & theCircleOrArc,
1056 const ModelHighAPI_Double & theValue)
1058 std::shared_ptr<ModelAPI_Feature> aFeature =
1059 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1060 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1061 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1062 aFeature->execute();
1063 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1066 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1067 const ModelHighAPI_RefAttr & theLine,
1068 const ModelHighAPI_RefAttr & theCircle)
1070 std::shared_ptr<ModelAPI_Feature> aFeature =
1071 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1072 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1073 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1074 aFeature->execute();
1075 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1078 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1079 const ModelHighAPI_RefAttr & theLine)
1081 std::shared_ptr<ModelAPI_Feature> aFeature =
1082 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1083 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1084 aFeature->execute();
1085 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1088 //--------------------------------------------------------------------------------------
1090 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1092 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1093 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1096 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1098 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1099 theFeature->attribute(SketchPlugin_Line::START_ID()));
1100 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1101 theFeature->attribute(SketchPlugin_Line::END_ID()));
1103 if (!aStartAttr || !aEndAttr)
1104 return std::shared_ptr<GeomAPI_Pnt2d>();
1106 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1107 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1108 return std::shared_ptr<GeomAPI_Pnt2d>(
1109 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1112 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1114 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1115 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1116 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1118 if (!aCenter || !aRadius)
1119 return std::shared_ptr<GeomAPI_Pnt2d>();
1121 return std::shared_ptr<GeomAPI_Pnt2d>(
1122 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1125 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1127 static const double PI = 3.141592653589793238463;
1129 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1130 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1131 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1132 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1133 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1134 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1136 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1137 return std::shared_ptr<GeomAPI_Pnt2d>();
1139 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1140 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1141 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1142 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1144 double anAngle = aStartDir->angle(aEndDir);
1145 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1146 if (isReversed && anAngle > 0.)
1147 anAngle -= 2.0 * PI;
1148 else if (!isReversed && anAngle <= 0.)
1149 anAngle += 2.0 * PI;
1151 double cosA = cos(anAngle);
1152 double sinA = sin(anAngle);
1154 // rotate start dir to find middle point on arc
1155 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1156 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1157 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1159 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1162 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature)
1164 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1165 theFeature->attribute(SketchPlugin_Ellipse::MAJOR_AXIS_END_ID()));
1166 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1169 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1171 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1172 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1174 // move only features of the following types
1175 const std::string& aFeatureKind = aFeature->getKind();
1176 if (aFeatureKind == SketchPlugin_Point::ID())
1177 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1178 else if (aFeatureKind == SketchPlugin_Line::ID())
1179 aMiddlePoint = middlePointOnLine(aFeature);
1180 else if (aFeatureKind == SketchPlugin_Circle::ID())
1181 aMiddlePoint = pointOnCircle(aFeature);
1182 else if (aFeatureKind == SketchPlugin_Arc::ID())
1183 aMiddlePoint = middlePointOnArc(aFeature);
1184 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1185 aMiddlePoint = pointOnEllipse(aFeature);
1187 return aMiddlePoint;
1190 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1191 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1193 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1194 theMovedEntity.fillMessage(aMessage);
1196 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1197 if (aMessage->movedAttribute())
1198 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1200 anOriginalPosition = middlePoint(aMessage->movedObject());
1202 if (!anOriginalPosition)
1203 return; // something has gone wrong, do not process movement
1205 aMessage->setOriginalPosition(anOriginalPosition);
1206 aMessage->setCurrentPosition(theTargetPoint);
1207 Events_Loop::loop()->send(aMessage);
1210 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1211 double theTargetX, double theTargetY)
1213 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1214 move(theMovedEntity, aTargetPoint);
1217 //--------------------------------------------------------------------------------------
1219 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1221 FeaturePtr aBase = feature();
1222 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1223 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1224 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1225 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1226 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1227 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1228 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1230 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1233 //--------------------------------------------------------------------------------------
1235 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1237 // collect edges of the first face
1238 std::list<GeomShapePtr> anEdges1;
1239 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1240 anEdges1.push_back(anExp.current());
1241 // compare edges of faces
1242 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1243 GeomShapePtr aCurrent = anExp.current();
1244 bool isFound = false;
1245 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1246 for (; anIt1 != anEdges1.end(); ++anIt1)
1247 if (aCurrent->isSameGeometry(*anIt1)) {
1249 anEdges1.erase(anIt1);
1255 return !anEdges1.empty();
1258 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1260 ResultConstructionPtr aSketchResult =
1261 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1265 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1266 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1270 // collect faces constructed by SketchBuilder algorithm
1271 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1272 aWires->norm(), aWires);
1273 const ListOfShape& aFaces = aSketchBuilder.faces();
1275 // compare faces stored in sketch with faces generated by SketchBuilder
1276 int aNbSketchFaces = aSketchResult->facesNum();
1278 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1279 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1280 ++aFIt, ++aFaceIndex) {
1281 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1282 GeomFacePtr aCurFace = (*aFIt)->face();
1283 if (isDifferent(aSketchFace, aCurFace))
1289 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1290 std::list<std::list<ResultPtr> >& theEdges)
1292 ResultConstructionPtr aSketchResult =
1293 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1297 // collect curves of the sketch
1298 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1299 int aSubNum = theSketch->numberOfSubs();
1300 for (int a = 0; a < aSubNum; ++a) {
1301 FeaturePtr aSub = theSketch->subFeature(a);
1302 const std::list<ResultPtr>& aResults = aSub->results();
1303 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1304 for (; aRes != aResults.cend(); aRes++) {
1305 GeomShapePtr aCurShape = (*aRes)->shape();
1306 if (aCurShape && aCurShape->isEdge())
1307 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1311 // convert each face to the list of results of its edges
1312 int aFacesNum = aSketchResult->facesNum();
1313 for (int a = 0; a < aFacesNum; ++a) {
1314 theEdges.push_back(std::list<ResultPtr>());
1315 std::list<ResultPtr>& aCurEdges = theEdges.back();
1317 GeomFacePtr aFace = aSketchResult->face(a);
1318 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1319 anExp.more(); anExp.next()) {
1320 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1321 aCurEdges.push_back(aCurves[aCurrent]);
1326 //--------------------------------------------------------------------------------------
1328 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1330 FeaturePtr aBase = feature();
1331 const std::string& aDocName = theDumper.name(aBase->document());
1333 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1334 if (anExternal->value()) {
1335 theDumper << aBase << " = model.addSketch(" << aDocName <<
1336 ", " << anExternal << ")" << std::endl;
1338 // Sketch is base on a plane.
1339 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1340 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1341 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1342 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1343 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1344 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1346 // Check the plane is coordinate plane
1347 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1348 if(anExternal->context()) { // checking for selected planes
1349 if (!aPlaneName.empty()
1350 && anExternal->context()->data()
1351 && anExternal->context()->data()->name() == aPlaneName) {
1352 // dump sketch based on coordinate plane
1353 theDumper << aBase << " = model.addSketch(" << aDocName
1354 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1355 } else { // some other plane
1356 theDumper << aBase << " = model.addSketch(" << aDocName <<
1357 ", " << anExternal<< ")" << std::endl;
1360 if (aPlaneName.empty()) {
1361 // needs import additional module
1362 theDumper.importModule("GeomAPI");
1363 // dump plane parameters
1364 const std::string& aSketchName = theDumper.name(aBase);
1365 std::string anOriginName = aSketchName + "_origin";
1366 std::string aNormalName = aSketchName + "_norm";
1367 std::string aDirXName = aSketchName + "_dirx";
1368 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1369 // and then dumplicate dumping it in the next line
1370 theDumper << anOriginName << " = " << anOrigin << "\n"
1371 << aNormalName << " = " << aNormal << "\n"
1372 << aDirXName << " = " << aDirX << "\n";
1373 // dump sketch based on arbitrary plane
1374 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1375 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1377 // dump sketch based on coordinate plane
1378 theDumper << aBase << " = model.addSketch(" << aDocName
1379 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1384 // dump sketch's subfeatures
1385 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1386 theDumper.processSubs(aCompFeat, true);
1388 // if face order differs to the order generated by SketchBuilder,
1389 // dump the list of faces for correct execution of the script
1390 if (isCustomFacesOrder(aCompFeat)) {
1391 std::list<std::list<ResultPtr> > aFaces;
1392 edgesOfSketchFaces(aCompFeat, aFaces);
1394 const std::string& aSketchName = theDumper.name(aBase);
1395 std::string aMethodName(".changeFacesOrder");
1396 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1398 theDumper << aSketchName << aMethodName << "([";
1399 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1400 aFIt != aFaces.end(); ++aFIt) {
1401 if (aFIt != aFaces.begin())
1402 theDumper << ",\n" << aSpaceShift << " ";
1405 theDumper << "\n" << aSpaceShift << " ])" << std::endl;