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