1 // Copyright (C) 2014-2019 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_BooleanFuse.h"
22 #include "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Validator.h>
33 #include <GeomAlgoAPI_Boolean.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_ShapeBuilder.h>
37 #include <GeomAlgoAPI_ShapeTools.h>
38 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomAlgoAPI_UnifySameDomain.h>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_ShapeIterator.h>
44 static const int THE_FUSE_VERSION_1 = 20190506;
46 //==================================================================================================
47 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
48 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
52 //==================================================================================================
53 void FeaturesPlugin_BooleanFuse::initAttributes()
55 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
58 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
65 AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
66 aVerAttr->setIsArgument(false);
67 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
68 if (!integer(VERSION_ID())->isInitialized() &&
69 !selectionList(OBJECT_LIST_ID())->isInitialized() &&
70 !selectionList(TOOL_LIST_ID())->isInitialized()) {
71 // this is a newly created feature (not read from file),
72 // so, initialize the latest version
73 integer(VERSION_ID())->setValue(THE_FUSE_VERSION_1);
77 //==================================================================================================
78 void FeaturesPlugin_BooleanFuse::execute()
81 ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
84 bool isSimpleCreation = false;
86 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
87 if (aCreationMethodAttr.get()
88 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
90 isSimpleCreation = true;
94 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
98 if (!isSimpleCreation &&
99 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
102 ListOfShape anObjects, aTools, anEdgesAndFaces;
103 // all objects except edges and faces
104 anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
105 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
106 aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
107 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
109 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
110 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
111 setError(aFeatureError);
115 // version of FUSE feature
116 AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
117 int aFuseVersion = 0;
118 if (aVersionAttr && aVersionAttr->isInitialized())
119 aFuseVersion = aVersionAttr->value();
121 //// isSimpleCreation = isSimpleCreation && aFuseVersion < THE_FUSE_VERSION_1;
123 // Collecting all solids which will be fused.
124 ListOfShape aSolidsToFuse;
125 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
126 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
128 // Collecting solids from compsolids which will not be modified
129 // in boolean operation and will be added to result.
130 ListOfShape aShapesToAdd;
131 for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
132 !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
134 GeomShapePtr anObject = *anObjectsIt;
135 GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
137 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
138 // mark all subs of this parent as precessed to avoid handling twice
139 aParent = anObjectsHierarchy.Parent(anObject);
141 ListOfShape aUsed, aNotUsed;
142 anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
143 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
147 ListOfShape anOriginalShapes = aSolidsToFuse;
148 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
150 // Cut edges and faces(if we have any) with solids.
151 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
152 GeomShapePtr aCuttedEdgesAndFaces;
153 if (!anEdgesAndFaces.empty()) {
154 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
155 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
156 if (aCutAlgo->isDone()) {
157 aCuttedEdgesAndFaces = aCutAlgo->shape();
158 aMakeShapeList->appendAlgo(aCutAlgo);
161 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
162 anEdgesAndFaces.end());
164 // If we have compsolids then cut with not used solids all others.
165 if (!aShapesToAdd.empty()) {
166 aSolidsToFuse.clear();
167 for (ListOfShape::iterator
168 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
169 ListOfShape aOneObjectList;
170 aOneObjectList.push_back(*anIt);
171 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
172 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
174 if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
175 aSolidsToFuse.push_back(aCutAlgo->shape());
176 aMakeShapeList->appendAlgo(aCutAlgo);
181 if (!aSolidsToFuse.empty()) {
183 anObjects.push_back(aSolidsToFuse.back());
184 aSolidsToFuse.pop_back();
185 aTools = aSolidsToFuse;
188 // Fuse all objects and all tools.
190 if (anObjects.size() == 1 && aTools.empty()) {
191 aShape = anObjects.front();
192 } else if (anObjects.empty() && aTools.size() == 1) {
193 aShape = aTools.front();
194 } else if ((anObjects.size() + aTools.size()) > 1) {
195 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
197 GeomAlgoAPI_Tools::BOOL_FUSE));
199 // Checking that the algorithm worked properly.
200 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
205 aShape = aFuseAlgo->shape();
206 aMakeShapeList->appendAlgo(aFuseAlgo);
209 // Combine result with not used solids from compsolid and edges and faces (if we have any).
210 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
211 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
213 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
215 if (!aShapesToAdd.empty()) {
217 aShapesToAdd.push_back(aShape);
219 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
220 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
221 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
226 aShape = aFillerAlgo->shape();
227 aMakeShapeList->appendAlgo(aFillerAlgo);
230 bool isRemoveEdges = false;
231 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
232 if (removeEdgesAttr.get()) {
233 isRemoveEdges = removeEdgesAttr->value();
237 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
238 new GeomAlgoAPI_UnifySameDomain(aShape));
240 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
245 aShape = aUnifyAlgo->shape();
246 aMakeShapeList->appendAlgo(aUnifyAlgo);
249 if (aFuseVersion == THE_FUSE_VERSION_1) {
250 // merge hierarchies of compounds containing objects and tools
251 // and append the result of the FUSE operation
252 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
255 int aResultIndex = 0;
257 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
259 ListOfShape anEmptyTools;
260 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
265 setResult(aResultBody, aResultIndex);
268 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
274 // remove the rest results if there were produced in the previous pass
275 removeResults(aResultIndex);
278 //==================================================================================================
279 GeomShapePtr FeaturesPlugin_BooleanFuse::keepUnusedSubsOfCompound(
280 const GeomShapePtr& theFuseResult,
281 const ObjectHierarchy& theObjectsHierarchy,
282 const ObjectHierarchy& theToolsHierarchy,
283 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
285 ListOfShape aCompounds;
286 theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
287 theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
289 GeomShapePtr aResultShape = theFuseResult;
290 if (!aCompounds.empty()) {
291 aResultShape = aCompounds.front();
292 aCompounds.pop_front();
294 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
295 for (ListOfShape::iterator anIt = aCompounds.begin(); anIt != aCompounds.end(); ++anIt) {
296 for (GeomAPI_ShapeIterator aSub(*anIt); aSub.more(); aSub.next())
297 aBuilder->add(aResultShape, aSub.current());
300 aBuilder->add(aResultShape, theFuseResult);
302 theMakeShapeList->appendAlgo(aBuilder);