X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FConstructionPlugin%2FConstructionPlugin_Plane.cpp;h=4d02af97f94a851faac0b50b2ece01d70a564271;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=754a5aa4680f3e810a636a9d78b9a904d09b8e5f;hpb=6ffc58e695559e8997bbc8b977b537f1c63d45a8;p=modules%2Fshaper.git diff --git a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp index 754a5aa46..4d02af97f 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp +++ b/src/ConstructionPlugin/ConstructionPlugin_Plane.cpp @@ -1,8 +1,21 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: ConstructionPlugin_Plane.cpp -// Created: 12 Dec 2014 -// Author: Vitaly Smetannikov +// Copyright (C) 2014-2023 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "ConstructionPlugin_Plane.h" @@ -11,6 +24,7 @@ #include #include #include +#include #include #include @@ -19,6 +33,7 @@ #include #include #include +#include #include #include @@ -27,10 +42,14 @@ #include #include #include +#include #include #include #include +#include + + static GeomShapePtr faceByThreeVertices(const std::shared_ptr theV1, const std::shared_ptr theV2, const std::shared_ptr theV3); @@ -81,55 +100,71 @@ void ConstructionPlugin_Plane::initAttributes() // By two parallel planes. data()->addAttribute(PLANE1(), ModelAPI_AttributeSelection::typeId()); data()->addAttribute(PLANE2(), ModelAPI_AttributeSelection::typeId()); + + // By other plane. + AttributeIntegerPtr aNbCopies = std::dynamic_pointer_cast( + data()->addAttribute(NB_COPIES(), ModelAPI_AttributeInteger::typeId())); + + if (!aNbCopies->isInitialized()) + aNbCopies->setValue(1); } //================================================================================================== void ConstructionPlugin_Plane::execute() { - GeomShapePtr aShape; + ListOfShape aShapes; std::string aCreationMethod = string(CREATION_METHOD())->value(); - if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() || + if(aCreationMethod == CREATION_METHOD_BY_GENERAL_EQUATION() || aCreationMethod == "PlaneByGeneralEquation") { - aShape = createByGeneralEquation(); + aShapes.push_back(createByGeneralEquation()); } else if(aCreationMethod == CREATION_METHOD_BY_THREE_POINTS()) { - aShape = createByThreePoints(); + aShapes.push_back(createByThreePoints()); } else if(aCreationMethod == CREATION_METHOD_BY_LINE_AND_POINT()) { - aShape = createByLineAndPoint(); + aShapes.push_back(createByLineAndPoint()); } else if(aCreationMethod == CREATION_METHOD_BY_OTHER_PLANE()) { std::string aCreationMethodOption = string(CREATION_METHOD_BY_OTHER_PLANE_OPTION())->value(); if(aCreationMethodOption == CREATION_METHOD_BY_DISTANCE_FROM_OTHER()) { - aShape = createByDistanceFromOther(); + createByDistanceFromOther(aShapes); } else if(aCreationMethodOption == CREATION_METHOD_BY_COINCIDENT_TO_POINT()) { - aShape = createByCoincidentPoint(); + aShapes.push_back(createByCoincidentPoint()); } else if(aCreationMethodOption == CREATION_METHOD_BY_ROTATION()) { - aShape = createByRotation(); + createByRotation(aShapes); } } else if(aCreationMethod == CREATION_METHOD_BY_TWO_PARALLEL_PLANES()) { - aShape = createByTwoParallelPlanes(); + aShapes.push_back(createByTwoParallelPlanes()); } else { setError("Error: Plane creation method \"" + aCreationMethod + "\" not supported."); return; } - if(!aShape.get()) { + if(aShapes.size() == 0) { setError("Error: Could not create a plane."); return; } - ResultConstructionPtr aConstr = document()->createConstruction(data()); - aConstr->setInfinite(true); - aConstr->setShape(aShape); - setResult(aConstr); + int anIndex = 0; + for (auto aShapeIter = aShapes.begin(); aShapeIter != aShapes.end(); ++aShapeIter, ++anIndex) + { + if (!aShapeIter->get()) + { + setError("Error: Could not create a plane."); + continue; + } + ResultConstructionPtr aConstr = document()->createConstruction(data(), anIndex); + aConstr->setInfinite(true); + aConstr->setShape(*aShapeIter); + setResult(aConstr, anIndex); + } + removeResults(anIndex); } //================================================================================================== -bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs, - std::shared_ptr theDefaultPrs) +bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs) { std::vector aColor; // get color from the attribute of the result - if (theResult.get() != NULL && + if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) { AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID()); if (aColorAttr.get() && aColorAttr->size()) { @@ -139,8 +174,7 @@ bool ConstructionPlugin_Plane::customisePresentation(ResultPtr theResult, AISObj } } if (aColor.empty()) - aColor = Config_PropManager::color("Visualization", "construction_plane_color", - ConstructionPlugin_Plane::DEFAULT_COLOR()); + aColor = Config_PropManager::color("Visualization", COLOR_NAME()); bool isCustomized = false; if (aColor.size() == 3) @@ -165,16 +199,16 @@ std::shared_ptr ConstructionPlugin_Plane::createByGeneralEquation anAttrC->isInitialized() && anAttrD->isInitialized() ) { double aA = anAttrA->value(), aB = anAttrB->value(), aC = anAttrC->value(), aD = anAttrD->value(); - std::shared_ptr aPlane = + std::shared_ptr aPlane = std::shared_ptr(new GeomAPI_Pln(aA, aB, aC, aD)); - std::string kDefaultPlaneSize = "200"; - double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size", kDefaultPlaneSize); + double aSize = Config_PropManager::real(SKETCH_TAB_NAME, "planes_size"); + if (aSize <= 1.e-7) + aSize = 200; // Set default value aSize *= 4.; aPlaneFace = GeomAlgoAPI_FaceBuilder::squareFace(aPlane, aSize); } return aPlaneFace; } - //================================================================================================== std::shared_ptr ConstructionPlugin_Plane::createByThreePoints() { @@ -216,7 +250,16 @@ std::shared_ptr ConstructionPlugin_Plane::createByLineAndPoint() if(!aLineShape.get()) { aLineShape = anEdgeSelection->context()->shape(); } - std::shared_ptr anEdge(new GeomAPI_Edge(aLineShape)); + std::shared_ptr anEdge; + if (aLineShape->isEdge()) { + anEdge = aLineShape->edge(); + } + else if (aLineShape->isCompound()) { + GeomAPI_ShapeIterator anIt(aLineShape); + anEdge = anIt.current()->edge(); + } + if (!anEdge) + return GeomShapePtr(); // Get point. AttributeSelectionPtr aPointSelection = selection(POINT()); @@ -249,39 +292,53 @@ std::shared_ptr ConstructionPlugin_Plane::createByLineAndPoint() } //================================================================================================== -std::shared_ptr ConstructionPlugin_Plane::createByDistanceFromOther() +void ConstructionPlugin_Plane::createByDistanceFromOther(ListOfShape& theShapes) { AttributeSelectionPtr aFaceAttr = data()->selection(ConstructionPlugin_Plane::PLANE()); AttributeDoublePtr aDistAttr = data()->real(ConstructionPlugin_Plane::DISTANCE()); - std::shared_ptr aPlane; + AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES()); if ((aFaceAttr.get() != NULL) && - (aDistAttr.get() != NULL) && + (aDistAttr.get() != NULL) && (aNbCopyAttr.get() != NULL) && aFaceAttr->isInitialized() && aDistAttr->isInitialized()) { double aDist = aDistAttr->value(); bool anIsReverse = boolean(REVERSE())->value(); + int aNumOfCopies = aNbCopyAttr->value(); if(anIsReverse) aDist = -aDist; GeomShapePtr aShape = aFaceAttr->value(); - if (!aShape.get()) { + if (!aShape.get() && aFaceAttr->context()) { aShape = aFaceAttr->context()->shape(); } if(!aShape.get()) { - return aPlane; + return; } - std::shared_ptr aFace(new GeomAPI_Face(aShape)); + std::shared_ptr aFace; + if (aShape->isFace()) { + aFace = aShape->face(); + } + else if (aShape->isCompound()) { + GeomAPI_ShapeIterator anIt(aShape); + aFace = anIt.current()->face(); + } + if (!aFace) + return; std::shared_ptr aPln = aFace->getPlane(); std::shared_ptr aOrig = aPln->location(); std::shared_ptr aDir = aPln->direction(); - aOrig->translate(aDir, aDist); - std::shared_ptr aNewPln(new GeomAPI_Pln(aOrig, aDir)); + for (int aNbCopy = 0; aNbCopy < aNumOfCopies; ++aNbCopy) + { + std::shared_ptr aPlane; + aOrig->translate(aDir, aDist); + std::shared_ptr aNewPln(new GeomAPI_Pln(aOrig, aDir)); - aPlane = makeRectangularFace(aFace, aNewPln); + aPlane = makeRectangularFace(aFace, aNewPln); + theShapes.push_back(aPlane); + } } - return aPlane; } //================================================================================================== @@ -293,7 +350,14 @@ std::shared_ptr ConstructionPlugin_Plane::createByCoincidentPoint if(!aFaceShape.get()) { aFaceShape = aFaceSelection->context()->shape(); } - std::shared_ptr aFace(new GeomAPI_Face(aFaceShape)); + std::shared_ptr aFace; + if (aFaceShape->isFace()) { + aFace = aFaceShape->face(); + } + else if (aFaceShape->isCompound()) { + GeomAPI_ShapeIterator anIt(aFaceShape); + aFace = anIt.current()->face(); + } // Get point. AttributeSelectionPtr aPointSelection = selection(COINCIDENT_POINT()); @@ -301,7 +365,9 @@ std::shared_ptr ConstructionPlugin_Plane::createByCoincidentPoint if(!aPointShape.get()) { aPointShape = aPointSelection->context()->shape(); } - std::shared_ptr aVertex(new GeomAPI_Vertex(aPointShape)); + std::shared_ptr aVertex = aPointShape->vertex(); + if (!aVertex) + return GeomShapePtr(); std::shared_ptr anOrig = aVertex->point(); std::shared_ptr aPln = aFace->getPlane(); @@ -313,7 +379,7 @@ std::shared_ptr ConstructionPlugin_Plane::createByCoincidentPoint } //================================================================================================== -std::shared_ptr ConstructionPlugin_Plane::createByRotation() +void ConstructionPlugin_Plane::createByRotation(ListOfShape& theShapes) { // Get face. AttributeSelectionPtr aFaceSelection = selection(PLANE()); @@ -321,7 +387,16 @@ std::shared_ptr ConstructionPlugin_Plane::createByRotation() if(!aFaceShape.get()) { aFaceShape = aFaceSelection->context()->shape(); } - std::shared_ptr aFace(new GeomAPI_Face(aFaceShape)); + std::shared_ptr aFace; + if (aFaceShape->isFace()) { + aFace = aFaceShape->face(); + } + else if (aFaceShape->isCompound()) { + GeomAPI_ShapeIterator anIt(aFaceShape); + aFace = anIt.current()->face(); + } + if (!aFace) + return; aFace = makeRectangularFace(aFace, aFace->getPlane()); // Get axis. @@ -330,19 +405,45 @@ std::shared_ptr ConstructionPlugin_Plane::createByRotation() if(!anAxisShape.get()) { anAxisShape = anAxisSelection->context()->shape(); } - std::shared_ptr anEdge(new GeomAPI_Edge(anAxisShape)); + std::shared_ptr anEdge; + if (anAxisShape->isEdge()) { + anEdge = anAxisShape->edge(); + } + else if (anAxisShape->isCompound()) { + GeomAPI_ShapeIterator anIt(anAxisShape); + anEdge = anIt.current()->edge(); + } + if (!anEdge) + return; + + AttributeIntegerPtr aNbCopyAttr = data()->integer(ConstructionPlugin_Plane::NB_COPIES()); + int aNBCopy; + if (!aNbCopyAttr.get()) + return; - std::shared_ptr anAxis = + aNBCopy = aNbCopyAttr->value(); + + std::shared_ptr anAxis = std::shared_ptr(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction())); // Getting angle. double anAngle = real(ANGLE())->value(); - GeomAlgoAPI_Rotation aRotationAlgo(aFace, anAxis, anAngle); - std::shared_ptr aRes(new GeomAPI_Face(aRotationAlgo.shape())); + for (int anIndex = 1; anIndex <= aNBCopy; ++anIndex) + { + std::shared_ptr aRotationAlgo( + new GeomAlgoAPI_Rotation(aFace, anAxis, anAngle * anIndex)); + // Checking that the algorithm worked properly. + std::string anError; + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aRotationAlgo, getKind(), anError)) { + setError("Error: Failed to rotate plane"); + return; + } - return aRes; + std::shared_ptr aRes(new GeomAPI_Face(aRotationAlgo->shape())); + theShapes.push_back(aRes); + } } //================================================================================================== @@ -354,7 +455,16 @@ std::shared_ptr ConstructionPlugin_Plane::createByTwoParallelPlan if(!aFaceShape1.get()) { aFaceShape1 = aFaceSelection1->context()->shape(); } - std::shared_ptr aFace1(new GeomAPI_Face(aFaceShape1)); + std::shared_ptr aFace1; + if (aFaceShape1->isFace()) { + aFace1 = aFaceShape1->face(); + } + else if (aFaceShape1->isCompound()) { + GeomAPI_ShapeIterator anIt(aFaceShape1); + aFace1 = anIt.current()->face(); + } + if (!aFace1) + return GeomShapePtr(); std::shared_ptr aPln1 = aFace1->getPlane(); // Get plane 2. @@ -363,7 +473,16 @@ std::shared_ptr ConstructionPlugin_Plane::createByTwoParallelPlan if(!aFaceShape2.get()) { aFaceShape2 = aFaceSelection2->context()->shape(); } - std::shared_ptr aFace2(new GeomAPI_Face(aFaceShape2)); + std::shared_ptr aFace2; + if (aFaceShape2->isFace()) { + aFace2 = aFaceShape2->face(); + } + else if (aFaceShape2->isCompound()) { + GeomAPI_ShapeIterator anIt(aFaceShape2); + aFace2 = anIt.current()->face(); + } + if (!aFace2) + return GeomShapePtr(); std::shared_ptr aPln2 = aFace2->getPlane(); std::shared_ptr anOrig1 = aPln1->location(); @@ -391,7 +510,7 @@ GeomShapePtr faceByThreeVertices(const std::shared_ptr theV1, anObjects.push_back(theV1); anObjects.push_back(theV2); anObjects.push_back(theV3); - std::list > aBoundingPoints = + std::list > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0); GeomShapePtr aRes = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aFace, aBoundingPoints); @@ -408,7 +527,8 @@ std::shared_ptr makeRectangularFace(const std::shared_ptr makeRectangularFace(const std::shared_ptr aResFace = GeomAlgoAPI_FaceBuilder::planarFace(thePln, - aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, + aMinX2d - aWgap, aMinY2d - aHgap, aMaxX2d - aMinX2d + 2. * aWgap, aMaxY2d - aMinY2d + 2. * aHgap); return aResFace;