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>
81 //--------------------------------------------------------------------------------------
82 SketchAPI_Sketch::SketchAPI_Sketch(
83 const std::shared_ptr<ModelAPI_Feature> & theFeature)
84 : ModelHighAPI_Interface(theFeature)
89 SketchAPI_Sketch::SketchAPI_Sketch(
90 const std::shared_ptr<ModelAPI_Feature> & theFeature,
91 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
92 : ModelHighAPI_Interface(theFeature)
99 SketchAPI_Sketch::SketchAPI_Sketch(
100 const std::shared_ptr<ModelAPI_Feature> & theFeature,
101 const ModelHighAPI_Selection & theExternal)
102 : ModelHighAPI_Interface(theFeature)
105 setExternal(theExternal);
109 SketchAPI_Sketch::SketchAPI_Sketch(
110 const std::shared_ptr<ModelAPI_Feature> & theFeature,
111 std::shared_ptr<ModelAPI_Object> thePlaneObject)
112 : ModelHighAPI_Interface(theFeature)
115 setExternal(thePlaneObject);
119 SketchAPI_Sketch::~SketchAPI_Sketch()
124 //--------------------------------------------------------------------------------------
125 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
127 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
130 //--------------------------------------------------------------------------------------
131 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
133 fillAttribute(thePlane->origin(), myorigin);
134 fillAttribute(thePlane->dirX(), mydirX);
135 fillAttribute(thePlane->normal(), mynormal);
140 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
141 bool theRemoveExternalDependency)
143 FeaturePtr aSketch = feature();
145 DocumentPtr aDoc = aSketch->document();
146 bool useVisible = false;
147 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
148 aDoc->setCurrentFeature(aSketch, useVisible);
150 if (theRemoveExternalDependency)
151 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
153 setExternal(thePlane);
155 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
158 //--------------------------------------------------------------------------------------
159 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
161 fillAttribute(theExternal, myexternal);
166 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
168 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
169 ModelHighAPI_Selection aSel(aRes);
173 //--------------------------------------------------------------------------------------
174 void SketchAPI_Sketch::setValue(
175 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
176 const ModelHighAPI_Double & theValue)
178 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
180 // theConstraint->execute();
183 //--------------------------------------------------------------------------------------
184 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
186 const_cast<SketchAPI_Sketch*>(this)->execute();
188 std::list<ModelHighAPI_Selection> aSelectionList;
190 ResultConstructionPtr aResultConstruction =
191 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
192 if (aResultConstruction.get() == NULL)
193 return aSelectionList;
195 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
196 aSelectionList.push_back(
197 ModelHighAPI_Selection(aResultConstruction,
198 aResultConstruction->face(anIndex)));
201 return aSelectionList;
204 //--------------------------------------------------------------------------------------
205 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
206 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
208 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
209 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
212 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
213 const ModelHighAPI_Selection & theExternal)
215 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
216 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
219 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
220 const std::string & theExternalName)
222 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
224 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
227 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
228 std::shared_ptr<ModelAPI_Object> thePlaneObject)
230 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
231 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
235 //--------------------------------------------------------------------------------------
236 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
238 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
239 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
240 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
241 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
242 PointPtr aPoint(new SketchAPI_Point(aFeature));
243 aFreePoints.push_back(aPoint);
248 //--------------------------------------------------------------------------------------
249 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
251 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
252 if (aCurve->isTrimmed())
253 aCurve = aCurve->basisCurve();
257 void SketchAPI_Sketch::changeFacesOrder(
258 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
260 // collect faces of the sketch
261 ResultConstructionPtr aSketchResult =
262 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
263 if (!aSketchResult) {
264 // sketch is nested to a boolean operation, thus, it has no result yet.
265 feature()->execute();
267 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
269 std::list<GeomFacePtr> aFaces;
270 int aFacesNum = aSketchResult->facesNum();
271 for (int i = 0; i < aFacesNum; ++i)
272 aFaces.push_back(aSketchResult->face(i));
273 // find new faces order according to the given lists of edges
274 std::list<GeomFacePtr> aNewFacesOrder;
275 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
276 for (; anIt != theFaces.end(); ++anIt) {
277 // find the appropriate face
278 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
279 for (; aFIt != aFaces.end(); ++aFIt) {
280 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
281 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
282 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
283 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
286 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
287 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
288 if (!aCurve1->isEqual(aCurve2))
292 if (aEdgeIt == anIt->end() && !aFExp.more()) {
294 aNewFacesOrder.push_back(*aFIt);
300 // place the rest faces at the end of new faces list
302 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
303 // update the result of the sketch with the new order of faces
304 aSketchResult->setFacesOrder(aNewFacesOrder);
307 //--------------------------------------------------------------------------------------
308 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
309 double theX, double theY)
311 std::shared_ptr<ModelAPI_Feature> aFeature =
312 compositeFeature()->addFeature(SketchPlugin_Point::ID());
313 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
315 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
316 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
318 std::shared_ptr<ModelAPI_Feature> aFeature =
319 compositeFeature()->addFeature(SketchPlugin_Point::ID());
320 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
322 std::shared_ptr<SketchAPI_Point>
323 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
325 std::shared_ptr<ModelAPI_Feature> aFeature =
326 compositeFeature()->addFeature(SketchPlugin_Point::ID());
327 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
329 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
331 std::shared_ptr<ModelAPI_Feature> aFeature =
332 compositeFeature()->addFeature(SketchPlugin_Point::ID());
333 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
336 //--------------------------------------------------------------------------------------
337 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
338 const ModelHighAPI_Selection & theExternal,
341 std::shared_ptr<ModelAPI_Feature> aFeature =
342 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
343 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
344 anIntersection->setIncludeToResult(theKeepResult);
345 return anIntersection;
347 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
348 const std::string & theExternalName,
351 std::shared_ptr<ModelAPI_Feature> aFeature =
352 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
353 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
354 anIntersection->setIncludeToResult(theKeepResult);
355 return anIntersection;
358 //--------------------------------------------------------------------------------------
359 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
360 double theX2, double theY2)
362 std::shared_ptr<ModelAPI_Feature> aFeature =
363 compositeFeature()->addFeature(SketchPlugin_Line::ID());
364 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
366 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
367 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
368 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
370 std::shared_ptr<ModelAPI_Feature> aFeature =
371 compositeFeature()->addFeature(SketchPlugin_Line::ID());
372 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
374 std::shared_ptr<SketchAPI_Line>
375 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
377 std::shared_ptr<ModelAPI_Feature> aFeature =
378 compositeFeature()->addFeature(SketchPlugin_Line::ID());
379 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
381 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
383 std::shared_ptr<ModelAPI_Feature> aFeature =
384 compositeFeature()->addFeature(SketchPlugin_Line::ID());
385 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
388 //--------------------------------------------------------------------------------------
389 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
390 double theX2, double theY2)
392 std::shared_ptr<ModelAPI_Feature> aFeature =
393 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
394 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
396 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
397 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
398 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
400 std::shared_ptr<ModelAPI_Feature> aFeature =
401 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
402 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
405 //--------------------------------------------------------------------------------------
406 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
410 std::shared_ptr<ModelAPI_Feature> aFeature =
411 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
412 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
415 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
416 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
419 std::shared_ptr<ModelAPI_Feature> aFeature =
420 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
421 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
424 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
429 std::shared_ptr<ModelAPI_Feature> aFeature =
430 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
431 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
432 thePassedX, thePassedY));
435 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
436 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
437 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
439 std::shared_ptr<ModelAPI_Feature> aFeature =
440 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
441 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
444 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
445 double theX2, double theY2,
446 double theX3, double theY3)
448 std::shared_ptr<ModelAPI_Feature> aFeature =
449 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
450 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
455 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
456 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
457 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
458 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
460 std::shared_ptr<ModelAPI_Feature> aFeature =
461 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
462 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
465 std::shared_ptr<SketchAPI_Circle>
466 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
468 std::shared_ptr<ModelAPI_Feature> aFeature =
469 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
470 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
473 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
475 std::shared_ptr<ModelAPI_Feature> aFeature =
476 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
477 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
480 //--------------------------------------------------------------------------------------
481 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
482 double theStartX, double theStartY,
483 double theEndX, double theEndY,
486 std::shared_ptr<ModelAPI_Feature> aFeature =
487 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
488 return ArcPtr(new SketchAPI_Arc(aFeature,
489 theCenterX, theCenterY,
490 theStartX, theStartY,
495 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
496 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
497 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
498 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
501 std::shared_ptr<ModelAPI_Feature> aFeature =
502 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
503 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
506 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
507 double theEndX, double theEndY,
508 double thePassedX, double thePassedY)
510 std::shared_ptr<ModelAPI_Feature> aFeature =
511 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
512 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
513 theStartX, theStartY,
515 thePassedX, thePassedY));
518 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
519 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
520 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
521 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
523 std::shared_ptr<ModelAPI_Feature> aFeature =
524 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
525 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
528 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
529 const ModelHighAPI_RefAttr& theTangentPoint,
530 double theEndX, double theEndY,
534 std::shared_ptr<ModelAPI_Feature> aFeature =
535 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
536 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
538 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
540 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
544 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
545 const ModelHighAPI_RefAttr& theTangentPoint,
546 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
550 std::shared_ptr<ModelAPI_Feature> aFeature =
551 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
552 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
554 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
556 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
560 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
562 std::shared_ptr<ModelAPI_Feature> aFeature =
563 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
564 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
567 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
569 std::shared_ptr<ModelAPI_Feature> aFeature =
570 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
571 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
574 //--------------------------------------------------------------------------------------
575 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
576 double theCenterX, double theCenterY,
577 double theFocusX, double theFocusY,
578 double theMinorRadius)
580 std::shared_ptr<ModelAPI_Feature> aFeature =
581 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
582 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
583 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
586 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
587 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
588 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
589 double theMinorRadius)
591 std::shared_ptr<ModelAPI_Feature> aFeature =
592 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
593 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
596 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
597 double thePoint1X, double thePoint1Y,
598 double thePoint2X, double thePoint2Y,
599 double thePassedX, double thePassedY,
602 std::shared_ptr<ModelAPI_Feature> aFeature =
603 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
604 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
605 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
608 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
609 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint1,
610 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePoint2,
611 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& thePassedPoint,
614 std::shared_ptr<ModelAPI_Feature> aFeature =
615 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
616 MacroEllipsePtr anEllipse;
617 if (thePoint1.second.isEmpty() &&
618 thePoint2.second.isEmpty() &&
619 thePassedPoint.second.isEmpty()) {
620 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
621 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
624 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
625 thePoint1.first, thePoint1.second,
626 thePoint2.first, thePoint2.second,
627 thePassedPoint.first, thePassedPoint.second,
633 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
634 const ModelHighAPI_Selection & theExternal)
636 std::shared_ptr<ModelAPI_Feature> aFeature =
637 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
638 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
641 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
642 const std::string & theExternalName)
644 std::shared_ptr<ModelAPI_Feature> aFeature =
645 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
646 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
649 //--------------------------------------------------------------------------------------
650 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
651 double theCenterX, double theCenterY,
652 double theFocusX, double theFocusY,
653 double theStartX, double theStartY,
654 double theEndX, double theEndY,
657 std::shared_ptr<ModelAPI_Feature> aFeature =
658 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
659 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
660 theCenterX, theCenterY,
661 theFocusX, theFocusY,
662 theStartX, theStartY,
667 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
668 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theCenter,
669 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theMajorAxisPoint,
670 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theStartPoint,
671 const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr>& theEndPoint,
674 std::shared_ptr<ModelAPI_Feature> aFeature =
675 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
676 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
677 theCenter.first, theCenter.second,
678 theMajorAxisPoint.first, theMajorAxisPoint.second,
679 theStartPoint.first, theStartPoint.second,
680 theEndPoint.first, theEndPoint.second,
684 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
685 const ModelHighAPI_Selection & theExternal)
687 std::shared_ptr<ModelAPI_Feature> aFeature =
688 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
689 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
692 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
693 const std::string & theExternalName)
695 std::shared_ptr<ModelAPI_Feature> aFeature =
696 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
697 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
700 //--------------------------------------------------------------------------------------
701 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
702 const ModelHighAPI_Selection & theExternalFeature,
705 std::shared_ptr<ModelAPI_Feature> aFeature =
706 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
707 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
708 aProjection->setIncludeToResult(theKeepResult);
712 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
713 const std::string & theExternalName,
716 std::shared_ptr<ModelAPI_Feature> aFeature =
717 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
718 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
719 aProjection->setIncludeToResult(theKeepResult);
723 //--------------------------------------------------------------------------------------
724 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
725 const ModelHighAPI_RefAttr & theMirrorLine,
726 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
728 std::shared_ptr<ModelAPI_Feature> aFeature =
729 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
730 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
733 //--------------------------------------------------------------------------------------
734 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
735 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
736 const ModelHighAPI_RefAttr & thePoint1,
737 const ModelHighAPI_RefAttr & thePoint2,
738 const ModelHighAPI_Integer & theNumberOfObjects,
741 std::shared_ptr<ModelAPI_Feature> aFeature =
742 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
743 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
744 thePoint2, theNumberOfObjects, theFullValue));
747 //--------------------------------------------------------------------------------------
748 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
749 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
750 const ModelHighAPI_RefAttr & theCenter,
751 const ModelHighAPI_Double & theAngle,
752 const ModelHighAPI_Integer & theNumberOfObjects,
756 std::shared_ptr<ModelAPI_Feature> aFeature =
757 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
759 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
760 theAngle, theNumberOfObjects, theFullValue, theReversed));
763 //--------------------------------------------------------------------------------------
764 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
765 const ModelHighAPI_Reference& theFeature,
766 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
768 std::shared_ptr<ModelAPI_Feature> aFeature =
769 compositeFeature()->addFeature(SketchPlugin_Split::ID());
770 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
772 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
773 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
774 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
775 fillAttribute(thePositionPoint, aPointAttr);
778 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
781 //--------------------------------------------------------------------------------------
782 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
783 const ModelHighAPI_Reference& theFeature,
784 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
786 std::shared_ptr<ModelAPI_Feature> aFeature =
787 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
788 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
790 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
791 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
792 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
793 fillAttribute(thePositionPoint, aPointAttr);
796 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
799 //--------------------------------------------------------------------------------------
800 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
801 const ModelHighAPI_RefAttr & theLine1,
802 const ModelHighAPI_RefAttr & theLine2,
803 const ModelHighAPI_Double & theValue,
804 const std::string& theType)
806 std::shared_ptr<ModelAPI_Feature> aFeature =
807 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
809 const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
810 : SketchPlugin_ConstraintAngle::THE_VERSION_1;
811 fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
813 int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
814 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
815 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
817 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
818 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
820 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
821 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
823 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
824 std::string aTypeLC = theType;
825 std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(), ::tolower);
826 if (aTypeLC == "supplementary")
827 aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
828 else if (aTypeLC == "backward")
829 aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
831 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
832 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
836 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
839 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
840 const ModelHighAPI_RefAttr & theLine1,
841 const ModelHighAPI_RefAttr & theLine2,
842 const ModelHighAPI_Double & theValue)
844 std::shared_ptr<ModelAPI_Feature> aFeature =
845 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
846 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
847 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
848 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
849 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
850 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
851 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
852 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
854 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
857 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
858 const ModelHighAPI_RefAttr & theLine1,
859 const ModelHighAPI_RefAttr & theLine2,
860 const ModelHighAPI_Double & theValue)
862 std::shared_ptr<ModelAPI_Feature> aFeature =
863 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
864 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
865 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
866 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
867 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
868 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
869 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
870 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
872 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
875 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
876 const ModelHighAPI_RefAttr & thePoint1,
877 const ModelHighAPI_RefAttr & thePoint2)
879 std::shared_ptr<ModelAPI_Feature> aFeature =
880 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
881 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
882 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
884 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
887 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
888 const ModelHighAPI_RefAttr & theLine1,
889 const ModelHighAPI_RefAttr & theLine2)
891 std::shared_ptr<ModelAPI_Feature> aFeature =
892 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
893 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
894 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
896 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
899 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
900 const ModelHighAPI_RefAttr & thePoint,
901 const ModelHighAPI_RefAttr & thePointOrLine,
902 const ModelHighAPI_Double & theValue,
905 std::shared_ptr<ModelAPI_Feature> aFeature =
906 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
907 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
908 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
909 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
910 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
912 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
915 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
916 const ModelHighAPI_RefAttr & thePoint,
917 const ModelHighAPI_RefAttr & thePointOrLine,
918 const ModelHighAPI_Double & theValue)
920 return setDistance(thePoint, thePointOrLine, theValue, true);
923 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
924 const ModelHighAPI_RefAttr & thePoint,
925 const ModelHighAPI_RefAttr & thePointOrLine,
926 const ModelHighAPI_Double & theValue)
928 return setDistance(thePoint, thePointOrLine, theValue, false);
931 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
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_ConstraintDistanceHorizontal::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::setVerticalDistance(
947 const ModelHighAPI_RefAttr & thePoint1,
948 const ModelHighAPI_RefAttr & thePoint2,
949 const ModelHighAPI_Double & theValue)
951 std::shared_ptr<ModelAPI_Feature> aFeature =
952 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
953 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
954 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
955 fillAttribute(theValue,
956 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
958 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
961 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
962 const ModelHighAPI_RefAttr & theObject1,
963 const ModelHighAPI_RefAttr & theObject2)
965 std::shared_ptr<ModelAPI_Feature> aFeature =
966 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
967 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
968 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
970 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
973 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
974 const ModelHighAPI_RefAttr & thePoint)
976 std::shared_ptr<ModelAPI_Feature> aFeature =
977 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
978 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
979 apply(); // finish operation to remove Fillet feature correcly
980 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
983 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
984 const ModelHighAPI_RefAttr & thePoint,
985 const ModelHighAPI_Double & theRadius)
987 CompositeFeaturePtr aSketch = compositeFeature();
988 int aNbSubs = aSketch->numberOfSubs();
991 InterfacePtr aFilletFeature = setFillet(thePoint);
993 // set radius for just created arc
994 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
995 if (anArc->getKind() == SketchPlugin_Arc::ID())
996 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
998 return aFilletFeature;
1001 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1002 const ModelHighAPI_RefAttr & theObject)
1004 std::shared_ptr<ModelAPI_Feature> aFeature =
1005 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1006 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1007 aFeature->execute();
1008 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1011 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1012 const ModelHighAPI_RefAttr & theLine)
1014 std::shared_ptr<ModelAPI_Feature> aFeature =
1015 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1016 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1017 aFeature->execute();
1018 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1021 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1022 const ModelHighAPI_RefAttr & theLine,
1023 const ModelHighAPI_Double & theValue)
1025 std::shared_ptr<ModelAPI_Feature> aFeature =
1026 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1027 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1028 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1029 aFeature->execute();
1030 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1033 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1034 const ModelHighAPI_RefAttr & thePoint,
1035 const ModelHighAPI_RefAttr & theLine)
1037 std::shared_ptr<ModelAPI_Feature> aFeature =
1038 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1039 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1040 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1041 aFeature->execute();
1042 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1045 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1046 const ModelHighAPI_RefAttr & theLine1,
1047 const ModelHighAPI_RefAttr & theLine2)
1049 std::shared_ptr<ModelAPI_Feature> aFeature =
1050 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1051 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1052 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1053 aFeature->execute();
1054 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1057 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1058 const ModelHighAPI_RefAttr & theLine1,
1059 const ModelHighAPI_RefAttr & theLine2)
1061 std::shared_ptr<ModelAPI_Feature> aFeature =
1062 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1063 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1064 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1065 aFeature->execute();
1066 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1069 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1070 const ModelHighAPI_RefAttr & theCircleOrArc,
1071 const ModelHighAPI_Double & theValue)
1073 std::shared_ptr<ModelAPI_Feature> aFeature =
1074 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1075 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1076 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1077 aFeature->execute();
1078 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1081 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1082 const ModelHighAPI_RefAttr & theLine,
1083 const ModelHighAPI_RefAttr & theCircle)
1085 std::shared_ptr<ModelAPI_Feature> aFeature =
1086 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1087 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1088 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1089 aFeature->execute();
1090 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1093 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1094 const ModelHighAPI_RefAttr & theLine)
1096 std::shared_ptr<ModelAPI_Feature> aFeature =
1097 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1098 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1099 aFeature->execute();
1100 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1103 //--------------------------------------------------------------------------------------
1105 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1107 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1108 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1111 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1113 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1114 theFeature->attribute(SketchPlugin_Line::START_ID()));
1115 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1116 theFeature->attribute(SketchPlugin_Line::END_ID()));
1118 if (!aStartAttr || !aEndAttr)
1119 return std::shared_ptr<GeomAPI_Pnt2d>();
1121 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1122 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1123 return std::shared_ptr<GeomAPI_Pnt2d>(
1124 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1127 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1129 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1130 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1131 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1133 if (!aCenter || !aRadius)
1134 return std::shared_ptr<GeomAPI_Pnt2d>();
1136 return std::shared_ptr<GeomAPI_Pnt2d>(
1137 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1140 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1142 static const double PI = 3.141592653589793238463;
1144 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1145 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1146 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1147 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1148 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1149 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1151 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1152 return std::shared_ptr<GeomAPI_Pnt2d>();
1154 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1155 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1156 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1157 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1159 double anAngle = aStartDir->angle(aEndDir);
1160 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1161 if (isReversed && anAngle > 0.)
1162 anAngle -= 2.0 * PI;
1163 else if (!isReversed && anAngle <= 0.)
1164 anAngle += 2.0 * PI;
1166 double cosA = cos(anAngle);
1167 double sinA = sin(anAngle);
1169 // rotate start dir to find middle point on arc
1170 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1171 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1172 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1174 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1177 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1178 bool isEllipse = true)
1180 const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1181 SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1182 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1183 theFeature->attribute(anAttrName));
1184 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1187 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1189 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1190 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1192 // move only features of the following types
1193 const std::string& aFeatureKind = aFeature->getKind();
1194 if (aFeatureKind == SketchPlugin_Point::ID())
1195 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1196 else if (aFeatureKind == SketchPlugin_Line::ID())
1197 aMiddlePoint = middlePointOnLine(aFeature);
1198 else if (aFeatureKind == SketchPlugin_Circle::ID())
1199 aMiddlePoint = pointOnCircle(aFeature);
1200 else if (aFeatureKind == SketchPlugin_Arc::ID())
1201 aMiddlePoint = middlePointOnArc(aFeature);
1202 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1203 aMiddlePoint = pointOnEllipse(aFeature);
1204 else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1205 aMiddlePoint = pointOnEllipse(aFeature, false);
1207 return aMiddlePoint;
1210 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1211 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1213 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1214 theMovedEntity.fillMessage(aMessage);
1216 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1217 if (aMessage->movedAttribute())
1218 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1220 anOriginalPosition = middlePoint(aMessage->movedObject());
1222 if (!anOriginalPosition)
1223 return; // something has gone wrong, do not process movement
1225 aMessage->setOriginalPosition(anOriginalPosition);
1226 aMessage->setCurrentPosition(theTargetPoint);
1227 Events_Loop::loop()->send(aMessage);
1230 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1231 double theTargetX, double theTargetY)
1233 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1234 move(theMovedEntity, aTargetPoint);
1237 //--------------------------------------------------------------------------------------
1239 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1241 FeaturePtr aBase = feature();
1242 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1243 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1244 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1245 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1246 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1247 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1248 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1250 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1253 //--------------------------------------------------------------------------------------
1255 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1257 // collect edges of the first face
1258 std::list<GeomShapePtr> anEdges1;
1259 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1260 anEdges1.push_back(anExp.current());
1261 // compare edges of faces
1262 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1263 GeomShapePtr aCurrent = anExp.current();
1264 bool isFound = false;
1265 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1266 for (; anIt1 != anEdges1.end(); ++anIt1)
1267 if (aCurrent->isSameGeometry(*anIt1)) {
1269 anEdges1.erase(anIt1);
1275 return !anEdges1.empty();
1278 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1280 ResultConstructionPtr aSketchResult =
1281 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1285 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1286 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1290 // collect faces constructed by SketchBuilder algorithm
1291 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1292 aWires->norm(), aWires);
1293 const ListOfShape& aFaces = aSketchBuilder.faces();
1295 // compare faces stored in sketch with faces generated by SketchBuilder
1296 int aNbSketchFaces = aSketchResult->facesNum();
1298 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1299 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1300 ++aFIt, ++aFaceIndex) {
1301 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1302 GeomFacePtr aCurFace = (*aFIt)->face();
1303 if (isDifferent(aSketchFace, aCurFace))
1309 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1310 std::list<std::list<ResultPtr> >& theEdges)
1312 ResultConstructionPtr aSketchResult =
1313 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1317 // collect curves of the sketch
1318 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1319 int aSubNum = theSketch->numberOfSubs();
1320 for (int a = 0; a < aSubNum; ++a) {
1321 FeaturePtr aSub = theSketch->subFeature(a);
1322 const std::list<ResultPtr>& aResults = aSub->results();
1323 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1324 for (; aRes != aResults.cend(); aRes++) {
1325 GeomShapePtr aCurShape = (*aRes)->shape();
1326 if (aCurShape && aCurShape->isEdge())
1327 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1331 // convert each face to the list of results of its edges
1332 int aFacesNum = aSketchResult->facesNum();
1333 for (int a = 0; a < aFacesNum; ++a) {
1334 theEdges.push_back(std::list<ResultPtr>());
1335 std::list<ResultPtr>& aCurEdges = theEdges.back();
1337 GeomFacePtr aFace = aSketchResult->face(a);
1338 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1339 anExp.more(); anExp.next()) {
1340 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1341 aCurEdges.push_back(aCurves[aCurrent]);
1346 //--------------------------------------------------------------------------------------
1348 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1350 FeaturePtr aBase = feature();
1351 const std::string& aDocName = theDumper.name(aBase->document());
1353 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1354 if (anExternal->value()) {
1355 theDumper << aBase << " = model.addSketch(" << aDocName <<
1356 ", " << anExternal << ")" << std::endl;
1358 // Sketch is base on a plane.
1359 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1360 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1361 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1362 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1363 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1364 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1366 // Check the plane is coordinate plane
1367 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1368 if(anExternal->context()) { // checking for selected planes
1369 if (!aPlaneName.empty()
1370 && anExternal->context()->data()
1371 && anExternal->context()->data()->name() == aPlaneName) {
1372 // dump sketch based on coordinate plane
1373 theDumper << aBase << " = model.addSketch(" << aDocName
1374 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1375 } else { // some other plane
1376 theDumper << aBase << " = model.addSketch(" << aDocName <<
1377 ", " << anExternal<< ")" << std::endl;
1380 if (aPlaneName.empty()) {
1381 // needs import additional module
1382 theDumper.importModule("GeomAPI");
1383 // dump plane parameters
1384 const std::string& aSketchName = theDumper.name(aBase);
1385 std::string anOriginName = aSketchName + "_origin";
1386 std::string aNormalName = aSketchName + "_norm";
1387 std::string aDirXName = aSketchName + "_dirx";
1388 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1389 // and then dumplicate dumping it in the next line
1390 theDumper << anOriginName << " = " << anOrigin << "\n"
1391 << aNormalName << " = " << aNormal << "\n"
1392 << aDirXName << " = " << aDirX << "\n";
1393 // dump sketch based on arbitrary plane
1394 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1395 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1397 // dump sketch based on coordinate plane
1398 theDumper << aBase << " = model.addSketch(" << aDocName
1399 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1404 // dump sketch's subfeatures
1405 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1406 theDumper.processSubs(aCompFeat, true);
1408 // if face order differs to the order generated by SketchBuilder,
1409 // dump the list of faces for correct execution of the script
1410 if (isCustomFacesOrder(aCompFeat)) {
1411 std::list<std::list<ResultPtr> > aFaces;
1412 edgesOfSketchFaces(aCompFeat, aFaces);
1414 const std::string& aSketchName = theDumper.name(aBase);
1415 std::string aMethodName(".changeFacesOrder");
1416 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1418 theDumper << aSketchName << aMethodName << "([";
1419 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1420 aFIt != aFaces.end(); ++aFIt) {
1421 if (aFIt != aFaces.begin())
1422 theDumper << ",\n" << aSpaceShift << " ";
1425 theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1426 // call model.do() for correct update of the document's labels related to the changed faces
1427 theDumper << "model.do()" << std::endl;