Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Axis.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 "ConstructionAPI_Axis.h"
22
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 //==================================================================================================
27 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature)
28 : ModelHighAPI_Interface(theFeature)
29 {
30   initialize();
31 }
32
33 //==================================================================================================
34 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
35                                            const ModelHighAPI_Selection& theObject1,
36                                            const ModelHighAPI_Selection& theObject2)
37 : ModelHighAPI_Interface(theFeature)
38 {
39   if(initialize()) {
40     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
41     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
42     if(aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::VERTEX) {
43       setByPoints(theObject1, theObject2);
44     } else if(aType1 == GeomAPI_Shape::FACE && aType2 == GeomAPI_Shape::VERTEX) {
45       setByPlaneAndPoint(theObject1, theObject2);
46     } else if(aType1 == GeomAPI_Shape::FACE && aType2 == GeomAPI_Shape::FACE) {
47       setByTwoPlanes(theObject1, theObject2);
48     }
49   }
50 }
51
52 //==================================================================================================
53 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
54                                            const ModelHighAPI_Selection& theObject)
55 : ModelHighAPI_Interface(theFeature)
56 {
57   if(initialize()) {
58     GeomAPI_Shape::ShapeType aType = getShapeType(theObject);
59     if(aType == GeomAPI_Shape::EDGE) {
60       setByLine(theObject);
61     } else if(aType == GeomAPI_Shape::FACE) {
62       setByCylindricalFace(theObject);
63     }
64   }
65 }
66
67 //==================================================================================================
68 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
69                                            const ModelHighAPI_Selection& thePoint,
70                                            const ModelHighAPI_Double& theX,
71                                            const ModelHighAPI_Double& theY,
72                                            const ModelHighAPI_Double& theZ)
73 : ModelHighAPI_Interface(theFeature)
74 {
75   if(initialize()) {
76     setByPointAndDirection(thePoint, theX, theY, theZ);
77   }
78 }
79
80 //==================================================================================================
81 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
82                                            const ModelHighAPI_Double& theDX,
83                                            const ModelHighAPI_Double& theDY,
84                                            const ModelHighAPI_Double& theDZ)
85 : ModelHighAPI_Interface(theFeature)
86 {
87   if(initialize()) {
88     setByDimensions(theDX, theDY, theDZ);
89   }
90 }
91
92 //==================================================================================================
93 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
94                                            const ModelHighAPI_Selection& thePlane1,
95                                            const ModelHighAPI_Double& theOffset1,
96                                            const bool theReverseOffset1,
97                                            const ModelHighAPI_Selection& thePlane2,
98                                            const ModelHighAPI_Double& theOffset2,
99                                            const bool theReverseOffset2)
100 : ModelHighAPI_Interface(theFeature)
101 {
102   if(initialize()) {
103     setByTwoPlanes(thePlane1, theOffset1, theReverseOffset1,
104                    thePlane2, theOffset2, theReverseOffset2);
105   }
106 }
107
108 //==================================================================================================
109 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
110                                            const ModelHighAPI_Selection& thePlane1,
111                                            const ModelHighAPI_Selection& thePlane2,
112                                            const ModelHighAPI_Double& theOffset2,
113                                            const bool theReverseOffset2)
114 : ModelHighAPI_Interface(theFeature)
115 {
116   if(initialize()) {
117     setByTwoPlanes(thePlane1, thePlane2, theOffset2, theReverseOffset2);
118   }
119 }
120
121 //==================================================================================================
122 ConstructionAPI_Axis::ConstructionAPI_Axis(const std::shared_ptr<ModelAPI_Feature>& theFeature,
123                                            const ModelHighAPI_Selection& thePlane1,
124                                            const ModelHighAPI_Double& theOffset1,
125                                            const bool theReverseOffset1,
126                                            const ModelHighAPI_Selection& thePlane2)
127 : ModelHighAPI_Interface(theFeature)
128 {
129   if(initialize()) {
130     setByTwoPlanes(thePlane1, theOffset1, theReverseOffset1, thePlane2);
131   }
132 }
133
134 //==================================================================================================
135 ConstructionAPI_Axis::~ConstructionAPI_Axis()
136 {
137 }
138
139 //==================================================================================================
140 void ConstructionAPI_Axis::setByPoints(const ModelHighAPI_Selection& thePoint1,
141                                        const ModelHighAPI_Selection& thePoint2)
142 {
143   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_POINTS(), creationMethod());
144   fillAttribute(thePoint1, firstPoint());
145   fillAttribute(thePoint2, secondPoint());
146
147   execute();
148 }
149
150 //==================================================================================================
151 void ConstructionAPI_Axis::setByCylindricalFace(const ModelHighAPI_Selection& theCylindricalFace)
152 {
153   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_CYLINDRICAL_FACE(), creationMethod());
154   fillAttribute(theCylindricalFace, cylindricalFace());
155
156   execute();
157 }
158
159 //==================================================================================================
160 void ConstructionAPI_Axis::setByPointAndDirection(const ModelHighAPI_Selection& thePoint,
161                                                   const ModelHighAPI_Double& theX,
162                                                   const ModelHighAPI_Double& theY,
163                                                   const ModelHighAPI_Double& theZ)
164 {
165   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_POINT_AND_DIRECTION(),
166                 creationMethod());
167   fillAttribute(thePoint, firstPoint());
168   fillAttribute(theX, xDirection());
169   fillAttribute(theY, yDirection());
170   fillAttribute(theZ, zDirection());
171
172   execute();
173 }
174
175 //==================================================================================================
176 void ConstructionAPI_Axis::setByDimensions(const ModelHighAPI_Double& theDX,
177                                            const ModelHighAPI_Double& theDY,
178                                            const ModelHighAPI_Double& theDZ)
179 {
180   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_DIMENSIONS(), creationMethod());
181   fillAttribute(theDX, xDimension());
182   fillAttribute(theDY, yDimension());
183   fillAttribute(theDZ, zDimension());
184
185   execute();
186 }
187
188 //==================================================================================================
189 void ConstructionAPI_Axis::setByLine(const ModelHighAPI_Selection& theLine)
190 {
191   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_LINE(), creationMethod());
192   fillAttribute(theLine, line());
193
194   execute();
195 }
196
197 //==================================================================================================
198 void ConstructionAPI_Axis::setByPlaneAndPoint(const ModelHighAPI_Selection& thePlane,
199                                               const ModelHighAPI_Selection& thePoint)
200 {
201   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_PLANE_AND_POINT(), creationMethod());
202   fillAttribute(thePlane, plane());
203   fillAttribute(thePoint, point());
204
205   execute();
206 }
207
208 //==================================================================================================
209 void ConstructionAPI_Axis::setByTwoPlanes(const ModelHighAPI_Selection& thePlane1,
210                                           const ModelHighAPI_Selection& thePlane2)
211 {
212   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_PLANES(), creationMethod());
213   fillAttribute(thePlane1, plane1());
214   fillAttribute("", useOffset1());
215   fillAttribute(thePlane2, plane2());
216   fillAttribute("", useOffset2());
217
218   execute();
219 }
220
221 //==================================================================================================
222 void ConstructionAPI_Axis::setByTwoPlanes(const ModelHighAPI_Selection& thePlane1,
223                                           const ModelHighAPI_Double& theOffset1,
224                                           const bool theReverseOffset1,
225                                           const ModelHighAPI_Selection& thePlane2,
226                                           const ModelHighAPI_Double& theOffset2,
227                                           const bool theReverseOffset2)
228 {
229   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_PLANES(), creationMethod());
230   fillAttribute(thePlane1, plane1());
231   fillAttribute(ConstructionPlugin_Axis::USE_OFFSET1(), useOffset1());
232   fillAttribute(theOffset1, offset1());
233   fillAttribute(theReverseOffset1, reverseOffset1());
234   fillAttribute(thePlane2, plane2());
235   fillAttribute(ConstructionPlugin_Axis::USE_OFFSET2(), useOffset2());
236   fillAttribute(theOffset2, offset2());
237   fillAttribute(theReverseOffset2, reverseOffset2());
238
239   execute();
240 }
241
242 //==================================================================================================
243 void ConstructionAPI_Axis::setByTwoPlanes(const ModelHighAPI_Selection& thePlane1,
244                                           const ModelHighAPI_Selection& thePlane2,
245                                           const ModelHighAPI_Double& theOffset2,
246                                           const bool theReverseOffset2)
247 {
248   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_PLANES(), creationMethod());
249   fillAttribute(thePlane1, plane1());
250   fillAttribute("", useOffset1());
251   fillAttribute(thePlane2, plane2());
252   fillAttribute(ConstructionPlugin_Axis::USE_OFFSET2(), useOffset2());
253   fillAttribute(theOffset2, offset2());
254   fillAttribute(theReverseOffset2, reverseOffset2());
255
256   execute();
257 }
258
259 //==================================================================================================
260 void ConstructionAPI_Axis::setByTwoPlanes(const ModelHighAPI_Selection& thePlane1,
261                                           const ModelHighAPI_Double& theOffset1,
262                                           const bool theReverseOffset1,
263                                           const ModelHighAPI_Selection& thePlane2)
264 {
265   fillAttribute(ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_PLANES(), creationMethod());
266   fillAttribute(thePlane1, plane1());
267   fillAttribute(ConstructionPlugin_Axis::USE_OFFSET1(), useOffset1());
268   fillAttribute(theOffset1, offset1());
269   fillAttribute(theReverseOffset1, reverseOffset1());
270   fillAttribute(thePlane2, plane2());
271   fillAttribute("", useOffset2());
272
273   execute();
274 }
275
276 //==================================================================================================
277 void ConstructionAPI_Axis::dump(ModelHighAPI_Dumper& theDumper) const
278 {
279   FeaturePtr aBase = feature();
280   const std::string& aDocName = theDumper.name(aBase->document());
281
282   theDumper << aBase << " = model.addAxis(" << aDocName;
283
284   std::string aCreationMethod = aBase->string(ConstructionPlugin_Axis::METHOD())->value();
285
286   if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_DIMENSIONS()) {
287     AttributeDoublePtr anAttrDX = aBase->real(ConstructionPlugin_Axis::DX());
288     AttributeDoublePtr anAttrDY = aBase->real(ConstructionPlugin_Axis::DY());
289     AttributeDoublePtr anAttrDZ = aBase->real(ConstructionPlugin_Axis::DZ());
290
291     theDumper << ", " << anAttrDX << ", " << anAttrDY << ", " << anAttrDZ;
292   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_POINTS()) {
293     AttributeSelectionPtr anAttrFirstPnt =
294       aBase->selection(ConstructionPlugin_Axis::POINT_FIRST());
295     AttributeSelectionPtr anAttrSecondPnt =
296       aBase->selection(ConstructionPlugin_Axis::POINT_SECOND());
297
298     theDumper << ", " << anAttrFirstPnt << ", " << anAttrSecondPnt;
299   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_LINE()) {
300     AttributeSelectionPtr anAttrLine = aBase->selection(ConstructionPlugin_Axis::LINE());
301
302     theDumper << ", " << anAttrLine;
303   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_CYLINDRICAL_FACE()) {
304     AttributeSelectionPtr anAttrFace =
305       aBase->selection(ConstructionPlugin_Axis::CYLINDRICAL_FACE());
306
307     theDumper << ", " << anAttrFace;
308   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_PLANE_AND_POINT()) {
309     AttributeSelectionPtr anAttrPlane = aBase->selection(ConstructionPlugin_Axis::PLANE());
310     AttributeSelectionPtr anAttrPoint = aBase->selection(ConstructionPlugin_Axis::POINT());
311
312     theDumper << ", " << anAttrPlane << ", " << anAttrPoint;
313   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_TWO_PLANES()) {
314     AttributeSelectionPtr anAttrPlane1 = aBase->selection(ConstructionPlugin_Axis::PLANE1());
315     AttributeSelectionPtr anAttrPlane2 = aBase->selection(ConstructionPlugin_Axis::PLANE2());
316
317     theDumper << ", " << anAttrPlane1;
318     if(aBase->string(ConstructionPlugin_Axis::USE_OFFSET1())->isInitialized()
319       && !aBase->string(ConstructionPlugin_Axis::USE_OFFSET1())->value().empty()) {
320       AttributeDoublePtr anAttrOffset1 = aBase->real(ConstructionPlugin_Axis::OFFSET1());
321       AttributeBooleanPtr anAttrReverseOffset1 =
322         aBase->boolean(ConstructionPlugin_Axis::REVERSE_OFFSET1());
323       theDumper << ", " << anAttrOffset1 << ", " << anAttrReverseOffset1;
324     }
325
326     theDumper << ", " << anAttrPlane2;
327     if(aBase->string(ConstructionPlugin_Axis::USE_OFFSET2())->isInitialized()
328       && !aBase->string(ConstructionPlugin_Axis::USE_OFFSET2())->value().empty()) {
329       AttributeDoublePtr anAttrOffset2 = aBase->real(ConstructionPlugin_Axis::OFFSET2());
330       AttributeBooleanPtr anAttrReverseOffset2 =
331         aBase->boolean(ConstructionPlugin_Axis::REVERSE_OFFSET2());
332       theDumper << ", " << anAttrOffset2 << ", " << anAttrReverseOffset2;
333     }
334   } else if(aCreationMethod == ConstructionPlugin_Axis::CREATION_METHOD_BY_POINT_AND_DIRECTION()) {
335     AttributeSelectionPtr anAttrFirstPnt = aBase->selection(ConstructionPlugin_Axis::POINT_FIRST());
336     AttributeDoublePtr anAttrX = aBase->real(ConstructionPlugin_Axis::X_DIRECTION());
337     AttributeDoublePtr anAttrY = aBase->real(ConstructionPlugin_Axis::Y_DIRECTION());
338     AttributeDoublePtr anAttrZ = aBase->real(ConstructionPlugin_Axis::Z_DIRECTION());
339
340     theDumper << ", " << anAttrFirstPnt << ", " << anAttrX << ", " << anAttrY << ", " << anAttrZ;
341   }
342
343   theDumper << ")" << std::endl;
344 }
345
346 //==================================================================================================
347 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
348                 const ModelHighAPI_Selection& theObject1,
349                 const ModelHighAPI_Selection& theObject2)
350 {
351   // TODO(spo): check that thePart is not empty
352   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
353   return AxisPtr(new ConstructionAPI_Axis(aFeature, theObject1, theObject2));
354 }
355
356 //==================================================================================================
357 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
358                 const ModelHighAPI_Selection& theObject)
359 {
360   // TODO(spo): check that thePart is not empty
361   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
362   return AxisPtr(new ConstructionAPI_Axis(aFeature, theObject));
363 }
364
365 //==================================================================================================
366 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
367                 const ModelHighAPI_Selection& thePoint,
368                 const ModelHighAPI_Double& theX,
369                 const ModelHighAPI_Double& theY,
370                 const ModelHighAPI_Double& theZ)
371 {
372   // TODO(spo): check that thePart is not empty
373   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
374   return AxisPtr(new ConstructionAPI_Axis(aFeature, thePoint, theX, theY, theZ));
375 }
376
377 //==================================================================================================
378 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
379                 const ModelHighAPI_Double& theDX,
380                 const ModelHighAPI_Double& theDY,
381                 const ModelHighAPI_Double& theDZ)
382 {
383   // TODO(spo): check that thePart is not empty
384   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
385   return AxisPtr(new ConstructionAPI_Axis(aFeature, theDX, theDY, theDZ));
386 }
387
388 //==================================================================================================
389 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
390                 const ModelHighAPI_Selection& thePlane1,
391                 const ModelHighAPI_Double& theOffset1,
392                 const bool theReverseOffset1,
393                 const ModelHighAPI_Selection& thePlane2,
394                 const ModelHighAPI_Double& theOffset2,
395                 const bool theReverseOffset2)
396 {
397   // TODO(spo): check that thePart is not empty
398   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
399   return AxisPtr(new ConstructionAPI_Axis(aFeature, thePlane1, theOffset1, theReverseOffset1,
400                                                     thePlane2, theOffset2, theReverseOffset2));
401 }
402
403 //==================================================================================================
404 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
405                 const ModelHighAPI_Selection& thePlane1,
406                 const ModelHighAPI_Selection& thePlane2,
407                 const ModelHighAPI_Double& theOffset2,
408                 const bool theReverseOffset2)
409 {
410   // TODO(spo): check that thePart is not empty
411   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
412   return AxisPtr(new ConstructionAPI_Axis(aFeature, thePlane1,
413                                                     thePlane2, theOffset2, theReverseOffset2));
414 }
415
416 //==================================================================================================
417 AxisPtr addAxis(const std::shared_ptr<ModelAPI_Document>& thePart,
418                 const ModelHighAPI_Selection& thePlane1,
419                 const ModelHighAPI_Double& theOffset1,
420                 const bool theReverseOffset1,
421                 const ModelHighAPI_Selection& thePlane2)
422 {
423   // TODO(spo): check that thePart is not empty
424   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Axis::ID());
425   return AxisPtr(new ConstructionAPI_Axis(aFeature, thePlane1, theOffset1, theReverseOffset1,
426                                                     thePlane2));
427 }