Salome HOME
Merge branch 'occ/shaper2smesh'
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
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 <SketchPlugin_MacroBSpline.h>
44 #include <SketcherPrs_Tools.h>
45 //--------------------------------------------------------------------------------------
46 #include <ModelAPI_Events.h>
47 #include <ModelAPI_CompositeFeature.h>
48 #include <ModelAPI_ResultConstruction.h>
49 #include <ModelHighAPI_Double.h>
50 #include <ModelHighAPI_Dumper.h>
51 #include <ModelHighAPI_RefAttr.h>
52 #include <ModelHighAPI_Selection.h>
53 #include <ModelHighAPI_Services.h>
54 #include <ModelHighAPI_Tools.h>
55 //--------------------------------------------------------------------------------------
56 #include "SketchAPI_Arc.h"
57 #include "SketchAPI_BSpline.h"
58 #include "SketchAPI_Circle.h"
59 #include "SketchAPI_Ellipse.h"
60 #include "SketchAPI_EllipticArc.h"
61 #include "SketchAPI_IntersectionPoint.h"
62 #include "SketchAPI_Line.h"
63 #include "SketchAPI_MacroArc.h"
64 #include "SketchAPI_MacroCircle.h"
65 #include "SketchAPI_MacroEllipse.h"
66 #include "SketchAPI_MacroEllipticArc.h"
67 #include "SketchAPI_Mirror.h"
68 #include "SketchAPI_Point.h"
69 #include "SketchAPI_Projection.h"
70 #include "SketchAPI_Rectangle.h"
71 #include "SketchAPI_Rotation.h"
72 #include "SketchAPI_Translation.h"
73 //--------------------------------------------------------------------------------------
74 #include <GeomAPI_Curve.h>
75 #include <GeomAPI_Dir2d.h>
76 #include <GeomAPI_PlanarEdges.h>
77 #include <GeomAPI_ShapeExplorer.h>
78 #include <GeomAPI_XY.h>
79 #include <GeomAlgoAPI_SketchBuilder.h>
80
81 #include <algorithm>
82 #include <cmath>
83 //--------------------------------------------------------------------------------------
84 SketchAPI_Sketch::SketchAPI_Sketch(
85     const std::shared_ptr<ModelAPI_Feature> & theFeature)
86 : ModelHighAPI_Interface(theFeature)
87 {
88   initialize();
89 }
90
91 SketchAPI_Sketch::SketchAPI_Sketch(
92     const std::shared_ptr<ModelAPI_Feature> & theFeature,
93     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
94 : ModelHighAPI_Interface(theFeature)
95 {
96   if (initialize()) {
97     setPlane(thePlane);
98   }
99 }
100
101 SketchAPI_Sketch::SketchAPI_Sketch(
102     const std::shared_ptr<ModelAPI_Feature> & theFeature,
103     const ModelHighAPI_Selection & theExternal)
104 : ModelHighAPI_Interface(theFeature)
105 {
106   if (initialize()) {
107     setExternal(theExternal);
108   }
109 }
110
111 SketchAPI_Sketch::SketchAPI_Sketch(
112     const std::shared_ptr<ModelAPI_Feature> & theFeature,
113     std::shared_ptr<ModelAPI_Object> thePlaneObject)
114 : ModelHighAPI_Interface(theFeature)
115 {
116   if (initialize()) {
117     setExternal(thePlaneObject);
118   }
119 }
120
121 SketchAPI_Sketch::~SketchAPI_Sketch()
122 {
123
124 }
125
126 //--------------------------------------------------------------------------------------
127 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
128 {
129   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
130 }
131
132 //--------------------------------------------------------------------------------------
133 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
134 {
135   fillAttribute(thePlane->origin(), myorigin);
136   fillAttribute(thePlane->dirX(), mydirX);
137   fillAttribute(thePlane->normal(), mynormal);
138
139   execute();
140 }
141
142 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
143                                 bool theRemoveExternalDependency)
144 {
145   FeaturePtr aSketch = feature();
146
147   DocumentPtr aDoc = aSketch->document();
148   bool useVisible = false;
149   FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
150   aDoc->setCurrentFeature(aSketch, useVisible);
151
152   if (theRemoveExternalDependency)
153     aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
154
155   setExternal(thePlane);
156
157   aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
158 }
159
160 //--------------------------------------------------------------------------------------
161 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
162 {
163   fillAttribute(theExternal, myexternal);
164
165   execute();
166 }
167
168 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
169 {
170   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
171   ModelHighAPI_Selection aSel(aRes);
172   setExternal(aSel);
173 }
174
175 //--------------------------------------------------------------------------------------
176 void SketchAPI_Sketch::setValue(
177     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
178     const ModelHighAPI_Double & theValue)
179 {
180   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
181
182 //  theConstraint->execute();
183 }
184
185 //--------------------------------------------------------------------------------------
186 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
187 {
188   const_cast<SketchAPI_Sketch*>(this)->execute();
189
190   std::list<ModelHighAPI_Selection> aSelectionList;
191
192   ResultConstructionPtr aResultConstruction =
193       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
194   if (aResultConstruction.get() == NULL)
195     return aSelectionList;
196
197   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
198     aSelectionList.push_back(
199         ModelHighAPI_Selection(aResultConstruction,
200                                aResultConstruction->face(anIndex)));
201   }
202
203   return aSelectionList;
204 }
205
206 //--------------------------------------------------------------------------------------
207 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
208                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
209 {
210   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
211   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
212 }
213
214 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
215                     const ModelHighAPI_Selection & theExternal)
216 {
217   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
218   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
219 }
220
221 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
222                     const std::string & theExternalName)
223 {
224   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
225   return SketchPtr(
226     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
227 }
228
229 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
230                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
231 {
232   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
233   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
234 }
235
236
237 //--------------------------------------------------------------------------------------
238 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
239 {
240   std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
241   std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
242   for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
243     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
244     PointPtr aPoint(new SketchAPI_Point(aFeature));
245     aFreePoints.push_back(aPoint);
246   }
247   return aFreePoints;
248 }
249
250 //--------------------------------------------------------------------------------------
251 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
252 {
253   GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
254   if (aCurve->isTrimmed())
255     aCurve = aCurve->basisCurve();
256   return aCurve;
257 }
258
259 void SketchAPI_Sketch::changeFacesOrder(
260     const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
261 {
262   // collect faces of the sketch
263   ResultConstructionPtr aSketchResult =
264       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
265   if (!aSketchResult) {
266     // sketch is nested to a boolean operation, thus, it has no result yet.
267     feature()->execute();
268     aSketchResult =
269         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
270   }
271   std::list<GeomFacePtr> aFaces;
272   int aFacesNum = aSketchResult->facesNum();
273   for (int i = 0; i < aFacesNum; ++i)
274     aFaces.push_back(aSketchResult->face(i));
275   // find new faces order according to the given lists of edges
276   std::list<GeomFacePtr> aNewFacesOrder;
277   std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
278   for (; anIt != theFaces.end(); ++anIt) {
279     // find the appropriate face
280     std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
281     for (; aFIt != aFaces.end(); ++aFIt) {
282       std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
283       GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
284       for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
285         ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
286         if (!aCurRes)
287           break;
288         GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
289         GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
290         if (!aCurve1->isEqual(aCurve2))
291           break;
292       }
293
294       if (aEdgeIt == anIt->end() && !aFExp.more()) {
295         // face is found
296         aNewFacesOrder.push_back(*aFIt);
297         aFaces.erase(aFIt);
298         break;
299       }
300     }
301   }
302   // place the rest faces at the end of new faces list
303   if (!aFaces.empty())
304     aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
305   // update the result of the sketch with the new order of faces
306   aSketchResult->setFacesOrder(aNewFacesOrder);
307 }
308
309 //--------------------------------------------------------------------------------------
310 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
311     double theX, double theY)
312 {
313   std::shared_ptr<ModelAPI_Feature> aFeature =
314     compositeFeature()->addFeature(SketchPlugin_Point::ID());
315   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
316 }
317 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
318     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
319 {
320   std::shared_ptr<ModelAPI_Feature> aFeature =
321     compositeFeature()->addFeature(SketchPlugin_Point::ID());
322   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
323 }
324 std::shared_ptr<SketchAPI_Point>
325   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
326 {
327   std::shared_ptr<ModelAPI_Feature> aFeature =
328     compositeFeature()->addFeature(SketchPlugin_Point::ID());
329   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
330 }
331 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
332 {
333   std::shared_ptr<ModelAPI_Feature> aFeature =
334     compositeFeature()->addFeature(SketchPlugin_Point::ID());
335   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
336 }
337
338 //--------------------------------------------------------------------------------------
339 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
340     const ModelHighAPI_Selection & theExternal,
341     bool theKeepResult)
342 {
343   std::shared_ptr<ModelAPI_Feature> aFeature =
344     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
345   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
346   anIntersection->setIncludeToResult(theKeepResult);
347   return anIntersection;
348 }
349 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
350     const std::string & theExternalName,
351     bool theKeepResult)
352 {
353   std::shared_ptr<ModelAPI_Feature> aFeature =
354     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
355   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
356   anIntersection->setIncludeToResult(theKeepResult);
357   return anIntersection;
358 }
359
360 //--------------------------------------------------------------------------------------
361 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
362                                                           double theX2, double theY2)
363 {
364   std::shared_ptr<ModelAPI_Feature> aFeature =
365     compositeFeature()->addFeature(SketchPlugin_Line::ID());
366   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
367 }
368 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
369     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
370     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
371 {
372   std::shared_ptr<ModelAPI_Feature> aFeature =
373     compositeFeature()->addFeature(SketchPlugin_Line::ID());
374   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
375 }
376 std::shared_ptr<SketchAPI_Line>
377   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
378 {
379   std::shared_ptr<ModelAPI_Feature> aFeature =
380     compositeFeature()->addFeature(SketchPlugin_Line::ID());
381   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
382 }
383 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
384 {
385   std::shared_ptr<ModelAPI_Feature> aFeature =
386     compositeFeature()->addFeature(SketchPlugin_Line::ID());
387   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
388 }
389
390 //--------------------------------------------------------------------------------------
391 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
392                                                                     double theX2, double theY2)
393 {
394   std::shared_ptr<ModelAPI_Feature> aFeature =
395     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
396   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
397 }
398 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
399     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
400     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
401 {
402   std::shared_ptr<ModelAPI_Feature> aFeature =
403     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
404   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
405 }
406
407 //--------------------------------------------------------------------------------------
408 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
409                                                               double theCenterY,
410                                                               double theRadius)
411 {
412   std::shared_ptr<ModelAPI_Feature> aFeature =
413     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
414   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
415 }
416
417 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
418                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
419                                     double theRadius)
420 {
421   std::shared_ptr<ModelAPI_Feature> aFeature =
422     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
423   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
424 }
425
426 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
427                                                                    double theCenterY,
428                                                                    double thePassedX,
429                                                                    double thePassedY)
430 {
431   std::shared_ptr<ModelAPI_Feature> aFeature =
432     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
433   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
434                                                             thePassedX, thePassedY));
435 }
436
437 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
438     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
439     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
440 {
441   std::shared_ptr<ModelAPI_Feature> aFeature =
442     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
443   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
444 }
445
446 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
447                                                                    double theX2, double theY2,
448                                                                    double theX3, double theY3)
449 {
450   std::shared_ptr<ModelAPI_Feature> aFeature =
451     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
452   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
453                                                             theX2, theY2,
454                                                             theX3, theY3));
455 }
456
457 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
458     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
459     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
460     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
461 {
462   std::shared_ptr<ModelAPI_Feature> aFeature =
463     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
464   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
465 }
466
467 std::shared_ptr<SketchAPI_Circle>
468   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
469 {
470   std::shared_ptr<ModelAPI_Feature> aFeature =
471     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
472   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
473 }
474
475 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
476 {
477   std::shared_ptr<ModelAPI_Feature> aFeature =
478     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
479   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
480 }
481
482 //--------------------------------------------------------------------------------------
483 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
484                                                         double theStartX, double theStartY,
485                                                         double theEndX, double theEndY,
486                                                         bool theInversed)
487 {
488   std::shared_ptr<ModelAPI_Feature> aFeature =
489     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
490   return ArcPtr(new SketchAPI_Arc(aFeature,
491                                   theCenterX, theCenterY,
492                                   theStartX, theStartY,
493                                   theEndX, theEndY,
494                                   theInversed));
495 }
496
497 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
498                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
499                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
500                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
501                                               bool theInversed)
502 {
503   std::shared_ptr<ModelAPI_Feature> aFeature =
504     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
505   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
506 }
507
508 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
509                                                         double theEndX, double theEndY,
510                                                         double thePassedX, double thePassedY)
511 {
512   std::shared_ptr<ModelAPI_Feature> aFeature =
513     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
514   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
515                                        theStartX, theStartY,
516                                        theEndX, theEndY,
517                                        thePassedX, thePassedY));
518 }
519
520 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
521                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
522                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
523                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
524 {
525   std::shared_ptr<ModelAPI_Feature> aFeature =
526     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
527   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
528 }
529
530 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
531                                                 const ModelHighAPI_RefAttr& theTangentPoint,
532                                                 double theEndX, double theEndY,
533                                                 bool theInversed,
534                                                 bool theTransversal)
535 {
536   std::shared_ptr<ModelAPI_Feature> aFeature =
537     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
538   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
539   if (theTransversal)
540     aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
541   else
542     aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
543   return aMacroArc;
544 }
545
546 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
547                                               const ModelHighAPI_RefAttr& theTangentPoint,
548                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
549                                               bool theInversed,
550                                               bool theTransversal)
551 {
552   std::shared_ptr<ModelAPI_Feature> aFeature =
553     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
554   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
555   if (theTransversal)
556     aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
557   else
558     aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
559   return aMacroArc;
560 }
561
562 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
563 {
564   std::shared_ptr<ModelAPI_Feature> aFeature =
565     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
566   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
567 }
568
569 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
570 {
571   std::shared_ptr<ModelAPI_Feature> aFeature =
572     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
573   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
574 }
575
576 //--------------------------------------------------------------------------------------
577 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
578     double theCenterX, double theCenterY,
579     double theFocusX, double theFocusY,
580     double theMinorRadius)
581 {
582   std::shared_ptr<ModelAPI_Feature> aFeature =
583       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
584   return EllipsePtr(new SketchAPI_Ellipse(aFeature,
585       theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
586 }
587
588 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
589     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
590     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
591     double theMinorRadius)
592 {
593   std::shared_ptr<ModelAPI_Feature> aFeature =
594       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
595   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
596 }
597
598 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
599     double thePoint1X, double thePoint1Y,
600     double thePoint2X, double thePoint2Y,
601     double thePassedX, double thePassedY,
602     bool isPoint1Center)
603 {
604   std::shared_ptr<ModelAPI_Feature> aFeature =
605       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
606   return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
607       thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
608 }
609
610 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
611     const PointOrReference& thePoint1,
612     const PointOrReference& thePoint2,
613     const PointOrReference& thePassedPoint,
614     bool isPoint1Center)
615 {
616   std::shared_ptr<ModelAPI_Feature> aFeature =
617       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
618   MacroEllipsePtr anEllipse;
619   if (thePoint1.second.isEmpty() &&
620       thePoint2.second.isEmpty() &&
621       thePassedPoint.second.isEmpty()) {
622     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
623         thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
624   }
625   else {
626     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
627         thePoint1.first, thePoint1.second,
628         thePoint2.first, thePoint2.second,
629         thePassedPoint.first, thePassedPoint.second,
630         isPoint1Center));
631   }
632   return anEllipse;
633 }
634
635 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
636     const ModelHighAPI_Selection & theExternal)
637 {
638   std::shared_ptr<ModelAPI_Feature> aFeature =
639       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
640   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
641 }
642
643 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
644     const std::string & theExternalName)
645 {
646   std::shared_ptr<ModelAPI_Feature> aFeature =
647       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
648   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
649 }
650
651 //--------------------------------------------------------------------------------------
652 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
653     double theCenterX, double theCenterY,
654     double theFocusX, double theFocusY,
655     double theStartX, double theStartY,
656     double theEndX, double theEndY,
657     bool theInversed)
658 {
659   std::shared_ptr<ModelAPI_Feature> aFeature =
660       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
661   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
662       theCenterX, theCenterY,
663       theFocusX, theFocusY,
664       theStartX, theStartY,
665       theEndX, theEndY,
666       theInversed));
667 }
668
669 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
670     const PointOrReference& theCenter,
671     const PointOrReference& theMajorAxisPoint,
672     const PointOrReference& theStartPoint,
673     const PointOrReference& theEndPoint,
674     bool theInversed)
675 {
676   std::shared_ptr<ModelAPI_Feature> aFeature =
677       compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
678   return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
679       theCenter.first, theCenter.second,
680       theMajorAxisPoint.first, theMajorAxisPoint.second,
681       theStartPoint.first, theStartPoint.second,
682       theEndPoint.first, theEndPoint.second,
683       theInversed));
684 }
685
686 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
687     const ModelHighAPI_Selection & theExternal)
688 {
689   std::shared_ptr<ModelAPI_Feature> aFeature =
690       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
691   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
692 }
693
694 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
695     const std::string & theExternalName)
696 {
697   std::shared_ptr<ModelAPI_Feature> aFeature =
698       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
699   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
700 }
701
702 //--------------------------------------------------------------------------------------
703
704 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
705     const ModelHighAPI_Selection & external,
706     const int degree,
707     const std::list<PointOrReference>& poles,
708     const std::list<ModelHighAPI_Double>& weights,
709     const std::list<ModelHighAPI_Double>& knots,
710     const std::list<ModelHighAPI_Integer>& multiplicities,
711     const bool periodic)
712 {
713   // split poles and references to other shapes
714   bool hasReference = false;
715   std::list<GeomPnt2dPtr> aPoints;
716   std::list<ModelHighAPI_RefAttr> aReferences;
717   for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
718     aPoints.push_back(it->first);
719     aReferences.push_back(it->second);
720     if (!it->second.isEmpty())
721       hasReference = true;
722   }
723
724   BSplinePtr aBSpline;
725   CompositeFeaturePtr aSketch = compositeFeature();
726   if (hasReference) {
727     // use macro-feature to create coincidences to referred features
728     FeaturePtr aMacroFeature = aSketch->addFeature(
729         periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
730     AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
731         aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
732     AttributeDoubleArrayPtr aWeightsAttr =
733         aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
734     AttributeRefAttrListPtr aPolesRefAttr =
735         aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
736     // always generate a control polygon to apply coincidences correctly
737     aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
738     // initialize B-spline attributes
739     fillAttribute(aPoints, aPolesAttr);
740     if (weights.empty())
741       fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
742     else
743       fillAttribute(weights, aWeightsAttr);
744     fillAttribute(aReferences, aPolesRefAttr);
745     apply(); // to kill macro-feature
746
747     // find created B-spline feature
748     const std::string& aKindToFind =
749         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
750     int aNbSubs = aSketch->numberOfSubs();
751     for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
752       FeaturePtr aFeature = aSketch->subFeature(anIndex);
753       if (aFeature->getKind() == aKindToFind) {
754         aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
755                                 : new SketchAPI_BSpline(aFeature));
756         aBSpline->execute();
757         break;
758       }
759     }
760   }
761   else {
762     // compute B-spline by parameters
763     FeaturePtr aFeature = aSketch->addFeature(
764         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
765
766     aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
767                             : new SketchAPI_BSpline(aFeature));
768     if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
769       aBSpline->setByExternal(external);
770     else if (knots.empty() || multiplicities.empty())
771       aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
772     else
773       aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
774   }
775   return aBSpline;
776 }
777
778 //--------------------------------------------------------------------------------------
779 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
780     const ModelHighAPI_Selection & theExternalFeature,
781     bool theKeepResult)
782 {
783   std::shared_ptr<ModelAPI_Feature> aFeature =
784     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
785   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
786   aProjection->setIncludeToResult(theKeepResult);
787   return aProjection;
788 }
789
790 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
791     const std::string & theExternalName,
792     bool theKeepResult)
793 {
794   std::shared_ptr<ModelAPI_Feature> aFeature =
795     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
796   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
797   aProjection->setIncludeToResult(theKeepResult);
798   return aProjection;
799 }
800
801 //--------------------------------------------------------------------------------------
802 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
803     const ModelHighAPI_RefAttr & theMirrorLine,
804     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
805 {
806   std::shared_ptr<ModelAPI_Feature> aFeature =
807     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
808   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
809 }
810
811 //--------------------------------------------------------------------------------------
812 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
813     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
814     const ModelHighAPI_RefAttr & thePoint1,
815     const ModelHighAPI_RefAttr & thePoint2,
816     const ModelHighAPI_Integer & theNumberOfObjects,
817     bool theFullValue)
818 {
819   std::shared_ptr<ModelAPI_Feature> aFeature =
820     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
821   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
822                                                   thePoint2, theNumberOfObjects, theFullValue));
823 }
824
825 //--------------------------------------------------------------------------------------
826 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
827     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
828     const ModelHighAPI_RefAttr & theCenter,
829     const ModelHighAPI_Double & theAngle,
830     const ModelHighAPI_Integer & theNumberOfObjects,
831     bool theFullValue,
832     bool theReversed)
833 {
834   std::shared_ptr<ModelAPI_Feature> aFeature =
835     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
836   return RotationPtr(
837     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
838                            theAngle, theNumberOfObjects, theFullValue, theReversed));
839 }
840
841 //--------------------------------------------------------------------------------------
842 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
843                                           const ModelHighAPI_Reference& theFeature,
844                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
845 {
846   std::shared_ptr<ModelAPI_Feature> aFeature =
847     compositeFeature()->addFeature(SketchPlugin_Split::ID());
848   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
849
850   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
851   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
852     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
853     fillAttribute(thePositionPoint, aPointAttr);
854   }
855
856   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
857 }
858
859 //--------------------------------------------------------------------------------------
860 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
861                                         const ModelHighAPI_Reference& theFeature,
862                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
863 {
864   std::shared_ptr<ModelAPI_Feature> aFeature =
865     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
866   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
867
868   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
869   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
870     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
871     fillAttribute(thePositionPoint, aPointAttr);
872   }
873
874   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
875 }
876
877 //--------------------------------------------------------------------------------------
878 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
879     const ModelHighAPI_RefAttr & theLine1,
880     const ModelHighAPI_RefAttr & theLine2,
881     const ModelHighAPI_Double & theValue,
882     const std::string& theType)
883 {
884   std::shared_ptr<ModelAPI_Feature> aFeature =
885       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
886
887   const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
888                                        : SketchPlugin_ConstraintAngle::THE_VERSION_1;
889   fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
890
891   int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
892   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
893   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
894
895   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
896     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
897
898   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
899   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
900
901   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
902     std::string aTypeLC = theType;
903     std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(), ::tolower);
904     if (aTypeLC == "supplementary")
905       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
906     else if (aTypeLC == "backward")
907       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
908
909     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
910     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
911   }
912
913   aFeature->execute();
914   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
915 }
916
917 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
918     const ModelHighAPI_RefAttr & theLine1,
919     const ModelHighAPI_RefAttr & theLine2,
920     const ModelHighAPI_Double & theValue)
921 {
922   std::shared_ptr<ModelAPI_Feature> aFeature =
923       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
924   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
925       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
926   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
927       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
928   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
929   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
930   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
931   aFeature->execute();
932   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
933 }
934
935 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
936     const ModelHighAPI_RefAttr & theLine1,
937     const ModelHighAPI_RefAttr & theLine2,
938     const ModelHighAPI_Double & theValue)
939 {
940   std::shared_ptr<ModelAPI_Feature> aFeature =
941       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
942   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
943       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
944   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
945       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
946   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
947   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
948   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
949   aFeature->execute();
950   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
951 }
952
953 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
954     const ModelHighAPI_RefAttr & thePoint1,
955     const ModelHighAPI_RefAttr & thePoint2)
956 {
957   std::shared_ptr<ModelAPI_Feature> aFeature =
958       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
959   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
960   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
961   aFeature->execute();
962   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
963 }
964
965 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
966     const ModelHighAPI_RefAttr & theLine1,
967     const ModelHighAPI_RefAttr & theLine2)
968 {
969   std::shared_ptr<ModelAPI_Feature> aFeature =
970       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
971   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
972   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
973   aFeature->execute();
974   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
975 }
976
977 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
978     const ModelHighAPI_RefAttr & thePoint,
979     const ModelHighAPI_RefAttr & thePointOrLine,
980     const ModelHighAPI_Double & theValue,
981     bool isSigned)
982 {
983   std::shared_ptr<ModelAPI_Feature> aFeature =
984       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
985   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
986   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
987   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
988   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
989   aFeature->execute();
990   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
991 }
992
993 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
994     const ModelHighAPI_RefAttr & thePoint,
995     const ModelHighAPI_RefAttr & thePointOrLine,
996     const ModelHighAPI_Double & theValue)
997 {
998   return setDistance(thePoint, thePointOrLine, theValue, true);
999 }
1000
1001 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1002     const ModelHighAPI_RefAttr & thePoint,
1003     const ModelHighAPI_RefAttr & thePointOrLine,
1004     const ModelHighAPI_Double & theValue)
1005 {
1006   return setDistance(thePoint, thePointOrLine, theValue, false);
1007 }
1008
1009 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1010     const ModelHighAPI_RefAttr & thePoint1,
1011     const ModelHighAPI_RefAttr & thePoint2,
1012     const ModelHighAPI_Double & theValue)
1013 {
1014   std::shared_ptr<ModelAPI_Feature> aFeature =
1015       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1016   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1017   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1018   fillAttribute(theValue,
1019       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1020   aFeature->execute();
1021   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1022 }
1023
1024 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1025     const ModelHighAPI_RefAttr & thePoint1,
1026     const ModelHighAPI_RefAttr & thePoint2,
1027     const ModelHighAPI_Double & theValue)
1028 {
1029   std::shared_ptr<ModelAPI_Feature> aFeature =
1030       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1031   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1032   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1033   fillAttribute(theValue,
1034       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1035   aFeature->execute();
1036   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1037 }
1038
1039 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1040     const ModelHighAPI_RefAttr & theObject1,
1041     const ModelHighAPI_RefAttr & theObject2)
1042 {
1043   std::shared_ptr<ModelAPI_Feature> aFeature =
1044       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1045   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1046   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1047   aFeature->execute();
1048   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1049 }
1050
1051 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1052     const ModelHighAPI_RefAttr & thePoint)
1053 {
1054   std::shared_ptr<ModelAPI_Feature> aFeature =
1055       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1056   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1057   apply(); // finish operation to remove Fillet feature correcly
1058   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1059 }
1060
1061 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1062     const ModelHighAPI_RefAttr & thePoint,
1063     const ModelHighAPI_Double & theRadius)
1064 {
1065   CompositeFeaturePtr aSketch = compositeFeature();
1066   int aNbSubs = aSketch->numberOfSubs();
1067
1068   // create fillet
1069   InterfacePtr aFilletFeature = setFillet(thePoint);
1070
1071   // set radius for just created arc
1072   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1073   if (anArc->getKind() == SketchPlugin_Arc::ID())
1074     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1075
1076   return aFilletFeature;
1077 }
1078
1079 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1080     const ModelHighAPI_RefAttr & theObject)
1081 {
1082   std::shared_ptr<ModelAPI_Feature> aFeature =
1083       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1084   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1085   aFeature->execute();
1086   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1087 }
1088
1089 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1090     const ModelHighAPI_RefAttr & theLine)
1091 {
1092   std::shared_ptr<ModelAPI_Feature> aFeature =
1093       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1094   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1095   aFeature->execute();
1096   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1097 }
1098
1099 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1100     const ModelHighAPI_RefAttr & theLine,
1101     const ModelHighAPI_Double & theValue)
1102 {
1103   std::shared_ptr<ModelAPI_Feature> aFeature =
1104       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1105   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1106   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1107   aFeature->execute();
1108   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1109 }
1110
1111 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1112     const ModelHighAPI_RefAttr & thePoint,
1113     const ModelHighAPI_RefAttr & theLine)
1114 {
1115   std::shared_ptr<ModelAPI_Feature> aFeature =
1116       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1117   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1118   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1119   aFeature->execute();
1120   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1121 }
1122
1123 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1124     const ModelHighAPI_RefAttr & theLine1,
1125     const ModelHighAPI_RefAttr & theLine2)
1126 {
1127   std::shared_ptr<ModelAPI_Feature> aFeature =
1128       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1129   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1130   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1131   aFeature->execute();
1132   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1133 }
1134
1135 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1136     const ModelHighAPI_RefAttr & theLine1,
1137     const ModelHighAPI_RefAttr & theLine2)
1138 {
1139   std::shared_ptr<ModelAPI_Feature> aFeature =
1140       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1141   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1142   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1143   aFeature->execute();
1144   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1145 }
1146
1147 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1148     const ModelHighAPI_RefAttr & theCircleOrArc,
1149     const ModelHighAPI_Double & theValue)
1150 {
1151   std::shared_ptr<ModelAPI_Feature> aFeature =
1152       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1153   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1154   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1155   aFeature->execute();
1156   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1157 }
1158
1159 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1160     const ModelHighAPI_RefAttr & theLine,
1161     const ModelHighAPI_RefAttr & theCircle)
1162 {
1163   std::shared_ptr<ModelAPI_Feature> aFeature =
1164       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1165   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1166   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1167   aFeature->execute();
1168   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1169 }
1170
1171 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1172     const ModelHighAPI_RefAttr & theLine)
1173 {
1174   std::shared_ptr<ModelAPI_Feature> aFeature =
1175       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1176   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1177   aFeature->execute();
1178   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1179 }
1180
1181 //--------------------------------------------------------------------------------------
1182
1183 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1184 {
1185   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1186   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1187 }
1188
1189 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1190 {
1191   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1192       theFeature->attribute(SketchPlugin_Line::START_ID()));
1193   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1194       theFeature->attribute(SketchPlugin_Line::END_ID()));
1195
1196   if (!aStartAttr || !aEndAttr)
1197     return std::shared_ptr<GeomAPI_Pnt2d>();
1198
1199   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1200   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1201   return std::shared_ptr<GeomAPI_Pnt2d>(
1202       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1203 }
1204
1205 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1206 {
1207   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1208       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1209   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1210
1211   if (!aCenter || !aRadius)
1212     return std::shared_ptr<GeomAPI_Pnt2d>();
1213
1214   return std::shared_ptr<GeomAPI_Pnt2d>(
1215       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1216 }
1217
1218 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1219 {
1220   static const double PI = 3.141592653589793238463;
1221
1222   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1223       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1224   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1225       theFeature->attribute(SketchPlugin_Arc::START_ID()));
1226   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1227       theFeature->attribute(SketchPlugin_Arc::END_ID()));
1228
1229   if (!aCenterAttr || !aStartAttr || !aEndAttr)
1230     return std::shared_ptr<GeomAPI_Pnt2d>();
1231
1232   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1233       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1234   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1235       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1236
1237   double anAngle = aStartDir->angle(aEndDir);
1238   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1239   if (isReversed && anAngle > 0.)
1240     anAngle -= 2.0 * PI;
1241   else if (!isReversed && anAngle <= 0.)
1242     anAngle += 2.0 * PI;
1243
1244   double cosA = cos(anAngle);
1245   double sinA = sin(anAngle);
1246
1247   // rotate start dir to find middle point on arc
1248   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1249   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1250   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1251
1252   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1253 }
1254
1255 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1256                                                      bool isEllipse = true)
1257 {
1258   const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1259                                   SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1260   AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1261       theFeature->attribute(anAttrName));
1262   return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1263 }
1264
1265 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1266                                                            SketchAPI_Sketch* theSketch)
1267 {
1268   GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1269   GeomPointPtr aMiddle = anEdge.middlePoint();
1270   return theSketch->to2D(aMiddle);
1271 }
1272
1273 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1274                                                   SketchAPI_Sketch* theSketch)
1275 {
1276   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1277   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1278   if (aFeature) {
1279     // move only features of the following types
1280     const std::string& aFeatureKind = aFeature->getKind();
1281     if (aFeatureKind == SketchPlugin_Point::ID())
1282       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1283     else if (aFeatureKind == SketchPlugin_Line::ID())
1284       aMiddlePoint = middlePointOnLine(aFeature);
1285     else if (aFeatureKind == SketchPlugin_Circle::ID())
1286       aMiddlePoint = pointOnCircle(aFeature);
1287     else if (aFeatureKind == SketchPlugin_Arc::ID())
1288       aMiddlePoint = middlePointOnArc(aFeature);
1289     else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1290       aMiddlePoint = pointOnEllipse(aFeature);
1291     else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1292       aMiddlePoint = pointOnEllipse(aFeature, false);
1293     else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1294              aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1295       aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1296   }
1297   return aMiddlePoint;
1298 }
1299
1300 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1301                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1302 {
1303   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1304   theMovedEntity.fillMessage(aMessage);
1305
1306   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1307   if (aMessage->movedAttribute())
1308     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1309   else
1310     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1311
1312   if (!anOriginalPosition)
1313     return; // something has gone wrong, do not process movement
1314
1315   aMessage->setOriginalPosition(anOriginalPosition);
1316   aMessage->setCurrentPosition(theTargetPoint);
1317   Events_Loop::loop()->send(aMessage);
1318 }
1319
1320 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1321                             double theTargetX, double theTargetY)
1322 {
1323   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1324   move(theMovedEntity, aTargetPoint);
1325 }
1326
1327 //--------------------------------------------------------------------------------------
1328
1329 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1330 {
1331   FeaturePtr aBase = feature();
1332   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1333       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1334   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1335       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1336   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1337       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1338   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1339
1340   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1341 }
1342
1343 //--------------------------------------------------------------------------------------
1344
1345 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1346 {
1347   // collect edges of the first face
1348   std::list<GeomShapePtr> anEdges1;
1349   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1350     anEdges1.push_back(anExp.current());
1351   // compare edges of faces
1352   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1353     GeomShapePtr aCurrent = anExp.current();
1354     bool isFound = false;
1355     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1356     for (; anIt1 != anEdges1.end(); ++anIt1)
1357       if (aCurrent->isSameGeometry(*anIt1)) {
1358         isFound = true;
1359         anEdges1.erase(anIt1);
1360         break;
1361       }
1362     if (!isFound)
1363       return true;
1364   }
1365   return !anEdges1.empty();
1366 }
1367
1368 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1369 {
1370   ResultConstructionPtr aSketchResult =
1371       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1372   if (!aSketchResult)
1373     return false;
1374
1375   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1376       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1377   if (!aWires)
1378     return false;
1379
1380   // collect faces constructed by SketchBuilder algorithm
1381   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1382                                            aWires->norm(), aWires);
1383   const ListOfShape& aFaces = aSketchBuilder.faces();
1384
1385   // compare faces stored in sketch with faces generated by SketchBuilder
1386   int aNbSketchFaces = aSketchResult->facesNum();
1387   int aFaceIndex = 0;
1388   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1389        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1390        ++aFIt, ++aFaceIndex) {
1391     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1392     GeomFacePtr aCurFace = (*aFIt)->face();
1393     if (isDifferent(aSketchFace, aCurFace))
1394       return true;
1395   }
1396   return false;
1397 }
1398
1399 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1400                                std::list<std::list<ResultPtr> >& theEdges)
1401 {
1402   ResultConstructionPtr aSketchResult =
1403       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1404   if (!aSketchResult)
1405     return;
1406
1407   // collect curves of the sketch
1408   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1409   int aSubNum = theSketch->numberOfSubs();
1410   for (int a = 0; a < aSubNum; ++a) {
1411     FeaturePtr aSub = theSketch->subFeature(a);
1412     const std::list<ResultPtr>& aResults = aSub->results();
1413     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1414     for (; aRes != aResults.cend(); aRes++) {
1415       GeomShapePtr aCurShape = (*aRes)->shape();
1416       if (aCurShape && aCurShape->isEdge())
1417         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1418     }
1419   }
1420
1421   // convert each face to the list of results of its edges
1422   int aFacesNum = aSketchResult->facesNum();
1423   for (int a = 0; a < aFacesNum; ++a) {
1424     theEdges.push_back(std::list<ResultPtr>());
1425     std::list<ResultPtr>& aCurEdges = theEdges.back();
1426
1427     GeomFacePtr aFace = aSketchResult->face(a);
1428     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1429          anExp.more(); anExp.next()) {
1430       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1431       aCurEdges.push_back(aCurves[aCurrent]);
1432     }
1433   }
1434 }
1435
1436 //--------------------------------------------------------------------------------------
1437
1438 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1439 {
1440   FeaturePtr aBase = feature();
1441   const std::string& aDocName = theDumper.name(aBase->document());
1442
1443   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1444   if (anExternal->value()) {
1445     theDumper << aBase << " = model.addSketch(" << aDocName <<
1446       ", " << anExternal << ")" << std::endl;
1447   } else {
1448     // Sketch is base on a plane.
1449     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1450         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1451     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1452         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1453     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1454         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1455
1456     // Check the plane is coordinate plane
1457     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1458     if(anExternal->context()) { // checking for selected planes
1459       if (!aPlaneName.empty()
1460           && anExternal->context()->data()
1461           && anExternal->context()->data()->name() == aPlaneName) {
1462         // dump sketch based on coordinate plane
1463         theDumper << aBase << " = model.addSketch(" << aDocName
1464                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1465       } else { // some other plane
1466         theDumper << aBase << " = model.addSketch(" << aDocName <<
1467           ", " << anExternal<< ")" << std::endl;
1468       }
1469     } else {
1470       if (aPlaneName.empty()) {
1471         // needs import additional module
1472         theDumper.importModule("GeomAPI");
1473         // dump plane parameters
1474         const std::string& aSketchName = theDumper.name(aBase);
1475         std::string anOriginName = aSketchName + "_origin";
1476         std::string aNormalName  = aSketchName + "_norm";
1477         std::string aDirXName    = aSketchName + "_dirx";
1478         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1479         // and then dumplicate dumping it in the next line
1480         theDumper << anOriginName << " = " << anOrigin << "\n"
1481                   << aNormalName  << " = " << aNormal  << "\n"
1482                   << aDirXName    << " = " << aDirX    << "\n";
1483         // dump sketch based on arbitrary plane
1484         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1485                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1486       } else {
1487         // dump sketch based on coordinate plane
1488         theDumper << aBase << " = model.addSketch(" << aDocName
1489                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1490       }
1491     }
1492   }
1493
1494   // dump sketch's subfeatures
1495   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1496   theDumper.processSubs(aCompFeat, true);
1497
1498   // if face order differs to the order generated by SketchBuilder,
1499   // dump the list of faces for correct execution of the script
1500   if (isCustomFacesOrder(aCompFeat)) {
1501     std::list<std::list<ResultPtr> > aFaces;
1502     edgesOfSketchFaces(aCompFeat, aFaces);
1503
1504     const std::string& aSketchName = theDumper.name(aBase);
1505     std::string aMethodName(".changeFacesOrder");
1506     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1507
1508     theDumper << aSketchName << aMethodName << "([";
1509     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1510          aFIt != aFaces.end(); ++aFIt) {
1511       if (aFIt != aFaces.begin())
1512         theDumper << ",\n" << aSpaceShift << "  ";
1513       theDumper << *aFIt;
1514     }
1515     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1516     // call model.do() for correct update of the document's labels related to the changed faces
1517     theDumper << "model.do()" << std::endl;
1518   }
1519 }