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