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