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_Union.h"
22 #include <GeomAlgoAPI_Boolean.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_PaveFiller.h>
26 #include <GeomAlgoAPI_ShapeBuilder.h>
27 #include <GeomAlgoAPI_Tools.h>
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_ShapeIterator.h>
32 #include <ModelAPI_AttributeBoolean.h>>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_Tools.h>
39 static const double DEFAULT_FUZZY = 1.e-5;
42 //=================================================================================================
43 FeaturesPlugin_Union::FeaturesPlugin_Union()
47 //=================================================================================================
48 void FeaturesPlugin_Union::initAttributes()
50 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
52 data()->addAttribute(USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
53 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
54 boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
55 real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
57 initVersion(BOP_VERSION_9_4(), selectionList(BASE_OBJECTS_ID()));
60 //=================================================================================================
61 void FeaturesPlugin_Union::execute()
63 GeomAPI_ShapeHierarchy anObjects;
64 ListOfShape anEmptyList;
67 if (!processAttribute(BASE_OBJECTS_ID(), anObjects, anEmptyList))
70 if(anObjects.objects().size() < 2) {
71 setError("Error: Not enough objects for operation. Should be at least 2.");
75 // Getting fuzzy parameter.
76 // Used as additional tolerance to eliminate tiny results.
77 // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
78 bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
79 double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
83 std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
84 ListOfShape aResultShapesList;
86 GeomShapePtr aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(ListOfShape());
90 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
91 anObjectsIt != anObjects.end() && isOk;
93 GeomShapePtr anObject = *anObjectsIt;
94 GeomShapePtr aParent = anObjects.parent(anObject, false);
96 if (aParent && aParent->shapeType() <= GeomAPI_Shape::COMPSOLID) {
97 // get parent once again to mark it and the subs as processed
98 aParent = anObjects.parent(anObject);
100 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_FUSE,
101 anObjects, aParent, anEmptyList, anEmptyList,
103 aResultIndex, aResultBaseAlgoList, aResultShapesList,
106 // process object as is
107 isOk = processObject(GeomAlgoAPI_Tools::BOOL_FUSE,
108 anObject, anEmptyList, anEmptyList,
110 aResultIndex, aResultBaseAlgoList, aResultShapesList,
115 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
116 for (std::vector<ModelAPI_Tools::ResultBaseAlgo>::iterator
117 aRBAIt = aResultBaseAlgoList.begin();
118 aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
119 aMakeShapeList->appendAlgo(aRBAIt->makeShape);
123 GeomAPI_ShapeIterator aCIt(aResultCompound);
124 if (data()->version().empty()) {
125 // if the compound consists of a single sub-shape, take it,
126 // otherwise, take the full compound
127 aShape = aCIt.current();
130 aShape = aResultCompound;
133 // merge hierarchies of compounds containing objects and tools
134 aShape = keepUnusedSubsOfCompound(aCIt.current(), anObjects, GeomAPI_ShapeHierarchy(),
136 for (aCIt.next(); aCIt.more(); aCIt.next()) {
137 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
138 aBuilder->add(aShape, aCIt.current());
139 aMakeShapeList->appendAlgo(aBuilder);
143 // Store result and naming.
144 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
145 ListOfShape anObjectsList = anObjects.objects();
146 aResultBody->storeModified(anObjectsList.front(), aShape);
148 for(ListOfShape::const_iterator anIter = anObjectsList.begin();
149 anIter != anObjectsList.end(); ++anIter) {
150 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
151 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
152 //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
155 setResult(aResultBody);