Salome HOME
Fix the point by XYZ coordinates python export/dump
[modules/shaper.git] / src / ConstructionAPI / ConstructionAPI_Point.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_Point.h"
22
23 #include <GeomAPI_Shape.h>
24
25 #include <ModelHighAPI_Dumper.h>
26 #include <ModelHighAPI_Selection.h>
27 #include <ModelHighAPI_Tools.h>
28
29 //==================================================================================================
30 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature)
31 : ModelHighAPI_Interface(theFeature)
32 {
33   initialize();
34 }
35
36 //==================================================================================================
37 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38                                              const ModelHighAPI_Double& theX,
39                                              const ModelHighAPI_Double& theY,
40                                              const ModelHighAPI_Double& theZ)
41 : ModelHighAPI_Interface(theFeature)
42 {
43   if(initialize()) {
44     setByXYZ(theX, theY, theZ);
45   }
46 }
47
48 //==================================================================================================
49 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
50                                              const ModelHighAPI_Selection& theEdge,
51                                              const ModelHighAPI_Double& theOffset,
52                                              const bool theUseRatio,
53                                              const bool theReverse)
54 : ModelHighAPI_Interface(theFeature)
55 {
56   if(initialize()) {
57     setByOffsetOnEdge(theEdge, theOffset, theUseRatio, theReverse);
58   }
59 }
60
61 //==================================================================================================
62 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
63                                              const ModelHighAPI_Selection& theObject1,
64                                              const ModelHighAPI_Selection& theObject2)
65 : ModelHighAPI_Interface(theFeature)
66 {
67   if(initialize()) {
68     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
69     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
70     if(aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::FACE) {
71       // If first object is vertex and second object is face then set by projection.
72       setByProjectionOnFace(theObject1, theObject2);
73     } else if (aType1 == GeomAPI_Shape::VERTEX && aType2 == GeomAPI_Shape::EDGE) {
74       // If first object is vertex and second object is edge then set by projection.
75       setByProjectionOnEdge(theObject1, theObject2);
76     } else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::EDGE) {
77       // If both objects are edges then set by lines intersection.
78       setByLinesIntersection(theObject1, theObject2);
79     } else if(aType1 == GeomAPI_Shape::EDGE && aType2 == GeomAPI_Shape::FACE) {
80       // If first object is edge and second object is face then set by line and plane intersection.
81       setByLineAndPlaneIntersection(theObject1, theObject2);
82     }
83   }
84 }
85
86 //==================================================================================================
87 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
88                                              const ModelHighAPI_Selection& theObject1,
89                                              const ModelHighAPI_Selection& theObject2,
90                                              const ModelHighAPI_Selection& theObject3)
91 : ModelHighAPI_Interface(theFeature)
92 {
93   if (initialize())
94   {
95     GeomAPI_Shape::ShapeType aType1 = getShapeType(theObject1);
96     GeomAPI_Shape::ShapeType aType2 = getShapeType(theObject2);
97     GeomAPI_Shape::ShapeType aType3 = getShapeType(theObject3);
98     if (aType1 == GeomAPI_Shape::FACE
99         && aType2 == GeomAPI_Shape::FACE
100         && aType3 == GeomAPI_Shape::FACE)
101     {
102       setByPlanesIntersection(theObject1, theObject2, theObject3);
103     }
104   }
105 }
106
107 //==================================================================================================
108 ConstructionAPI_Point::ConstructionAPI_Point(const std::shared_ptr<ModelAPI_Feature>& theFeature,
109                                              const ModelHighAPI_Selection& theObject,
110                                              const bool theIsCircularEdge)
111 : ModelHighAPI_Interface(theFeature)
112 {
113   if (initialize())
114   {
115     if (theIsCircularEdge) {
116       setByCenterOfCircle(theObject);
117     } else {
118       setByCenterOfGravity(theObject);
119     }
120   }
121 }
122
123 //==================================================================================================
124 ConstructionAPI_Point::~ConstructionAPI_Point()
125 {
126
127 }
128
129 //==================================================================================================
130 void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
131                                      const ModelHighAPI_Double& theY,
132                                      const ModelHighAPI_Double& theZ)
133 {
134   //fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
135
136   // TODO: Fill point attribute
137   //fillAttribute(theX, myx);
138   //fillAttribute(theY, myy);
139   //fillAttribute(theZ, myz);
140
141   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
142   fillAttribute(theX, theY, theZ, mypoint);
143
144   execute(false);
145 }
146
147 //==================================================================================================
148 void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
149                                               const ModelHighAPI_Double& theOffset,
150                                               const bool theUseRatio,
151                                               const bool theReverse)
152 {
153   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
154   fillAttribute(theEdge, myedge);
155   if (theUseRatio) {
156     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
157     fillAttribute(theOffset, myratio);
158   }
159   else {
160     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
161     fillAttribute(theOffset, mydistance);
162   }
163   fillAttribute(theReverse, myreverse);
164
165   execute();
166 }
167
168 //==================================================================================================
169 void ConstructionAPI_Point::setByProjectionOnEdge(const ModelHighAPI_Selection& theVertex,
170                                                   const ModelHighAPI_Selection& theEdge)
171 {
172   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
173                 mycreationMethod);
174   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE(),
175                 myprojectionType);
176   fillAttribute(theVertex, mypoinToProject);
177   fillAttribute(theEdge, myedgeForPointProjection);
178
179   execute();
180 }
181
182 //==================================================================================================
183 void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
184                                                   const ModelHighAPI_Selection& theFace)
185 {
186   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
187                 mycreationMethod);
188   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_FACE(),
189                 myprojectionType);
190   fillAttribute(theVertex, mypoinToProject);
191   fillAttribute(theFace, myfaceForPointProjection);
192
193   execute();
194 }
195
196 //==================================================================================================
197 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
198                                                    const ModelHighAPI_Selection& theEdge2)
199 {
200   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
201   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES(),
202                 myintersectionType);
203   fillAttribute(theEdge1, myintersectionLine1);
204   fillAttribute(theEdge2, myintersectionLine2);
205
206   execute();
207 }
208
209 //==================================================================================================
210 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
211                                                           const ModelHighAPI_Selection& theFace)
212 {
213   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
214   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE(),
215                 myintersectionType);
216   fillAttribute(theEdge, myintersectionLine);
217   fillAttribute(theFace, myintersectionPlane);
218   fillAttribute("", useOffset()); // not used by default
219   execute();
220 }
221
222 //==================================================================================================
223 void ConstructionAPI_Point::setByPlanesIntersection(const ModelHighAPI_Selection& theFace1,
224                                                     const ModelHighAPI_Selection& theFace2,
225                                                     const ModelHighAPI_Selection& theFace3)
226 {
227   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
228   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES(),
229                 myintersectionType);
230   fillAttribute(theFace1, myintersectionPlane1);
231   fillAttribute(theFace2, myintersectionPlane2);
232   fillAttribute(theFace3, myintersectionPlane3);
233
234   execute();
235 }
236
237 //==================================================================================================
238 void ConstructionAPI_Point::setByCenterOfGravity(const ModelHighAPI_Selection& theObject)
239 {
240   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
241                 mycreationMethod);
242   fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY(),
243                 mygeometricalPropertyType);
244   fillAttribute(theObject, myobjectForCenterOfGravity);
245 }
246
247 //==================================================================================================
248 void ConstructionAPI_Point::setByCenterOfCircle(const ModelHighAPI_Selection& theObject)
249 {
250   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
251                 mycreationMethod);
252   fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_CIRCLE(),
253                 mygeometricalPropertyType);
254   fillAttribute(theObject, myobjectForCenterOfCircle);
255 }
256
257 //==================================================================================================
258 void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
259 {
260   FeaturePtr aBase = feature();
261   const std::string& aDocName = theDumper.name(aBase->document());
262   const std::string aMeth = creationMethod()->value();
263
264   // common part
265   theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
266
267   if (aMeth == "" || // default is XYZ
268       aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
269     theDumper << point();
270   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION()) {
271     const std::string anIntersectionType = intersectionType()->value();
272     if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES())
273     {
274       theDumper << intersectionLine1() << ", " << intersectionLine2();
275     }
276     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE())
277     {
278       theDumper << intersectionLine() << ", " << intersectionPlane();
279       if (!useOffset()->value().empty()) { // call method with defined offset
280         theDumper << ", " << offset() << ", " << reverseOffset();
281       }
282     }
283     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES())
284     {
285       theDumper << intersectionPlane1() << ", "
286                 << intersectionPlane2() << ", "
287                 << intersectionPlane3();
288     }
289   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
290     theDumper << edge() << ", ";
291     if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
292       theDumper << distance() << ", " << false;
293     }
294     else {
295       theDumper << ratio() << ", " << true;
296     }
297     theDumper << ", " << reverse()->value();
298   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION()) {
299     theDumper << poinToProject() << ", ";
300     if (projectionType()->value() == ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE()) {
301       theDumper << edgeForPointProjection();
302     } else {
303       theDumper << faceForPointProjection();
304     }
305   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
306     if (geometricalPropertyType()->value() ==
307           ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY())
308     {
309       theDumper << objectForCenterOfGravity();
310     }
311     else
312     {
313       theDumper << objectForCenterOfCircle() << ", " << true;
314     }
315   }
316
317   theDumper << ")" << std::endl;
318 }
319
320 //==================================================================================================
321 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
322                   const ModelHighAPI_Double& theX,
323                   const ModelHighAPI_Double& theY,
324                   const ModelHighAPI_Double& theZ)
325 {
326   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
327   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
328 }
329
330 //==================================================================================================
331 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
332                   const ModelHighAPI_Selection& theEdge,
333                   const ModelHighAPI_Double& theOffset,
334                   const bool theUseRatio,
335                   const bool theReverse)
336 {
337   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
338   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
339 }
340
341 //==================================================================================================
342 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
343                   const ModelHighAPI_Selection& theObject1,
344                   const ModelHighAPI_Selection& theObject2)
345 {
346   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
347   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
348 }
349
350 //==================================================================================================
351 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
352                   const ModelHighAPI_Selection& theObject1,
353                   const ModelHighAPI_Selection& theObject2,
354                   const ModelHighAPI_Double& theDistanceValue,
355                   const bool theReverse)
356 {
357   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
358   PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
359
360   fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
361   fillAttribute(theDistanceValue, anAPI->offset());
362   fillAttribute(theReverse, anAPI->reverseOffset());
363
364   return anAPI;
365 }
366
367 //==================================================================================================
368 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
369                   const ModelHighAPI_Selection& theObject1,
370                   const ModelHighAPI_Selection& theObject2,
371                   const ModelHighAPI_Selection& theObject3)
372 {
373   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
374   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2, theObject3));
375 }
376
377 //==================================================================================================
378 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
379                   const ModelHighAPI_Selection& theObject,
380                   const bool theIsCircularEdge)
381 {
382   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
383   return PointPtr(new ConstructionAPI_Point(aFeature, theObject, theIsCircularEdge));
384 }