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