]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConstructionPlugin/ConstructionPlugin_Plane.cpp
Salome HOME
Issue #1649: Added "Reverse" flag handling.
[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     double 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     bool anIsReverse = boolean(REVERSE())->value();
252     if(anIsReverse) aDist = -aDist;
253     GeomShapePtr aShape = aFaceAttr->value();
254     if (!aShape.get()) {
255       aShape = aFaceAttr->context()->shape();
256     }
257
258     if(!aShape.get()) {
259       return aPlane;
260     }
261
262     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
263
264     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
265     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
266     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
267
268     aOrig->translate(aDir, aDist);
269     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
270
271     aPlane = makeRectangularFace(aFace, aNewPln);
272   }
273   return aPlane;
274 }
275
276 //==================================================================================================
277 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
278 {
279   // Get face.
280   AttributeSelectionPtr aFaceSelection = selection(PLANE());
281   GeomShapePtr aFaceShape = aFaceSelection->value();
282   if(!aFaceShape.get()) {
283     aFaceShape = aFaceSelection->context()->shape();
284   }
285   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
286
287   // Get point.
288   AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
289   GeomShapePtr aPointShape = aPointSelection->value();
290   if(!aPointShape.get()) {
291     aPointShape = aPointSelection->context()->shape();
292   }
293   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
294
295   std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
296   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
297   std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
298
299   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
300
301   return makeRectangularFace(aFace, aNewPln);
302 }
303
304 //==================================================================================================
305 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
306 {
307   // Get face.
308   AttributeSelectionPtr aFaceSelection = selection(PLANE());
309   GeomShapePtr aFaceShape = aFaceSelection->value();
310   if(!aFaceShape.get()) {
311     aFaceShape = aFaceSelection->context()->shape();
312   }
313   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
314   aFace = makeRectangularFace(aFace, aFace->getPlane());
315
316   // Get axis.
317   AttributeSelectionPtr anAxisSelection = selection(AXIS());
318   GeomShapePtr anAxisShape = anAxisSelection->value();
319   if(!anAxisShape.get()) {
320     anAxisShape = anAxisSelection->context()->shape();
321   }
322   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
323
324   std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
325                                                                                      anEdge->line()->direction()));
326
327   // Getting angle.
328   double anAngle = real(ANGLE())->value();
329
330   GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
331   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
332
333   return aRes;
334 }
335
336 //==================================================================================================
337 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
338 {
339   // Get plane 1.
340   AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
341   GeomShapePtr aFaceShape1 = aFaceSelection1->value();
342   if(!aFaceShape1.get()) {
343     aFaceShape1 = aFaceSelection1->context()->shape();
344   }
345   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
346   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
347
348   // Get plane 2.
349   AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
350   GeomShapePtr aFaceShape2 = aFaceSelection2->value();
351   if(!aFaceShape2.get()) {
352     aFaceShape2 = aFaceSelection2->context()->shape();
353   }
354   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
355   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
356
357   double aDist = aPln1->distance(aPln2) / 2.0;
358
359   std::shared_ptr<GeomAPI_Pnt> aOrig1 = aPln1->location();
360   std::shared_ptr<GeomAPI_Dir> aDir1 = aPln1->direction();
361
362   aOrig1->translate(aDir1, aDist);
363   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig1, aDir1));
364
365   if((aNewPln->distance(aPln2) - aDist) > 1.e-7) {
366     aDir1->reverse();
367     aOrig1->translate(aDir1, 2.0 * aDist);
368     aNewPln.reset(new GeomAPI_Pln(aOrig1, aDir1));
369   }
370
371   std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
372
373   return aRes;
374 }
375
376 //==================================================================================================
377 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
378                                  const std::shared_ptr<GeomAPI_Vertex> theV2,
379                                  const std::shared_ptr<GeomAPI_Vertex> theV3)
380 {
381   std::shared_ptr<GeomAPI_Face> aFace = GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
382
383   ListOfShape anObjects;
384   anObjects.push_back(theV1);
385   anObjects.push_back(theV2);
386   anObjects.push_back(theV3);
387   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
388   GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
389
390   return aRes;
391 }
392
393 //==================================================================================================
394 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
395                                                   const std::shared_ptr<GeomAPI_Pln> thePln)
396 {
397   // Create rectangular face close to the selected
398   double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
399   theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
400
401   // use all 8 points of the bounding box to find the 2D bounds
402   bool isFirst = true;
403   double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
404   for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
405     for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
406       for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
407         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
408           aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
409         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
410         if (isFirst || aPnt2d->x() < aMinX2d)
411           aMinX2d = aPnt2d->x();
412         if (isFirst || aPnt2d->y() < aMinY2d)
413           aMinY2d = aPnt2d->y();
414         if (isFirst || aPnt2d->x() > aMaxX2d)
415           aMaxX2d = aPnt2d->x();
416         if (isFirst || aPnt2d->y() > aMaxY2d)
417           aMaxY2d = aPnt2d->y();
418         if (isFirst)
419           isFirst = !isFirst;
420       }
421     }
422   }
423   double aWgap = (aMaxX2d - aMinX2d) * 0.1;
424   double aHgap = (aMaxY2d - aMinY2d) * 0.1;
425   std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
426     aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap);
427
428   return aResFace;
429 }