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