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