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