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