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