Salome HOME
Merge branch 'BR_EDF_2018_Lot1'
[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
143   execute(false);
144 }
145
146 //==================================================================================================
147 void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
148                                               const ModelHighAPI_Double& theOffset,
149                                               const bool theUseRatio,
150                                               const bool theReverse)
151 {
152   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
153   fillAttribute(theEdge, myedge);
154   if (theUseRatio) {
155     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
156     fillAttribute(theOffset, myratio);
157   }
158   else {
159     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
160     fillAttribute(theOffset, mydistance);
161   }
162   fillAttribute(theReverse, myreverse);
163
164   execute();
165 }
166
167 //==================================================================================================
168 void ConstructionAPI_Point::setByProjectionOnEdge(const ModelHighAPI_Selection& theVertex,
169                                                   const ModelHighAPI_Selection& theEdge)
170 {
171   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
172                 mycreationMethod);
173   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE(),
174                 myprojectionType);
175   fillAttribute(theVertex, mypoinToProject);
176   fillAttribute(theEdge, myedgeForPointProjection);
177
178   execute();
179 }
180
181 //==================================================================================================
182 void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
183                                                   const ModelHighAPI_Selection& theFace)
184 {
185   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
186                 mycreationMethod);
187   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_FACE(),
188                 myprojectionType);
189   fillAttribute(theVertex, mypoinToProject);
190   fillAttribute(theFace, myfaceForPointProjection);
191
192   execute();
193 }
194
195 //==================================================================================================
196 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
197                                                    const ModelHighAPI_Selection& theEdge2)
198 {
199   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
200   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES(),
201                 myintersectionType);
202   fillAttribute(theEdge1, myintersectionLine1);
203   fillAttribute(theEdge2, myintersectionLine2);
204
205   execute();
206 }
207
208 //==================================================================================================
209 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
210                                                           const ModelHighAPI_Selection& theFace)
211 {
212   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
213   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE(),
214                 myintersectionType);
215   fillAttribute(theEdge, myintersectionLine);
216   fillAttribute(theFace, myintersectionPlane);
217   fillAttribute("", useOffset()); // not used by default
218   execute();
219 }
220
221 //==================================================================================================
222 void ConstructionAPI_Point::setByPlanesIntersection(const ModelHighAPI_Selection& theFace1,
223                                                     const ModelHighAPI_Selection& theFace2,
224                                                     const ModelHighAPI_Selection& theFace3)
225 {
226   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
227   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES(),
228                 myintersectionType);
229   fillAttribute(theFace1, myintersectionPlane1);
230   fillAttribute(theFace2, myintersectionPlane2);
231   fillAttribute(theFace3, myintersectionPlane3);
232
233   execute();
234 }
235
236 //==================================================================================================
237 void ConstructionAPI_Point::setByCenterOfGravity(const ModelHighAPI_Selection& theObject)
238 {
239   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
240                 mycreationMethod);
241   fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY(),
242                 mygeometricalPropertyType);
243   fillAttribute(theObject, myobjectForCenterOfGravity);
244 }
245
246 //==================================================================================================
247 void ConstructionAPI_Point::setByCenterOfCircle(const ModelHighAPI_Selection& theObject)
248 {
249   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY(),
250                 mycreationMethod);
251   fillAttribute(ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_CIRCLE(),
252                 mygeometricalPropertyType);
253   fillAttribute(theObject, myobjectForCenterOfCircle);
254 }
255
256 //==================================================================================================
257 void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
258 {
259   FeaturePtr aBase = feature();
260   const std::string& aDocName = theDumper.name(aBase->document());
261   const std::string aMeth = creationMethod()->value();
262
263   // common part
264   theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
265
266   if (aMeth == "" || // default is XYZ
267       aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
268     theDumper << point() << ")" << std::endl;
269   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION()) {
270     const std::string anIntersectionType = intersectionType()->value();
271     if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES())
272     {
273       theDumper << intersectionLine1() << ", " << intersectionLine2();
274     }
275     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE())
276     {
277       theDumper << intersectionLine() << ", " << intersectionPlane();
278       if (!useOffset()->value().empty()) { // call method with defined offset
279         theDumper << ", " << offset() << ", " << reverseOffset();
280       }
281     }
282     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES())
283     {
284       theDumper << intersectionPlane1() << ", "
285                 << intersectionPlane2() << ", "
286                 << intersectionPlane3();
287     }
288   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
289     theDumper << edge() << ", ";
290     if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
291       theDumper << distance() << ", " << false;
292     }
293     else {
294       theDumper << ratio() << ", " << true;
295     }
296     theDumper << ", " << reverse()->value();
297   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION()) {
298     theDumper << poinToProject() << ", ";
299     if (projectionType()->value() == ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE()) {
300       theDumper << edgeForPointProjection();
301     } else {
302       theDumper << faceForPointProjection();
303     }
304   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_GEOMETRICAL_PROPERTY()) {
305     if (geometricalPropertyType()->value() ==
306           ConstructionPlugin_Point::GEOMETRICAL_PROPERTY_TYPE_BY_CENTER_OF_GRAVITY())
307     {
308       theDumper << objectForCenterOfGravity();
309     }
310     else
311     {
312       theDumper << objectForCenterOfCircle() << ", " << true;
313     }
314   }
315
316   theDumper << ")" << std::endl;
317 }
318
319 //==================================================================================================
320 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
321                   const ModelHighAPI_Double& theX,
322                   const ModelHighAPI_Double& theY,
323                   const ModelHighAPI_Double& theZ)
324 {
325   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
326   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
327 }
328
329 //==================================================================================================
330 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
331                   const ModelHighAPI_Selection& theEdge,
332                   const ModelHighAPI_Double& theOffset,
333                   const bool theUseRatio,
334                   const bool theReverse)
335 {
336   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
337   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
338 }
339
340 //==================================================================================================
341 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
342                   const ModelHighAPI_Selection& theObject1,
343                   const ModelHighAPI_Selection& theObject2)
344 {
345   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
346   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
347 }
348
349 //==================================================================================================
350 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
351                   const ModelHighAPI_Selection& theObject1,
352                   const ModelHighAPI_Selection& theObject2,
353                   const ModelHighAPI_Double& theDistanceValue,
354                   const bool theReverse)
355 {
356   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
357   PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
358
359   fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
360   fillAttribute(theDistanceValue, anAPI->offset());
361   fillAttribute(theReverse, anAPI->reverseOffset());
362
363   return anAPI;
364 }
365
366 //==================================================================================================
367 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
368                   const ModelHighAPI_Selection& theObject1,
369                   const ModelHighAPI_Selection& theObject2,
370                   const ModelHighAPI_Selection& theObject3)
371 {
372   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
373   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2, theObject3));
374 }
375
376 //==================================================================================================
377 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
378                   const ModelHighAPI_Selection& theObject,
379                   const bool theIsCircularEdge)
380 {
381   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
382   return PointPtr(new ConstructionAPI_Point(aFeature, theObject, theIsCircularEdge));
383 }