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