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_MacroArc.h"
57 #include "SketchAPI_Circle.h"
58 #include "SketchAPI_IntersectionPoint.h"
59 #include "SketchAPI_Line.h"
60 #include "SketchAPI_MacroCircle.h"
61 #include "SketchAPI_Mirror.h"
62 #include "SketchAPI_Point.h"
63 #include "SketchAPI_Projection.h"
64 #include "SketchAPI_Rectangle.h"
65 #include "SketchAPI_Rotation.h"
66 #include "SketchAPI_Translation.h"
67 //--------------------------------------------------------------------------------------
68 #include <GeomAPI_Curve.h>
69 #include <GeomAPI_Dir2d.h>
70 #include <GeomAPI_PlanarEdges.h>
71 #include <GeomAPI_ShapeExplorer.h>
72 #include <GeomAPI_XY.h>
73 #include <GeomAlgoAPI_SketchBuilder.h>
75 //--------------------------------------------------------------------------------------
76 SketchAPI_Sketch::SketchAPI_Sketch(
77 const std::shared_ptr<ModelAPI_Feature> & theFeature)
78 : ModelHighAPI_Interface(theFeature)
83 SketchAPI_Sketch::SketchAPI_Sketch(
84 const std::shared_ptr<ModelAPI_Feature> & theFeature,
85 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
86 : ModelHighAPI_Interface(theFeature)
93 SketchAPI_Sketch::SketchAPI_Sketch(
94 const std::shared_ptr<ModelAPI_Feature> & theFeature,
95 const ModelHighAPI_Selection & theExternal)
96 : ModelHighAPI_Interface(theFeature)
99 setExternal(theExternal);
103 SketchAPI_Sketch::SketchAPI_Sketch(
104 const std::shared_ptr<ModelAPI_Feature> & theFeature,
105 std::shared_ptr<ModelAPI_Object> thePlaneObject)
106 : ModelHighAPI_Interface(theFeature)
109 setExternal(thePlaneObject);
113 SketchAPI_Sketch::~SketchAPI_Sketch()
118 //--------------------------------------------------------------------------------------
119 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
121 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
124 //--------------------------------------------------------------------------------------
125 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
127 fillAttribute(thePlane->origin(), myorigin);
128 fillAttribute(thePlane->dirX(), mydirX);
129 fillAttribute(thePlane->normal(), mynormal);
134 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
135 bool theRemoveExternalDependency)
137 FeaturePtr aSketch = feature();
139 DocumentPtr aDoc = aSketch->document();
140 bool useVisible = false;
141 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
142 aDoc->setCurrentFeature(aSketch, useVisible);
144 if (theRemoveExternalDependency)
145 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
147 setExternal(thePlane);
149 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
152 //--------------------------------------------------------------------------------------
153 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
155 fillAttribute(theExternal, myexternal);
160 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
162 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
163 ModelHighAPI_Selection aSel(aRes);
167 //--------------------------------------------------------------------------------------
168 void SketchAPI_Sketch::setValue(
169 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
170 const ModelHighAPI_Double & theValue)
172 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
174 // theConstraint->execute();
177 //--------------------------------------------------------------------------------------
178 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
180 const_cast<SketchAPI_Sketch*>(this)->execute();
182 std::list<ModelHighAPI_Selection> aSelectionList;
184 ResultConstructionPtr aResultConstruction =
185 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
186 if (aResultConstruction.get() == NULL)
187 return aSelectionList;
189 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
190 aSelectionList.push_back(
191 ModelHighAPI_Selection(aResultConstruction,
192 aResultConstruction->face(anIndex)));
195 return aSelectionList;
198 //--------------------------------------------------------------------------------------
199 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
200 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
202 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
203 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
206 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
207 const ModelHighAPI_Selection & theExternal)
209 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
210 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
213 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
214 const std::string & theExternalName)
216 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
218 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
221 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
222 std::shared_ptr<ModelAPI_Object> thePlaneObject)
224 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
225 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
229 //--------------------------------------------------------------------------------------
230 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
232 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
233 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
234 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
235 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
236 PointPtr aPoint(new SketchAPI_Point(aFeature));
237 aFreePoints.push_back(aPoint);
242 //--------------------------------------------------------------------------------------
243 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
245 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
246 if (aCurve->isTrimmed())
247 aCurve = aCurve->basisCurve();
251 void SketchAPI_Sketch::changeFacesOrder(
252 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
254 // collect faces of the sketch
255 ResultConstructionPtr aSketchResult =
256 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
257 if (!aSketchResult) {
258 // sketch is nested to a boolean operation, thus, it has no result yet.
259 feature()->execute();
261 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
263 std::list<GeomFacePtr> aFaces;
264 int aFacesNum = aSketchResult->facesNum();
265 for (int i = 0; i < aFacesNum; ++i)
266 aFaces.push_back(aSketchResult->face(i));
267 // find new faces order according to the given lists of edges
268 std::list<GeomFacePtr> aNewFacesOrder;
269 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
270 for (; anIt != theFaces.end(); ++anIt) {
271 // find the appropriate face
272 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
273 for (; aFIt != aFaces.end(); ++aFIt) {
274 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
275 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
276 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
277 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
280 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
281 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
282 if (!aCurve1->isEqual(aCurve2))
286 if (aEdgeIt == anIt->end() && !aFExp.more()) {
288 aNewFacesOrder.push_back(*aFIt);
294 // place the rest faces at the end of new faces list
296 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
297 // update the result of the sketch with the new order of faces
298 aSketchResult->setFacesOrder(aNewFacesOrder);
301 //--------------------------------------------------------------------------------------
302 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
303 double theX, double theY)
305 std::shared_ptr<ModelAPI_Feature> aFeature =
306 compositeFeature()->addFeature(SketchPlugin_Point::ID());
307 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
309 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
310 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
312 std::shared_ptr<ModelAPI_Feature> aFeature =
313 compositeFeature()->addFeature(SketchPlugin_Point::ID());
314 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
316 std::shared_ptr<SketchAPI_Point>
317 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
319 std::shared_ptr<ModelAPI_Feature> aFeature =
320 compositeFeature()->addFeature(SketchPlugin_Point::ID());
321 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
323 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
325 std::shared_ptr<ModelAPI_Feature> aFeature =
326 compositeFeature()->addFeature(SketchPlugin_Point::ID());
327 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
330 //--------------------------------------------------------------------------------------
331 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
332 const ModelHighAPI_Selection & theExternal,
335 std::shared_ptr<ModelAPI_Feature> aFeature =
336 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
337 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
338 anIntersection->setIncludeToResult(theKeepResult);
339 return anIntersection;
341 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
342 const std::string & theExternalName,
345 std::shared_ptr<ModelAPI_Feature> aFeature =
346 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
347 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
348 anIntersection->setIncludeToResult(theKeepResult);
349 return anIntersection;
352 //--------------------------------------------------------------------------------------
353 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
354 double theX2, double theY2)
356 std::shared_ptr<ModelAPI_Feature> aFeature =
357 compositeFeature()->addFeature(SketchPlugin_Line::ID());
358 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
360 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
361 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
362 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
364 std::shared_ptr<ModelAPI_Feature> aFeature =
365 compositeFeature()->addFeature(SketchPlugin_Line::ID());
366 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
368 std::shared_ptr<SketchAPI_Line>
369 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
371 std::shared_ptr<ModelAPI_Feature> aFeature =
372 compositeFeature()->addFeature(SketchPlugin_Line::ID());
373 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
375 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
377 std::shared_ptr<ModelAPI_Feature> aFeature =
378 compositeFeature()->addFeature(SketchPlugin_Line::ID());
379 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
382 //--------------------------------------------------------------------------------------
383 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
384 double theX2, double theY2)
386 std::shared_ptr<ModelAPI_Feature> aFeature =
387 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
388 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
390 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
391 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
392 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
394 std::shared_ptr<ModelAPI_Feature> aFeature =
395 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
396 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
399 //--------------------------------------------------------------------------------------
400 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
404 std::shared_ptr<ModelAPI_Feature> aFeature =
405 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
406 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
409 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
410 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
413 std::shared_ptr<ModelAPI_Feature> aFeature =
414 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
415 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
418 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
423 std::shared_ptr<ModelAPI_Feature> aFeature =
424 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
425 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
426 thePassedX, thePassedY));
429 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
430 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
431 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
433 std::shared_ptr<ModelAPI_Feature> aFeature =
434 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
435 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
438 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
439 double theX2, double theY2,
440 double theX3, double theY3)
442 std::shared_ptr<ModelAPI_Feature> aFeature =
443 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
444 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
449 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
450 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
451 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
452 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
454 std::shared_ptr<ModelAPI_Feature> aFeature =
455 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
456 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
459 std::shared_ptr<SketchAPI_Circle>
460 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
462 std::shared_ptr<ModelAPI_Feature> aFeature =
463 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
464 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
467 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
469 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
470 std::shared_ptr<ModelAPI_Feature> aFeature =
471 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
472 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
475 //--------------------------------------------------------------------------------------
476 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
477 double theStartX, double theStartY,
478 double theEndX, double theEndY,
481 std::shared_ptr<ModelAPI_Feature> aFeature =
482 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
483 return ArcPtr(new SketchAPI_Arc(aFeature,
484 theCenterX, theCenterY,
485 theStartX, theStartY,
490 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
491 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
492 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
493 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
496 std::shared_ptr<ModelAPI_Feature> aFeature =
497 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
498 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
501 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
502 double theEndX, double theEndY,
503 double thePassedX, double thePassedY)
505 std::shared_ptr<ModelAPI_Feature> aFeature =
506 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
507 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
508 theStartX, theStartY,
510 thePassedX, thePassedY));
513 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
514 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
515 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
516 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
518 std::shared_ptr<ModelAPI_Feature> aFeature =
519 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
520 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
523 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
524 const ModelHighAPI_RefAttr& theTangentPoint,
525 double theEndX, double theEndY,
528 std::shared_ptr<ModelAPI_Feature> aFeature =
529 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
530 return MacroArcPtr(new SketchAPI_MacroArc(
531 aFeature, theTangentPoint, theEndX, theEndY, theInversed));
534 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
535 const ModelHighAPI_RefAttr& theTangentPoint,
536 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
539 std::shared_ptr<ModelAPI_Feature> aFeature =
540 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
541 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theTangentPoint, theEnd, theInversed));
544 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
546 std::shared_ptr<ModelAPI_Feature> aFeature =
547 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
548 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
551 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
553 // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
554 std::shared_ptr<ModelAPI_Feature> aFeature =
555 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
556 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
559 //--------------------------------------------------------------------------------------
560 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
561 const ModelHighAPI_Selection & theExternalFeature,
564 std::shared_ptr<ModelAPI_Feature> aFeature =
565 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
566 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
567 aProjection->setIncludeToResult(theKeepResult);
571 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
572 const std::string & theExternalName,
575 std::shared_ptr<ModelAPI_Feature> aFeature =
576 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
577 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
578 aProjection->setIncludeToResult(theKeepResult);
582 //--------------------------------------------------------------------------------------
583 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
584 const ModelHighAPI_RefAttr & theMirrorLine,
585 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
587 std::shared_ptr<ModelAPI_Feature> aFeature =
588 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
589 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
592 //--------------------------------------------------------------------------------------
593 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
594 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
595 const ModelHighAPI_RefAttr & thePoint1,
596 const ModelHighAPI_RefAttr & thePoint2,
597 const ModelHighAPI_Integer & theNumberOfObjects,
600 std::shared_ptr<ModelAPI_Feature> aFeature =
601 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
602 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
603 thePoint2, theNumberOfObjects, theFullValue));
606 //--------------------------------------------------------------------------------------
607 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
608 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
609 const ModelHighAPI_RefAttr & theCenter,
610 const ModelHighAPI_Double & theAngle,
611 const ModelHighAPI_Integer & theNumberOfObjects,
615 std::shared_ptr<ModelAPI_Feature> aFeature =
616 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
618 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
619 theAngle, theNumberOfObjects, theFullValue, theReversed));
622 //--------------------------------------------------------------------------------------
623 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
624 const ModelHighAPI_Reference& theFeature,
625 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
627 std::shared_ptr<ModelAPI_Feature> aFeature =
628 compositeFeature()->addFeature(SketchPlugin_Split::ID());
629 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
631 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
632 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
633 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
634 fillAttribute(thePositionPoint, aPointAttr);
637 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
640 //--------------------------------------------------------------------------------------
641 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
642 const ModelHighAPI_Reference& theFeature,
643 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
645 std::shared_ptr<ModelAPI_Feature> aFeature =
646 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
647 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
649 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
650 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
651 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
652 fillAttribute(thePositionPoint, aPointAttr);
655 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
658 //--------------------------------------------------------------------------------------
659 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
660 const ModelHighAPI_RefAttr & theLine1,
661 const ModelHighAPI_RefAttr & theLine2,
662 const ModelHighAPI_Double & theValue)
664 std::shared_ptr<ModelAPI_Feature> aFeature =
665 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
666 fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
667 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
668 // fill the value before llines to avoid calculation of angle value by the Angle feature
669 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
670 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
671 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
673 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
676 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
677 const ModelHighAPI_RefAttr & theLine1,
678 const ModelHighAPI_RefAttr & theLine2,
679 const ModelHighAPI_Double & theValue)
681 std::shared_ptr<ModelAPI_Feature> aFeature =
682 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
683 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
684 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
685 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
686 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
687 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
689 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
692 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
693 const ModelHighAPI_RefAttr & theLine1,
694 const ModelHighAPI_RefAttr & theLine2,
695 const ModelHighAPI_Double & theValue)
697 std::shared_ptr<ModelAPI_Feature> aFeature =
698 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
699 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
700 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
701 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
702 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
703 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
705 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
708 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
709 const ModelHighAPI_RefAttr & thePoint1,
710 const ModelHighAPI_RefAttr & thePoint2)
712 std::shared_ptr<ModelAPI_Feature> aFeature =
713 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
714 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
715 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
717 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
720 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
721 const ModelHighAPI_RefAttr & theLine1,
722 const ModelHighAPI_RefAttr & theLine2)
724 std::shared_ptr<ModelAPI_Feature> aFeature =
725 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
726 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
727 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
729 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
732 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
733 const ModelHighAPI_RefAttr & thePoint,
734 const ModelHighAPI_RefAttr & thePointOrLine,
735 const ModelHighAPI_Double & theValue,
738 std::shared_ptr<ModelAPI_Feature> aFeature =
739 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
740 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
741 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
742 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
743 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
745 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
748 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
749 const ModelHighAPI_RefAttr & thePoint,
750 const ModelHighAPI_RefAttr & thePointOrLine,
751 const ModelHighAPI_Double & theValue)
753 return setDistance(thePoint, thePointOrLine, theValue, true);
756 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
757 const ModelHighAPI_RefAttr & thePoint,
758 const ModelHighAPI_RefAttr & thePointOrLine,
759 const ModelHighAPI_Double & theValue)
761 return setDistance(thePoint, thePointOrLine, theValue, false);
764 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
765 const ModelHighAPI_RefAttr & thePoint1,
766 const ModelHighAPI_RefAttr & thePoint2,
767 const ModelHighAPI_Double & theValue)
769 std::shared_ptr<ModelAPI_Feature> aFeature =
770 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
771 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
772 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
773 fillAttribute(theValue,
774 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
776 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
779 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
780 const ModelHighAPI_RefAttr & thePoint1,
781 const ModelHighAPI_RefAttr & thePoint2,
782 const ModelHighAPI_Double & theValue)
784 std::shared_ptr<ModelAPI_Feature> aFeature =
785 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
786 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
787 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
788 fillAttribute(theValue,
789 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
791 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
794 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
795 const ModelHighAPI_RefAttr & theObject1,
796 const ModelHighAPI_RefAttr & theObject2)
798 std::shared_ptr<ModelAPI_Feature> aFeature =
799 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
800 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
801 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
803 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
806 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
807 const ModelHighAPI_RefAttr & thePoint)
809 std::shared_ptr<ModelAPI_Feature> aFeature =
810 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
811 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
812 apply(); // finish operation to remove Fillet feature correcly
813 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
816 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
817 const ModelHighAPI_RefAttr & thePoint,
818 const ModelHighAPI_Double & theRadius)
820 CompositeFeaturePtr aSketch = compositeFeature();
821 int aNbSubs = aSketch->numberOfSubs();
824 InterfacePtr aFilletFeature = setFillet(thePoint);
826 // set radius for just created arc
827 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
828 if (anArc->getKind() == SketchPlugin_Arc::ID())
829 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
831 return aFilletFeature;
834 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
835 const ModelHighAPI_RefAttr & theObject)
837 std::shared_ptr<ModelAPI_Feature> aFeature =
838 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
839 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
841 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
844 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
845 const ModelHighAPI_RefAttr & theLine)
847 std::shared_ptr<ModelAPI_Feature> aFeature =
848 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
849 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
851 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
854 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
855 const ModelHighAPI_RefAttr & theLine,
856 const ModelHighAPI_Double & theValue)
858 std::shared_ptr<ModelAPI_Feature> aFeature =
859 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
860 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
861 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
863 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
866 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
867 const ModelHighAPI_RefAttr & thePoint,
868 const ModelHighAPI_RefAttr & theLine)
870 std::shared_ptr<ModelAPI_Feature> aFeature =
871 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
872 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
873 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
875 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
878 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
879 const ModelHighAPI_RefAttr & theLine1,
880 const ModelHighAPI_RefAttr & theLine2)
882 std::shared_ptr<ModelAPI_Feature> aFeature =
883 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
884 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
885 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
887 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
890 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
891 const ModelHighAPI_RefAttr & theLine1,
892 const ModelHighAPI_RefAttr & theLine2)
894 std::shared_ptr<ModelAPI_Feature> aFeature =
895 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
896 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
897 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
899 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
902 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
903 const ModelHighAPI_RefAttr & theCircleOrArc,
904 const ModelHighAPI_Double & theValue)
906 std::shared_ptr<ModelAPI_Feature> aFeature =
907 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
908 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
909 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
911 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
914 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
915 const ModelHighAPI_RefAttr & theLine,
916 const ModelHighAPI_RefAttr & theCircle)
918 std::shared_ptr<ModelAPI_Feature> aFeature =
919 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
920 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
921 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
923 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
926 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
927 const ModelHighAPI_RefAttr & theLine)
929 std::shared_ptr<ModelAPI_Feature> aFeature =
930 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
931 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
933 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
936 //--------------------------------------------------------------------------------------
938 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
940 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
941 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
944 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
946 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
947 theFeature->attribute(SketchPlugin_Line::START_ID()));
948 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
949 theFeature->attribute(SketchPlugin_Line::END_ID()));
951 if (!aStartAttr || !aEndAttr)
952 return std::shared_ptr<GeomAPI_Pnt2d>();
954 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
955 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
956 return std::shared_ptr<GeomAPI_Pnt2d>(
957 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
960 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
962 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
963 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
964 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
966 if (!aCenter || !aRadius)
967 return std::shared_ptr<GeomAPI_Pnt2d>();
969 return std::shared_ptr<GeomAPI_Pnt2d>(
970 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
973 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
975 static const double PI = 3.141592653589793238463;
977 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
978 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
979 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
980 theFeature->attribute(SketchPlugin_Arc::START_ID()));
981 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
982 theFeature->attribute(SketchPlugin_Arc::END_ID()));
984 if (!aCenterAttr || !aStartAttr || !aEndAttr)
985 return std::shared_ptr<GeomAPI_Pnt2d>();
987 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
988 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
989 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
990 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
992 double anAngle = aStartDir->angle(aEndDir);
993 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
994 if (isReversed && anAngle > 0.)
996 else if (!isReversed && anAngle <= 0.)
999 double cosA = cos(anAngle);
1000 double sinA = sin(anAngle);
1002 // rotate start dir to find middle point on arc
1003 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1004 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1005 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1007 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1010 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1012 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1013 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1015 // move only features of the following types
1016 const std::string& aFeatureKind = aFeature->getKind();
1017 if (aFeatureKind == SketchPlugin_Point::ID())
1018 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1019 else if (aFeatureKind == SketchPlugin_Line::ID())
1020 aMiddlePoint = middlePointOnLine(aFeature);
1021 else if (aFeatureKind == SketchPlugin_Circle::ID())
1022 aMiddlePoint = pointOnCircle(aFeature);
1023 else if (aFeatureKind == SketchPlugin_Arc::ID())
1024 aMiddlePoint = middlePointOnArc(aFeature);
1026 return aMiddlePoint;
1029 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1030 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1032 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1033 theMovedEntity.fillMessage(aMessage);
1035 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1036 if (aMessage->movedAttribute())
1037 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1039 anOriginalPosition = middlePoint(aMessage->movedObject());
1041 if (!anOriginalPosition)
1042 return; // something has gone wrong, do not process movement
1044 aMessage->setOriginalPosition(anOriginalPosition);
1045 aMessage->setCurrentPosition(theTargetPoint);
1046 Events_Loop::loop()->send(aMessage);
1049 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1050 double theTargetX, double theTargetY)
1052 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1053 move(theMovedEntity, aTargetPoint);
1056 //--------------------------------------------------------------------------------------
1058 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1060 FeaturePtr aBase = feature();
1061 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1062 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1063 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1064 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1065 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1066 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1067 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1069 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1072 //--------------------------------------------------------------------------------------
1074 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1076 // collect edges of the first face
1077 std::list<GeomShapePtr> anEdges1;
1078 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1079 anEdges1.push_back(anExp.current());
1080 // compare edges of faces
1081 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1082 GeomShapePtr aCurrent = anExp.current();
1083 bool isFound = false;
1084 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1085 for (; anIt1 != anEdges1.end(); ++anIt1)
1086 if (aCurrent->isSameGeometry(*anIt1)) {
1088 anEdges1.erase(anIt1);
1094 return !anEdges1.empty();
1097 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1099 ResultConstructionPtr aSketchResult =
1100 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1104 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1105 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1109 // collect faces constructed by SketchBuilder algorithm
1110 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1111 aWires->norm(), aWires);
1112 const ListOfShape& aFaces = aSketchBuilder.faces();
1114 // compare faces stored in sketch with faces generated by SketchBuilder
1115 int aNbSketchFaces = aSketchResult->facesNum();
1117 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1118 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1119 ++aFIt, ++aFaceIndex) {
1120 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1121 GeomFacePtr aCurFace = (*aFIt)->face();
1122 if (isDifferent(aSketchFace, aCurFace))
1128 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1129 std::list<std::list<ResultPtr> >& theEdges)
1131 ResultConstructionPtr aSketchResult =
1132 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1136 // collect curves of the sketch
1137 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1138 int aSubNum = theSketch->numberOfSubs();
1139 for (int a = 0; a < aSubNum; ++a) {
1140 FeaturePtr aSub = theSketch->subFeature(a);
1141 const std::list<ResultPtr>& aResults = aSub->results();
1142 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1143 for (; aRes != aResults.cend(); aRes++) {
1144 GeomShapePtr aCurShape = (*aRes)->shape();
1145 if (aCurShape && aCurShape->isEdge())
1146 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1150 // convert each face to the list of results of its edges
1151 int aFacesNum = aSketchResult->facesNum();
1152 for (int a = 0; a < aFacesNum; ++a) {
1153 theEdges.push_back(std::list<ResultPtr>());
1154 std::list<ResultPtr>& aCurEdges = theEdges.back();
1156 GeomFacePtr aFace = aSketchResult->face(a);
1157 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1158 anExp.more(); anExp.next()) {
1159 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1160 aCurEdges.push_back(aCurves[aCurrent]);
1165 //--------------------------------------------------------------------------------------
1167 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1169 FeaturePtr aBase = feature();
1170 const std::string& aDocName = theDumper.name(aBase->document());
1172 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1173 if (anExternal->value()) {
1174 theDumper << aBase << " = model.addSketch(" << aDocName <<
1175 ", " << anExternal << ")" << std::endl;
1177 // Sketch is base on a plane.
1178 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1179 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1180 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1181 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1182 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1183 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1185 // Check the plane is coordinate plane
1186 std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1187 if(anExternal->context()) { // checking for selected planes
1188 if (!aPlaneName.empty()
1189 && anExternal->context()->data()
1190 && anExternal->context()->data()->name() == aPlaneName) {
1191 // dump sketch based on coordinate plane
1192 theDumper << aBase << " = model.addSketch(" << aDocName
1193 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1194 } else { // some other plane
1195 theDumper << aBase << " = model.addSketch(" << aDocName <<
1196 ", " << anExternal<< ")" << std::endl;
1199 if (aPlaneName.empty()) {
1200 // needs import additional module
1201 theDumper.importModule("GeomAPI");
1202 // dump plane parameters
1203 const std::string& aSketchName = theDumper.name(aBase);
1204 std::string anOriginName = aSketchName + "_origin";
1205 std::string aNormalName = aSketchName + "_norm";
1206 std::string aDirXName = aSketchName + "_dirx";
1207 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1208 // and then dumplicate dumping it in the next line
1209 theDumper << anOriginName << " = " << anOrigin << "\n"
1210 << aNormalName << " = " << aNormal << "\n"
1211 << aDirXName << " = " << aDirX << "\n";
1212 // dump sketch based on arbitrary plane
1213 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1214 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1216 // dump sketch based on coordinate plane
1217 theDumper << aBase << " = model.addSketch(" << aDocName
1218 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1223 // dump sketch's subfeatures
1224 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1225 theDumper.processSubs(aCompFeat, true);
1227 // if face order differs to the order generated by SketchBuilder,
1228 // dump the list of faces for correct execution of the script
1229 if (isCustomFacesOrder(aCompFeat)) {
1230 std::list<std::list<ResultPtr> > aFaces;
1231 edgesOfSketchFaces(aCompFeat, aFaces);
1233 const std::string& aSketchName = theDumper.name(aBase);
1234 std::string aMethodName(".changeFacesOrder");
1235 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1237 theDumper << aSketchName << aMethodName << "([";
1238 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1239 aFIt != aFaces.end(); ++aFIt) {
1240 if (aFIt != aFaces.begin())
1241 theDumper << ",\n" << aSpaceShift << " ";
1244 theDumper << "\n" << aSpaceShift << " ])" << std::endl;