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_Sewing.h>
22 //#include <GeomAlgoAPI_MakeShape.h>
23 #include <GeomAlgoAPI_Sewing.h>
24 #include <GeomAlgoAPI_Tools.h>
26 //#include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_Shape.h>
28 //#include <GeomAPI_ShapeExplorer.h>
29 //#include <GeomAPI_ShapeIterator.h>
31 #include <ModelAPI_AttributeBoolean.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_ResultBody.h>
35 //#include <ModelAPI_ResultConstruction.h>
37 //---------------------------------------------------------
40 // static const char *dbg_class = "FeaturesPlugin_Sewing";
41 // #include "MBDebug.h"
42 // #include "MBModel.h"
43 // #include "MBGeom.h"
44 //---------------------------------------------------------
48 //=================================================================================================
49 FeaturesPlugin_Sewing::FeaturesPlugin_Sewing()
54 //=================================================================================================
55 void FeaturesPlugin_Sewing::initAttributes()
59 data()->addAttribute(OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60 data()->addAttribute(ALLOW_NON_MANIFOLD_ID(), ModelAPI_AttributeBoolean::typeId());
61 data()->addAttribute(TOLERANCE_ID(), ModelAPI_AttributeDouble::typeId());
62 data()->addAttribute(ALWAYS_CREATE_RESULT_ID(), ModelAPI_AttributeBoolean::typeId());
65 //=================================================================================================
66 void FeaturesPlugin_Sewing::execute()
70 // Collect all base shapes
72 getOriginalShapes(OBJECTS_LIST_ID(), aShapes);
75 // Get all other feature arguments
76 bool isAllowNonManifold = boolean(FeaturesPlugin_Sewing::ALLOW_NON_MANIFOLD_ID())->value();
77 bool isAlwaysCreateResult = boolean(FeaturesPlugin_Sewing::ALWAYS_CREATE_RESULT_ID())->value();
78 double aTolerance = real(FeaturesPlugin_Sewing::TOLERANCE_ID())->value();
79 // SHOW(isAllowNonManifold);
80 // SHOW(isAlwaysCreateResult);
83 std::shared_ptr<GeomAlgoAPI_Sewing> aSewingAlgo(new GeomAlgoAPI_Sewing(aShapes, isAllowNonManifold, aTolerance));
86 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSewingAlgo, getKind(), anError))
94 GeomShapePtr aResult = aSewingAlgo->shape();
98 ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
100 aResultBody->storeModified(aShapes, aResult, aSewingAlgo);
101 // SHOW(aResultBody);
103 if (!isSewn(aShapes, aResult) && !isAlwaysCreateResult)
105 static const std::string anError = "Error: No faces were sewn.";
110 setResult(aResultBody, anIndex);
113 //=================================================================================================
114 void FeaturesPlugin_Sewing::getOriginalShapes(const std::string& theAttributeName,
115 ListOfShape& theShapes)
118 // ARG(theAttributeName);
120 // Collect all selections into a single list of shapes
121 AttributeSelectionListPtr aSelectionList = selectionList(theAttributeName);
122 for (int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex)
124 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
125 GeomShapePtr aShape = aSelection->value();
126 GeomShapePtr aContext = aSelection->context()->shape();
131 theShapes.push_back(aShape);
135 //=================================================================================================
136 bool FeaturesPlugin_Sewing::isSewn(const ListOfShape& theInputs,
137 const GeomShapePtr theResult)
143 // Consider the list of input shapes the same as the result, if
144 // * both arguments have the same number of shells
145 // * the total number of faces in these shells did NOT change.
146 int nbInputShells = 0, nbInputFaces = 0;
147 for (ListOfShape::const_iterator anIt = theInputs.cbegin();
148 anIt != theInputs.cend();
151 GeomShapePtr aShape = *anIt;
152 if (aShape.get() && aShape->isShell())
155 nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
158 // SHOW(nbInputShells);
159 // SHOW(nbInputFaces);
161 int nbResultShells = 0, nbResultFaces = 0;
162 if (theResult->isCompound())
164 // MSGEL("...result is COMPOUND");
165 ListOfShape shells = theResult->subShapes(GeomAPI_Shape::SHELL, true);
166 nbResultShells = shells.size();
167 for (ListOfShape::const_iterator anIt = shells.cbegin();
168 anIt != shells.cend();
171 GeomShapePtr aShape = *anIt;
172 if (aShape.get() && aShape->isShell())
174 nbInputFaces += aShape->subShapes(GeomAPI_Shape::FACE, true).size();
178 else if (theResult->isShell())
180 // MSGEL("...result is single SHELL");
182 nbResultFaces = theResult->subShapes(GeomAPI_Shape::FACE, true).size();
184 // SHOW(nbResultShells);
185 // SHOW(nbResultFaces);
187 return (nbResultShells >= nbInputShells && nbResultFaces > nbInputFaces);