]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Sketch.cpp
Salome HOME
Add SketchAPI_Point
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Name   : SketchAPI_Sketch.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "SketchAPI_Sketch.h"
9 //--------------------------------------------------------------------------------------
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintFillet.h>
17 #include <SketchPlugin_ConstraintHorizontal.h>
18 #include <SketchPlugin_ConstraintLength.h>
19 #include <SketchPlugin_ConstraintMiddle.h>
20 #include <SketchPlugin_ConstraintMirror.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
27 //--------------------------------------------------------------------------------------
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelHighAPI_RefAttr.h>
31 #include <ModelHighAPI_Selection.h>
32 #include <ModelHighAPI_Tools.h>
33 //--------------------------------------------------------------------------------------
34 #include "SketchAPI_Arc.h"
35 #include "SketchAPI_Circle.h"
36 #include "SketchAPI_Line.h"
37 #include "SketchAPI_Point.h"
38 //--------------------------------------------------------------------------------------
39 SketchAPI_Sketch::SketchAPI_Sketch(
40     const std::shared_ptr<ModelAPI_Feature> & theFeature)
41 : ModelHighAPI_Interface(theFeature)
42 {
43   initialize();
44 }
45
46 SketchAPI_Sketch::SketchAPI_Sketch(
47     const std::shared_ptr<ModelAPI_Feature> & theFeature,
48     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
49 : ModelHighAPI_Interface(theFeature)
50 {
51   if (initialize()) {
52     setPlane(thePlane);
53   }
54 }
55
56 SketchAPI_Sketch::SketchAPI_Sketch(
57     const std::shared_ptr<ModelAPI_Feature> & theFeature,
58     const ModelHighAPI_Selection & theExternal)
59 : ModelHighAPI_Interface(theFeature)
60 {
61   if (initialize()) {
62     setExternal(theExternal);
63   }
64 }
65
66 SketchAPI_Sketch::~SketchAPI_Sketch()
67 {
68
69 }
70
71 //--------------------------------------------------------------------------------------
72 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
73 {
74   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
75 }
76
77 //--------------------------------------------------------------------------------------
78 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
79 {
80   fillAttribute(thePlane->origin(), myorigin);
81   fillAttribute(thePlane->dirX(), mydirX);
82   fillAttribute(thePlane->normal(), mynormal);
83
84   execute();
85 }
86
87 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
88 {
89   fillAttribute(theExternal, myexternal);
90
91   execute();
92 }
93
94 //--------------------------------------------------------------------------------------
95 void SketchAPI_Sketch::setValue(
96     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
97     const ModelHighAPI_Double & theValue)
98 {
99   // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
100   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
101
102 //  theConstraint->execute();
103 }
104
105 //--------------------------------------------------------------------------------------
106 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
107 {
108   const_cast<SketchAPI_Sketch*>(this)->execute();
109
110   std::list<ModelHighAPI_Selection> aSelectionList;
111
112   ResultConstructionPtr aResultConstruction =
113       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
114   if (aResultConstruction.get() == NULL)
115     return aSelectionList;
116
117   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
118     aSelectionList.push_back(
119         ModelHighAPI_Selection(aResultConstruction,
120                                aResultConstruction->face(anIndex)));
121   }
122
123   return aSelectionList;
124 }
125
126 //--------------------------------------------------------------------------------------
127 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
128                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
129 {
130   // TODO(spo): check that thePart is not empty
131   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
132   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
133 }
134
135 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
136                     const ModelHighAPI_Selection & theExternal)
137 {
138   // TODO(spo): check that thePart is not empty
139   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
140   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
141 }
142
143 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
144                     const std::string & theExternalName)
145 {
146   // TODO(spo): check that thePart is not empty
147   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
148   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
149 }
150
151 //--------------------------------------------------------------------------------------
152 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
153     double theX, double theY)
154 {
155   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
156   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
157 }
158 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
159     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
160 {
161   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
162   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
163 }
164 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
165 {
166   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
167   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
168 }
169 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
170 {
171   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
172   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
173 }
174
175 //--------------------------------------------------------------------------------------
176 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
177 {
178   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
179   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
180 }
181 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
182     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
183     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
184 {
185   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
186   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
187 }
188 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
189 {
190   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
191   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
192 }
193 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
194 {
195   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
196   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
197   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
198 }
199
200 //--------------------------------------------------------------------------------------
201 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
202                                                               double theCenterY,
203                                                               double theRadius)
204 {
205   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
206   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
207 }
208
209 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
210                                                               double theRadius)
211 {
212   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
213   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
214 }
215
216 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
217                                                               double theX2, double theY2,
218                                                               double theX3, double theY3)
219 {
220   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
221   return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
222 }
223
224 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
225                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
226                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
227 {
228   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
229   return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
230 }
231
232 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
233 {
234   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
235   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
236 }
237
238 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
239 {
240   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
241   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
242   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
243 }
244
245 //--------------------------------------------------------------------------------------
246 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
247                                                         double theStartX, double theStartY,
248                                                         double theEndX, double theEndY,
249                                                         bool theInversed)
250 {
251   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
252   return ArcPtr(new SketchAPI_Arc(aFeature,
253                                   theCenterX, theCenterY,
254                                   theStartX, theStartY,
255                                   theEndX, theEndY,
256                                   theInversed));
257 }
258
259 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
260                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
261                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
262                                                         bool theInversed)
263 {
264   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
265   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
266 }
267
268 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
269                                                         double theEndX, double theEndY,
270                                                         double thePassedX, double thePassedY)
271 {
272   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
273   return ArcPtr(new SketchAPI_Arc(aFeature,
274                                   theStartX, theStartY,
275                                   theEndX, theEndY,
276                                   thePassedX, thePassedY));
277 }
278
279 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
280                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
281                                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
282 {
283   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
284   return ArcPtr(new SketchAPI_Arc(aFeature, theStart, theEnd, thePassed));
285 }
286
287 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
288                                                         double theEndX, double theEndY,
289                                                         bool theInversed)
290 {
291   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
292   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEndX, theEndY, theInversed));
293 }
294
295 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
296                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
297                                                         bool theInversed)
298 {
299   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
300   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEnd, theInversed));
301 }
302
303 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
304 {
305   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
306   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
307 }
308
309 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
310 {
311   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
312   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
313   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
314 }
315
316 //--------------------------------------------------------------------------------------
317 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
318     const ModelHighAPI_RefAttr & thePoint1,
319     const ModelHighAPI_RefAttr & thePoint2)
320 {
321   std::shared_ptr<ModelAPI_Feature> aFeature =
322       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
323   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
324   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
325   aFeature->execute();
326   return aFeature;
327 }
328
329 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
330     const ModelHighAPI_RefAttr & thePoint,
331     const ModelHighAPI_RefAttr & theLine,
332     const ModelHighAPI_Double & theValue)
333 {
334   std::shared_ptr<ModelAPI_Feature> aFeature =
335       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
336   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
337   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
338   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
339   aFeature->execute();
340   return aFeature;
341 }
342
343 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
344     const std::list<ModelHighAPI_RefAttr> & thePoints,
345     const ModelHighAPI_Double & theRadius)
346 {
347   std::shared_ptr<ModelAPI_Feature> aFeature =
348       compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
349   fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
350   fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
351   aFeature->execute();
352   return aFeature;
353 }
354
355 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
356     const ModelHighAPI_RefAttr & theLine)
357 {
358   std::shared_ptr<ModelAPI_Feature> aFeature =
359       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
360   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
361   aFeature->execute();
362   return aFeature;
363 }
364
365 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
366     const ModelHighAPI_RefAttr & theLine,
367     const ModelHighAPI_Double & theValue)
368 {
369   std::shared_ptr<ModelAPI_Feature> aFeature =
370       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
371   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
372   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
373   aFeature->execute();
374   return aFeature;
375 }
376
377 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
378     const ModelHighAPI_RefAttr & theLine1,
379     const ModelHighAPI_RefAttr & theLine2)
380 {
381   std::shared_ptr<ModelAPI_Feature> aFeature =
382       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
383   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
384   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
385   aFeature->execute();
386   return aFeature;
387 }
388
389 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
390     const ModelHighAPI_RefAttr & theLine1,
391     const ModelHighAPI_RefAttr & theLine2)
392 {
393   std::shared_ptr<ModelAPI_Feature> aFeature =
394       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
395   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
396   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
397   aFeature->execute();
398   return aFeature;
399 }
400
401 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRigid(
402     const ModelHighAPI_RefAttr & theObject)
403 {
404   std::shared_ptr<ModelAPI_Feature> aFeature =
405       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
406   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
407   aFeature->execute();
408   return aFeature;
409 }
410
411 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
412     const ModelHighAPI_RefAttr & theLine)
413 {
414   std::shared_ptr<ModelAPI_Feature> aFeature =
415       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
416   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
417   aFeature->execute();
418   return aFeature;
419 }
420
421 //--------------------------------------------------------------------------------------