Salome HOME
2.3.4 Point creation: by intersection
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_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 "ConstructionPlugin_Point.h"
22
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_ResultConstruction.h>
28
29 #include <GeomAlgoAPI_PointBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Vertex.h>
35 #include <GeomAPI_Pln.h>
36
37 //==================================================================================================
38 ConstructionPlugin_Point::ConstructionPlugin_Point()
39 {
40 }
41
42 //==================================================================================================
43 const std::string& ConstructionPlugin_Point::getKind()
44 {
45   static std::string MY_KIND = ConstructionPlugin_Point::ID();
46   return MY_KIND;
47 }
48
49 //==================================================================================================
50 void ConstructionPlugin_Point::initAttributes()
51 {
52   data()->addAttribute(X(), ModelAPI_AttributeDouble::typeId());
53   data()->addAttribute(Y(), ModelAPI_AttributeDouble::typeId());
54   data()->addAttribute(Z(), ModelAPI_AttributeDouble::typeId());
55
56   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
57
58   data()->addAttribute(INTERSECTION_LINE_1(), ModelAPI_AttributeSelection::typeId());
59   data()->addAttribute(INTERSECTION_LINE_2(), ModelAPI_AttributeSelection::typeId());
60
61   data()->addAttribute(INTERSECTION_LINE(), ModelAPI_AttributeSelection::typeId());
62   data()->addAttribute(INTERSECTION_PLANE(), ModelAPI_AttributeSelection::typeId());
63   data()->addAttribute(USE_OFFSET(), ModelAPI_AttributeString::typeId());
64   data()->addAttribute(OFFSET(), ModelAPI_AttributeDouble::typeId());
65   data()->addAttribute(REVERSE_OFFSET(), ModelAPI_AttributeBoolean::typeId());
66
67   data()->addAttribute(EDGE(), ModelAPI_AttributeSelection::typeId());
68   data()->addAttribute(OFFSET_TYPE(), ModelAPI_AttributeString::typeId());
69   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
70   data()->addAttribute(RATIO(), ModelAPI_AttributeDouble::typeId());
71   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
72
73   data()->addAttribute(POINT_TO_PROJECT(), ModelAPI_AttributeSelection::typeId());
74   data()->addAttribute(PROJECTION_TYPE(), ModelAPI_AttributeString::typeId());
75   data()->addAttribute(EDGE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
76   data()->addAttribute(FACE_FOR_POINT_PROJECTION(), ModelAPI_AttributeSelection::typeId());
77
78   data()->addAttribute(INTERSECTION_TYPE(), ModelAPI_AttributeString::typeId());
79
80   data()->addAttribute(INTERSECTION_PLANE_1(), ModelAPI_AttributeSelection::typeId());
81   data()->addAttribute(INTERSECTION_PLANE_2(), ModelAPI_AttributeSelection::typeId());
82   data()->addAttribute(INTERSECTION_PLANE_3(), ModelAPI_AttributeSelection::typeId());
83 }
84
85 //==================================================================================================
86 void ConstructionPlugin_Point::execute()
87 {
88   GeomShapePtr aShape;
89
90   // to support compatibility with old documents where aCreationMethod did not exist
91   std::string aCreationMethod =
92     string(CREATION_METHOD()).get() && !string(CREATION_METHOD())->value().empty() ?
93     string(CREATION_METHOD())->value() : CREATION_METHOD_BY_XYZ();
94   if(aCreationMethod == CREATION_METHOD_BY_XYZ()) {
95     aShape = createByXYZ();
96   } else if(aCreationMethod == CREATION_METHOD_BY_DISTANCE_ON_EDGE()) {
97     aShape = createByDistanceOnEdge();
98   } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) {
99     if (string(PROJECTION_TYPE())->value() == PROJECTION_TYPE_ON_EDGE()) {
100       aShape = createByProjectionOnEdge();
101     } else {
102       aShape = createByProjectionOnFace();
103     }
104   } else if(aCreationMethod == CREATION_METHOD_BY_INTERSECTION()) {
105     std::string anIntersectionType = string(INTERSECTION_TYPE())->value();
106     if (anIntersectionType == INTERSECTION_TYPE_BY_LINES()) {
107       aShape = createByLinesIntersection();
108     } else if (anIntersectionType == INTERSECTION_TYPE_BY_LINE_AND_PLANE()) {
109       // this may produce several points
110       std::list<std::shared_ptr<GeomAPI_Vertex> > aPoints = createByLineAndPlaneIntersection();
111       if (!aPoints.empty()) { // if no points found produce the standard error later
112         int anIndex = 0;
113         std::list<std::shared_ptr<GeomAPI_Vertex> >::iterator aPIter = aPoints.begin();
114         for (; aPIter != aPoints.end(); aPIter++, anIndex++) {
115           std::shared_ptr<ModelAPI_ResultConstruction> aConstr =
116             document()->createConstruction(data(), anIndex);
117           aConstr->setShape(*aPIter);
118           setResult(aConstr, anIndex);
119         }
120         removeResults(anIndex);
121         return;
122       }
123     } else {
124       aShape = createByPlanesIntersection();
125     }
126   }
127
128   if(!aShape.get()) {
129     setError("Error: intersection not found.");
130     return;
131   }
132
133   removeResults(1); // for case the point type was switched from multi-results type
134   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
135   aConstr->setShape(aShape);
136   setResult(aConstr);
137 }
138
139 //==================================================================================================
140 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
141                                                      AISObjectPtr thePrs,
142                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
143 {
144   bool isCustomized = theDefaultPrs.get() != NULL &&
145                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
146   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
147   return true;
148 }
149
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
152 {
153   return GeomAlgoAPI_PointBuilder::vertex(real(X())->value(),
154                                           real(Y())->value(),
155                                           real(Z())->value());
156 }
157
158 //==================================================================================================
159 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
160 {
161   // Get edge.
162   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
163   GeomShapePtr aShape = anEdgeSelection->value();
164   if(!aShape.get()) {
165     aShape = anEdgeSelection->context()->shape();
166   }
167   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
168
169   // Get distance value and percent flag.
170   double aValue;
171   bool anIsPercent = false;
172   if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
173     aValue = real(DISTANCE())->value();
174     anIsPercent = false;
175   } else {
176     aValue = real(RATIO())->value() * 100.0;
177     anIsPercent = true;
178   }
179
180   // Get reverse flag.
181   bool anIsReverse = boolean(REVERSE())->value();
182
183   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
184 }
185
186 //==================================================================================================
187 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
188 {
189   // Get point.
190   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
191   GeomShapePtr aPointShape = aPointSelection->value();
192   if (!aPointShape.get()) {
193     aPointShape = aPointSelection->context()->shape();
194   }
195   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
196
197   // Get edge.
198   AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
199   GeomShapePtr anEdgeShape = anEdgeSelection->value();
200   if (!anEdgeShape.get()) {
201     anEdgeShape = anEdgeSelection->context()->shape();
202   }
203   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
204
205   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
206 }
207
208 //==================================================================================================
209 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
210 {
211   // Get point.
212   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
213   GeomShapePtr aPointShape = aPointSelection->value();
214   if(!aPointShape.get()) {
215     aPointShape = aPointSelection->context()->shape();
216   }
217   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
218
219   // Get plane.
220   AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
221   GeomShapePtr aPlaneShape = aPlaneSelection->value();
222   if(!aPlaneShape.get()) {
223     aPlaneShape = aPlaneSelection->context()->shape();
224   }
225   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
226
227   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
228 }
229
230 //==================================================================================================
231 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
232 {
233   // Get first line.
234   AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
235   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
236   if(!aFirstLineShape.get()) {
237     aFirstLineShape = aFirstLineSelection->context()->shape();
238   }
239   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
240
241   // Get second line.
242   AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
243   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
244   if(!aSecondLineShape.get()) {
245     aSecondLineShape = aSecondLineSelection->context()->shape();
246   }
247   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
248
249   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
250 }
251
252 //==================================================================================================
253 std::list<std::shared_ptr<GeomAPI_Vertex> >
254   ConstructionPlugin_Point::createByLineAndPlaneIntersection()
255 {
256   // Get line.
257   AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
258   GeomShapePtr aLineShape = aLineSelection->value();
259   if(!aLineShape.get()) {
260     aLineShape = aLineSelection->context()->shape();
261   }
262   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
263
264   // Get plane.
265   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
266   GeomShapePtr aPlaneShape = aPlaneSelection->value();
267   if(!aPlaneShape.get()) {
268     aPlaneShape = aPlaneSelection->context()->shape();
269   }
270   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
271
272   if (!string(USE_OFFSET())->value().empty()) {
273     double anOffset = real(OFFSET())->value();
274     if (boolean(REVERSE_OFFSET())->value())
275       anOffset = -anOffset;
276     if (fabs(anOffset) > 1.e-9) { // move face
277       aFace->translate(aFace->getPlane()->direction(), anOffset);
278     }
279   }
280
281   return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
282     aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
283 }
284
285 //==================================================================================================
286 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
287 {
288   // Get plane.
289   AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
290   GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
291   if (!aPlaneShape1.get()) {
292     aPlaneShape1 = aPlaneSelection1->context()->shape();
293   }
294   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
295   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
296
297   // Get plane.
298   AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
299   GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
300   if (!aPlaneShape2.get()) {
301     aPlaneShape2 = aPlaneSelection2->context()->shape();
302   }
303   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
304   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
305
306   // Get plane.
307   AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
308   GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
309   if (!aPlaneShape3.get()) {
310     aPlaneShape3 = aPlaneSelection3->context()->shape();
311   }
312   std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
313   std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
314
315   std::shared_ptr<GeomAPI_Vertex> aVertex;
316
317   std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
318   if (!anIntersectLine.get()) {
319     return aVertex;
320   }
321
322   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
323   if (aPnt.get()) {
324     aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
325   }
326
327   return aVertex;
328 }