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