Salome HOME
2.3.4 Point creation: by intersection
[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()
109 {
110
111 }
112
113 //==================================================================================================
114 void ConstructionAPI_Point::setByXYZ(const ModelHighAPI_Double& theX,
115                                      const ModelHighAPI_Double& theY,
116                                      const ModelHighAPI_Double& theZ)
117 {
118   //fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
119   fillAttribute(theX, myx);
120   fillAttribute(theY, myy);
121   fillAttribute(theZ, myz);
122   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ(), mycreationMethod);
123
124   execute(false);
125 }
126
127 //==================================================================================================
128 void ConstructionAPI_Point::setByOffsetOnEdge(const ModelHighAPI_Selection& theEdge,
129                                               const ModelHighAPI_Double& theOffset,
130                                               const bool theUseRatio,
131                                               const bool theReverse)
132 {
133   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE(), mycreationMethod);
134   fillAttribute(theEdge, myedge);
135   if (theUseRatio) {
136     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_RATIO(), myoffsetType);
137     fillAttribute(theOffset, myratio);
138   }
139   else {
140     fillAttribute(ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE(), myoffsetType);
141     fillAttribute(theOffset, mydistance);
142   }
143   fillAttribute(theReverse, myreverse);
144
145   execute();
146 }
147
148 //==================================================================================================
149 void ConstructionAPI_Point::setByProjectionOnEdge(const ModelHighAPI_Selection& theVertex,
150                                                   const ModelHighAPI_Selection& theEdge)
151 {
152   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
153                 mycreationMethod);
154   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE(),
155                 myprojectionType);
156   fillAttribute(theVertex, mypoinToProject);
157   fillAttribute(theEdge, myedgeForPointProjection);
158
159   execute();
160 }
161
162 //==================================================================================================
163 void ConstructionAPI_Point::setByProjectionOnFace(const ModelHighAPI_Selection& theVertex,
164                                                   const ModelHighAPI_Selection& theFace)
165 {
166   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION(),
167                 mycreationMethod);
168   fillAttribute(ConstructionPlugin_Point::PROJECTION_TYPE_ON_FACE(),
169                 myprojectionType);
170   fillAttribute(theVertex, mypoinToProject);
171   fillAttribute(theFace, myfaceForPointProjection);
172
173   execute();
174 }
175
176 //==================================================================================================
177 void ConstructionAPI_Point::setByLinesIntersection(const ModelHighAPI_Selection& theEdge1,
178                                                    const ModelHighAPI_Selection& theEdge2)
179 {
180   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
181   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES(),
182                 myintersectionType);
183   fillAttribute(theEdge1, myintersectionLine1);
184   fillAttribute(theEdge2, myintersectionLine2);
185
186   execute();
187 }
188
189 //==================================================================================================
190 void ConstructionAPI_Point::setByLineAndPlaneIntersection(const ModelHighAPI_Selection& theEdge,
191                                                           const ModelHighAPI_Selection& theFace)
192 {
193   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
194   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE(),
195                 myintersectionType);
196   fillAttribute(theEdge, myintersectionLine);
197   fillAttribute(theFace, myintersectionPlane);
198   fillAttribute("", useOffset()); // not used by default
199   execute();
200 }
201
202 //==================================================================================================
203 void ConstructionAPI_Point::setByPlanesIntersection(const ModelHighAPI_Selection& theFace1,
204                                                     const ModelHighAPI_Selection& theFace2,
205                                                     const ModelHighAPI_Selection& theFace3)
206 {
207   fillAttribute(ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION(), mycreationMethod);
208   fillAttribute(ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES(),
209                 myintersectionType);
210   fillAttribute(theFace1, myintersectionPlane1);
211   fillAttribute(theFace2, myintersectionPlane2);
212   fillAttribute(theFace3, myintersectionPlane3);
213
214   execute();
215 }
216
217 //==================================================================================================
218 void ConstructionAPI_Point::dump(ModelHighAPI_Dumper& theDumper) const
219 {
220   FeaturePtr aBase = feature();
221   const std::string& aDocName = theDumper.name(aBase->document());
222   const std::string aMeth = creationMethod()->value();
223
224   // common part
225   theDumper << aBase << " = model.addPoint(" << aDocName << ", ";
226
227   if (aMeth == "" || // default is XYZ
228       aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_XYZ()) {
229     theDumper << x() << ", " << y() << ", " << z();
230   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_INTERSECTION()) {
231     const std::string anIntersectionType = intersectionType()->value();
232     if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINES())
233     {
234       theDumper << intersectionLine1() << ", " << intersectionLine2();
235     }
236     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_LINE_AND_PLANE())
237     {
238       theDumper << intersectionLine() << ", " << intersectionPlane();
239       if (!useOffset()->value().empty()) { // call method with defined offset
240         theDumper << ", " << offset() << ", " << reverseOffset();
241       }
242     }
243     else if (anIntersectionType == ConstructionPlugin_Point::INTERSECTION_TYPE_BY_PLANES())
244     {
245       theDumper << intersectionPlane1() << ", "
246                 << intersectionPlane2() << ", "
247                 << intersectionPlane3();
248     }
249   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
250     theDumper << edge() << ", ";
251     if (offsetType()->value() == ConstructionPlugin_Point::OFFSET_TYPE_BY_DISTANCE()) {
252       theDumper << distance() << ", " << false;
253     }
254     else {
255       theDumper << ratio() << ", " << true;
256     }
257     theDumper << ", " << reverse()->value();
258   } else if (aMeth == ConstructionPlugin_Point::CREATION_METHOD_BY_PROJECTION()) {
259     theDumper << mypoinToProject << ", ";
260     if (projectionType()->value() == ConstructionPlugin_Point::PROJECTION_TYPE_ON_EDGE()) {
261       theDumper << myedgeForPointProjection;
262     } else {
263       theDumper << myfaceForPointProjection;
264     }
265   }
266
267   theDumper << ")" << std::endl;
268 }
269
270 //==================================================================================================
271 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document>& thePart,
272                   const ModelHighAPI_Double& theX,
273                   const ModelHighAPI_Double& theY,
274                   const ModelHighAPI_Double& theZ)
275 {
276   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
277   return PointPtr(new ConstructionAPI_Point(aFeature, theX, theY, theZ));
278 }
279
280 //==================================================================================================
281 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
282                   const ModelHighAPI_Selection& theEdge,
283                   const ModelHighAPI_Double& theOffset,
284                   const bool theUseRatio,
285                   const bool theReverse)
286 {
287   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
288   return PointPtr(new ConstructionAPI_Point(aFeature, theEdge, theOffset, theUseRatio, theReverse));
289 }
290
291 //==================================================================================================
292 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
293                   const ModelHighAPI_Selection& theObject1,
294                   const ModelHighAPI_Selection& theObject2)
295 {
296   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
297   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
298 }
299
300 //==================================================================================================
301 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
302                   const ModelHighAPI_Selection& theObject1,
303                   const ModelHighAPI_Selection& theObject2,
304                   const ModelHighAPI_Double& theDistanceValue,
305                   const bool theReverse)
306 {
307   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
308   PointPtr anAPI(new ConstructionAPI_Point(aFeature, theObject1, theObject2));
309
310   fillAttribute(ConstructionPlugin_Point::USE_OFFSET(), anAPI->useOffset());
311   fillAttribute(theDistanceValue, anAPI->offset());
312   fillAttribute(theReverse, anAPI->reverseOffset());
313
314   return anAPI;
315 }
316
317 //==================================================================================================
318 PointPtr addPoint(const std::shared_ptr<ModelAPI_Document> & thePart,
319                   const ModelHighAPI_Selection& theObject1,
320                   const ModelHighAPI_Selection& theObject2,
321                   const ModelHighAPI_Selection& theObject3)
322 {
323   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ConstructionAPI_Point::ID());
324   return PointPtr(new ConstructionAPI_Point(aFeature, theObject1, theObject2, theObject3));
325 }