Salome HOME
Some improvements in GUI
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Plane.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_Plane.h"
22
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 //==================================================================================================
27 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature)
28 : ModelHighAPI_Interface(theFeature)
29 {
30   initialize();
31 }
32
33 //==================================================================================================
34 ConstructionAPI_Plane::ConstructionAPI_Plane(const std::shared_ptr<ModelAPI_Feature>& theFeature,
35                                              const ModelHighAPI_Selection& theFace,
36                                              const ModelHighAPI_Double& theDistance,
37                                              const bool theIsReverse)
38 : ModelHighAPI_Interface(theFeature)
39 {
40   if(initialize()) {
41     setByFaceAndDistance(theFace, theDistance, theIsReverse);
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 : ModelHighAPI_Interface(theFeature)
105 {
106   if(initialize()) {
107     setByRotation(thePlane, theAxis, theAngle);
108   }
109 }
110
111 //==================================================================================================
112 ConstructionAPI_Plane::~ConstructionAPI_Plane()
113 {
114 }
115
116 //==================================================================================================
117 void ConstructionAPI_Plane::setByFaceAndDistance(const ModelHighAPI_Selection& theFace,
118                                                  const ModelHighAPI_Double& theDistance,
119                                                  const bool theIsReverse)
120 {
121   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
122   fillAttribute(theFace, myplane);
123   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_DISTANCE_FROM_OTHER(),
124                 mycreationMethodByOtherPlane);
125   fillAttribute(theDistance, mydistance);
126   fillAttribute(theIsReverse, myreverse);
127
128   execute();
129 }
130
131 //==================================================================================================
132 void ConstructionAPI_Plane::setByGeneralEquation(const ModelHighAPI_Double& theA,
133                                                  const ModelHighAPI_Double& theB,
134                                                  const ModelHighAPI_Double& theC,
135                                                  const ModelHighAPI_Double& theD)
136 {
137   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_GENERAL_EQUATION(), mycreationMethod);
138   fillAttribute(theA, myA);
139   fillAttribute(theB, myB);
140   fillAttribute(theC, myC);
141   fillAttribute(theD, myD);
142
143   execute();
144 }
145
146 //==================================================================================================
147 void ConstructionAPI_Plane::setByThreePoints(const ModelHighAPI_Selection& thePoint1,
148                                              const ModelHighAPI_Selection& thePoint2,
149                                              const ModelHighAPI_Selection& thePoint3)
150 {
151   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_THREE_POINTS(), mycreationMethod);
152   fillAttribute(thePoint1, mypoint1);
153   fillAttribute(thePoint2, mypoint2);
154   fillAttribute(thePoint3, mypoint3);
155
156   execute();
157 }
158
159 //==================================================================================================
160 void ConstructionAPI_Plane::setByLineAndPoint(const ModelHighAPI_Selection& theLine,
161                                               const ModelHighAPI_Selection& thePoint,
162                                               const bool theIsPerpendicular)
163 {
164   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_LINE_AND_POINT(), mycreationMethod);
165   fillAttribute(theLine, myline);
166   fillAttribute(thePoint, mypoint);
167   fillAttribute(theIsPerpendicular, myperpendicular);
168
169   execute();
170 }
171
172 //==================================================================================================
173 void ConstructionAPI_Plane::setByTwoParallelPlanes(const ModelHighAPI_Selection& thePlane1,
174                                                    const ModelHighAPI_Selection& thePlane2)
175 {
176   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_TWO_PARALLEL_PLANES(),
177                 mycreationMethod);
178   fillAttribute(thePlane1, myplane1);
179   fillAttribute(thePlane2, myplane2);
180
181   execute();
182 }
183
184 //==================================================================================================
185 void ConstructionAPI_Plane::setByCoincidentToPoint(const ModelHighAPI_Selection& thePlane,
186                                                    const ModelHighAPI_Selection& thePoint)
187 {
188   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
189   fillAttribute(thePlane, myplane);
190   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_COINCIDENT_TO_POINT(),
191                 mycreationMethodByOtherPlane);
192   fillAttribute(thePoint, mycoincidentPoint);
193
194   execute();
195 }
196
197 //==================================================================================================
198 void ConstructionAPI_Plane::setByRotation(const ModelHighAPI_Selection& thePlane,
199                                           const ModelHighAPI_Selection& theAxis,
200                                           const ModelHighAPI_Double& theAngle)
201 {
202   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE(), mycreationMethod);
203   fillAttribute(thePlane, myplane);
204   fillAttribute(ConstructionPlugin_Plane::CREATION_METHOD_BY_ROTATION(),
205                 mycreationMethodByOtherPlane);
206   fillAttribute(theAxis, myaxis);
207   fillAttribute(theAngle, myangle);
208
209   execute();
210 }
211
212 //==================================================================================================
213 void ConstructionAPI_Plane::dump(ModelHighAPI_Dumper& theDumper) const
214 {
215   FeaturePtr aBase = feature();
216   const std::string& aDocName = theDumper.name(aBase->document());
217
218   theDumper << aBase << " = model.addPlane(" << aDocName;
219
220   std::string aCreationMethod =
221     aBase->string(ConstructionPlugin_Plane::CREATION_METHOD())->value();
222
223   if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_GENERAL_EQUATION()) {
224     AttributeDoublePtr anAttrA = aBase->real(ConstructionPlugin_Plane::A());
225     AttributeDoublePtr anAttrB = aBase->real(ConstructionPlugin_Plane::B());
226     AttributeDoublePtr anAttrC = aBase->real(ConstructionPlugin_Plane::C());
227     AttributeDoublePtr anAttrD = aBase->real(ConstructionPlugin_Plane::D());
228
229     theDumper << ", " << anAttrA << ", " << anAttrB << ", " << anAttrC << ", " << anAttrD;
230   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_THREE_POINTS()) {
231     AttributeSelectionPtr anAttrPnt1 = aBase->selection(ConstructionPlugin_Plane::POINT1());
232     AttributeSelectionPtr anAttrPnt2 = aBase->selection(ConstructionPlugin_Plane::POINT2());
233     AttributeSelectionPtr anAttrPnt3 = aBase->selection(ConstructionPlugin_Plane::POINT3());
234
235     theDumper << ", " << anAttrPnt1 << ", " << anAttrPnt2 << ", " << anAttrPnt3;
236   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_LINE_AND_POINT()) {
237     AttributeSelectionPtr anAttrLine = aBase->selection(ConstructionPlugin_Plane::LINE());
238     AttributeSelectionPtr anAttrPoint = aBase->selection(ConstructionPlugin_Plane::POINT());
239     AttributeBooleanPtr anAttrPerpendicular =
240       aBase->boolean(ConstructionPlugin_Plane::PERPENDICULAR());
241
242     theDumper << ", " << anAttrLine << ", " << anAttrPoint << ", " << anAttrPerpendicular;
243   } else if(aCreationMethod == ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE()) {
244     AttributeSelectionPtr anAttrPlane = aBase->selection(ConstructionPlugin_Plane::PLANE());
245
246     std::string aCreationMethodOption =
247         aBase->string(ConstructionPlugin_Plane::CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
248     if(aCreationMethodOption ==
249        ConstructionPlugin_Plane::CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
250       AttributeDoublePtr anAttrDistance = aBase->real(ConstructionPlugin_Plane::DISTANCE());
251       AttributeBooleanPtr anAttrReverse = aBase->boolean(ConstructionPlugin_Plane::REVERSE());
252
253       theDumper << ", " << anAttrPlane << ", " << anAttrDistance << ", " << anAttrReverse;
254     } else if(aCreationMethodOption ==
255               ConstructionPlugin_Plane::CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
256       AttributeSelectionPtr anAttrPoint =
257         aBase->selection(ConstructionPlugin_Plane::COINCIDENT_POINT());
258
259       theDumper << ", " << anAttrPlane << ", " << anAttrPoint;
260     } else if(aCreationMethodOption == ConstructionPlugin_Plane::CREATION_METHOD_BY_ROTATION()) {
261       AttributeSelectionPtr anAttrAxis = aBase->selection(ConstructionPlugin_Plane::AXIS());
262       AttributeDoublePtr anAttrAngle = aBase->real(ConstructionPlugin_Plane::ANGLE());
263
264       theDumper << ", " << anAttrPlane << ", " << anAttrAxis << ", " << anAttrAngle;
265     }
266   } else if(aCreationMethod ==
267             ConstructionPlugin_Plane::CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
268     AttributeSelectionPtr anAttrPlane1 = aBase->selection(ConstructionPlugin_Plane::PLANE1());
269     AttributeSelectionPtr anAttrPlane2 = aBase->selection(ConstructionPlugin_Plane::PLANE2());
270
271     theDumper << ", " << anAttrPlane1 << ", " << anAttrPlane2;
272   }
273
274   theDumper << ")" << std::endl;
275 }
276
277 //==================================================================================================
278 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
279                   const ModelHighAPI_Selection& theFace,
280                   const ModelHighAPI_Double& theDistance,
281                   const bool theIsReverse)
282 {
283   // TODO(spo): check that thePart is not empty
284   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
285   return PlanePtr(new ConstructionAPI_Plane(aFeature, theFace, theDistance, theIsReverse));
286 }
287
288 //==================================================================================================
289 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
290                   const ModelHighAPI_Double& theA,
291                   const ModelHighAPI_Double& theB,
292                   const ModelHighAPI_Double& theC,
293                   const ModelHighAPI_Double& theD)
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, theA, theB, theC, theD));
298 }
299
300 //==================================================================================================
301 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
302                   const ModelHighAPI_Selection& thePoint1,
303                   const ModelHighAPI_Selection& thePoint2,
304                   const ModelHighAPI_Selection& thePoint3)
305 {
306   // TODO(spo): check that thePart is not empty
307   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
308   return PlanePtr(new ConstructionAPI_Plane(aFeature, thePoint1, thePoint2, thePoint3));
309 }
310
311 //==================================================================================================
312 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
313                   const ModelHighAPI_Selection& theLine,
314                   const ModelHighAPI_Selection& thePoint,
315                   const bool theIsPerpendicular)
316 {
317   // TODO(spo): check that thePart is not empty
318   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
319   return PlanePtr(new ConstructionAPI_Plane(aFeature, theLine, thePoint, theIsPerpendicular));
320 }
321
322 //==================================================================================================
323 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
324                   const ModelHighAPI_Selection& theObject1,
325                   const ModelHighAPI_Selection& theObject2)
326 {
327   // TODO(spo): check that thePart is not empty
328   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
329   return PlanePtr(new ConstructionAPI_Plane(aFeature, theObject1, theObject2));
330 }
331
332 //==================================================================================================
333 PlanePtr addPlane(const std::shared_ptr<ModelAPI_Document>& thePart,
334                   const ModelHighAPI_Selection& thePlane,
335                   const ModelHighAPI_Selection& theAxis,
336                   const ModelHighAPI_Double& theAngle)
337 {
338   // TODO(spo): check that thePart is not empty
339   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Plane::ID());
340   return PlanePtr(new ConstructionAPI_Plane(aFeature, thePlane, theAxis, theAngle));
341 }