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