Salome HOME
a3b9c4d4c07bb0708f2f8741fb752b100657b54e
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Plane.cpp
1 // Copyright (C) 2014-2023  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "ConstructionAPI_Plane.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 //==================================================================================================
26 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature)
27 : ModelHighAPI_Interface(theFeature)
28 {
29   initialize();
30 }
31
32 //==================================================================================================
33 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
34                                              const ModelHighAPI_Selection& theFace,
35                                              const ModelHighAPI_Double& theDistance,
36                                              const bool theIsReverse,
37                                              const ModelHighAPI_Integer& theNbCopy)
38 : ModelHighAPI_Interface(theFeature)
39 {
40   if(initialize()) {
41     setByFaceAndDistance(theFace, theDistance, theIsReverse, theNbCopy);
42   }
43 }
44
45 //==================================================================================================
46 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
47                                              const ModelHighAPI_Double& theA,
48                                              const ModelHighAPI_Double& theB,
49                                              const ModelHighAPI_Double& theC,
50                                              const ModelHighAPI_Double& theD)
51 : ModelHighAPI_Interface(theFeature)
52 {
53   if(initialize()) {
54     setByGeneralEquation(theA, theB, theC, theD);
55   }
56 }
57
58 //==================================================================================================
59 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
60                                              const ModelHighAPI_Selection& thePoint1,
61                                              const ModelHighAPI_Selection& thePoint2,
62                                              const ModelHighAPI_Selection& thePoint3)
63 : ModelHighAPI_Interface(theFeature)
64 {
65   if(initialize()) {
66     setByThreePoints(thePoint1, thePoint2, thePoint3);
67   }
68 }
69
70 //==================================================================================================
71 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
72                                              const ModelHighAPI_Selection& theLine,
73                                              const ModelHighAPI_Selection& thePoint,
74                                              const bool theIsPerpendicular)
75 : ModelHighAPI_Interface(theFeature)
76 {
77   if(initialize()) {
78     setByLineAndPoint(theLine, thePoint, theIsPerpendicular);
79   }
80 }
81
82 //==================================================================================================
83 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
84                                              const ModelHighAPI_Selection& theObject1,
85                                              const ModelHighAPI_Selection& theObject2)
86 : ModelHighAPI_Interface(theFeature)
87 {
88   if(initialize()) {
89     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
90     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
91     if(aType1 == GeomAPI_Shape::FACE && aType2 == GeomAPI_Shape::VERTEX) {
92       setByCoincidentToPoint(theObject1, theObject2);
93     } else if(aType1 == GeomAPI_Shape::FACE && aType2 == GeomAPI_Shape::FACE) {
94       setByTwoParallelPlanes(theObject1, theObject2);
95     }
96   }
97 }
98
99 //==================================================================================================
100 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
101                                              const ModelHighAPI_Selection& thePlane,
102                                              const ModelHighAPI_Selection& theAxis,
103                                              const ModelHighAPI_Double& theAngle,
104                                              const ModelHighAPI_Integer& theNbCopy)
105 : ModelHighAPI_Interface(theFeature)
106 {
107   if(initialize()) {
108     setByRotation(thePlane, theAxis, theAngle, theNbCopy);
109   }
110 }
111
112 //==================================================================================================
113 ConstructionAPI_Plane::~ConstructionAPI_Plane()
114 {
115 }
116
117 //==================================================================================================
118 void ConstructionAPI_Plane::setByFaceAndDistance(const ModelHighAPI_Selection& theFace,
119                                                  const ModelHighAPI_Double& theDistance,
120                                                  const bool theIsReverse,
121                                                  const ModelHighAPI_Integer& theNbCopy)
122 {
123   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
124   fillAttribute(theFace, myplane);
125   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_DISTANCE_FROM_OTHER(),
126                 mycreationMethodByOtherPlane);
127   fillAttribute(theDistance, mydistance);
128   fillAttribute(theIsReverse, myreverse);
129   fillAttribute(theNbCopy, mynbcopy);
130
131   execute();
132 }
133
134 //==================================================================================================
135 void ConstructionAPI_Plane::setByGeneralEquation(const ModelHighAPI_Double& theA,
136                                                  const ModelHighAPI_Double& theB,
137                                                  const ModelHighAPI_Double& theC,
138                                                  const ModelHighAPI_Double& theD)
139 {
140   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_GENERAL_EQUATION(), mycreationMethod);
141   fillAttribute(theA, myA);
142   fillAttribute(theB, myB);
143   fillAttribute(theC, myC);
144   fillAttribute(theD, myD);
145
146   execute();
147 }
148
149 //==================================================================================================
150 void ConstructionAPI_Plane::setByThreePoints(const ModelHighAPI_Selection& thePoint1,
151                                              const ModelHighAPI_Selection& thePoint2,
152                                              const ModelHighAPI_Selection& thePoint3)
153 {
154   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_THREE_POINTS(), mycreationMethod);
155   fillAttribute(thePoint1, mypoint1);
156   fillAttribute(thePoint2, mypoint2);
157   fillAttribute(thePoint3, mypoint3);
158
159   execute();
160 }
161
162 //==================================================================================================
163 void ConstructionAPI_Plane::setByLineAndPoint(const ModelHighAPI_Selection& theLine,
164                                               const ModelHighAPI_Selection& thePoint,
165                                               const bool theIsPerpendicular)
166 {
167   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_LINE_AND_POINT(), mycreationMethod);
168   fillAttribute(theLine, myline);
169   fillAttribute(thePoint, mypoint);
170   fillAttribute(theIsPerpendicular, myperpendicular);
171
172   execute();
173 }
174
175 //==================================================================================================
176 void ConstructionAPI_Plane::setByTwoParallelPlanes(const ModelHighAPI_Selection& thePlane1,
177                                                    const ModelHighAPI_Selection& thePlane2)
178 {
179   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_TWO_PARALLEL_PLANES(),
180                 mycreationMethod);
181   fillAttribute(thePlane1, myplane1);
182   fillAttribute(thePlane2, myplane2);
183
184   execute();
185 }
186
187 //==================================================================================================
188 void ConstructionAPI_Plane::setByCoincidentToPoint(const ModelHighAPI_Selection& thePlane,
189                                                    const ModelHighAPI_Selection& thePoint)
190 {
191   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
192   fillAttribute(thePlane, myplane);
193   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_COINCIDENT_TO_POINT(),
194                 mycreationMethodByOtherPlane);
195   fillAttribute(thePoint, mycoincidentPoint);
196
197   execute();
198 }
199
200 //==================================================================================================
201 void ConstructionAPI_Plane::setByRotation(const ModelHighAPI_Selection& thePlane,
202                                           const ModelHighAPI_Selection& theAxis,
203                                           const ModelHighAPI_Double& theAngle,
204                                           const ModelHighAPI_Integer& theNbCopy)
205 {
206   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
207   fillAttribute(thePlane, myplane);
208   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_ROTATION(),
209                 mycreationMethodByOtherPlane);
210   fillAttribute(theAxis, myaxis);
211   fillAttribute(theAngle, myangle);
212   fillAttribute(theNbCopy, mynbcopy);
213
214   execute();
215 }
216
217 //==================================================================================================
218 void ConstructionAPI_Plane::dump(ModelHighAPI_Dumper& theDumper) const
219 {
220   FeaturePtr aBase = feature();
221   const std::string& aDocName = theDumper.name(aBase->document());
222
223   theDumper << aBase << " = model.addPlane(" << aDocName;
224
225   std::string aCreationMethod =
226     aBase->string(ConstructionPlugin_Plane::CREATION_METHOD())->value();
227
228   if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_GENERAL_EQUATION()) {
229     AttributeDoublePtr anAttrA = aBase->real(ConstructionPlugin_Plane::A());
230     AttributeDoublePtr anAttrB = aBase->real(ConstructionPlugin_Plane::B());
231     AttributeDoublePtr anAttrC = aBase->real(ConstructionPlugin_Plane::C());
232     AttributeDoublePtr anAttrD = aBase->real(ConstructionPlugin_Plane::D());
233
234     theDumper << ", " << anAttrA << ", " << anAttrB << ", " << anAttrC << ", " << anAttrD;
235   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_THREE_POINTS()) {
236     AttributeSelectionPtr anAttrPnt1 = aBase->selection(ConstructionPlugin_Plane::POINT1());
237     AttributeSelectionPtr anAttrPnt2 = aBase->selection(ConstructionPlugin_Plane::POINT2());
238     AttributeSelectionPtr anAttrPnt3 = aBase->selection(ConstructionPlugin_Plane::POINT3());
239
240     theDumper << ", " << anAttrPnt1 << ", " << anAttrPnt2 << ", " << anAttrPnt3;
241   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_LINE_AND_POINT()) {
242     AttributeSelectionPtr anAttrLine = aBase->selection(ConstructionPlugin_Plane::LINE());
243     AttributeSelectionPtr anAttrPoint = aBase->selection(ConstructionPlugin_Plane::POINT());
244     AttributeBooleanPtr anAttrPerpendicular =
245       aBase->boolean(ConstructionPlugin_Plane::PERPENDICULAR());
246
247     theDumper << ", " << anAttrLine << ", " << anAttrPoint << ", " << anAttrPerpendicular;
248   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE()) {
249     AttributeSelectionPtr anAttrPlane = aBase->selection(ConstructionPlugin_Plane::PLANE());
250
251     std::string aCreationMethodOption =
252         aBase->string(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
253     if(aCreationMethodOption ==
254        ConstructionPlugin_Plane::CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
255       AttributeDoublePtr anAttrDistance = aBase->real(ConstructionPlugin_Plane::DISTANCE());
256       AttributeBooleanPtr anAttrReverse = aBase->boolean(ConstructionPlugin_Plane::REVERSE());
257       AttributeIntegerPtr anAttrNbCopy = aBase->integer(ConstructionPlugin_Plane::NB_COPIES());
258
259       theDumper << ", " << anAttrPlane << ", " << anAttrDistance << ", " << anAttrReverse;
260       if(anAttrNbCopy.get() && anAttrNbCopy->value() > 1)
261         theDumper << ", " << anAttrNbCopy;
262     } else if(aCreationMethodOption ==
263               ConstructionPlugin_Plane::CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
264       AttributeSelectionPtr anAttrPoint =
265         aBase->selection(ConstructionPlugin_Plane::COINCIDENT_POINT());
266
267       theDumper << ", " << anAttrPlane << ", " << anAttrPoint;
268     } else if(aCreationMethodOption == ConstructionPlugin_Plane::CREATION_METHOD_BY_ROTATION()) {
269       AttributeSelectionPtr anAttrAxis = aBase->selection(ConstructionPlugin_Plane::AXIS());
270       AttributeDoublePtr anAttrAngle = aBase->real(ConstructionPlugin_Plane::ANGLE());
271       AttributeIntegerPtr anAttrNbCopy = aBase->integer(ConstructionPlugin_Plane::NB_COPIES());
272
273       theDumper << ", " << anAttrPlane << ", " << anAttrAxis << ", " << anAttrAngle;
274       if (anAttrNbCopy.get() && anAttrNbCopy->value() > 1)
275          theDumper << ", " << anAttrNbCopy;
276     }
277   } else if(aCreationMethod ==
278             ConstructionPlugin_Plane::CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
279     AttributeSelectionPtr anAttrPlane1 = aBase->selection(ConstructionPlugin_Plane::PLANE1());
280     AttributeSelectionPtr anAttrPlane2 = aBase->selection(ConstructionPlugin_Plane::PLANE2());
281
282     theDumper << ", " << anAttrPlane1 << ", " << anAttrPlane2;
283   }
284
285   theDumper << ")" << std::endl;
286 }
287
288 //==================================================================================================
289 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
290                   const ModelHighAPI_Selection& theFace,
291                   const ModelHighAPI_Double& theDistance,
292                   const bool theIsReverse,
293                   const ModelHighAPI_Integer& theNbCopies)
294 {
295   // TODO(spo): check that thePart is not empty
296   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
297   return PlanePtr(new ConstructionAPI_Plane(aFeature, theFace, theDistance,
298                                             theIsReverse, theNbCopies));
299 }
300
301 //==================================================================================================
302 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
303                   const ModelHighAPI_Double& theA,
304                   const ModelHighAPI_Double& theB,
305                   const ModelHighAPI_Double& theC,
306                   const ModelHighAPI_Double& theD)
307 {
308   // TODO(spo): check that thePart is not empty
309   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
310   return PlanePtr(new ConstructionAPI_Plane(aFeature, theA, theB, theC, theD));
311 }
312
313 //==================================================================================================
314 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
315                   const ModelHighAPI_Selection& thePoint1,
316                   const ModelHighAPI_Selection& thePoint2,
317                   const ModelHighAPI_Selection& thePoint3)
318 {
319   // TODO(spo): check that thePart is not empty
320   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
321   return PlanePtr(new ConstructionAPI_Plane(aFeature, thePoint1, thePoint2, thePoint3));
322 }
323
324 //==================================================================================================
325 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
326                   const ModelHighAPI_Selection& theLine,
327                   const ModelHighAPI_Selection& thePoint,
328                   const bool theIsPerpendicular)
329 {
330   // TODO(spo): check that thePart is not empty
331   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
332   return PlanePtr(new ConstructionAPI_Plane(aFeature, theLine, thePoint, theIsPerpendicular));
333 }
334
335 //==================================================================================================
336 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
337                   const ModelHighAPI_Selection& theObject1,
338                   const ModelHighAPI_Selection& theObject2)
339 {
340   // TODO(spo): check that thePart is not empty
341   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
342   return PlanePtr(new ConstructionAPI_Plane(aFeature, theObject1, theObject2));
343 }
344
345 //==================================================================================================
346 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
347                   const ModelHighAPI_Selection& thePlane,
348                   const ModelHighAPI_Selection& theAxis,
349                   const ModelHighAPI_Double& theAngle,
350                   const ModelHighAPI_Integer& theNbCopies)
351 {
352   // TODO(spo): check that thePart is not empty
353   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
354   return PlanePtr(new ConstructionAPI_Plane(aFeature, thePlane, theAxis, theAngle, theNbCopies));
355 }