1 // Copyright (C) 2014-2023 CEA, EDF
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_AttributeInteger.h>
46 #include <ModelAPI_ResultConstruction.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Validator.h>
53 static GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
54 const std::shared_ptr<GeomAPI_Vertex> theV2,
55 const std::shared_ptr<GeomAPI_Vertex> theV3);
56 static std::shared_ptr<GeomAPI_Face> makeRectangularFace(
57 const std::shared_ptr<GeomAPI_Face> theFace,
58 const std::shared_ptr<GeomAPI_Pln> thePln);
60 //==================================================================================================
61 ConstructionPlugin_Plane::ConstructionPlugin_Plane()
65 //==================================================================================================
66 void ConstructionPlugin_Plane::initAttributes()
68 data()->addAttribute(ConstructionPlugin_Plane::CREATION_METHOD(),
69 ModelAPI_AttributeString::typeId());
71 data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId());
72 data()->addAttribute(DISTANCE(), ModelAPI_AttributeDouble::typeId());
73 // By general equation.
74 data()->addAttribute(A(), ModelAPI_AttributeDouble::typeId());
75 data()->addAttribute(B(), ModelAPI_AttributeDouble::typeId());
76 data()->addAttribute(C(), ModelAPI_AttributeDouble::typeId());
77 data()->addAttribute(D(), ModelAPI_AttributeDouble::typeId());
78 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), A());
79 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), B());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), C());
81 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), D());
84 data()->addAttribute(POINT1(), ModelAPI_AttributeSelection::typeId());
85 data()->addAttribute(POINT2(), ModelAPI_AttributeSelection::typeId());
86 data()->addAttribute(POINT3(), ModelAPI_AttributeSelection::typeId());
89 data()->addAttribute(LINE(), ModelAPI_AttributeSelection::typeId());
90 data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId());
91 data()->addAttribute(PERPENDICULAR(), ModelAPI_AttributeBoolean::typeId());
94 data()->addAttribute(CREATION_METHOD_BY_OTHER_PLANE_OPTION(), ModelAPI_AttributeString::typeId());
95 data()->addAttribute(REVERSE(), ModelAPI_AttributeBoolean::typeId());
96 data()->addAttribute(COINCIDENT_POINT(), ModelAPI_AttributeSelection::typeId());
97 data()->addAttribute(AXIS(), ModelAPI_AttributeSelection::typeId());
98 data()->addAttribute(ANGLE(), ModelAPI_AttributeDouble::typeId());
100 // By two parallel planes.
101 data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId());
102 data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId());
105 AttributeIntegerPtr aNbCopies = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(
106 data()->addAttribute(NB_COPIES(), ModelAPI_AttributeInteger::typeId()));
108 if (!aNbCopies->isInitialized())
109 aNbCopies->setValue(1);
112 //==================================================================================================
113 void ConstructionPlugin_Plane::execute()
117 std::string aCreationMethod = string(CREATION_METHOD())->value();
118 if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() ||
119 aCreationMethod == "PlaneByGeneralEquation") {
120 aShapes.push_back(createByGeneralEquation());
121 } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) {
122 aShapes.push_back(createByThreePoints());
123 } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) {
124 aShapes.push_back(createByLineAndPoint());
125 } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) {
126 std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value();
127 if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) {
128 createByDistanceFromOther(aShapes);
129 } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) {
130 aShapes.push_back(createByCoincidentPoint());
131 } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) {
132 createByRotation(aShapes);
134 } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) {
135 aShapes.push_back(createByTwoParallelPlanes());
137 setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported.");
141 if(aShapes.size() == 0) {
142 setError("Error: Could not create a plane.");
147 for (auto aShapeIter = aShapes.begin(); aShapeIter != aShapes.end(); ++aShapeIter, ++anIndex)
149 if (!aShapeIter->get())
151 setError("Error: Could not create a plane.");
154 ResultConstructionPtr aConstr = document()->createConstruction(data(), anIndex);
155 aConstr->setInfinite(true);
156 aConstr->setShape(*aShapeIter);
157 setResult(aConstr, anIndex);
159 removeResults(anIndex);
162 //==================================================================================================
163 bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs)
165 std::vector<int> aColor;
166 // get color from the attribute of the result
167 if (theResult.get() != NULL &&
168 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
169 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
170 if (aColorAttr.get() && aColorAttr->size()) {
171 aColor.push_back(aColorAttr->value(0));
172 aColor.push_back(aColorAttr->value(1));
173 aColor.push_back(aColorAttr->value(2));
177 aColor = Config_PropManager::color("Visualization", COLOR_NAME());
179 bool isCustomized = false;
180 if (aColor.size() == 3)
181 isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
183 isCustomized = thePrs->setTransparensy(0.6) || isCustomized;
188 //==================================================================================================
189 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByGeneralEquation()
191 AttributeDoublePtr anAttrA = real(ConstructionPlugin_Plane::A());
192 AttributeDoublePtr anAttrB = real(ConstructionPlugin_Plane::B());
193 AttributeDoublePtr anAttrC = real(ConstructionPlugin_Plane::C());
194 AttributeDoublePtr anAttrD = real(ConstructionPlugin_Plane::D());
195 std::shared_ptr<GeomAPI_Shape> aPlaneFace;
196 if ((anAttrA.get() != NULL) && (anAttrB.get() != NULL) &&
197 (anAttrC.get() != NULL) && (anAttrD.get() != NULL) &&
198 anAttrA->isInitialized() && anAttrB->isInitialized() &&
199 anAttrC->isInitialized() && anAttrD->isInitialized() ) {
200 double aA = anAttrA->value(), aB = anAttrB->value(),
201 aC = anAttrC->value(), aD = anAttrD->value();
202 std::shared_ptr<GeomAPI_Pln> aPlane =
203 std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
204 double aSize = Config_PropManager::real(SKETCH_TAB_NAME, "planes_size");
206 aSize = 200; // Set default value
208 aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize);
212 //==================================================================================================
213 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByThreePoints()
216 AttributeSelectionPtr aPointSelection1 = selection(POINT1());
217 GeomShapePtr aPointShape1 = aPointSelection1->value();
218 if(!aPointShape1.get()) {
219 aPointShape1 = aPointSelection1->context()->shape();
221 std::shared_ptr<GeomAPI_Vertex> aVertex1(new GeomAPI_Vertex(aPointShape1));
224 AttributeSelectionPtr aPointSelection2 = selection(POINT2());
225 GeomShapePtr aPointShape2 = aPointSelection2->value();
226 if(!aPointShape2.get()) {
227 aPointShape2 = aPointSelection2->context()->shape();
229 std::shared_ptr<GeomAPI_Vertex> aVertex2(new GeomAPI_Vertex(aPointShape2));
232 AttributeSelectionPtr aPointSelection3 = selection(POINT3());
233 GeomShapePtr aPointShape3 = aPointSelection3->value();
234 if(!aPointShape3.get()) {
235 aPointShape3 = aPointSelection3->context()->shape();
237 std::shared_ptr<GeomAPI_Vertex> aVertex3(new GeomAPI_Vertex(aPointShape3));
239 GeomShapePtr aRes = faceByThreeVertices(aVertex1, aVertex2, aVertex3);
244 //==================================================================================================
245 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByLineAndPoint()
248 AttributeSelectionPtr anEdgeSelection = selection(LINE());
249 GeomShapePtr aLineShape = anEdgeSelection->value();
250 if(!aLineShape.get()) {
251 aLineShape = anEdgeSelection->context()->shape();
253 std::shared_ptr<GeomAPI_Edge> anEdge;
254 if (aLineShape->isEdge()) {
255 anEdge = aLineShape->edge();
257 else if (aLineShape->isCompound()) {
258 GeomAPI_ShapeIterator anIt(aLineShape);
259 anEdge = anIt.current()->edge();
262 return GeomShapePtr();
265 AttributeSelectionPtr aPointSelection = selection(POINT());
266 GeomShapePtr aPointShape = aPointSelection->value();
267 if(!aPointShape.get()) {
268 aPointShape = aPointSelection->context()->shape();
270 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aPointShape));
272 // Get perpendicular flag.
273 bool anIsPerpendicular= boolean(PERPENDICULAR())->value();
276 if(anIsPerpendicular) {
277 std::shared_ptr<GeomAPI_Lin> aLin = anEdge->line();
278 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
279 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aPnt, aLin->direction()));
280 double aSize = aLin->distance(aPnt) * 2;
281 // point may belong to line, so for the face size use maximum distance between point and line
282 // and the line size (distance between the start and end point)
283 double aDistance = anEdge->firstPoint()->distance(anEdge->lastPoint());
284 aRes = GeomAlgoAPI_FaceBuilder::squareFace(aNewPln, aSize > aDistance ? aSize : aDistance);
286 std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
287 GeomAlgoAPI_ShapeTools::findBounds(anEdge, aV1, aV2);
288 aRes = faceByThreeVertices(aV1, aV2, aVertex);
294 //==================================================================================================
295 void ConstructionPlugin_Plane::createByDistanceFromOther(ListOfShape& theShapes)
297 AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE());
298 AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE());
299 AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES());
300 if ((aFaceAttr.get() != NULL) &&
301 (aDistAttr.get() != NULL) && (aNbCopyAttr.get() != NULL) &&
302 aFaceAttr->isInitialized() && aDistAttr->isInitialized()) {
304 double aDist = aDistAttr->value();
305 bool anIsReverse = boolean(REVERSE())->value();
306 int aNumOfCopies = aNbCopyAttr->value();
307 if(anIsReverse) aDist = -aDist;
308 GeomShapePtr aShape = aFaceAttr->value();
309 if (!aShape.get() && aFaceAttr->context()) {
310 aShape = aFaceAttr->context()->shape();
317 std::shared_ptr<GeomAPI_Face> aFace;
318 if (aShape->isFace()) {
319 aFace = aShape->face();
321 else if (aShape->isCompound()) {
322 GeomAPI_ShapeIterator anIt(aShape);
323 aFace = anIt.current()->face();
328 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
329 std::shared_ptr<GeomAPI_Pnt> aOrig = aPln->location();
330 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
332 for (int aNbCopy = 0; aNbCopy < aNumOfCopies; ++aNbCopy)
334 std::shared_ptr<GeomAPI_Shape> aPlane;
335 aOrig->translate(aDir, aDist);
336 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aOrig, aDir));
338 aPlane = makeRectangularFace(aFace, aNewPln);
339 theShapes.push_back(aPlane);
344 //==================================================================================================
345 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByCoincidentPoint()
348 AttributeSelectionPtr aFaceSelection = selection(PLANE());
349 GeomShapePtr aFaceShape = aFaceSelection->value();
350 if(!aFaceShape.get()) {
351 aFaceShape = aFaceSelection->context()->shape();
353 std::shared_ptr<GeomAPI_Face> aFace;
354 if (aFaceShape->isFace()) {
355 aFace = aFaceShape->face();
357 else if (aFaceShape->isCompound()) {
358 GeomAPI_ShapeIterator anIt(aFaceShape);
359 aFace = anIt.current()->face();
363 AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT());
364 GeomShapePtr aPointShape = aPointSelection->value();
365 if(!aPointShape.get()) {
366 aPointShape = aPointSelection->context()->shape();
368 std::shared_ptr<GeomAPI_Vertex> aVertex = aPointShape->vertex();
370 return GeomShapePtr();
372 std::shared_ptr<GeomAPI_Pnt> anOrig = aVertex->point();
373 std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
374 std::shared_ptr<GeomAPI_Dir> aDir = aPln->direction();
376 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(anOrig, aDir));
378 return makeRectangularFace(aFace, aNewPln);
381 //==================================================================================================
382 void ConstructionPlugin_Plane::createByRotation(ListOfShape& theShapes)
385 AttributeSelectionPtr aFaceSelection = selection(PLANE());
386 GeomShapePtr aFaceShape = aFaceSelection->value();
387 if(!aFaceShape.get()) {
388 aFaceShape = aFaceSelection->context()->shape();
390 std::shared_ptr<GeomAPI_Face> aFace;
391 if (aFaceShape->isFace()) {
392 aFace = aFaceShape->face();
394 else if (aFaceShape->isCompound()) {
395 GeomAPI_ShapeIterator anIt(aFaceShape);
396 aFace = anIt.current()->face();
400 aFace = makeRectangularFace(aFace, aFace->getPlane());
403 AttributeSelectionPtr anAxisSelection = selection(AXIS());
404 GeomShapePtr anAxisShape = anAxisSelection->value();
405 if(!anAxisShape.get()) {
406 anAxisShape = anAxisSelection->context()->shape();
408 std::shared_ptr<GeomAPI_Edge> anEdge;
409 if (anAxisShape->isEdge()) {
410 anEdge = anAxisShape->edge();
412 else if (anAxisShape->isCompound()) {
413 GeomAPI_ShapeIterator anIt(anAxisShape);
414 anEdge = anIt.current()->edge();
419 AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES());
421 if (!aNbCopyAttr.get())
424 aNBCopy = aNbCopyAttr->value();
426 std::shared_ptr<GeomAPI_Ax1> anAxis =
427 std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
428 anEdge->line()->direction()));
431 double anAngle = real(ANGLE())->value();
433 for (int anIndex = 1; anIndex <= aNBCopy; ++anIndex)
435 std::shared_ptr<GeomAlgoAPI_Rotation> aRotationAlgo(
436 new GeomAlgoAPI_Rotation(aFace, anAxis, anAngle * anIndex));
437 // Checking that the algorithm worked properly.
439 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) {
440 setError("Error: Failed to rotate plane");
444 std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face(aRotationAlgo->shape()));
445 theShapes.push_back(aRes);
449 //==================================================================================================
450 std::shared_ptr<GeomAPI_Shape> ConstructionPlugin_Plane::createByTwoParallelPlanes()
453 AttributeSelectionPtr aFaceSelection1 = selection(PLANE1());
454 GeomShapePtr aFaceShape1 = aFaceSelection1->value();
455 if(!aFaceShape1.get()) {
456 aFaceShape1 = aFaceSelection1->context()->shape();
458 std::shared_ptr<GeomAPI_Face> aFace1;
459 if (aFaceShape1->isFace()) {
460 aFace1 = aFaceShape1->face();
462 else if (aFaceShape1->isCompound()) {
463 GeomAPI_ShapeIterator anIt(aFaceShape1);
464 aFace1 = anIt.current()->face();
467 return GeomShapePtr();
468 std::shared_ptr<GeomAPI_Pln> aPln1 = aFace1->getPlane();
471 AttributeSelectionPtr aFaceSelection2 = selection(PLANE2());
472 GeomShapePtr aFaceShape2 = aFaceSelection2->value();
473 if(!aFaceShape2.get()) {
474 aFaceShape2 = aFaceSelection2->context()->shape();
476 std::shared_ptr<GeomAPI_Face> aFace2;
477 if (aFaceShape2->isFace()) {
478 aFace2 = aFaceShape2->face();
480 else if (aFaceShape2->isCompound()) {
481 GeomAPI_ShapeIterator anIt(aFaceShape2);
482 aFace2 = anIt.current()->face();
485 return GeomShapePtr();
486 std::shared_ptr<GeomAPI_Pln> aPln2 = aFace2->getPlane();
488 std::shared_ptr<GeomAPI_Pnt> anOrig1 = aPln1->location();
489 std::shared_ptr<GeomAPI_Pnt> aPntOnPln2 = aPln2->project(anOrig1);
491 std::shared_ptr<GeomAPI_Pnt> aNewOrig(new GeomAPI_Pnt(anOrig1->xyz()->added(
492 aPntOnPln2->xyz())->multiplied(0.5)));
494 std::shared_ptr<GeomAPI_Pln> aNewPln(new GeomAPI_Pln(aNewOrig, aPln1->direction()));
496 std::shared_ptr<GeomAPI_Face> aRes = makeRectangularFace(aFace1, aNewPln);
501 //==================================================================================================
502 GeomShapePtr faceByThreeVertices(const std::shared_ptr<GeomAPI_Vertex> theV1,
503 const std::shared_ptr<GeomAPI_Vertex> theV2,
504 const std::shared_ptr<GeomAPI_Vertex> theV3)
506 std::shared_ptr<GeomAPI_Face> aFace =
507 GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(theV1, theV2, theV3);
509 ListOfShape anObjects;
510 anObjects.push_back(theV1);
511 anObjects.push_back(theV2);
512 anObjects.push_back(theV3);
513 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
514 GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
515 GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints);
520 //==================================================================================================
521 std::shared_ptr<GeomAPI_Face> makeRectangularFace(const std::shared_ptr<GeomAPI_Face> theFace,
522 const std::shared_ptr<GeomAPI_Pln> thePln)
524 // Create rectangular face close to the selected
525 double aXmin, aYmin, Zmin, aXmax, aYmax, Zmax;
526 theFace->computeSize(aXmin, aYmin, Zmin, aXmax, aYmax, Zmax);
528 // use all 8 points of the bounding box to find the 2D bounds
530 double aMinX2d = DBL_MAX, aMaxX2d = -DBL_MAX;
531 double aMinY2d = DBL_MAX, aMaxY2d = -DBL_MAX;
532 for(int aXIsMin = 0; aXIsMin < 2; aXIsMin++) {
533 for(int aYIsMin = 0; aYIsMin < 2; aYIsMin++) {
534 for(int aZIsMin = 0; aZIsMin < 2; aZIsMin++) {
535 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(
536 aXIsMin ? aXmin : aXmax, aYIsMin ? aYmin : aYmax, aZIsMin ? Zmin : Zmax));
537 std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aPnt->to2D(thePln);
538 if (isFirst || aPnt2d->x() < aMinX2d)
539 aMinX2d = aPnt2d->x();
540 if (isFirst || aPnt2d->y() < aMinY2d)
541 aMinY2d = aPnt2d->y();
542 if (isFirst || aPnt2d->x() > aMaxX2d)
543 aMaxX2d = aPnt2d->x();
544 if (isFirst || aPnt2d->y() > aMaxY2d)
545 aMaxY2d = aPnt2d->y();
551 double aWgap = (aMaxX2d - aMinX2d) * 0.1;
552 double aHgap = (aMaxY2d - aMinY2d) * 0.1;
553 std::shared_ptr<GeomAPI_Face> aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln,
554 aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap,
555 aMaxY2d - aMinY2d + 2. * aHgap);