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