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