]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConstructionPlugin/ConstructionPlugin_Plane.cpp
Salome HOME
Issue #1649: Added CPP High API for Plane feature;
[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   // By general equation.
50   data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
51   data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
52   data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
53   data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
54   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
55   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
56   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
58
59   // By three points.
60   data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
61   data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
62   data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
63
64   // By line and point.
65   data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
66   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
67   data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
68
69   // By other plane.
70   data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
71   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
72   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::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()) {
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   }
107
108   if(!aShape.get()) {
109     setError("Error: Could not create a plane.");
110     return;
111   }
112
113   ResultConstructionPtr aConstr = document()->createConstruction(data());
114   aConstr->setInfinite(true);
115   aConstr->setShape(aShape);
116   setResult(aConstr);
117 }
118
119 //==================================================================================================
120 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
121                                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
122 {
123   std::vector<int> aColor;
124   // get color from the attribute of the result
125   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
126     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
127     if (aColorAttr.get() && aColorAttr->size()) {
128       aColor.push_back(aColorAttr->value(0));
129       aColor.push_back(aColorAttr->value(1));
130       aColor.push_back(aColorAttr->value(2));
131     }
132   }
133   if (aColor.empty())
134     aColor = Config_PropManager::color("Visualization", "construction_plane_color",
135                                        ConstructionPlugin_Plane::DEFAULT_COLOR());
136
137   bool isCustomized = false;
138   if (aColor.size() == 3)
139     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
140
141   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
142
143   return isCustomized;
144 }
145
146 //==================================================================================================
147 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
148 {
149   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
150   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
151   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
152   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
153   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
154   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
155       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
156       anAttrA->isInitialized() && anAttrB->isInitialized() &&
157       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
158     double aA = anAttrA->value(), aB = anAttrB->value(),
159            aC = anAttrC->value(), aD = anAttrD->value();
160     std::shared_ptr<GeomAPI_Pln> aPlane = 
161       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
162     std::string kDefaultPlaneSize = "200";
163     double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize);
164     aSize *= 4.;
165     aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
166   }
167   return aPlaneFace;
168 }
169
170 //==================================================================================================
171 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
172 {
173   // Get first point.
174   AttributeSelectionPtr aPointSelection1 = selection(POINT1());
175   GeomShapePtr aPointShape1 = aPointSelection1->value();
176   if(!aPointShape1.get()) {
177     aPointShape1 = aPointSelection1->context()->shape();
178   }
179   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
180
181   // Get second point.
182   AttributeSelectionPtr aPointSelection2 = selection(POINT2());
183   GeomShapePtr aPointShape2 = aPointSelection2->value();
184   if(!aPointShape2.get()) {
185     aPointShape2 = aPointSelection2->context()->shape();
186   }
187   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
188
189   // Get third point.
190   AttributeSelectionPtr aPointSelection3 = selection(POINT3());
191   GeomShapePtr aPointShape3 = aPointSelection3->value();
192   if(!aPointShape3.get()) {
193     aPointShape3 = aPointSelection3->context()->shape();
194   }
195   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
196
197   GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
198
199   return aRes;
200 }
201
202 //==================================================================================================
203 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
204 {
205   // Get edge.
206   AttributeSelectionPtr anEdgeSelection = selection(LINE());
207   GeomShapePtr aLineShape = anEdgeSelection->value();
208   if(!aLineShape.get()) {
209     aLineShape = anEdgeSelection->context()->shape();
210   }
211   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
212
213   // Get point.
214   AttributeSelectionPtr aPointSelection = selection(POINT());
215   GeomShapePtr aPointShape = aPointSelection->value();
216   if(!aPointShape.get()) {
217     aPointShape = aPointSelection->context()->shape();
218   }
219   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
220
221   // Get perpendicular flag.
222   bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
223
224   GeomShapePtr aRes;
225   if(anIsPerpendicular) {
226     std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
227     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
228     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
229     double aSize = aLin->distance(aPnt);
230     aSize *= 2.0;
231     aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize);
232   } else {
233     std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
234     GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
235     aRes = faceByThreeVertices(aV1, aV2, aVertex);
236   }
237
238   return aRes;
239 }
240
241 //==================================================================================================
242 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
243 {
244   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
245   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
246   std::shared_ptr<GeomAPI_Shape> aPlane;
247   if ((aFaceAttr.get() != NULL) &&
248       (aDistAttr.get() != NULL) &&
249       aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
250
251     double aDist = aDistAttr->value();
252     bool anIsReverse = boolean(REVERSE())->value();
253     if(anIsReverse) aDist = -aDist;
254     GeomShapePtr aShape = aFaceAttr->value();
255     if (!aShape.get()) {
256       aShape = aFaceAttr->context()->shape();
257     }
258
259     if(!aShape.get()) {
260       return aPlane;
261     }
262
263     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
264
265     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_FaceBuilder::plane(aFace);
266     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
267     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
268
269     aOrig->translate(aDir, aDist);
270     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
271
272     aPlane = makeRectangularFace(aFace, aNewPln);
273   }
274   return aPlane;
275 }
276
277 //==================================================================================================
278 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
279 {
280   // Get face.
281   AttributeSelectionPtr aFaceSelection = selection(PLANE());
282   GeomShapePtr aFaceShape = aFaceSelection->value();
283   if(!aFaceShape.get()) {
284     aFaceShape = aFaceSelection->context()->shape();
285   }
286   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
287
288   // Get point.
289   AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
290   GeomShapePtr aPointShape = aPointSelection->value();
291   if(!aPointShape.get()) {
292     aPointShape = aPointSelection->context()->shape();
293   }
294   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
295
296   std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
297   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
298   std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
299
300   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
301
302   return makeRectangularFace(aFace, aNewPln);
303 }
304
305 //==================================================================================================
306 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
307 {
308   // Get face.
309   AttributeSelectionPtr aFaceSelection = selection(PLANE());
310   GeomShapePtr aFaceShape = aFaceSelection->value();
311   if(!aFaceShape.get()) {
312     aFaceShape = aFaceSelection->context()->shape();
313   }
314   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
315   aFace = makeRectangularFace(aFace, aFace->getPlane());
316
317   // Get axis.
318   AttributeSelectionPtr anAxisSelection = selection(AXIS());
319   GeomShapePtr anAxisShape = anAxisSelection->value();
320   if(!anAxisShape.get()) {
321     anAxisShape = anAxisSelection->context()->shape();
322   }
323   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
324
325   std::shared_ptr<GeomAPI_Ax1> anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
326                                                                                      anEdge->line()->direction()));
327
328   // Getting angle.
329   double anAngle = real(ANGLE())->value();
330
331   GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
332   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
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 }