1 // Copyright (C) 2014-2020 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_CurveFitting.h>
40 #include <SketchPlugin_Trim.h>
41 #include <SketchPlugin_Split.h>
42 #include <SketchPlugin_ConstraintTangent.h>
43 #include <SketchPlugin_ConstraintVertical.h>
44 #include <SketchPlugin_MacroBSpline.h>
45 #include <SketchPlugin_SketchCopy.h>
46 #include <SketchPlugin_Offset.h>
47 #include <SketcherPrs_Tools.h>
48 //--------------------------------------------------------------------------------------
49 #include <ModelAPI_Events.h>
50 #include <ModelAPI_CompositeFeature.h>
51 #include <ModelAPI_ResultConstruction.h>
52 #include <ModelHighAPI_Double.h>
53 #include <ModelHighAPI_Dumper.h>
54 #include <ModelHighAPI_RefAttr.h>
55 #include <ModelHighAPI_Selection.h>
56 #include <ModelHighAPI_Services.h>
57 #include <ModelHighAPI_Tools.h>
58 //--------------------------------------------------------------------------------------
59 #include "SketchAPI_Arc.h"
60 #include "SketchAPI_BSpline.h"
61 #include "SketchAPI_Circle.h"
62 #include "SketchAPI_Ellipse.h"
63 #include "SketchAPI_EllipticArc.h"
64 #include "SketchAPI_IntersectionPoint.h"
65 #include "SketchAPI_Line.h"
66 #include "SketchAPI_MacroArc.h"
67 #include "SketchAPI_MacroCircle.h"
68 #include "SketchAPI_MacroEllipse.h"
69 #include "SketchAPI_MacroEllipticArc.h"
70 #include "SketchAPI_Mirror.h"
71 #include "SketchAPI_Offset.h"
72 #include "SketchAPI_Point.h"
73 #include "SketchAPI_Projection.h"
74 #include "SketchAPI_Rectangle.h"
75 #include "SketchAPI_Rotation.h"
76 #include "SketchAPI_Translation.h"
77 //--------------------------------------------------------------------------------------
78 #include <GeomAPI_Curve.h>
79 #include <GeomAPI_Dir2d.h>
80 #include <GeomAPI_PlanarEdges.h>
81 #include <GeomAPI_ShapeExplorer.h>
82 #include <GeomAPI_XY.h>
83 #include <GeomAlgoAPI_SketchBuilder.h>
87 //--------------------------------------------------------------------------------------
88 SketchAPI_Sketch::SketchAPI_Sketch(
89 const std::shared_ptr<ModelAPI_Feature> & theFeature)
90 : ModelHighAPI_Interface(theFeature)
95 SketchAPI_Sketch::SketchAPI_Sketch(
96 const std::shared_ptr<ModelAPI_Feature> & theFeature,
97 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
98 : ModelHighAPI_Interface(theFeature)
105 SketchAPI_Sketch::SketchAPI_Sketch(
106 const std::shared_ptr<ModelAPI_Feature> & theFeature,
107 const ModelHighAPI_Selection & theExternal)
108 : ModelHighAPI_Interface(theFeature)
111 setExternal(theExternal);
115 SketchAPI_Sketch::SketchAPI_Sketch(
116 const std::shared_ptr<ModelAPI_Feature> & theFeature,
117 std::shared_ptr<ModelAPI_Object> thePlaneObject)
118 : ModelHighAPI_Interface(theFeature)
121 setExternal(thePlaneObject);
125 SketchAPI_Sketch::~SketchAPI_Sketch()
130 //--------------------------------------------------------------------------------------
131 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
133 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
136 //--------------------------------------------------------------------------------------
137 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
139 fillAttribute(thePlane->origin(), myorigin);
140 fillAttribute(thePlane->dirX(), mydirX);
141 fillAttribute(thePlane->normal(), mynormal);
146 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
147 bool theRemoveExternalDependency)
149 FeaturePtr aSketch = feature();
151 DocumentPtr aDoc = aSketch->document();
152 bool useVisible = false;
153 FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
154 aDoc->setCurrentFeature(aSketch, useVisible);
156 if (theRemoveExternalDependency)
157 aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
159 setExternal(thePlane);
161 aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
164 //--------------------------------------------------------------------------------------
165 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
167 fillAttribute(theExternal, myexternal);
172 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
174 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
175 ModelHighAPI_Selection aSel(aRes);
179 //--------------------------------------------------------------------------------------
180 void SketchAPI_Sketch::setValue(
181 const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
182 const ModelHighAPI_Double & theValue)
184 fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
186 // theConstraint->execute();
189 //--------------------------------------------------------------------------------------
190 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
192 const_cast<SketchAPI_Sketch*>(this)->execute();
194 std::list<ModelHighAPI_Selection> aSelectionList;
196 ResultConstructionPtr aResultConstruction =
197 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
198 if (aResultConstruction.get() == NULL)
199 return aSelectionList;
201 for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
202 aSelectionList.push_back(
203 ModelHighAPI_Selection(aResultConstruction,
204 aResultConstruction->face(anIndex)));
207 return aSelectionList;
210 //--------------------------------------------------------------------------------------
211 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
212 const std::shared_ptr<GeomAPI_Ax3> & thePlane)
214 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
215 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
218 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
219 const ModelHighAPI_Selection & theExternal)
221 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
222 return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
225 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
226 const std::wstring & theExternalName)
228 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
230 new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
233 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
234 std::shared_ptr<ModelAPI_Object> thePlaneObject)
236 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
237 return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
240 //--------------------------------------------------------------------------------------
241 SketchPtr copySketch(const std::shared_ptr<ModelAPI_Document> & thePart,
242 const SketchPtr & theSketch)
244 FeaturePtr aCopyer = thePart->addFeature(SketchPlugin_SketchCopy::ID());
245 aCopyer->reference(SketchPlugin_SketchCopy::BASE_ID())->setValue(theSketch->feature());
248 FeaturePtr aNewSketch = thePart->nextFeature(aCopyer);
250 // perform removing the macro-feature
251 thePart->removeFeature(aCopyer);
254 return SketchPtr(new SketchAPI_Sketch(aNewSketch));
258 //--------------------------------------------------------------------------------------
259 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
261 std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
262 std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
263 for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
264 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
265 PointPtr aPoint(new SketchAPI_Point(aFeature));
266 aFreePoints.push_back(aPoint);
271 //--------------------------------------------------------------------------------------
272 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
274 GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
275 if (aCurve->isTrimmed())
276 aCurve = aCurve->basisCurve();
280 void SketchAPI_Sketch::changeFacesOrder(
281 const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
283 // collect faces of the sketch
284 ResultConstructionPtr aSketchResult =
285 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
286 if (!aSketchResult) {
287 // sketch is nested to a boolean operation, thus, it has no result yet.
288 feature()->execute();
290 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
292 std::list<GeomFacePtr> aFaces;
293 int aFacesNum = aSketchResult->facesNum();
294 for (int i = 0; i < aFacesNum; ++i)
295 aFaces.push_back(aSketchResult->face(i));
296 // find new faces order according to the given lists of edges
297 std::list<GeomFacePtr> aNewFacesOrder;
298 std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
299 for (; anIt != theFaces.end(); ++anIt) {
300 // find the appropriate face
301 std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
302 for (; aFIt != aFaces.end(); ++aFIt) {
303 std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
304 GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
305 for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
306 ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
309 GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
310 GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
311 if (!aCurve1->isEqual(aCurve2))
315 if (aEdgeIt == anIt->end() && !aFExp.more()) {
317 aNewFacesOrder.push_back(*aFIt);
323 // place the rest faces at the end of new faces list
325 aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
326 // update the result of the sketch with the new order of faces
327 aSketchResult->setFacesOrder(aNewFacesOrder);
330 //--------------------------------------------------------------------------------------
331 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
332 double theX, double theY)
334 std::shared_ptr<ModelAPI_Feature> aFeature =
335 compositeFeature()->addFeature(SketchPlugin_Point::ID());
336 return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
338 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
339 const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
341 std::shared_ptr<ModelAPI_Feature> aFeature =
342 compositeFeature()->addFeature(SketchPlugin_Point::ID());
343 return PointPtr(new SketchAPI_Point(aFeature, thePoint));
345 std::shared_ptr<SketchAPI_Point>
346 SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
348 std::shared_ptr<ModelAPI_Feature> aFeature =
349 compositeFeature()->addFeature(SketchPlugin_Point::ID());
350 return PointPtr(new SketchAPI_Point(aFeature, theExternal));
352 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::wstring & theExternalName)
354 std::shared_ptr<ModelAPI_Feature> aFeature =
355 compositeFeature()->addFeature(SketchPlugin_Point::ID());
356 return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
359 //--------------------------------------------------------------------------------------
360 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
361 const ModelHighAPI_Selection & theExternal,
364 std::shared_ptr<ModelAPI_Feature> aFeature =
365 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
366 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
367 anIntersection->setIncludeToResult(theKeepResult);
368 return anIntersection;
370 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
371 const std::wstring & theExternalName,
374 std::shared_ptr<ModelAPI_Feature> aFeature =
375 compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
376 IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
377 anIntersection->setIncludeToResult(theKeepResult);
378 return anIntersection;
381 //--------------------------------------------------------------------------------------
382 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
383 double theX2, double theY2)
385 std::shared_ptr<ModelAPI_Feature> aFeature =
386 compositeFeature()->addFeature(SketchPlugin_Line::ID());
387 return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
389 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
390 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
391 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
393 std::shared_ptr<ModelAPI_Feature> aFeature =
394 compositeFeature()->addFeature(SketchPlugin_Line::ID());
395 return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
397 std::shared_ptr<SketchAPI_Line>
398 SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
400 std::shared_ptr<ModelAPI_Feature> aFeature =
401 compositeFeature()->addFeature(SketchPlugin_Line::ID());
402 return LinePtr(new SketchAPI_Line(aFeature, theExternal));
404 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::wstring & theExternalName)
406 std::shared_ptr<ModelAPI_Feature> aFeature =
407 compositeFeature()->addFeature(SketchPlugin_Line::ID());
408 return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
411 //--------------------------------------------------------------------------------------
412 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
413 double theX2, double theY2)
415 std::shared_ptr<ModelAPI_Feature> aFeature =
416 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
417 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
419 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
420 const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
421 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
423 std::shared_ptr<ModelAPI_Feature> aFeature =
424 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
425 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
428 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
429 double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter)
431 std::shared_ptr<ModelAPI_Feature> aFeature =
432 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
433 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2, isFirstPointCenter));
435 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
436 const std::shared_ptr<GeomAPI_Pnt2d> & theFirstPoint,
437 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint, bool isFirstPointCenter)
439 std::shared_ptr<ModelAPI_Feature> aFeature =
440 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
441 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theFirstPoint, theEndPoint, isFirstPointCenter));
444 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangleCentered(
445 double theX1, double theY1, double theX2, double theY2)
447 std::shared_ptr<ModelAPI_Feature> aFeature =
448 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
449 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2, true));
451 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangleCentered(
452 const std::shared_ptr<GeomAPI_Pnt2d> & theFirstPoint,
453 const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
455 std::shared_ptr<ModelAPI_Feature> aFeature =
456 compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
457 return RectanglePtr(new SketchAPI_Rectangle(aFeature, theFirstPoint, theEndPoint, true));
460 //--------------------------------------------------------------------------------------
461 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
465 std::shared_ptr<ModelAPI_Feature> aFeature =
466 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
467 return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
470 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
471 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
474 std::shared_ptr<ModelAPI_Feature> aFeature =
475 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
476 return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
479 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
484 std::shared_ptr<ModelAPI_Feature> aFeature =
485 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
486 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
487 thePassedX, thePassedY));
490 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
491 const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
492 const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
494 std::shared_ptr<ModelAPI_Feature> aFeature =
495 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
496 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
499 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
500 double theX2, double theY2,
501 double theX3, double theY3)
503 std::shared_ptr<ModelAPI_Feature> aFeature =
504 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
505 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
510 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
511 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
512 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
513 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
515 std::shared_ptr<ModelAPI_Feature> aFeature =
516 compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
517 return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
520 std::shared_ptr<SketchAPI_Circle>
521 SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
523 std::shared_ptr<ModelAPI_Feature> aFeature =
524 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
525 return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
528 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::wstring & theExternalName)
530 std::shared_ptr<ModelAPI_Feature> aFeature =
531 compositeFeature()->addFeature(SketchPlugin_Circle::ID());
532 return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
535 //--------------------------------------------------------------------------------------
536 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
537 double theStartX, double theStartY,
538 double theEndX, double theEndY,
541 std::shared_ptr<ModelAPI_Feature> aFeature =
542 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
543 return ArcPtr(new SketchAPI_Arc(aFeature,
544 theCenterX, theCenterY,
545 theStartX, theStartY,
550 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
551 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
552 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
553 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
556 std::shared_ptr<ModelAPI_Feature> aFeature =
557 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
558 return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
561 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
562 double theEndX, double theEndY,
563 double thePassedX, double thePassedY)
565 std::shared_ptr<ModelAPI_Feature> aFeature =
566 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
567 return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
568 theStartX, theStartY,
570 thePassedX, thePassedY));
573 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
574 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
575 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
576 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
578 std::shared_ptr<ModelAPI_Feature> aFeature =
579 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
580 return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
583 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
584 const ModelHighAPI_RefAttr& theTangentPoint,
585 double theEndX, double theEndY,
589 std::shared_ptr<ModelAPI_Feature> aFeature =
590 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
591 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
593 aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
595 aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
599 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
600 const ModelHighAPI_RefAttr& theTangentPoint,
601 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
605 std::shared_ptr<ModelAPI_Feature> aFeature =
606 compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
607 MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
609 aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
611 aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
615 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
617 std::shared_ptr<ModelAPI_Feature> aFeature =
618 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
619 return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
622 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::wstring & theExternalName)
624 std::shared_ptr<ModelAPI_Feature> aFeature =
625 compositeFeature()->addFeature(SketchPlugin_Arc::ID());
626 return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
629 //--------------------------------------------------------------------------------------
630 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
631 double theCenterX, double theCenterY,
632 double theFocusX, double theFocusY,
633 double theMinorRadius)
635 std::shared_ptr<ModelAPI_Feature> aFeature =
636 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
637 return EllipsePtr(new SketchAPI_Ellipse(aFeature,
638 theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
641 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
642 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
643 const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
644 double theMinorRadius)
646 std::shared_ptr<ModelAPI_Feature> aFeature =
647 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
648 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
651 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
652 double thePoint1X, double thePoint1Y,
653 double thePoint2X, double thePoint2Y,
654 double thePassedX, double thePassedY,
657 std::shared_ptr<ModelAPI_Feature> aFeature =
658 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
659 return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
660 thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
663 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
664 const PointOrReference& thePoint1,
665 const PointOrReference& thePoint2,
666 const PointOrReference& thePassedPoint,
669 std::shared_ptr<ModelAPI_Feature> aFeature =
670 compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
671 MacroEllipsePtr anEllipse;
672 if (thePoint1.second.isEmpty() &&
673 thePoint2.second.isEmpty() &&
674 thePassedPoint.second.isEmpty()) {
675 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
676 thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
679 anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
680 thePoint1.first, thePoint1.second,
681 thePoint2.first, thePoint2.second,
682 thePassedPoint.first, thePassedPoint.second,
688 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
689 const ModelHighAPI_Selection & theExternal)
691 std::shared_ptr<ModelAPI_Feature> aFeature =
692 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
693 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
696 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
697 const std::wstring & theExternalName)
699 std::shared_ptr<ModelAPI_Feature> aFeature =
700 compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
701 return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
704 //--------------------------------------------------------------------------------------
705 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
706 double theCenterX, double theCenterY,
707 double theFocusX, double theFocusY,
708 double theStartX, double theStartY,
709 double theEndX, double theEndY,
712 std::shared_ptr<ModelAPI_Feature> aFeature =
713 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
714 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
715 theCenterX, theCenterY,
716 theFocusX, theFocusY,
717 theStartX, theStartY,
722 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
723 const PointOrReference& theCenter,
724 const PointOrReference& theMajorAxisPoint,
725 const PointOrReference& theStartPoint,
726 const PointOrReference& theEndPoint,
729 std::shared_ptr<ModelAPI_Feature> aFeature =
730 compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
731 return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
732 theCenter.first, theCenter.second,
733 theMajorAxisPoint.first, theMajorAxisPoint.second,
734 theStartPoint.first, theStartPoint.second,
735 theEndPoint.first, theEndPoint.second,
739 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
740 const ModelHighAPI_Selection & theExternal)
742 std::shared_ptr<ModelAPI_Feature> aFeature =
743 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
744 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
747 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
748 const std::wstring & theExternalName)
750 std::shared_ptr<ModelAPI_Feature> aFeature =
751 compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
752 return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
755 //--------------------------------------------------------------------------------------
757 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
758 const ModelHighAPI_Selection & external,
760 const std::list<PointOrReference>& poles,
761 const std::list<ModelHighAPI_Double>& weights,
762 const std::list<ModelHighAPI_Double>& knots,
763 const std::list<ModelHighAPI_Integer>& multiplicities,
766 // split poles and references to other shapes
767 bool hasReference = false;
768 std::list<GeomPnt2dPtr> aPoints;
769 std::list<ModelHighAPI_RefAttr> aReferences;
770 for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
771 aPoints.push_back(it->first);
772 aReferences.push_back(it->second);
773 if (!it->second.isEmpty())
778 CompositeFeaturePtr aSketch = compositeFeature();
780 // use macro-feature to create coincidences to referred features
781 FeaturePtr aMacroFeature = aSketch->addFeature(
782 periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
783 AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
784 aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
785 AttributeDoubleArrayPtr aWeightsAttr =
786 aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
787 AttributeRefAttrListPtr aPolesRefAttr =
788 aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
789 // always generate a control polygon to apply coincidences correctly
790 aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
791 // initialize B-spline attributes
792 fillAttribute(aPoints, aPolesAttr);
794 fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
796 fillAttribute(weights, aWeightsAttr);
797 fillAttribute(aReferences, aPolesRefAttr);
798 apply(); // to kill macro-feature
800 // find created B-spline feature
801 const std::string& aKindToFind =
802 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
803 int aNbSubs = aSketch->numberOfSubs();
804 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
805 FeaturePtr aFeature = aSketch->subFeature(anIndex);
806 if (aFeature->getKind() == aKindToFind) {
807 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
808 : new SketchAPI_BSpline(aFeature));
815 // compute B-spline by parameters
816 FeaturePtr aFeature = aSketch->addFeature(
817 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
819 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
820 : new SketchAPI_BSpline(aFeature));
821 if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
822 aBSpline->setByExternal(external);
823 else if (knots.empty() || multiplicities.empty())
824 aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
826 aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
831 //--------------------------------------------------------------------------------------
832 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
833 const CompositeFeaturePtr& theSketch,
834 const FeaturePtr& theCurveFittingFeature,
835 const std::list<ModelHighAPI_RefAttr>& points,
839 AttributeBooleanPtr aPeriodicAttr =
840 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
841 fillAttribute(periodic, aPeriodicAttr);
842 AttributeBooleanPtr aClosedAttr =
843 theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
844 fillAttribute(closed, aClosedAttr);
845 AttributeRefAttrListPtr aPointsAttr =
846 theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
847 fillAttribute(points, aPointsAttr);
848 apply(); // to execute and kill the macro-feature
850 // find created B-spline feature
852 const std::string& aKindToFind =
853 periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
854 int aNbSubs = theSketch->numberOfSubs();
855 for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
856 FeaturePtr aFeature = theSketch->subFeature(anIndex);
857 if (aFeature->getKind() == aKindToFind) {
858 aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
859 : new SketchAPI_BSpline(aFeature));
867 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
868 const std::list<ModelHighAPI_RefAttr>& points,
872 CompositeFeaturePtr aSketch = compositeFeature();
873 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
874 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
875 ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
876 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
879 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
880 const std::list<ModelHighAPI_RefAttr>& points,
881 const ModelHighAPI_Double& precision,
885 CompositeFeaturePtr aSketch = compositeFeature();
886 FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
887 anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
888 ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
889 fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
890 return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
893 //--------------------------------------------------------------------------------------
894 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
895 const ModelHighAPI_Selection & theExternalFeature,
897 bool keepRefToOriginal)
899 std::shared_ptr<ModelAPI_Feature> aFeature =
900 compositeFeature()->addFeature(SketchPlugin_Projection::ID());
901 ProjectionPtr aProjection(new SketchAPI_Projection(aFeature));
902 aProjection->setIncludeToResult(keepResult);
903 aProjection->setKeepReferenceToOriginal(keepRefToOriginal);
904 aProjection->setExternalFeature(theExternalFeature);
908 //--------------------------------------------------------------------------------------
909 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
910 const ModelHighAPI_RefAttr & theMirrorLine,
911 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
913 std::shared_ptr<ModelAPI_Feature> aFeature =
914 compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
915 return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
918 //--------------------------------------------------------------------------------------
919 std::shared_ptr<SketchAPI_Offset> SketchAPI_Sketch::addOffset(
920 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
921 const ModelHighAPI_Double & theValue,
922 const bool theReversed)
924 std::shared_ptr<ModelAPI_Feature> aFeature =
925 compositeFeature()->addFeature(SketchPlugin_Offset::ID());
926 return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed));
929 //--------------------------------------------------------------------------------------
930 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
931 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
932 const ModelHighAPI_RefAttr & thePoint1,
933 const ModelHighAPI_RefAttr & thePoint2,
934 const ModelHighAPI_Integer & theNumberOfObjects,
937 std::shared_ptr<ModelAPI_Feature> aFeature =
938 compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
939 return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
940 thePoint2, theNumberOfObjects, theFullValue));
943 //--------------------------------------------------------------------------------------
944 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
945 const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
946 const ModelHighAPI_RefAttr & theCenter,
947 const ModelHighAPI_Double & theAngle,
948 const ModelHighAPI_Integer & theNumberOfObjects,
952 std::shared_ptr<ModelAPI_Feature> aFeature =
953 compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
955 new SketchAPI_Rotation(aFeature, theObjects, theCenter,
956 theAngle, theNumberOfObjects, theFullValue, theReversed));
959 //--------------------------------------------------------------------------------------
960 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
961 const ModelHighAPI_Reference& theFeature,
962 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
964 std::shared_ptr<ModelAPI_Feature> aFeature =
965 compositeFeature()->addFeature(SketchPlugin_Split::ID());
966 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
968 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
969 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
970 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
971 fillAttribute(thePositionPoint, aPointAttr);
974 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
977 //--------------------------------------------------------------------------------------
978 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
979 const ModelHighAPI_Reference& theFeature,
980 const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
982 std::shared_ptr<ModelAPI_Feature> aFeature =
983 compositeFeature()->addFeature(SketchPlugin_Trim::ID());
984 fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
986 AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
987 if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
988 AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
989 fillAttribute(thePositionPoint, aPointAttr);
992 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
995 //--------------------------------------------------------------------------------------
996 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
997 const ModelHighAPI_RefAttr & theLine1,
998 const ModelHighAPI_RefAttr & theLine2,
999 const ModelHighAPI_Double & theValue,
1000 const std::string& theType)
1002 std::shared_ptr<ModelAPI_Feature> aFeature =
1003 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1005 const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
1006 : SketchPlugin_ConstraintAngle::THE_VERSION_1;
1007 fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1009 int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
1010 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
1011 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1013 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
1014 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1016 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1017 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1019 if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
1020 std::string aTypeLC = theType;
1021 std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
1022 [](char c) { return static_cast<char>(::tolower(c)); });
1023 if (aTypeLC == "supplementary")
1024 aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
1025 else if (aTypeLC == "backward")
1026 aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
1028 fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1029 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1032 aFeature->execute();
1033 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1036 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1037 const ModelHighAPI_RefAttr & theLine1,
1038 const ModelHighAPI_RefAttr & theLine2,
1039 const ModelHighAPI_Double & theValue)
1041 std::shared_ptr<ModelAPI_Feature> aFeature =
1042 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1043 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1044 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1045 fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1046 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1047 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1048 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1049 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1050 aFeature->execute();
1051 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1054 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1055 const ModelHighAPI_RefAttr & theLine1,
1056 const ModelHighAPI_RefAttr & theLine2,
1057 const ModelHighAPI_Double & theValue)
1059 std::shared_ptr<ModelAPI_Feature> aFeature =
1060 compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1061 fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1062 aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1063 fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1064 aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1065 fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1066 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1067 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1068 aFeature->execute();
1069 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1072 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1073 const ModelHighAPI_RefAttr & thePoint1,
1074 const ModelHighAPI_RefAttr & thePoint2)
1076 std::shared_ptr<ModelAPI_Feature> aFeature =
1077 compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1078 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1079 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1080 aFeature->execute();
1081 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1084 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1085 const ModelHighAPI_RefAttr & theLine1,
1086 const ModelHighAPI_RefAttr & theLine2)
1088 std::shared_ptr<ModelAPI_Feature> aFeature =
1089 compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1090 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1091 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1092 aFeature->execute();
1093 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1096 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1097 const ModelHighAPI_RefAttr & thePoint,
1098 const ModelHighAPI_RefAttr & thePointOrLine,
1099 const ModelHighAPI_Double & theValue,
1102 std::shared_ptr<ModelAPI_Feature> aFeature =
1103 compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1104 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1105 fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1106 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1107 fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1108 aFeature->execute();
1109 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1112 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1113 const ModelHighAPI_RefAttr & thePoint,
1114 const ModelHighAPI_RefAttr & thePointOrLine,
1115 const ModelHighAPI_Double & theValue)
1117 return setDistance(thePoint, thePointOrLine, theValue, true);
1120 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1121 const ModelHighAPI_RefAttr & thePoint,
1122 const ModelHighAPI_RefAttr & thePointOrLine,
1123 const ModelHighAPI_Double & theValue)
1125 return setDistance(thePoint, thePointOrLine, theValue, false);
1128 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1129 const ModelHighAPI_RefAttr & thePoint1,
1130 const ModelHighAPI_RefAttr & thePoint2,
1131 const ModelHighAPI_Double & theValue)
1133 std::shared_ptr<ModelAPI_Feature> aFeature =
1134 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1135 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1136 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1137 fillAttribute(theValue,
1138 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1139 aFeature->execute();
1140 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1143 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1144 const ModelHighAPI_RefAttr & thePoint1,
1145 const ModelHighAPI_RefAttr & thePoint2,
1146 const ModelHighAPI_Double & theValue)
1148 std::shared_ptr<ModelAPI_Feature> aFeature =
1149 compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1150 fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1151 fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1152 fillAttribute(theValue,
1153 aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1154 aFeature->execute();
1155 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1158 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1159 const ModelHighAPI_RefAttr & theObject1,
1160 const ModelHighAPI_RefAttr & theObject2)
1162 std::shared_ptr<ModelAPI_Feature> aFeature =
1163 compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1164 fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1165 fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1166 aFeature->execute();
1167 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1170 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1171 const ModelHighAPI_RefAttr & thePoint)
1173 std::shared_ptr<ModelAPI_Feature> aFeature =
1174 compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1175 fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1176 apply(); // finish operation to remove Fillet feature correcly
1177 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1180 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1181 const ModelHighAPI_RefAttr & thePoint,
1182 const ModelHighAPI_Double & theRadius)
1184 CompositeFeaturePtr aSketch = compositeFeature();
1185 int aNbSubs = aSketch->numberOfSubs();
1188 InterfacePtr aFilletFeature = setFillet(thePoint);
1190 // set radius for just created arc
1191 FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1192 if (anArc->getKind() == SketchPlugin_Arc::ID())
1193 setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1195 return aFilletFeature;
1198 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1199 const ModelHighAPI_RefAttr & theObject)
1201 std::shared_ptr<ModelAPI_Feature> aFeature =
1202 compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1203 fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1204 aFeature->execute();
1205 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1208 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1209 const ModelHighAPI_RefAttr & theLine)
1211 std::shared_ptr<ModelAPI_Feature> aFeature =
1212 compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1213 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1214 aFeature->execute();
1215 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1218 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1219 const ModelHighAPI_RefAttr & theLine,
1220 const ModelHighAPI_Double & theValue)
1222 std::shared_ptr<ModelAPI_Feature> aFeature =
1223 compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1224 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1225 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1226 aFeature->execute();
1227 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1230 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1231 const ModelHighAPI_RefAttr & thePoint,
1232 const ModelHighAPI_RefAttr & theLine)
1234 std::shared_ptr<ModelAPI_Feature> aFeature =
1235 compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1236 fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1237 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1238 aFeature->execute();
1239 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1242 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1243 const ModelHighAPI_RefAttr & theLine1,
1244 const ModelHighAPI_RefAttr & theLine2)
1246 std::shared_ptr<ModelAPI_Feature> aFeature =
1247 compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1248 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1249 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1250 aFeature->execute();
1251 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1254 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1255 const ModelHighAPI_RefAttr & theLine1,
1256 const ModelHighAPI_RefAttr & theLine2)
1258 std::shared_ptr<ModelAPI_Feature> aFeature =
1259 compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1260 fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1261 fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1262 aFeature->execute();
1263 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1266 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1267 const ModelHighAPI_RefAttr & theCircleOrArc,
1268 const ModelHighAPI_Double & theValue)
1270 std::shared_ptr<ModelAPI_Feature> aFeature =
1271 compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1272 fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1273 fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1274 aFeature->execute();
1275 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1278 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1279 const ModelHighAPI_RefAttr & theLine,
1280 const ModelHighAPI_RefAttr & theCircle)
1282 std::shared_ptr<ModelAPI_Feature> aFeature =
1283 compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1284 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1285 fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1286 aFeature->execute();
1287 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1290 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1291 const ModelHighAPI_RefAttr & theLine)
1293 std::shared_ptr<ModelAPI_Feature> aFeature =
1294 compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1295 fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1296 aFeature->execute();
1297 return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1300 //--------------------------------------------------------------------------------------
1302 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1304 AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1305 return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1308 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1310 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1311 theFeature->attribute(SketchPlugin_Line::START_ID()));
1312 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1313 theFeature->attribute(SketchPlugin_Line::END_ID()));
1315 if (!aStartAttr || !aEndAttr)
1316 return std::shared_ptr<GeomAPI_Pnt2d>();
1318 std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1319 std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1320 return std::shared_ptr<GeomAPI_Pnt2d>(
1321 new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1324 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1326 AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1327 theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1328 AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1330 if (!aCenter || !aRadius)
1331 return std::shared_ptr<GeomAPI_Pnt2d>();
1333 return std::shared_ptr<GeomAPI_Pnt2d>(
1334 new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1337 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1339 static const double PI = 3.141592653589793238463;
1341 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1342 theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1343 AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1344 theFeature->attribute(SketchPlugin_Arc::START_ID()));
1345 AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1346 theFeature->attribute(SketchPlugin_Arc::END_ID()));
1348 if (!aCenterAttr || !aStartAttr || !aEndAttr)
1349 return std::shared_ptr<GeomAPI_Pnt2d>();
1351 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1352 aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1353 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1354 aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1356 double anAngle = aStartDir->angle(aEndDir);
1357 bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1358 if (isReversed && anAngle > 0.)
1359 anAngle -= 2.0 * PI;
1360 else if (!isReversed && anAngle <= 0.)
1361 anAngle += 2.0 * PI;
1363 double cosA = cos(anAngle);
1364 double sinA = sin(anAngle);
1366 // rotate start dir to find middle point on arc
1367 double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1368 double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1369 double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1371 return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1374 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1375 bool isEllipse = true)
1377 const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1378 SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1379 AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1380 theFeature->attribute(anAttrName));
1381 return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1384 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1385 SketchAPI_Sketch* theSketch)
1387 GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1388 GeomPointPtr aMiddle = anEdge.middlePoint();
1389 return theSketch->to2D(aMiddle);
1392 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1393 SketchAPI_Sketch* theSketch)
1395 std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1396 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1398 // move only features of the following types
1399 const std::string& aFeatureKind = aFeature->getKind();
1400 if (aFeatureKind == SketchPlugin_Point::ID())
1401 aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1402 else if (aFeatureKind == SketchPlugin_Line::ID())
1403 aMiddlePoint = middlePointOnLine(aFeature);
1404 else if (aFeatureKind == SketchPlugin_Circle::ID())
1405 aMiddlePoint = pointOnCircle(aFeature);
1406 else if (aFeatureKind == SketchPlugin_Arc::ID())
1407 aMiddlePoint = middlePointOnArc(aFeature);
1408 else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1409 aMiddlePoint = pointOnEllipse(aFeature);
1410 else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1411 aMiddlePoint = pointOnEllipse(aFeature, false);
1412 else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1413 aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1414 aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1416 return aMiddlePoint;
1419 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1420 const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1422 std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1423 theMovedEntity.fillMessage(aMessage);
1425 std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1426 if (aMessage->movedAttribute())
1427 anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1429 anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1431 if (!anOriginalPosition)
1432 return; // something has gone wrong, do not process movement
1434 aMessage->setOriginalPosition(anOriginalPosition);
1435 aMessage->setCurrentPosition(theTargetPoint);
1436 Events_Loop::loop()->send(aMessage);
1439 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1440 double theTargetX, double theTargetY)
1442 std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1443 move(theMovedEntity, aTargetPoint);
1446 //--------------------------------------------------------------------------------------
1448 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1450 FeaturePtr aBase = feature();
1451 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1452 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1453 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1454 aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1455 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1456 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1457 std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1459 return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1462 //--------------------------------------------------------------------------------------
1464 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1466 // collect edges of the first face
1467 std::list<GeomShapePtr> anEdges1;
1468 for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1469 anEdges1.push_back(anExp.current());
1470 // compare edges of faces
1471 for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1472 GeomShapePtr aCurrent = anExp.current();
1473 bool isFound = false;
1474 std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1475 for (; anIt1 != anEdges1.end(); ++anIt1)
1476 if (aCurrent->isSameGeometry(*anIt1)) {
1478 anEdges1.erase(anIt1);
1484 return !anEdges1.empty();
1487 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1489 ResultConstructionPtr aSketchResult =
1490 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1494 std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1495 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1499 // collect faces constructed by SketchBuilder algorithm
1500 GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1501 aWires->norm(), aWires);
1502 const ListOfShape& aFaces = aSketchBuilder.faces();
1504 // compare faces stored in sketch with faces generated by SketchBuilder
1505 int aNbSketchFaces = aSketchResult->facesNum();
1507 for (ListOfShape::const_iterator aFIt = aFaces.begin();
1508 aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1509 ++aFIt, ++aFaceIndex) {
1510 GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1511 GeomFacePtr aCurFace = (*aFIt)->face();
1512 if (isDifferent(aSketchFace, aCurFace))
1518 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1519 std::list<std::list<ResultPtr> >& theEdges)
1521 ResultConstructionPtr aSketchResult =
1522 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1526 // collect curves of the sketch
1527 std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1528 int aSubNum = theSketch->numberOfSubs();
1529 for (int a = 0; a < aSubNum; ++a) {
1530 FeaturePtr aSub = theSketch->subFeature(a);
1531 const std::list<ResultPtr>& aResults = aSub->results();
1532 std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1533 for (; aRes != aResults.cend(); aRes++) {
1534 GeomShapePtr aCurShape = (*aRes)->shape();
1535 if (aCurShape && aCurShape->isEdge())
1536 aCurves[untrimmedCurve(aCurShape)] = *aRes;
1540 // convert each face to the list of results of its edges
1541 int aFacesNum = aSketchResult->facesNum();
1542 for (int a = 0; a < aFacesNum; ++a) {
1543 theEdges.push_back(std::list<ResultPtr>());
1544 std::list<ResultPtr>& aCurEdges = theEdges.back();
1546 GeomFacePtr aFace = aSketchResult->face(a);
1547 for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1548 anExp.more(); anExp.next()) {
1549 GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1550 aCurEdges.push_back(aCurves[aCurrent]);
1555 //--------------------------------------------------------------------------------------
1557 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1559 FeaturePtr aBase = feature();
1560 const std::string& aDocName = theDumper.name(aBase->document());
1562 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1563 if (anExternal->value()) {
1564 theDumper << aBase << " = model.addSketch(" << aDocName <<
1565 ", " << anExternal << ")" << std::endl;
1567 // Sketch is base on a plane.
1568 std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1569 aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1570 std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1571 aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1572 std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1573 aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1575 // Check the plane is coordinate plane
1576 std::wstring aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1577 if(anExternal->context()) { // checking for selected planes
1578 if (!aPlaneName.empty()
1579 && anExternal->context()->data()
1580 && anExternal->context()->data()->name() == aPlaneName) {
1581 // dump sketch based on coordinate plane
1582 theDumper << aBase << " = model.addSketch(" << aDocName
1583 << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1584 } else { // some other plane
1585 theDumper << aBase << " = model.addSketch(" << aDocName <<
1586 ", " << anExternal<< ")" << std::endl;
1589 if (aPlaneName.empty()) {
1590 // needs import additional module
1591 theDumper.importModule("GeomAPI");
1592 // dump plane parameters
1593 const std::string& aSketchName = theDumper.name(aBase);
1594 std::string anOriginName = aSketchName + "_origin";
1595 std::string aNormalName = aSketchName + "_norm";
1596 std::string aDirXName = aSketchName + "_dirx";
1597 // use "\n" instead of std::endl to avoid automatic dumping sketch here
1598 // and then dumplicate dumping it in the next line
1599 theDumper << anOriginName << " = " << anOrigin << "\n"
1600 << aNormalName << " = " << aNormal << "\n"
1601 << aDirXName << " = " << aDirX << "\n";
1602 // dump sketch based on arbitrary plane
1603 theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1604 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1606 // dump sketch based on coordinate plane
1607 theDumper << aBase << " = model.addSketch(" << aDocName
1608 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1613 // dump sketch's subfeatures
1614 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1615 theDumper.processSubs(aCompFeat, true);
1617 // if face order differs to the order generated by SketchBuilder,
1618 // dump the list of faces for correct execution of the script
1619 if (isCustomFacesOrder(aCompFeat)) {
1620 std::list<std::list<ResultPtr> > aFaces;
1621 edgesOfSketchFaces(aCompFeat, aFaces);
1623 /// remove faces that must not be dumped
1624 std::vector< std::list<std::list<ResultPtr>>::iterator> aFacesToRemove;
1625 for(auto itFaces = aFaces.begin(); itFaces != aFaces.end(); ++itFaces)
1627 auto & facesGroup = *itFaces;
1628 std::vector<std::list<ResultPtr>::iterator> subFacestoRemove;
1629 for(auto itGroup = facesGroup.begin(); itGroup != facesGroup.end(); ++itGroup)
1631 FeaturePtr aFeature = ModelAPI_Feature::feature(*itGroup);
1632 if(theDumper.isDumped(aFeature)){
1633 subFacestoRemove.push_back(itGroup);
1636 for(auto itGroup :subFacestoRemove){
1637 facesGroup.erase(itGroup);
1640 if(!facesGroup.size()){
1641 aFacesToRemove.push_back(itFaces);
1644 for(auto itFaces :aFacesToRemove){
1645 aFaces.erase(itFaces);
1651 const std::string& aSketchName = theDumper.name(aBase);
1652 std::string aMethodName(".changeFacesOrder");
1653 std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1655 theDumper << aSketchName << aMethodName << "([";
1656 for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1657 aFIt != aFaces.end(); ++aFIt) {
1658 if (aFIt != aFaces.begin())
1659 theDumper << ",\n" << aSpaceShift << " ";
1662 theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1663 // call model.do() for correct update of the document's labels related to the changed faces
1664 theDumper << "model.do()" << std::endl;