Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Sketch.h"
22 //--------------------------------------------------------------------------------------
23 #include <SketchPlugin_Constraint.h>
24 #include <SketchPlugin_ConstraintAngle.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintCollinear.h>
27 #include <SketchPlugin_ConstraintDistance.h>
28 #include <SketchPlugin_ConstraintEqual.h>
29 #include <SketchPlugin_Fillet.h>
30 #include <SketchPlugin_ConstraintHorizontal.h>
31 #include <SketchPlugin_ConstraintLength.h>
32 #include <SketchPlugin_ConstraintMiddle.h>
33 #include <SketchPlugin_ConstraintMirror.h>
34 #include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintPerpendicular.h>
36 #include <SketchPlugin_ConstraintRadius.h>
37 #include <SketchPlugin_ConstraintRigid.h>
38 #include <SketchPlugin_Trim.h>
39 #include <SketchPlugin_Split.h>
40 #include <SketchPlugin_ConstraintTangent.h>
41 #include <SketchPlugin_ConstraintVertical.h>
42 #include <SketcherPrs_Tools.h>
43 //--------------------------------------------------------------------------------------
44 #include <ModelAPI_CompositeFeature.h>
45 #include <ModelAPI_ResultConstruction.h>
46 #include <ModelHighAPI_Double.h>
47 #include <ModelHighAPI_Dumper.h>
48 #include <ModelHighAPI_RefAttr.h>
49 #include <ModelHighAPI_Selection.h>
50 #include <ModelHighAPI_Services.h>
51 #include <ModelHighAPI_Tools.h>
52 //--------------------------------------------------------------------------------------
53 #include "SketchAPI_Arc.h"
54 #include "SketchAPI_MacroArc.h"
55 #include "SketchAPI_Circle.h"
56 #include "SketchAPI_IntersectionPoint.h"
57 #include "SketchAPI_Line.h"
58 #include "SketchAPI_MacroCircle.h"
59 #include "SketchAPI_Mirror.h"
60 #include "SketchAPI_Point.h"
61 #include "SketchAPI_Projection.h"
62 #include "SketchAPI_Rectangle.h"
63 #include "SketchAPI_Rotation.h"
64 #include "SketchAPI_Translation.h"
65 //--------------------------------------------------------------------------------------
66 SketchAPI_Sketch::SketchAPI_Sketch(
67     const std::shared_ptr<ModelAPI_Feature> & theFeature)
68 : ModelHighAPI_Interface(theFeature)
69 {
70   initialize();
71 }
72
73 SketchAPI_Sketch::SketchAPI_Sketch(
74     const std::shared_ptr<ModelAPI_Feature> & theFeature,
75     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
76 : ModelHighAPI_Interface(theFeature)
77 {
78   if (initialize()) {
79     setPlane(thePlane);
80   }
81 }
82
83 SketchAPI_Sketch::SketchAPI_Sketch(
84     const std::shared_ptr<ModelAPI_Feature> & theFeature,
85     const ModelHighAPI_Selection & theExternal)
86 : ModelHighAPI_Interface(theFeature)
87 {
88   if (initialize()) {
89     setExternal(theExternal);
90   }
91 }
92
93 SketchAPI_Sketch::SketchAPI_Sketch(
94     const std::shared_ptr<ModelAPI_Feature> & theFeature,
95     std::shared_ptr<ModelAPI_Object> thePlaneObject)
96 : ModelHighAPI_Interface(theFeature)
97 {
98   if (initialize()) {
99     setExternal(thePlaneObject);
100   }
101 }
102
103 SketchAPI_Sketch::~SketchAPI_Sketch()
104 {
105
106 }
107
108 //--------------------------------------------------------------------------------------
109 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
110 {
111   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
112 }
113
114 //--------------------------------------------------------------------------------------
115 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
116 {
117   fillAttribute(thePlane->origin(), myorigin);
118   fillAttribute(thePlane->dirX(), mydirX);
119   fillAttribute(thePlane->normal(), mynormal);
120
121   execute();
122 }
123
124 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
125 {
126   fillAttribute(theExternal, myexternal);
127
128   execute();
129 }
130
131 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
132 {
133   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
134   ModelHighAPI_Selection aSel(aRes);
135   setExternal(aSel);
136 }
137
138 //--------------------------------------------------------------------------------------
139 void SketchAPI_Sketch::setValue(
140     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
141     const ModelHighAPI_Double & theValue)
142 {
143   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
144
145 //  theConstraint->execute();
146 }
147
148 //--------------------------------------------------------------------------------------
149 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
150 {
151   const_cast<SketchAPI_Sketch*>(this)->execute();
152
153   std::list<ModelHighAPI_Selection> aSelectionList;
154
155   ResultConstructionPtr aResultConstruction =
156       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
157   if (aResultConstruction.get() == NULL)
158     return aSelectionList;
159
160   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
161     aSelectionList.push_back(
162         ModelHighAPI_Selection(aResultConstruction,
163                                aResultConstruction->face(anIndex)));
164   }
165
166   return aSelectionList;
167 }
168
169 //--------------------------------------------------------------------------------------
170 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
171                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
172 {
173   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
174   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
175 }
176
177 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
178                     const ModelHighAPI_Selection & theExternal)
179 {
180   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
181   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
182 }
183
184 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
185                     const std::string & theExternalName)
186 {
187   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
188   return SketchPtr(
189     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
190 }
191
192 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
193                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
194 {
195   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
196   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
197 }
198
199
200 //--------------------------------------------------------------------------------------
201 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
202     double theX, double theY)
203 {
204   std::shared_ptr<ModelAPI_Feature> aFeature =
205     compositeFeature()->addFeature(SketchPlugin_Point::ID());
206   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
207 }
208 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
209     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
210 {
211   std::shared_ptr<ModelAPI_Feature> aFeature =
212     compositeFeature()->addFeature(SketchPlugin_Point::ID());
213   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
214 }
215 std::shared_ptr<SketchAPI_Point>
216   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
217 {
218   std::shared_ptr<ModelAPI_Feature> aFeature =
219     compositeFeature()->addFeature(SketchPlugin_Point::ID());
220   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
221 }
222 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
223 {
224   std::shared_ptr<ModelAPI_Feature> aFeature =
225     compositeFeature()->addFeature(SketchPlugin_Point::ID());
226   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
227 }
228
229 //--------------------------------------------------------------------------------------
230 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
231     const ModelHighAPI_Selection & theExternal)
232 {
233   std::shared_ptr<ModelAPI_Feature> aFeature =
234     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
235   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
236 }
237 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
238     const std::string & theExternalName)
239 {
240   std::shared_ptr<ModelAPI_Feature> aFeature =
241     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
242   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
243 }
244
245 //--------------------------------------------------------------------------------------
246 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
247                                                           double theX2, double theY2)
248 {
249   std::shared_ptr<ModelAPI_Feature> aFeature =
250     compositeFeature()->addFeature(SketchPlugin_Line::ID());
251   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
252 }
253 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
254     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
255     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
256 {
257   std::shared_ptr<ModelAPI_Feature> aFeature =
258     compositeFeature()->addFeature(SketchPlugin_Line::ID());
259   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
260 }
261 std::shared_ptr<SketchAPI_Line>
262   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
263 {
264   std::shared_ptr<ModelAPI_Feature> aFeature =
265     compositeFeature()->addFeature(SketchPlugin_Line::ID());
266   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
267 }
268 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
269 {
270   std::shared_ptr<ModelAPI_Feature> aFeature =
271     compositeFeature()->addFeature(SketchPlugin_Line::ID());
272   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
273 }
274
275 //--------------------------------------------------------------------------------------
276 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
277                                                                     double theX2, double theY2)
278 {
279   std::shared_ptr<ModelAPI_Feature> aFeature =
280     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
281   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
282 }
283 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
284     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
285     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
286 {
287   std::shared_ptr<ModelAPI_Feature> aFeature =
288     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
289   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
290 }
291
292 //--------------------------------------------------------------------------------------
293 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
294                                                               double theCenterY,
295                                                               double theRadius)
296 {
297   std::shared_ptr<ModelAPI_Feature> aFeature =
298     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
299   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
300 }
301
302 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
303                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
304                                     double theRadius)
305 {
306   std::shared_ptr<ModelAPI_Feature> aFeature =
307     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
308   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
309 }
310
311 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
312                                                                    double theCenterY,
313                                                                    double thePassedX,
314                                                                    double thePassedY)
315 {
316   std::shared_ptr<ModelAPI_Feature> aFeature =
317     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
318   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
319                                                             thePassedX, thePassedY));
320 }
321
322 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
323     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
324     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
325 {
326   std::shared_ptr<ModelAPI_Feature> aFeature =
327     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
328   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
329 }
330
331 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
332                                                                    double theX2, double theY2,
333                                                                    double theX3, double theY3)
334 {
335   std::shared_ptr<ModelAPI_Feature> aFeature =
336     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
337   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
338                                                             theX2, theY2,
339                                                             theX3, theY3));
340 }
341
342 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
343     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
344     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
345     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
346 {
347   std::shared_ptr<ModelAPI_Feature> aFeature =
348     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
349   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
350 }
351
352 std::shared_ptr<SketchAPI_Circle>
353   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
354 {
355   std::shared_ptr<ModelAPI_Feature> aFeature =
356     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
357   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
358 }
359
360 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
361 {
362   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
363   std::shared_ptr<ModelAPI_Feature> aFeature =
364     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
365   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
366 }
367
368 //--------------------------------------------------------------------------------------
369 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
370                                                         double theStartX, double theStartY,
371                                                         double theEndX, double theEndY,
372                                                         bool theInversed)
373 {
374   std::shared_ptr<ModelAPI_Feature> aFeature =
375     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
376   return ArcPtr(new SketchAPI_Arc(aFeature,
377                                   theCenterX, theCenterY,
378                                   theStartX, theStartY,
379                                   theEndX, theEndY,
380                                   theInversed));
381 }
382
383 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
384                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
385                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
386                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
387                                               bool theInversed)
388 {
389   std::shared_ptr<ModelAPI_Feature> aFeature =
390     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
391   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
392 }
393
394 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
395                                                         double theEndX, double theEndY,
396                                                         double thePassedX, double thePassedY)
397 {
398   std::shared_ptr<ModelAPI_Feature> aFeature =
399     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
400   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
401                                        theStartX, theStartY,
402                                        theEndX, theEndY,
403                                        thePassedX, thePassedY));
404 }
405
406 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
407                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
408                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
409                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
410 {
411   std::shared_ptr<ModelAPI_Feature> aFeature =
412     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
413   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
414 }
415
416 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
417                                                 const ModelHighAPI_RefAttr& theTangentPoint,
418                                                 double theEndX, double theEndY,
419                                                 bool theInversed)
420 {
421   std::shared_ptr<ModelAPI_Feature> aFeature =
422     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
423   return MacroArcPtr(new SketchAPI_MacroArc(
424     aFeature, theTangentPoint, theEndX, theEndY, theInversed));
425 }
426
427 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
428                                               const ModelHighAPI_RefAttr& theTangentPoint,
429                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
430                                               bool theInversed)
431 {
432   std::shared_ptr<ModelAPI_Feature> aFeature =
433     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
434   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theTangentPoint, theEnd, theInversed));
435 }
436
437 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
438 {
439   std::shared_ptr<ModelAPI_Feature> aFeature =
440     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
441   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
442 }
443
444 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
445 {
446   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
447   std::shared_ptr<ModelAPI_Feature> aFeature =
448     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
449   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
450 }
451
452 //--------------------------------------------------------------------------------------
453 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
454     const ModelHighAPI_Selection & theExternalFeature)
455 {
456   std::shared_ptr<ModelAPI_Feature> aFeature =
457     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
458   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
459 }
460
461 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
462     const std::string & theExternalName)
463 {
464   std::shared_ptr<ModelAPI_Feature> aFeature =
465     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
466   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalName));
467 }
468
469 //--------------------------------------------------------------------------------------
470 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
471     const ModelHighAPI_RefAttr & theMirrorLine,
472     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
473 {
474   std::shared_ptr<ModelAPI_Feature> aFeature =
475     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
476   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
477 }
478
479 //--------------------------------------------------------------------------------------
480 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
481     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
482     const ModelHighAPI_RefAttr & thePoint1,
483     const ModelHighAPI_RefAttr & thePoint2,
484     const ModelHighAPI_Integer & theNumberOfObjects,
485     bool theFullValue)
486 {
487   std::shared_ptr<ModelAPI_Feature> aFeature =
488     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
489   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
490                                                   thePoint2, theNumberOfObjects, theFullValue));
491 }
492
493 //--------------------------------------------------------------------------------------
494 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
495     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
496     const ModelHighAPI_RefAttr & theCenter,
497     const ModelHighAPI_Double & theAngle,
498     const ModelHighAPI_Integer & theNumberOfObjects,
499     bool theFullValue)
500 {
501   std::shared_ptr<ModelAPI_Feature> aFeature =
502     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
503   return RotationPtr(
504     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
505                            theAngle, theNumberOfObjects, theFullValue));
506 }
507
508 //--------------------------------------------------------------------------------------
509 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
510                                           const ModelHighAPI_Reference& theFeature,
511                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
512 {
513   std::shared_ptr<ModelAPI_Feature> aFeature =
514     compositeFeature()->addFeature(SketchPlugin_Split::ID());
515   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
516
517   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
518   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
519     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
520     fillAttribute(thePositionPoint, aPointAttr);
521   }
522
523   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
524 }
525
526 //--------------------------------------------------------------------------------------
527 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
528                                         const ModelHighAPI_Reference& theFeature,
529                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
530 {
531   std::shared_ptr<ModelAPI_Feature> aFeature =
532     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
533   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
534
535   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
536   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
537     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
538     fillAttribute(thePositionPoint, aPointAttr);
539   }
540
541   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
542 }
543
544 //--------------------------------------------------------------------------------------
545 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
546     const ModelHighAPI_RefAttr & theLine1,
547     const ModelHighAPI_RefAttr & theLine2,
548     const ModelHighAPI_Double & theValue)
549 {
550   std::shared_ptr<ModelAPI_Feature> aFeature =
551       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
552   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
553       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
554   // fill the value before llines to avoid calculation of angle value by the Angle feature
555   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
556   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
557   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
558   aFeature->execute();
559   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
560 }
561
562 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
563     const ModelHighAPI_RefAttr & theLine1,
564     const ModelHighAPI_RefAttr & theLine2,
565     const ModelHighAPI_Double & theValue)
566 {
567   std::shared_ptr<ModelAPI_Feature> aFeature =
568       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
569   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
570       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
571   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
572   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
573   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
574 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
575   aFeature->execute();
576   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
577 }
578
579 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
580     const ModelHighAPI_RefAttr & theLine1,
581     const ModelHighAPI_RefAttr & theLine2,
582     const ModelHighAPI_Double & theValue)
583 {
584   std::shared_ptr<ModelAPI_Feature> aFeature =
585       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
586   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
587       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
588   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
589   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
590   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
591 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
592   aFeature->execute();
593   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
594 }
595
596 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
597     const ModelHighAPI_RefAttr & thePoint1,
598     const ModelHighAPI_RefAttr & thePoint2)
599 {
600   std::shared_ptr<ModelAPI_Feature> aFeature =
601       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
602   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
603   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
604   aFeature->execute();
605   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
606 }
607
608 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
609     const ModelHighAPI_RefAttr & theLine1,
610     const ModelHighAPI_RefAttr & theLine2)
611 {
612   std::shared_ptr<ModelAPI_Feature> aFeature =
613       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
614   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
615   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
616   aFeature->execute();
617   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
618 }
619
620 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
621     const ModelHighAPI_RefAttr & thePoint,
622     const ModelHighAPI_RefAttr & thePointOrLine,
623     const ModelHighAPI_Double & theValue)
624 {
625   std::shared_ptr<ModelAPI_Feature> aFeature =
626       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
627   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
628   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
629   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
630   aFeature->execute();
631   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
632 }
633
634 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
635     const ModelHighAPI_RefAttr & theObject1,
636     const ModelHighAPI_RefAttr & theObject2)
637 {
638   std::shared_ptr<ModelAPI_Feature> aFeature =
639       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
640   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
641   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
642   aFeature->execute();
643   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
644 }
645
646 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
647     const ModelHighAPI_RefAttr & thePoint)
648 {
649   std::shared_ptr<ModelAPI_Feature> aFeature =
650       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
651   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
652   apply(); // finish operation to remove Fillet feature correcly
653   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
654 }
655
656 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
657     const ModelHighAPI_RefAttr & thePoint,
658     const ModelHighAPI_Double & theRadius)
659 {
660   CompositeFeaturePtr aSketch = compositeFeature();
661   int aNbSubs = aSketch->numberOfSubs();
662
663   // create fillet
664   InterfacePtr aFilletFeature = setFillet(thePoint);
665
666   // set radius for just created arc
667   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
668   if (anArc->getKind() == SketchPlugin_Arc::ID())
669     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
670
671   return aFilletFeature;
672 }
673
674 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
675     const ModelHighAPI_RefAttr & theObject)
676 {
677   std::shared_ptr<ModelAPI_Feature> aFeature =
678       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
679   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
680   aFeature->execute();
681   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
682 }
683
684 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
685     const ModelHighAPI_RefAttr & theLine)
686 {
687   std::shared_ptr<ModelAPI_Feature> aFeature =
688       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
689   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
690   aFeature->execute();
691   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
692 }
693
694 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
695     const ModelHighAPI_RefAttr & theLine,
696     const ModelHighAPI_Double & theValue)
697 {
698   std::shared_ptr<ModelAPI_Feature> aFeature =
699       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
700   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
701   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
702   aFeature->execute();
703   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
704 }
705
706 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
707     const ModelHighAPI_RefAttr & thePoint,
708     const ModelHighAPI_RefAttr & theLine)
709 {
710   std::shared_ptr<ModelAPI_Feature> aFeature =
711       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
712   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
713   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
714   aFeature->execute();
715   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
716 }
717
718 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
719     const ModelHighAPI_RefAttr & theLine1,
720     const ModelHighAPI_RefAttr & theLine2)
721 {
722   std::shared_ptr<ModelAPI_Feature> aFeature =
723       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
724   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
725   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
726   aFeature->execute();
727   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
728 }
729
730 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
731     const ModelHighAPI_RefAttr & theLine1,
732     const ModelHighAPI_RefAttr & theLine2)
733 {
734   std::shared_ptr<ModelAPI_Feature> aFeature =
735       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
736   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
737   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
738   aFeature->execute();
739   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
740 }
741
742 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
743     const ModelHighAPI_RefAttr & theCircleOrArc,
744     const ModelHighAPI_Double & theValue)
745 {
746   std::shared_ptr<ModelAPI_Feature> aFeature =
747       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
748   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
749   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
750   aFeature->execute();
751   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
752 }
753
754 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
755     const ModelHighAPI_RefAttr & theLine,
756     const ModelHighAPI_RefAttr & theCircle)
757 {
758   std::shared_ptr<ModelAPI_Feature> aFeature =
759       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
760   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
761   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
762   aFeature->execute();
763   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
764 }
765
766 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
767     const ModelHighAPI_RefAttr & theLine)
768 {
769   std::shared_ptr<ModelAPI_Feature> aFeature =
770       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
771   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
772   aFeature->execute();
773   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
774 }
775
776 //--------------------------------------------------------------------------------------
777
778 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
779 {
780   FeaturePtr aBase = feature();
781   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
782       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
783   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
784       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
785   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
786       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
787   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
788
789   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
790 }
791
792 //--------------------------------------------------------------------------------------
793
794 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
795 {
796   FeaturePtr aBase = feature();
797   const std::string& aDocName = theDumper.name(aBase->document());
798
799   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
800   if (anExternal->value()) {
801     theDumper << aBase << " = model.addSketch(" << aDocName <<
802       ", " << anExternal << ")" << std::endl;
803   } else {
804     // Sketch is base on a plane.
805     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
806         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
807     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
808         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
809     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
810         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
811
812     // Check the plane is coordinate plane
813     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
814     if(anExternal->context()) { // checking for selected planes
815       if (!aPlaneName.empty()
816           && anExternal->context()->data()
817           && anExternal->context()->data()->name() == aPlaneName) {
818         // dump sketch based on coordinate plane
819         theDumper << aBase << " = model.addSketch(" << aDocName
820                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
821       } else { // some other plane
822         theDumper << aBase << " = model.addSketch(" << aDocName <<
823           ", " << anExternal<< ")" << std::endl;
824       }
825     } else {
826       if (aPlaneName.empty()) {
827         // needs import additional module
828         theDumper.importModule("GeomAPI");
829         // dump plane parameters
830         const std::string& aSketchName = theDumper.name(aBase);
831         std::string anOriginName = aSketchName + "_origin";
832         std::string aNormalName  = aSketchName + "_norm";
833         std::string aDirXName    = aSketchName + "_dirx";
834         // use "\n" instead of std::endl to avoid automatic dumping sketch here
835         // and then dumplicate dumping it in the next line
836         theDumper << anOriginName << " = " << anOrigin << "\n"
837                   << aNormalName  << " = " << aNormal  << "\n"
838                   << aDirXName    << " = " << aDirX    << "\n";
839         // dump sketch based on arbitrary plane
840         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
841                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
842       } else {
843         // dump sketch based on coordinate plane
844         theDumper << aBase << " = model.addSketch(" << aDocName
845                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
846       }
847     }
848   }
849
850   // dump sketch's subfeatures
851   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
852   theDumper.processSubs(aCompFeat, true);
853 }