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