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