Salome HOME
8622766b5aac871bee6e01bf3f1b2e678171219c
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Plane.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_Plane.h"
21
22 #include <Config_PropManager.h>
23
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAlgoAPI_Rotation.h>
26 #include <GeomAlgoAPI_ShapeTools.h>
27 #include <GeomAlgoAPI_Tools.h>
28
29 #include <GeomAPI_Ax1.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_Vertex.h>
38 #include <GeomAPI_XYZ.h>
39
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_AttributeIntArray.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeBoolean.h>
45 #include <ModelAPI_AttributeInteger.h>
46 #include <ModelAPI_ResultConstruction.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Validator.h>
49
50 #include <cfloat>
51
52
53 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
54                                         const std::shared_ptr<GeomAPI_Vertex> theV2,
55                                         const std::shared_ptr<GeomAPI_Vertex> theV3);
56 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
57   const std::shared_ptr<GeomAPI_Face> theFace,
58   const std::shared_ptr<GeomAPI_Pln> thePln);
59
60 //==================================================================================================
61 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
62 {
63 }
64
65 //==================================================================================================
66 void ConstructionPlugin_Plane::initAttributes()
67 {
68   data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
69                        ModelAPI_AttributeString::typeId());
70
71   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
72   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
73   // By general equation.
74   data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
75   data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
76   data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
77   data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
78   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
79   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
80   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
81   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
82
83   // By three points.
84   data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
85   data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
86   data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
87
88   // By line and point.
89   data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
90   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
91   data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
92
93   // By other plane.
94   data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
95   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
96   data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
97   data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
98   data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
99
100   // By two parallel planes.
101   data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
102   data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
103
104   // By other plane.
105   AttributeIntegerPtr aNbCopies = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(
106     data()->addAttribute(NB_COPIES(), ModelAPI_AttributeInteger::typeId()));
107
108   if (!aNbCopies->isInitialized())
109     aNbCopies->setValue(1);
110 }
111
112 //==================================================================================================
113 void ConstructionPlugin_Plane::execute()
114 {
115   ListOfShape aShapes;
116
117   std::string aCreationMethod = string(CREATION_METHOD())->value();
118   if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
119       aCreationMethod == "PlaneByGeneralEquation") {
120     aShapes.push_back(createByGeneralEquation());
121   } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
122     aShapes.push_back(createByThreePoints());
123   } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
124     aShapes.push_back(createByLineAndPoint());
125   } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
126     std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
127     if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
128       createByDistanceFromOther(aShapes);
129     } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
130       aShapes.push_back(createByCoincidentPoint());
131     } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
132       createByRotation(aShapes);
133     }
134   } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
135     aShapes.push_back(createByTwoParallelPlanes());
136   } else {
137     setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
138     return;
139   }
140
141   if(aShapes.size() == 0) {
142     setError("Error: Could not create a plane.");
143     return;
144   }
145
146   int anIndex = 0;
147   for (auto aShapeIter = aShapes.begin(); aShapeIter != aShapes.end(); ++aShapeIter, ++anIndex)
148   {
149     if (!aShapeIter->get())
150     {
151       setError("Error: Could not create a plane.");
152       continue;
153     }
154     ResultConstructionPtr aConstr = document()->createConstruction(data(), anIndex);
155     aConstr->setInfinite(true);
156     aConstr->setShape(*aShapeIter);
157     setResult(aConstr, anIndex);
158   }
159   removeResults(anIndex);
160 }
161
162 //==================================================================================================
163 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs)
164 {
165   std::vector<int> aColor;
166   // get color from the attribute of the result
167   if (theResult.get() != NULL &&
168       theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
169     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
170     if (aColorAttr.get() && aColorAttr->size()) {
171       aColor.push_back(aColorAttr->value(0));
172       aColor.push_back(aColorAttr->value(1));
173       aColor.push_back(aColorAttr->value(2));
174     }
175   }
176   if (aColor.empty())
177     aColor = Config_PropManager::color("Visualization", COLOR_NAME());
178
179   bool isCustomized = false;
180   if (aColor.size() == 3)
181     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
182
183   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
184
185   return isCustomized;
186 }
187
188 //==================================================================================================
189 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
190 {
191   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
192   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
193   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
194   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
195   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
196   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
197       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
198       anAttrA->isInitialized() && anAttrB->isInitialized() &&
199       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
200     double aA = anAttrA->value(), aB = anAttrB->value(),
201            aC = anAttrC->value(), aD = anAttrD->value();
202     std::shared_ptr<GeomAPI_Pln> aPlane =
203       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
204     double aSize = Config_PropManager::real(SKETCH_TAB_NAME, "planes_size");
205     if (aSize <= 1.e-7)
206       aSize = 200;  // Set default value
207     aSize *= 4.;
208     aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
209   }
210   return aPlaneFace;
211 }
212 //==================================================================================================
213 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
214 {
215   // Get first point.
216   AttributeSelectionPtr aPointSelection1 = selection(POINT1());
217   GeomShapePtr aPointShape1 = aPointSelection1->value();
218   if(!aPointShape1.get()) {
219     aPointShape1 = aPointSelection1->context()->shape();
220   }
221   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
222
223   // Get second point.
224   AttributeSelectionPtr aPointSelection2 = selection(POINT2());
225   GeomShapePtr aPointShape2 = aPointSelection2->value();
226   if(!aPointShape2.get()) {
227     aPointShape2 = aPointSelection2->context()->shape();
228   }
229   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
230
231   // Get third point.
232   AttributeSelectionPtr aPointSelection3 = selection(POINT3());
233   GeomShapePtr aPointShape3 = aPointSelection3->value();
234   if(!aPointShape3.get()) {
235     aPointShape3 = aPointSelection3->context()->shape();
236   }
237   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
238
239   GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
240
241   return aRes;
242 }
243
244 //==================================================================================================
245 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
246 {
247   // Get edge.
248   AttributeSelectionPtr anEdgeSelection = selection(LINE());
249   GeomShapePtr aLineShape = anEdgeSelection->value();
250   if(!aLineShape.get()) {
251     aLineShape = anEdgeSelection->context()->shape();
252   }
253   std::shared_ptr<GeomAPI_Edge> anEdge;
254   if (aLineShape->isEdge()) {
255     anEdge = aLineShape->edge();
256   }
257   else if (aLineShape->isCompound()) {
258     GeomAPI_ShapeIterator anIt(aLineShape);
259     anEdge = anIt.current()->edge();
260   }
261   if (!anEdge)
262     return GeomShapePtr();
263
264   // Get point.
265   AttributeSelectionPtr aPointSelection = selection(POINT());
266   GeomShapePtr aPointShape = aPointSelection->value();
267   if(!aPointShape.get()) {
268     aPointShape = aPointSelection->context()->shape();
269   }
270   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
271
272   // Get perpendicular flag.
273   bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
274
275   GeomShapePtr aRes;
276   if(anIsPerpendicular) {
277     std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
278     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
279     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
280     double aSize = aLin->distance(aPnt) * 2;
281     // point may belong to line, so for the face size use maximum distance between point and line
282     // and the line size (distance between the start and end point)
283     double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
284     aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
285   } else {
286     std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
287     GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
288     aRes = faceByThreeVertices(aV1, aV2, aVertex);
289   }
290
291   return aRes;
292 }
293
294 //==================================================================================================
295 void ConstructionPlugin_Plane::createByDistanceFromOther(ListOfShape& theShapes)
296 {
297   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
298   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
299   AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES());
300   if ((aFaceAttr.get() != NULL) &&
301       (aDistAttr.get() != NULL) && (aNbCopyAttr.get() != NULL) &&
302       aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
303
304     double aDist = aDistAttr->value();
305     bool anIsReverse = boolean(REVERSE())->value();
306     int aNumOfCopies = aNbCopyAttr->value();
307     if(anIsReverse) aDist = -aDist;
308     GeomShapePtr aShape = aFaceAttr->value();
309     if (!aShape.get() && aFaceAttr->context()) {
310       aShape = aFaceAttr->context()->shape();
311     }
312
313     if(!aShape.get()) {
314       return;
315     }
316
317     std::shared_ptr<GeomAPI_Face> aFace;
318     if (aShape->isFace()) {
319       aFace = aShape->face();
320     }
321     else if (aShape->isCompound()) {
322       GeomAPI_ShapeIterator anIt(aShape);
323       aFace = anIt.current()->face();
324     }
325     if (!aFace)
326       return;
327
328     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
329     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
330     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
331
332     for (int aNbCopy = 0; aNbCopy < aNumOfCopies; ++aNbCopy)
333     {
334       std::shared_ptr<GeomAPI_Shape> aPlane;
335       aOrig->translate(aDir, aDist);
336       std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
337
338       aPlane = makeRectangularFace(aFace, aNewPln);
339       theShapes.push_back(aPlane);
340     }
341   }
342 }
343
344 //==================================================================================================
345 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
346 {
347   // Get face.
348   AttributeSelectionPtr aFaceSelection = selection(PLANE());
349   GeomShapePtr aFaceShape = aFaceSelection->value();
350   if(!aFaceShape.get()) {
351     aFaceShape = aFaceSelection->context()->shape();
352   }
353   std::shared_ptr<GeomAPI_Face> aFace;
354   if (aFaceShape->isFace()) {
355     aFace = aFaceShape->face();
356   }
357   else if (aFaceShape->isCompound()) {
358     GeomAPI_ShapeIterator anIt(aFaceShape);
359     aFace = anIt.current()->face();
360   }
361
362   // Get point.
363   AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
364   GeomShapePtr aPointShape = aPointSelection->value();
365   if(!aPointShape.get()) {
366     aPointShape = aPointSelection->context()->shape();
367   }
368   std::shared_ptr<GeomAPI_Vertex> aVertex = aPointShape->vertex();
369   if (!aVertex)
370     return GeomShapePtr();
371
372   std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
373   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
374   std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
375
376   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
377
378   return makeRectangularFace(aFace, aNewPln);
379 }
380
381 //==================================================================================================
382 void ConstructionPlugin_Plane::createByRotation(ListOfShape& theShapes)
383 {
384   // Get face.
385   AttributeSelectionPtr aFaceSelection = selection(PLANE());
386   GeomShapePtr aFaceShape = aFaceSelection->value();
387   if(!aFaceShape.get()) {
388     aFaceShape = aFaceSelection->context()->shape();
389   }
390   std::shared_ptr<GeomAPI_Face> aFace;
391   if (aFaceShape->isFace()) {
392     aFace = aFaceShape->face();
393   }
394   else if (aFaceShape->isCompound()) {
395     GeomAPI_ShapeIterator anIt(aFaceShape);
396     aFace = anIt.current()->face();
397   }
398   if (!aFace)
399     return;
400   aFace = makeRectangularFace(aFace, aFace->getPlane());
401
402   // Get axis.
403   AttributeSelectionPtr anAxisSelection = selection(AXIS());
404   GeomShapePtr anAxisShape = anAxisSelection->value();
405   if(!anAxisShape.get()) {
406     anAxisShape = anAxisSelection->context()->shape();
407   }
408   std::shared_ptr<GeomAPI_Edge> anEdge;
409   if (anAxisShape->isEdge()) {
410     anEdge = anAxisShape->edge();
411   }
412   else if (anAxisShape->isCompound()) {
413     GeomAPI_ShapeIterator anIt(anAxisShape);
414     anEdge = anIt.current()->edge();
415   }
416   if (!anEdge)
417     return;
418
419   AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES());
420   int aNBCopy;
421   if (!aNbCopyAttr.get())
422     return;
423
424   aNBCopy = aNbCopyAttr->value();
425
426   std::shared_ptr<GeomAPI_Ax1> anAxis =
427     std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
428                                                  anEdge->line()->direction()));
429
430   // Getting angle.
431   double anAngle = real(ANGLE())->value();
432
433   for (int anIndex = 1; anIndex <= aNBCopy; ++anIndex)
434   {
435     std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(
436       new GeomAlgoAPI_Rotation(aFace, anAxis, anAngle * anIndex));
437     // Checking that the algorithm worked properly.
438     std::string anError;
439     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
440       setError("Error: Failed to rotate plane");
441       return;
442     }
443
444     std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo->shape()));
445     theShapes.push_back(aRes);
446   }
447 }
448
449 //==================================================================================================
450 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
451 {
452   // Get plane 1.
453   AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
454   GeomShapePtr aFaceShape1 = aFaceSelection1->value();
455   if(!aFaceShape1.get()) {
456     aFaceShape1 = aFaceSelection1->context()->shape();
457   }
458   std::shared_ptr<GeomAPI_Face> aFace1;
459   if (aFaceShape1->isFace()) {
460     aFace1 = aFaceShape1->face();
461   }
462   else if (aFaceShape1->isCompound()) {
463     GeomAPI_ShapeIterator anIt(aFaceShape1);
464     aFace1 = anIt.current()->face();
465   }
466   if (!aFace1)
467     return GeomShapePtr();
468   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
469
470   // Get plane 2.
471   AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
472   GeomShapePtr aFaceShape2 = aFaceSelection2->value();
473   if(!aFaceShape2.get()) {
474     aFaceShape2 = aFaceSelection2->context()->shape();
475   }
476   std::shared_ptr<GeomAPI_Face> aFace2;
477   if (aFaceShape2->isFace()) {
478     aFace2 = aFaceShape2->face();
479   }
480   else if (aFaceShape2->isCompound()) {
481     GeomAPI_ShapeIterator anIt(aFaceShape2);
482     aFace2 = anIt.current()->face();
483   }
484   if (!aFace2)
485     return GeomShapePtr();
486   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
487
488   std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
489   std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
490
491   std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
492     aPntOnPln2->xyz())->multiplied(0.5)));
493
494   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
495
496   std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
497
498   return aRes;
499 }
500
501 //==================================================================================================
502 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
503                                  const std::shared_ptr<GeomAPI_Vertex> theV2,
504                                  const std::shared_ptr<GeomAPI_Vertex> theV3)
505 {
506   std::shared_ptr<GeomAPI_Face> aFace =
507     GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
508
509   ListOfShape anObjects;
510   anObjects.push_back(theV1);
511   anObjects.push_back(theV2);
512   anObjects.push_back(theV3);
513   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
514     GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
515   GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
516
517   return aRes;
518 }
519
520 //==================================================================================================
521 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
522                                                   const std::shared_ptr<GeomAPI_Pln> thePln)
523 {
524   // Create rectangular face close to the selected
525   double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
526   theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
527
528   // use all 8 points of the bounding box to find the 2D bounds
529   bool isFirst = true;
530   double aMinX2d = DBL_MAX, aMaxX2d = -DBL_MAX;
531   double aMinY2d = DBL_MAX, aMaxY2d = -DBL_MAX;
532   for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
533     for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
534       for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
535         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
536           aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
537         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
538         if (isFirst || aPnt2d->x() < aMinX2d)
539           aMinX2d = aPnt2d->x();
540         if (isFirst || aPnt2d->y() < aMinY2d)
541           aMinY2d = aPnt2d->y();
542         if (isFirst || aPnt2d->x() > aMaxX2d)
543           aMaxX2d = aPnt2d->x();
544         if (isFirst || aPnt2d->y() > aMaxY2d)
545           aMaxY2d = aPnt2d->y();
546         if (isFirst)
547           isFirst = !isFirst;
548       }
549     }
550   }
551   double aWgap = (aMaxX2d - aMinX2d) * 0.1;
552   double aHgap = (aMaxY2d - aMinY2d) * 0.1;
553   std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
554     aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
555     aMaxY2d - aMinY2d + 2. * aHgap);
556
557   return aResFace;
558 }