1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ConstructionPlugin_Plane.h"
22 #include <Config_PropManager.h>
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAlgoAPI_Rotation.h>
26 #include <GeomAlgoAPI_ShapeTools.h>
27 #include <GeomAlgoAPI_Tools.h>
29 #include <GeomAPI_Ax1.h>
30 #include <GeomAPI_Edge.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_Lin.h>
33 #include <GeomAPI_Pln.h>
34 #include <GeomAPI_Pnt.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_Vertex.h>
38 #include <GeomAPI_XYZ.h>
40 #include <ModelAPI_AttributeDouble.h>
41 #include <ModelAPI_AttributeIntArray.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeBoolean.h>
45 #include <ModelAPI_ResultConstruction.h>
46 #include <ModelAPI_Session.h>
47 #include <ModelAPI_Validator.h>
50 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
51 const std::shared_ptr<GeomAPI_Vertex> theV2,
52 const std::shared_ptr<GeomAPI_Vertex> theV3);
53 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
54 const std::shared_ptr<GeomAPI_Face> theFace,
55 const std::shared_ptr<GeomAPI_Pln> thePln);
57 //==================================================================================================
58 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
62 //==================================================================================================
63 void ConstructionPlugin_Plane::initAttributes()
65 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
66 ModelAPI_AttributeString::typeId());
68 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
69 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
70 // By general equation.
71 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
72 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
73 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
74 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
77 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
78 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
81 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
82 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
83 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
86 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
87 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
88 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
91 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
92 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
93 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
94 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
95 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
97 // By two parallel planes.
98 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
99 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
102 //==================================================================================================
103 void ConstructionPlugin_Plane::execute()
107 std::string aCreationMethod = string(CREATION_METHOD())->value();
108 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
109 aCreationMethod == "PlaneByGeneralEquation") {
110 aShape = createByGeneralEquation();
111 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
112 aShape = createByThreePoints();
113 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
114 aShape = createByLineAndPoint();
115 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
116 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
117 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
118 aShape = createByDistanceFromOther();
119 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
120 aShape = createByCoincidentPoint();
121 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
122 aShape = createByRotation();
124 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
125 aShape = createByTwoParallelPlanes();
127 setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
132 setError("Error: Could not create a plane.");
136 ResultConstructionPtr aConstr = document()->createConstruction(data());
137 aConstr->setInfinite(true);
138 aConstr->setShape(aShape);
142 //==================================================================================================
143 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
144 std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
146 std::vector<int> aColor;
147 // get color from the attribute of the result
148 if (theResult.get() != NULL &&
149 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
150 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
151 if (aColorAttr.get() && aColorAttr->size()) {
152 aColor.push_back(aColorAttr->value(0));
153 aColor.push_back(aColorAttr->value(1));
154 aColor.push_back(aColorAttr->value(2));
158 aColor = Config_PropManager::color("Visualization", "construction_plane_color");
160 bool isCustomized = false;
161 if (aColor.size() == 3)
162 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
164 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
169 //==================================================================================================
170 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
172 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
173 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
174 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
175 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
176 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
177 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
178 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
179 anAttrA->isInitialized() && anAttrB->isInitialized() &&
180 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
181 double aA = anAttrA->value(), aB = anAttrB->value(),
182 aC = anAttrC->value(), aD = anAttrD->value();
183 std::shared_ptr<GeomAPI_Pln> aPlane =
184 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
185 double aSize = Config_PropManager::real(SKETCH_TAB_NAME, "planes_size");
187 aSize = 200; // Set default value
189 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
194 //==================================================================================================
195 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
198 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
199 GeomShapePtr aPointShape1 = aPointSelection1->value();
200 if(!aPointShape1.get()) {
201 aPointShape1 = aPointSelection1->context()->shape();
203 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
206 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
207 GeomShapePtr aPointShape2 = aPointSelection2->value();
208 if(!aPointShape2.get()) {
209 aPointShape2 = aPointSelection2->context()->shape();
211 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
214 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
215 GeomShapePtr aPointShape3 = aPointSelection3->value();
216 if(!aPointShape3.get()) {
217 aPointShape3 = aPointSelection3->context()->shape();
219 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
221 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
226 //==================================================================================================
227 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
230 AttributeSelectionPtr anEdgeSelection = selection(LINE());
231 GeomShapePtr aLineShape = anEdgeSelection->value();
232 if(!aLineShape.get()) {
233 aLineShape = anEdgeSelection->context()->shape();
235 std::shared_ptr<GeomAPI_Edge> anEdge;
236 if (aLineShape->isEdge()) {
237 anEdge = aLineShape->edge();
239 else if (aLineShape->isCompound()) {
240 GeomAPI_ShapeIterator anIt(aLineShape);
241 anEdge = anIt.current()->edge();
244 return GeomShapePtr();
247 AttributeSelectionPtr aPointSelection = selection(POINT());
248 GeomShapePtr aPointShape = aPointSelection->value();
249 if(!aPointShape.get()) {
250 aPointShape = aPointSelection->context()->shape();
252 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
254 // Get perpendicular flag.
255 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
258 if(anIsPerpendicular) {
259 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
260 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
261 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
262 double aSize = aLin->distance(aPnt) * 2;
263 // point may belong to line, so for the face size use maximum distance between point and line
264 // and the line size (distance between the start and end point)
265 double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
266 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
268 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
269 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
270 aRes = faceByThreeVertices(aV1, aV2, aVertex);
276 //==================================================================================================
277 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByDistanceFromOther()
279 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
280 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
281 std::shared_ptr<GeomAPI_Shape> aPlane;
282 if ((aFaceAttr.get() != NULL) &&
283 (aDistAttr.get() != NULL) &&
284 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
286 double aDist = aDistAttr->value();
287 bool anIsReverse = boolean(REVERSE())->value();
288 if(anIsReverse) aDist = -aDist;
289 GeomShapePtr aShape = aFaceAttr->value();
290 if (!aShape.get() && aFaceAttr->context()) {
291 aShape = aFaceAttr->context()->shape();
298 std::shared_ptr<GeomAPI_Face> aFace;
299 if (aShape->isFace()) {
300 aFace = aShape->face();
302 else if (aShape->isCompound()) {
303 GeomAPI_ShapeIterator anIt(aShape);
304 aFace = anIt.current()->face();
307 return GeomShapePtr();
309 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
310 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
311 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
313 aOrig->translate(aDir, aDist);
314 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
316 aPlane = makeRectangularFace(aFace, aNewPln);
321 //==================================================================================================
322 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
325 AttributeSelectionPtr aFaceSelection = selection(PLANE());
326 GeomShapePtr aFaceShape = aFaceSelection->value();
327 if(!aFaceShape.get()) {
328 aFaceShape = aFaceSelection->context()->shape();
330 std::shared_ptr<GeomAPI_Face> aFace;
331 if (aFaceShape->isFace()) {
332 aFace = aFaceShape->face();
334 else if (aFaceShape->isCompound()) {
335 GeomAPI_ShapeIterator anIt(aFaceShape);
336 aFace = anIt.current()->face();
340 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
341 GeomShapePtr aPointShape = aPointSelection->value();
342 if(!aPointShape.get()) {
343 aPointShape = aPointSelection->context()->shape();
345 std::shared_ptr<GeomAPI_Vertex> aVertex = aPointShape->vertex();
347 return GeomShapePtr();
349 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
350 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
351 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
353 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
355 return makeRectangularFace(aFace, aNewPln);
358 //==================================================================================================
359 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByRotation()
362 AttributeSelectionPtr aFaceSelection = selection(PLANE());
363 GeomShapePtr aFaceShape = aFaceSelection->value();
364 if(!aFaceShape.get()) {
365 aFaceShape = aFaceSelection->context()->shape();
367 std::shared_ptr<GeomAPI_Face> aFace;
368 if (aFaceShape->isFace()) {
369 aFace = aFaceShape->face();
371 else if (aFaceShape->isCompound()) {
372 GeomAPI_ShapeIterator anIt(aFaceShape);
373 aFace = anIt.current()->face();
376 return GeomShapePtr();
377 aFace = makeRectangularFace(aFace, aFace->getPlane());
380 AttributeSelectionPtr anAxisSelection = selection(AXIS());
381 GeomShapePtr anAxisShape = anAxisSelection->value();
382 if(!anAxisShape.get()) {
383 anAxisShape = anAxisSelection->context()->shape();
385 std::shared_ptr<GeomAPI_Edge> anEdge;
386 if (anAxisShape->isEdge()) {
387 anEdge = anAxisShape->edge();
389 else if (anAxisShape->isCompound()) {
390 GeomAPI_ShapeIterator anIt(anAxisShape);
391 anEdge = anIt.current()->edge();
394 return GeomShapePtr();
396 std::shared_ptr<GeomAPI_Ax1> anAxis =
397 std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
398 anEdge->line()->direction()));
401 double anAngle = real(ANGLE())->value();
403 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(
404 new GeomAlgoAPI_Rotation(aFace, anAxis, anAngle));
405 // Checking that the algorithm worked properly.
407 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
408 setError("Error: Failed to rotate plane");
409 return GeomShapePtr();
412 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo->shape()));
416 //==================================================================================================
417 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
420 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
421 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
422 if(!aFaceShape1.get()) {
423 aFaceShape1 = aFaceSelection1->context()->shape();
425 std::shared_ptr<GeomAPI_Face> aFace1;
426 if (aFaceShape1->isFace()) {
427 aFace1 = aFaceShape1->face();
429 else if (aFaceShape1->isCompound()) {
430 GeomAPI_ShapeIterator anIt(aFaceShape1);
431 aFace1 = anIt.current()->face();
434 return GeomShapePtr();
435 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
438 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
439 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
440 if(!aFaceShape2.get()) {
441 aFaceShape2 = aFaceSelection2->context()->shape();
443 std::shared_ptr<GeomAPI_Face> aFace2;
444 if (aFaceShape2->isFace()) {
445 aFace2 = aFaceShape2->face();
447 else if (aFaceShape2->isCompound()) {
448 GeomAPI_ShapeIterator anIt(aFaceShape2);
449 aFace2 = anIt.current()->face();
452 return GeomShapePtr();
453 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
455 std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
456 std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
458 std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
459 aPntOnPln2->xyz())->multiplied(0.5)));
461 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
463 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
468 //==================================================================================================
469 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
470 const std::shared_ptr<GeomAPI_Vertex> theV2,
471 const std::shared_ptr<GeomAPI_Vertex> theV3)
473 std::shared_ptr<GeomAPI_Face> aFace =
474 GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
476 ListOfShape anObjects;
477 anObjects.push_back(theV1);
478 anObjects.push_back(theV2);
479 anObjects.push_back(theV3);
480 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
481 GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
482 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
487 //==================================================================================================
488 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
489 const std::shared_ptr<GeomAPI_Pln> thePln)
491 // Create rectangular face close to the selected
492 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
493 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
495 // use all 8 points of the bounding box to find the 2D bounds
497 double aMinX2d, aMaxX2d, aMinY2d, aMaxY2d;
498 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
499 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
500 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
501 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
502 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
503 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
504 if (isFirst || aPnt2d->x() < aMinX2d)
505 aMinX2d = aPnt2d->x();
506 if (isFirst || aPnt2d->y() < aMinY2d)
507 aMinY2d = aPnt2d->y();
508 if (isFirst || aPnt2d->x() > aMaxX2d)
509 aMaxX2d = aPnt2d->x();
510 if (isFirst || aPnt2d->y() > aMaxY2d)
511 aMaxY2d = aPnt2d->y();
517 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
518 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
519 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
520 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
521 aMaxY2d - aMinY2d + 2. * aHgap);