]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConstructionPlugin/ConstructionPlugin_Plane.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ConstructionPlugin / ConstructionPlugin_Plane.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "ConstructionPlugin_Plane.h"
21
22 #include <Config_PropManager.h>
23
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAlgoAPI_Rotation.h>
26 #include <GeomAlgoAPI_ShapeTools.h>
27
28 #include <GeomAPI_Ax1.h>
29 #include <GeomAPI_Edge.h>
30 #include <GeomAPI_Face.h>
31 #include <GeomAPI_Lin.h>
32 #include <GeomAPI_Pln.h>
33 #include <GeomAPI_Pnt.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_Vertex.h>
36 #include <GeomAPI_XYZ.h>
37
38 #include <ModelAPI_AttributeDouble.h>
39 #include <ModelAPI_AttributeIntArray.h>
40 #include <ModelAPI_AttributeSelection.h>
41 #include <ModelAPI_AttributeString.h>
42 #include <ModelAPI_AttributeBoolean.h>
43 #include <ModelAPI_ResultConstruction.h>
44 #include <ModelAPI_Session.h>
45 #include <ModelAPI_Validator.h>
46
47 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
48                                         const std::shared_ptr<GeomAPI_Vertex> theV2,
49                                         const std::shared_ptr<GeomAPI_Vertex> theV3);
50 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
51   const std::shared_ptr<GeomAPI_Face> theFace,
52   const std::shared_ptr<GeomAPI_Pln> thePln);
53
54 //==================================================================================================
55 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
56 {
57 }
58
59 //==================================================================================================
60 void ConstructionPlugin_Plane::initAttributes()
61 {
62   data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
63                        ModelAPI_AttributeString::typeId());
64
65   data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
66   data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
67   // By general equation.
68   data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
69   data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
70   data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
71   data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
73   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
75   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
76
77   // By three points.
78   data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
79   data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
80   data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
81
82   // By line and point.
83   data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
84   data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
85   data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
86
87   // By other plane.
88   data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
89   data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
90   data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
91   data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
92   data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
93
94   // By two parallel planes.
95   data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
96   data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
97 }
98
99 //==================================================================================================
100 void ConstructionPlugin_Plane::execute()
101 {
102   GeomShapePtr aShape;
103
104   std::string aCreationMethod = string(CREATION_METHOD())->value();
105   if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
106       aCreationMethod == "PlaneByGeneralEquation") {
107     aShape = createByGeneralEquation();
108   } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
109     aShape = createByThreePoints();
110   } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
111     aShape = createByLineAndPoint();
112   } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
113     std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
114     if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
115       aShape = createByDistanceFromOther();
116     } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
117       aShape = createByCoincidentPoint();
118     } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
119       aShape = createByRotation();
120     }
121   } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
122     aShape = createByTwoParallelPlanes();
123   } else {
124     setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
125     return;
126   }
127
128   if(!aShape.get()) {
129     setError("Error: Could not create a plane.");
130     return;
131   }
132
133   ResultConstructionPtr aConstr = document()->createConstruction(data());
134   aConstr->setInfinite(true);
135   aConstr->setShape(aShape);
136   setResult(aConstr);
137 }
138
139 //==================================================================================================
140 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
141                                                 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
142 {
143   std::vector<int> aColor;
144   // get color from the attribute of the result
145   if (theResult.get() != NULL &&
146       theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
147     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
148     if (aColorAttr.get() && aColorAttr->size()) {
149       aColor.push_back(aColorAttr->value(0));
150       aColor.push_back(aColorAttr->value(1));
151       aColor.push_back(aColorAttr->value(2));
152     }
153   }
154   if (aColor.empty())
155     aColor = Config_PropManager::color("Visualization", "construction_plane_color");
156
157   bool isCustomized = false;
158   if (aColor.size() == 3)
159     isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
160
161   isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
162
163   return isCustomized;
164 }
165
166 //==================================================================================================
167 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
168 {
169   AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
170   AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
171   AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
172   AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
173   std::shared_ptr<GeomAPI_Shape> aPlaneFace;
174   if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
175       (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
176       anAttrA->isInitialized() && anAttrB->isInitialized() &&
177       anAttrC->isInitialized() && anAttrD->isInitialized() ) {
178     double aA = anAttrA->value(), aB = anAttrB->value(),
179            aC = anAttrC->value(), aD = anAttrD->value();
180     std::shared_ptr<GeomAPI_Pln> aPlane =
181       std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
182     double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size");
183     aSize *= 4.;
184     aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
185   }
186   return aPlaneFace;
187 }
188
189 //==================================================================================================
190 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
191 {
192   // Get first point.
193   AttributeSelectionPtr aPointSelection1 = selection(POINT1());
194   GeomShapePtr aPointShape1 = aPointSelection1->value();
195   if(!aPointShape1.get()) {
196     aPointShape1 = aPointSelection1->context()->shape();
197   }
198   std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
199
200   // Get second point.
201   AttributeSelectionPtr aPointSelection2 = selection(POINT2());
202   GeomShapePtr aPointShape2 = aPointSelection2->value();
203   if(!aPointShape2.get()) {
204     aPointShape2 = aPointSelection2->context()->shape();
205   }
206   std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
207
208   // Get third point.
209   AttributeSelectionPtr aPointSelection3 = selection(POINT3());
210   GeomShapePtr aPointShape3 = aPointSelection3->value();
211   if(!aPointShape3.get()) {
212     aPointShape3 = aPointSelection3->context()->shape();
213   }
214   std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
215
216   GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
217
218   return aRes;
219 }
220
221 //==================================================================================================
222 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
223 {
224   // Get edge.
225   AttributeSelectionPtr anEdgeSelection = selection(LINE());
226   GeomShapePtr aLineShape = anEdgeSelection->value();
227   if(!aLineShape.get()) {
228     aLineShape = anEdgeSelection->context()->shape();
229   }
230   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aLineShape));
231
232   // Get point.
233   AttributeSelectionPtr aPointSelection = selection(POINT());
234   GeomShapePtr aPointShape = aPointSelection->value();
235   if(!aPointShape.get()) {
236     aPointShape = aPointSelection->context()->shape();
237   }
238   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
239
240   // Get perpendicular flag.
241   bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
242
243   GeomShapePtr aRes;
244   if(anIsPerpendicular) {
245     std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
246     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
247     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
248     double aSize = aLin->distance(aPnt) * 2;
249     // point may belong to line, so for the face size use maximum distance between point and line
250     // and the line size (distance between the start and end point)
251     double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
252     aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
253   } else {
254     std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
255     GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
256     aRes = faceByThreeVertices(aV1, aV2, aVertex);
257   }
258
259   return aRes;
260 }
261
262 //==================================================================================================
263 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
264 {
265   AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
266   AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
267   std::shared_ptr<GeomAPI_Shape> aPlane;
268   if ((aFaceAttr.get() != NULL) &&
269       (aDistAttr.get() != NULL) &&
270       aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
271
272     double aDist = aDistAttr->value();
273     bool anIsReverse = boolean(REVERSE())->value();
274     if(anIsReverse) aDist = -aDist;
275     GeomShapePtr aShape = aFaceAttr->value();
276     if (!aShape.get()) {
277       aShape = aFaceAttr->context()->shape();
278     }
279
280     if(!aShape.get()) {
281       return aPlane;
282     }
283
284     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShape));
285
286     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
287     std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
288     std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
289
290     aOrig->translate(aDir, aDist);
291     std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
292
293     aPlane = makeRectangularFace(aFace, aNewPln);
294   }
295   return aPlane;
296 }
297
298 //==================================================================================================
299 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
300 {
301   // Get face.
302   AttributeSelectionPtr aFaceSelection = selection(PLANE());
303   GeomShapePtr aFaceShape = aFaceSelection->value();
304   if(!aFaceShape.get()) {
305     aFaceShape = aFaceSelection->context()->shape();
306   }
307   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
308
309   // Get point.
310   AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
311   GeomShapePtr aPointShape = aPointSelection->value();
312   if(!aPointShape.get()) {
313     aPointShape = aPointSelection->context()->shape();
314   }
315   std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
316
317   std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
318   std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
319   std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
320
321   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
322
323   return makeRectangularFace(aFace, aNewPln);
324 }
325
326 //==================================================================================================
327 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
328 {
329   // Get face.
330   AttributeSelectionPtr aFaceSelection = selection(PLANE());
331   GeomShapePtr aFaceShape = aFaceSelection->value();
332   if(!aFaceShape.get()) {
333     aFaceShape = aFaceSelection->context()->shape();
334   }
335   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aFaceShape));
336   aFace = makeRectangularFace(aFace, aFace->getPlane());
337
338   // Get axis.
339   AttributeSelectionPtr anAxisSelection = selection(AXIS());
340   GeomShapePtr anAxisShape = anAxisSelection->value();
341   if(!anAxisShape.get()) {
342     anAxisShape = anAxisSelection->context()->shape();
343   }
344   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(anAxisShape));
345
346   std::shared_ptr<GeomAPI_Ax1> anAxis =
347     std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
348                                                  anEdge->line()->direction()));
349
350   // Getting angle.
351   double anAngle = real(ANGLE())->value();
352
353   GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle);
354   if (!aRotationAlgo.check()) {
355     setError(aRotationAlgo.getError());
356     return GeomShapePtr();
357   }
358   aRotationAlgo.build();
359   if (!aRotationAlgo.isDone()) {
360     setError("Error: Failed to rotate plane");
361     return GeomShapePtr();
362   }
363
364   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo.shape()));
365   return aRes;
366 }
367
368 //==================================================================================================
369 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
370 {
371   // Get plane 1.
372   AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
373   GeomShapePtr aFaceShape1 = aFaceSelection1->value();
374   if(!aFaceShape1.get()) {
375     aFaceShape1 = aFaceSelection1->context()->shape();
376   }
377   std::shared_ptr<GeomAPI_Face> aFace1(new GeomAPI_Face(aFaceShape1));
378   std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
379
380   // Get plane 2.
381   AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
382   GeomShapePtr aFaceShape2 = aFaceSelection2->value();
383   if(!aFaceShape2.get()) {
384     aFaceShape2 = aFaceSelection2->context()->shape();
385   }
386   std::shared_ptr<GeomAPI_Face> aFace2(new GeomAPI_Face(aFaceShape2));
387   std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
388
389   std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
390   std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
391
392   std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
393     aPntOnPln2->xyz())->multiplied(0.5)));
394
395   std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
396
397   std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
398
399   return aRes;
400 }
401
402 //==================================================================================================
403 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
404                                  const std::shared_ptr<GeomAPI_Vertex> theV2,
405                                  const std::shared_ptr<GeomAPI_Vertex> theV3)
406 {
407   std::shared_ptr<GeomAPI_Face> aFace =
408     GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
409
410   ListOfShape anObjects;
411   anObjects.push_back(theV1);
412   anObjects.push_back(theV2);
413   anObjects.push_back(theV3);
414   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
415     GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
416   GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
417
418   return aRes;
419 }
420
421 //==================================================================================================
422 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
423                                                   const std::shared_ptr<GeomAPI_Pln> thePln)
424 {
425   // Create rectangular face close to the selected
426   double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
427   theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
428
429   // use all 8 points of the bounding box to find the 2D bounds
430   bool isFirst = true;
431   double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
432   for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
433     for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
434       for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
435         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
436           aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
437         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
438         if (isFirst || aPnt2d->x() < aMinX2d)
439           aMinX2d = aPnt2d->x();
440         if (isFirst || aPnt2d->y() < aMinY2d)
441           aMinY2d = aPnt2d->y();
442         if (isFirst || aPnt2d->x() > aMaxX2d)
443           aMaxX2d = aPnt2d->x();
444         if (isFirst || aPnt2d->y() > aMaxY2d)
445           aMaxY2d = aPnt2d->y();
446         if (isFirst)
447           isFirst = !isFirst;
448       }
449     }
450   }
451   double aWgap = (aMaxX2d - aMinX2d) * 0.1;
452   double aHgap = (aMaxY2d - aMinY2d) * 0.1;
453   std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
454     aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
455     aMaxY2d - aMinY2d + 2. * aHgap);
456
457   return aResFace;
458 }