]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_Sketch.cpp
Salome HOME
Add resilt() and selectFace()
[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_Circle.h"
35 #include "SketchAPI_Line.h"
36 //--------------------------------------------------------------------------------------
37 SketchAPI_Sketch::SketchAPI_Sketch(
38     const std::shared_ptr<ModelAPI_Feature> & theFeature)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   initialize();
42 }
43
44 SketchAPI_Sketch::SketchAPI_Sketch(
45     const std::shared_ptr<ModelAPI_Feature> & theFeature,
46     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
47 : ModelHighAPI_Interface(theFeature)
48 {
49   if (initialize()) {
50     setPlane(thePlane);
51   }
52 }
53
54 SketchAPI_Sketch::SketchAPI_Sketch(
55     const std::shared_ptr<ModelAPI_Feature> & theFeature,
56     const ModelHighAPI_Selection & theExternal)
57 : ModelHighAPI_Interface(theFeature)
58 {
59   if (initialize()) {
60     setExternal(theExternal);
61   }
62 }
63
64 SketchAPI_Sketch::~SketchAPI_Sketch()
65 {
66
67 }
68
69 //--------------------------------------------------------------------------------------
70 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
71 {
72   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
73 }
74
75 //--------------------------------------------------------------------------------------
76 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
77 {
78   fillAttribute(thePlane->origin(), myorigin);
79   fillAttribute(thePlane->dirX(), mydirX);
80   fillAttribute(thePlane->normal(), mynormal);
81
82   execute();
83 }
84
85 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
86 {
87   fillAttribute(theExternal, myexternal);
88
89   execute();
90 }
91
92 //--------------------------------------------------------------------------------------
93 void SketchAPI_Sketch::setValue(
94     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
95     const ModelHighAPI_Double & theValue)
96 {
97   // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
98   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
99
100   theConstraint->execute();
101 }
102
103 //--------------------------------------------------------------------------------------
104 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
105 {
106   std::list<ModelHighAPI_Selection> aSelectionList;
107
108   ResultConstructionPtr aResultConstruction =
109       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
110   if (aResultConstruction.get() == NULL)
111     return aSelectionList;
112
113   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
114     aSelectionList.push_back(
115         ModelHighAPI_Selection(aResultConstruction,
116                                aResultConstruction->face(anIndex)));
117   }
118
119   return aSelectionList;
120 }
121
122 //--------------------------------------------------------------------------------------
123 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
124                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
125 {
126   // TODO(spo): check that thePart is not empty
127   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
128   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
129 }
130
131 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
132                     const ModelHighAPI_Selection & theExternal)
133 {
134   // TODO(spo): check that thePart is not empty
135   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
136   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
137 }
138
139 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
140                     const std::string & theExternalName)
141 {
142   // TODO(spo): check that thePart is not empty
143   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
144   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
145 }
146
147 //--------------------------------------------------------------------------------------
148 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
149 {
150   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
151   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
152 }
153 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
154     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
155     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
156 {
157   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
158   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
159 }
160 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
161 {
162   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
163   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
164 }
165 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
166 {
167   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
168   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
169   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
170 }
171
172 //--------------------------------------------------------------------------------------
173 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
174                                                               double theCenterY,
175                                                               double theRadius)
176 {
177   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
178   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
179 }
180
181 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
182                                                               double theRadius)
183 {
184   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
185   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
186 }
187
188 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
189                                                               double theX2, double theY2,
190                                                               double theX3, double theY3)
191 {
192   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
193   return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
194 }
195
196 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
197                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
198                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
199 {
200   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
201   return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
202 }
203
204 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
205 {
206   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
207   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
208 }
209
210 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
211 {
212   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
213   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
214   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
215 }
216
217 //--------------------------------------------------------------------------------------
218 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
219     const ModelHighAPI_RefAttr & thePoint1,
220     const ModelHighAPI_RefAttr & thePoint2)
221 {
222   std::shared_ptr<ModelAPI_Feature> aFeature =
223       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
224   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
225   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
226   aFeature->execute();
227   return aFeature;
228 }
229
230 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
231     const ModelHighAPI_RefAttr & thePoint,
232     const ModelHighAPI_RefAttr & theLine,
233     const ModelHighAPI_Double & theValue)
234 {
235   std::shared_ptr<ModelAPI_Feature> aFeature =
236       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
237   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
238   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
239   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
240   aFeature->execute();
241   return aFeature;
242 }
243
244 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
245     const ModelHighAPI_RefAttr & theLine,
246     const ModelHighAPI_Double & theValue)
247 {
248   std::shared_ptr<ModelAPI_Feature> aFeature =
249       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
250   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
251   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
252   aFeature->execute();
253   return aFeature;
254 }
255
256 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
257     const ModelHighAPI_RefAttr & theLine1,
258     const ModelHighAPI_RefAttr & theLine2)
259 {
260   std::shared_ptr<ModelAPI_Feature> aFeature =
261       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
262   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
263   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
264   aFeature->execute();
265   return aFeature;
266 }
267
268 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
269     const ModelHighAPI_RefAttr & theLine1,
270     const ModelHighAPI_RefAttr & theLine2)
271 {
272   std::shared_ptr<ModelAPI_Feature> aFeature =
273       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
274   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
275   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
276   aFeature->execute();
277   return aFeature;
278 }
279 //--------------------------------------------------------------------------------------