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