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