Salome HOME
Fix for the issue #1794: can't create a plane by a vertex and an axis if the vertex...
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Plane.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ConstructionPlugin_Plane.cpp
4 // Created:     12 Dec 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ConstructionPlugin_Plane.h"
8
9 #include <Config_PropManager.h>
10
11 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAlgoAPI_Rotation.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
14
15 #include <GeomAPI_Ax1.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Face.h>
18 #include <GeomAPI_Lin.h>
19 #include <GeomAPI_Pln.h>
20 #include <GeomAPI_Pnt.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_Vertex.h>
23 #include <GeomAPI_XYZ.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeIntArray.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_AttributeBoolean.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
33
34 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
35                                         const std::shared_ptr<GeomAPI_Vertex> theV2,
36                                         const std::shared_ptr<GeomAPI_Vertex> theV3);
37 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
38                                                          const std::shared_ptr<GeomAPI_Pln> thePln);
39
40 //==================================================================================================
41 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
42 {
43 }
44
45 //==================================================================================================
46 void ConstructionPlugin_Plane::initAttributes()
47 {
48   data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49
50   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
51   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
52   // By general equation.
53   data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
54   data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
55   data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
56   data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
58   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
61
62   // By three points.
63   data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
64   data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
65   data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
66
67   // By line and point.
68   data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
69   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
70   data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
71
72   // By other plane.
73   data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
74   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
75   data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
76   data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
77   data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
78
79   // By two parallel planes.
80   data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
81   data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
82 }
83
84 //==================================================================================================
85 void ConstructionPlugin_Plane::execute()
86 {
87   GeomShapePtr aShape;
88
89   std::string aCreationMethod = string(CREATION_METHOD())->value();
90   if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() || aCreationMethod == "PlaneByGeneralEquation") {
91     aShape = createByGeneralEquation();
92   } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
93     aShape = createByThreePoints();
94   } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
95     aShape = createByLineAndPoint();
96   } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
97     std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
98     if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
99       aShape = createByDistanceFromOther();
100     } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
101       aShape = createByCoincidentPoint();
102     } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
103       aShape = createByRotation();
104     }
105   } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
106     aShape = createByTwoParallelPlanes();
107   } else {
108     setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
109     return;
110   }
111
112   if(!aShape.get()) {
113     setError("Error: Could not create a plane.");
114     return;
115   }
116
117   ResultConstructionPtr aConstr = document()->createConstruction(data());
118   aConstr->setInfinite(true);
119   aConstr->setShape(aShape);
120   setResult(aConstr);
121 }
122
123 //==================================================================================================
124 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
125                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
126 {
127   std::vector<int> aColor;
128   // get color from the attribute of the result
129   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
130     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
131     if (aColorAttr.get() && aColorAttr->size()) {
132       aColor.push_back(aColorAttr->value(0));
133       aColor.push_back(aColorAttr->value(1));
134       aColor.push_back(aColorAttr->value(2));
135     }
136   }
137   if (aColor.empty())
138     aColor = Config_PropManager::color("Visualization", "construction_plane_color",
139                                        ConstructionPlugin_Plane::DEFAULT_COLOR());
140
141   bool isCustomized = false;
142   if (aColor.size() == 3)
143     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
144
145   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
146
147   return isCustomized;
148 }
149
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
152 {
153   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
154   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
155   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
156   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
157   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
158   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
159       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
160       anAttrA->isInitialized() && anAttrB->isInitialized() &&
161       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
162     double aA = anAttrA->value(), aB = anAttrB->value(),
163            aC = anAttrC->value(), aD = anAttrD->value();
164     std::shared_ptr<GeomAPI_Pln> aPlane = 
165       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
166     std::string kDefaultPlaneSize = "200";
167     double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
168     aSize *= 4.;
169     aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
170   }
171   return aPlaneFace;
172 }
173
174 //==================================================================================================
175 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
176 {
177   // Get first point.
178   AttributeSelectionPtr aPointSelection1 = selection(POINT1());
179   GeomShapePtr aPointShape1 = aPointSelection1->value();
180   if(!aPointShape1.get()) {
181     aPointShape1 = aPointSelection1->context()->shape();
182   }
183   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
184
185   // Get second point.
186   AttributeSelectionPtr aPointSelection2 = selection(POINT2());
187   GeomShapePtr aPointShape2 = aPointSelection2->value();
188   if(!aPointShape2.get()) {
189     aPointShape2 = aPointSelection2->context()->shape();
190   }
191   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
192
193   // Get third point.
194   AttributeSelectionPtr aPointSelection3 = selection(POINT3());
195   GeomShapePtr aPointShape3 = aPointSelection3->value();
196   if(!aPointShape3.get()) {
197     aPointShape3 = aPointSelection3->context()->shape();
198   }
199   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
200
201   GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
202
203   return aRes;
204 }
205
206 //==================================================================================================
207 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
208 {
209   // Get edge.
210   AttributeSelectionPtr anEdgeSelection = selection(LINE());
211   GeomShapePtr aLineShape = anEdgeSelection->value();
212   if(!aLineShape.get()) {
213     aLineShape = anEdgeSelection->context()->shape();
214   }
215   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
216
217   // Get point.
218   AttributeSelectionPtr aPointSelection = selection(POINT());
219   GeomShapePtr aPointShape = aPointSelection->value();
220   if(!aPointShape.get()) {
221     aPointShape = aPointSelection->context()->shape();
222   }
223   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
224
225   // Get perpendicular flag.
226   bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
227
228   GeomShapePtr aRes;
229   if(anIsPerpendicular) {
230     std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
231     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
232     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
233     double aSize = aLin->distance(aPnt) * 2;
234     // point may belong to line, so for the face size use maximum distance between point and line
235     // and the line size (distance between the start and end point)
236     double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
237     aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
238   } else {
239     std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
240     GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
241     aRes = faceByThreeVertices(aV1, aV2, aVertex);
242   }
243
244   return aRes;
245 }
246
247 //==================================================================================================
248 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
249 {
250   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
251   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
252   std::shared_ptr<GeomAPI_Shape> aPlane;
253   if ((aFaceAttr.get() != NULL) &&
254       (aDistAttr.get() != NULL) &&
255       aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
256
257     double aDist = aDistAttr->value();
258     bool anIsReverse = boolean(REVERSE())->value();
259     if(anIsReverse) aDist = -aDist;
260     GeomShapePtr aShape = aFaceAttr->value();
261     if (!aShape.get()) {
262       aShape = aFaceAttr->context()->shape();
263     }
264
265     if(!aShape.get()) {
266       return aPlane;
267     }
268
269     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
270
271     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
272     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
273     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
274
275     aOrig->translate(aDir, aDist);
276     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
277
278     aPlane = makeRectangularFace(aFace, aNewPln);
279   }
280   return aPlane;
281 }
282
283 //==================================================================================================
284 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
285 {
286   // Get face.
287   AttributeSelectionPtr aFaceSelection = selection(PLANE());
288   GeomShapePtr aFaceShape = aFaceSelection->value();
289   if(!aFaceShape.get()) {
290     aFaceShape = aFaceSelection->context()->shape();
291   }
292   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
293
294   // Get point.
295   AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
296   GeomShapePtr aPointShape = aPointSelection->value();
297   if(!aPointShape.get()) {
298     aPointShape = aPointSelection->context()->shape();
299   }
300   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
301
302   std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
303   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
304   std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
305
306   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
307
308   return makeRectangularFace(aFace, aNewPln);
309 }
310
311 //==================================================================================================
312 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
313 {
314   // Get face.
315   AttributeSelectionPtr aFaceSelection = selection(PLANE());
316   GeomShapePtr aFaceShape = aFaceSelection->value();
317   if(!aFaceShape.get()) {
318     aFaceShape = aFaceSelection->context()->shape();
319   }
320   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
321   aFace = makeRectangularFace(aFace, aFace->getPlane());
322
323   // Get axis.
324   AttributeSelectionPtr anAxisSelection = selection(AXIS());
325   GeomShapePtr anAxisShape = anAxisSelection->value();
326   if(!anAxisShape.get()) {
327     anAxisShape = anAxisSelection->context()->shape();
328   }
329   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
330
331   std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
332                                                                                      anEdge->line()->direction()));
333
334   // Getting angle.
335   double anAngle = real(ANGLE())->value();
336
337   GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
338   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
339
340   return aRes;
341 }
342
343 //==================================================================================================
344 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
345 {
346   // Get plane 1.
347   AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
348   GeomShapePtr aFaceShape1 = aFaceSelection1->value();
349   if(!aFaceShape1.get()) {
350     aFaceShape1 = aFaceSelection1->context()->shape();
351   }
352   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
353   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
354
355   // Get plane 2.
356   AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
357   GeomShapePtr aFaceShape2 = aFaceSelection2->value();
358   if(!aFaceShape2.get()) {
359     aFaceShape2 = aFaceSelection2->context()->shape();
360   }
361   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
362   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
363
364   std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
365   std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
366
367   std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
368     aPntOnPln2->xyz())->multiplied(0.5)));
369
370   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
371
372   std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
373
374   return aRes;
375 }
376
377 //==================================================================================================
378 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
379                                  const std::shared_ptr<GeomAPI_Vertex> theV2,
380                                  const std::shared_ptr<GeomAPI_Vertex> theV3)
381 {
382   std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
383
384   ListOfShape anObjects;
385   anObjects.push_back(theV1);
386   anObjects.push_back(theV2);
387   anObjects.push_back(theV3);
388   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
389   GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
390
391   return aRes;
392 }
393
394 //==================================================================================================
395 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
396                                                   const std::shared_ptr<GeomAPI_Pln> thePln)
397 {
398   // Create rectangular face close to the selected
399   double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
400   theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
401
402   // use all 8 points of the bounding box to find the 2D bounds
403   bool isFirst = true;
404   double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
405   for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
406     for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
407       for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
408         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
409           aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
410         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
411         if (isFirst || aPnt2d->x() < aMinX2d)
412           aMinX2d = aPnt2d->x();
413         if (isFirst || aPnt2d->y() < aMinY2d)
414           aMinY2d = aPnt2d->y();
415         if (isFirst || aPnt2d->x() > aMaxX2d)
416           aMaxX2d = aPnt2d->x();
417         if (isFirst || aPnt2d->y() > aMaxY2d)
418           aMaxY2d = aPnt2d->y();
419         if (isFirst)
420           isFirst = !isFirst;
421       }
422     }
423   }
424   double aWgap = (aMaxX2d - aMinX2d) * 0.1;
425   double aHgap = (aMaxY2d - aMinY2d) * 0.1;
426   std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
427     aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);
428
429   return aResFace;
430 }