Salome HOME
Issue #2593: CEA 2018-2 Geometrical Naming
[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->setShape(aShape);
148   setResult(aConstr);
149 }
150
151 //==================================================================================================
152 bool ConstructionPlugin_Point::customisePresentation(ResultPtr theResult,
153                                                      AISObjectPtr thePrs,
154                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
155 {
156   bool isCustomized = theDefaultPrs.get() != NULL &&
157                       theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
158   //thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
159   return true;
160 }
161
162 //==================================================================================================
163 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByXYZ()
164 {
165   AttributePointPtr aPoint =
166     std::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(POINT3D()));
167   return GeomAlgoAPI_PointBuilder::vertex(aPoint->x(), aPoint->y(), aPoint->z());
168 }
169
170 //==================================================================================================
171 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByDistanceOnEdge()
172 {
173   // Get edge.
174   AttributeSelectionPtr anEdgeSelection = selection(EDGE());
175   GeomShapePtr aShape = anEdgeSelection->value();
176   if(!aShape.get()) {
177     aShape = anEdgeSelection->context()->shape();
178   }
179   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
180
181   // Get distance value and percent flag.
182   double aValue;
183   bool anIsPercent = false;
184   if (string(OFFSET_TYPE())->value() == OFFSET_TYPE_BY_DISTANCE()) {
185     aValue = real(DISTANCE())->value();
186     anIsPercent = false;
187   } else {
188     aValue = real(RATIO())->value() * 100.0;
189     anIsPercent = true;
190   }
191
192   // Get reverse flag.
193   bool anIsReverse = boolean(REVERSE())->value();
194
195   return GeomAlgoAPI_PointBuilder::vertexOnEdge(anEdge, aValue, anIsPercent, anIsReverse);
196 }
197
198 //==================================================================================================
199 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnEdge()
200 {
201   // Get point.
202   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
203   GeomShapePtr aPointShape = aPointSelection->value();
204   if (!aPointShape.get()) {
205     aPointShape = aPointSelection->context()->shape();
206   }
207   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
208
209   // Get edge.
210   AttributeSelectionPtr anEdgeSelection = selection(EDGE_FOR_POINT_PROJECTION());
211   GeomShapePtr anEdgeShape = anEdgeSelection->value();
212   if (!anEdgeShape.get()) {
213     anEdgeShape = anEdgeSelection->context()->shape();
214   }
215   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anEdgeShape));
216
217   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, anEdge);
218 }
219
220 //==================================================================================================
221 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByProjectionOnFace()
222 {
223   // Get point.
224   AttributeSelectionPtr aPointSelection = selection(POINT_TO_PROJECT());
225   GeomShapePtr aPointShape = aPointSelection->value();
226   if(!aPointShape.get()) {
227     aPointShape = aPointSelection->context()->shape();
228   }
229   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
230
231   // Get plane.
232   AttributeSelectionPtr aPlaneSelection = selection(FACE_FOR_POINT_PROJECTION());
233   GeomShapePtr aPlaneShape = aPlaneSelection->value();
234   if(!aPlaneShape.get()) {
235     aPlaneShape = aPlaneSelection->context()->shape();
236   }
237   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aPlaneShape));
238
239   return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace);
240 }
241
242 //==================================================================================================
243 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByLinesIntersection()
244 {
245   // Get first line.
246   AttributeSelectionPtr aFirstLineSelection= selection(INTERSECTION_LINE_1());
247   GeomShapePtr aFirstLineShape = aFirstLineSelection->value();
248   if(!aFirstLineShape.get()) {
249     aFirstLineShape = aFirstLineSelection->context()->shape();
250   }
251   std::shared_ptr<GeomAPI_Edge> aFirstEdge(new GeomAPI_Edge(aFirstLineShape));
252
253   // Get second line.
254   AttributeSelectionPtr aSecondLineSelection= selection(INTERSECTION_LINE_2());
255   GeomShapePtr aSecondLineShape = aSecondLineSelection->value();
256   if(!aSecondLineShape.get()) {
257     aSecondLineShape = aSecondLineSelection->context()->shape();
258   }
259   std::shared_ptr<GeomAPI_Edge> aSecondEdge(new GeomAPI_Edge(aSecondLineShape));
260
261   return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge);
262 }
263
264 //==================================================================================================
265 std::list<std::shared_ptr<GeomAPI_Vertex> >
266   ConstructionPlugin_Point::createByLineAndPlaneIntersection()
267 {
268   // Get line.
269   AttributeSelectionPtr aLineSelection = selection(INTERSECTION_LINE());
270   GeomShapePtr aLineShape = aLineSelection->value();
271   if(!aLineShape.get()) {
272     aLineShape = aLineSelection->context()->shape();
273   }
274   GeomEdgePtr anEdge;
275   if (aLineShape->isEdge()) {
276     anEdge = aLineShape->edge();
277   }
278   else if (aLineShape->isCompound()) {
279     GeomAPI_ShapeIterator anIt(aLineShape);
280     anEdge = anIt.current()->edge();
281   }
282
283   // Get plane.
284   AttributeSelectionPtr aPlaneSelection= selection(INTERSECTION_PLANE());
285   GeomShapePtr aPlaneShape = aPlaneSelection->value();
286   if(!aPlaneShape.get()) {
287     aPlaneShape = aPlaneSelection->context()->shape();
288   }
289   GeomFacePtr aFace;
290   if (aPlaneShape->isFace()) {
291     aFace = aPlaneShape->face();
292   }
293   else if (aPlaneShape->isCompound()) {
294     GeomAPI_ShapeIterator anIt(aPlaneShape);
295     aFace = anIt.current()->face();
296   }
297
298   if (!string(USE_OFFSET())->value().empty()) {
299     double anOffset = real(OFFSET())->value();
300     if (boolean(REVERSE_OFFSET())->value())
301       anOffset = -anOffset;
302     if (fabs(anOffset) > 1.e-9) { // move face
303       aFace->translate(aFace->getPlane()->direction(), anOffset);
304     }
305   }
306
307   return GeomAlgoAPI_ShapeTools::intersect(anEdge, aFace,
308     aPlaneSelection->context()->groupName() == ModelAPI_ResultConstruction::group());
309 }
310
311 //==================================================================================================
312 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByPlanesIntersection()
313 {
314   // Get plane.
315   AttributeSelectionPtr aPlaneSelection1 = selection(INTERSECTION_PLANE_1());
316   GeomShapePtr aPlaneShape1 = aPlaneSelection1->value();
317   if (!aPlaneShape1.get()) {
318     aPlaneShape1 = aPlaneSelection1->context()->shape();
319   }
320   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aPlaneShape1));
321   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
322
323   // Get plane.
324   AttributeSelectionPtr aPlaneSelection2 = selection(INTERSECTION_PLANE_2());
325   GeomShapePtr aPlaneShape2 = aPlaneSelection2->value();
326   if (!aPlaneShape2.get()) {
327     aPlaneShape2 = aPlaneSelection2->context()->shape();
328   }
329   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aPlaneShape2));
330   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
331
332   // Get plane.
333   AttributeSelectionPtr aPlaneSelection3 = selection(INTERSECTION_PLANE_3());
334   GeomShapePtr aPlaneShape3 = aPlaneSelection3->value();
335   if (!aPlaneShape3.get()) {
336     aPlaneShape3 = aPlaneSelection3->context()->shape();
337   }
338   std::shared_ptr<GeomAPI_Face> aFace3(new GeomAPI_Face(aPlaneShape3));
339   std::shared_ptr<GeomAPI_Pln> aPln3 = aFace3->getPlane();
340
341   std::shared_ptr<GeomAPI_Vertex> aVertex;
342
343   std::shared_ptr<GeomAPI_Lin> anIntersectLine = aPln1->intersect(aPln2);
344   if (!anIntersectLine.get()) {
345     return aVertex;
346   }
347
348   std::shared_ptr<GeomAPI_Pnt> aPnt = aPln3->intersect(anIntersectLine);
349   if (aPnt.get()) {
350     aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
351   }
352
353   return aVertex;
354 }
355
356 //==================================================================================================
357 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfGravity()
358 {
359   // Get shape.
360   AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_GRAVITY());
361   GeomShapePtr aShape = aShapeSelection->value();
362   if (!aShape.get())
363   {
364     aShape = aShapeSelection->context()->shape();
365   }
366
367   std::shared_ptr<GeomAPI_Vertex> aVertex;
368   std::shared_ptr<GeomAPI_Pnt> aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShape);
369   if (aPnt.get())
370   {
371     aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
372   }
373
374   return aVertex;
375 }
376
377 //==================================================================================================
378 std::shared_ptr<GeomAPI_Vertex> ConstructionPlugin_Point::createByCenterOfCircle()
379 {
380   // Get shape.
381   AttributeSelectionPtr aShapeSelection = selection(OBJECT_FOR_CENTER_OF_CIRCLE());
382   GeomShapePtr aShape = aShapeSelection->value();
383   if (!aShape.get()) {
384     aShape = aShapeSelection->context()->shape();
385   }
386   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
387   std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
388
389   std::shared_ptr<GeomAPI_Vertex> aVertex;
390   std::shared_ptr<GeomAPI_Pnt> aPnt = aCirc->center();
391   if (aPnt.get()) {
392     aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z()));
393   }
394
395   return aVertex;
396 }