1 // Copyright (C) 2014-2022 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 "FeaturesPlugin_GlueFaces.h"
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Tools.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAlgoAPI_CompoundBuilder.h>
35 #include <GeomAlgoAPI_GlueFaces.h>
36 #include <GeomAlgoAPI_Tools.h>
41 //==================================================================================================
42 FeaturesPlugin_GlueFaces::FeaturesPlugin_GlueFaces()
46 //==================================================================================================
47 void FeaturesPlugin_GlueFaces::initAttributes()
49 data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
50 data()->addAttribute(TOLERANCE_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(KEEP_NON_SOLIDS_ID(), ModelAPI_AttributeBoolean::typeId());
54 //==================================================================================================
55 void FeaturesPlugin_GlueFaces::execute()
57 // Collect all selected shapes into a single list of shapes
59 getOriginalShapes(OBJECTS_LIST_ID(), aShapes);
61 // Get all other feature arguments
62 double aTolerance = real(FeaturesPlugin_GlueFaces::TOLERANCE_ID())->value();
63 bool isKeepNonSolids = boolean(FeaturesPlugin_GlueFaces::KEEP_NON_SOLIDS_ID())->value();
65 std::shared_ptr<GeomAlgoAPI_GlueFaces> aGluingAlgo(new GeomAlgoAPI_GlueFaces(aShapes, aTolerance, isKeepNonSolids));
68 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aGluingAlgo, getKind(), anError))
76 GeomShapePtr aResult = aGluingAlgo->shape();
78 if (!isGlued(aShapes, aResult))
80 static const std::string anError2 = "Error: No faces were glued.";
82 data()->execState(ModelAPI_ExecState::ModelAPI_StateDone);
84 setResultFromInput(aShapes);
89 ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
90 aResultBody->storeModified(aShapes, aResult, aGluingAlgo);
92 // Ensure the result body is named after this feature
93 std::wstring aBaseName = feature(aResultBody)->data()->name();
94 std::wostringstream aNameStr;
95 aNameStr << aBaseName << "_" << (anIndex+1);
96 std::wstring aName = aNameStr.str();
97 aResultBody->data()->setName(aName);
99 setResult(aResultBody, anIndex);
102 //=================================================================================================
103 void FeaturesPlugin_GlueFaces::getOriginalShapes(const std::string& theAttributeName,
104 ListOfShape& theShapes)
106 // Collect all selections into a single list of shapes
107 AttributeSelectionListPtr aSelectionList = selectionList(theAttributeName);
108 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex)
110 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
111 GeomShapePtr aShape = aSelection->value();
112 GeomShapePtr aContext = aSelection->context()->shape();
117 theShapes.push_back(aShape);
121 //=================================================================================================
122 bool FeaturesPlugin_GlueFaces::isGlued(const ListOfShape& theInputs,
123 const GeomShapePtr theResult)
125 if (!theResult.get())
128 // Consider the list of input shapes the same as the result, if
129 // * the total number of faces did NOT change.
130 int nbInputFaces = 0, nbInputEdges = 0;
131 for (ListOfShape::const_iterator anIt = theInputs.cbegin();
132 anIt != theInputs.cend();
135 GeomShapePtr aShape = *anIt;
138 nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
139 nbInputEdges += aShape->subShapes(GeomAPI_Shape::EDGE, true).size();
143 int nbResultFaces = 0, nbResultEdges = 0;
144 nbResultFaces = theResult->subShapes(GeomAPI_Shape::FACE, true).size();
145 nbResultEdges = theResult->subShapes(GeomAPI_Shape::EDGE, true).size();
146 return(0 < nbResultFaces && ((nbResultFaces < nbInputFaces) || (nbResultFaces == nbInputFaces && nbResultEdges < nbInputEdges)));
149 //=================================================================================================
150 void FeaturesPlugin_GlueFaces::setResultFromInput(const ListOfShape& theInputs)
152 GeomShapePtr aResult;
154 // Make sure the result will be a compound of the input shapes, if not already
155 if (theInputs.size() == 1 && theInputs.front().get() && theInputs.front()->isCompound())
157 aResult = theInputs.front();
161 aResult = GeomAlgoAPI_CompoundBuilder::compound(theInputs);
165 ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
166 aResultBody->store(aResult);
168 setResult(aResultBody, anIndex);