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_Union.h"
22 #include <GeomAlgoAPI_Boolean.h>
23 #include <GeomAlgoAPI_MakeShapeList.h>
24 #include <GeomAlgoAPI_PaveFiller.h>
25 #include <GeomAlgoAPI_Tools.h>
26 #include <GeomAlgoAPI_UnifySameDomain.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_ResultBody.h>
33 #include <ModelAPI_Tools.h>
35 //=================================================================================================
36 FeaturesPlugin_Union::FeaturesPlugin_Union()
40 //=================================================================================================
41 void FeaturesPlugin_Union::initAttributes()
43 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
46 //=================================================================================================
47 void FeaturesPlugin_Union::execute()
49 ListOfShape anObjects;
50 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
53 AttributeSelectionListPtr anObjectsSelList =
54 selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
55 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
56 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
57 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
61 ResultPtr aContext = anObjectAttr->context();
62 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
63 if(aResCompSolidPtr.get()) {
64 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
65 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
66 anIt = aCompSolidsObjects.begin();
67 for(; anIt != aCompSolidsObjects.end(); anIt++) {
68 if(anIt->first->isEqual(aContextShape)) {
69 aCompSolidsObjects[anIt->first].push_back(anObject);
73 if(anIt == aCompSolidsObjects.end()) {
74 aCompSolidsObjects[aContextShape].push_back(anObject);
77 anObjects.push_back(anObject);
81 // Collecting solids from compsolids which will not be modified in
82 // boolean operation and will be added to result.
83 ListOfShape aShapesToAdd;
84 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
85 anIt = aCompSolidsObjects.begin();
86 anIt != aCompSolidsObjects.end(); anIt++) {
87 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
88 ListOfShape& aUsedInOperationSolids = anIt->second;
89 anObjects.insert(anObjects.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
91 // Collect solids from compsolid which will not be modified in boolean operation.
92 for(GeomAPI_ShapeIterator anExp(aCompSolid); anExp.more(); anExp.next()) {
93 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
94 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
95 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
96 if(aSolidInCompSolid->isEqual(*anIt)) {
100 if(anIt == aUsedInOperationSolids.end()) {
101 aShapesToAdd.push_back(aSolidInCompSolid);
106 if(anObjects.size() < 2) {
107 setError("Error: Not enough objects for operation. Should be at least 2.");
113 std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
115 if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
116 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
117 anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
119 GeomAlgoAPI_Boolean::BOOL_FUSE));
121 anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
124 // Checking that the algorithm worked properly.
125 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
126 GeomAPI_DataMapOfShapeShape aMapOfShapes;
127 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgo, getKind(), anError)) {
132 GeomShapePtr aShape = anAlgo->shape();
133 aMakeShapeList->appendAlgo(anAlgo);
134 aMapOfShapes.merge(anAlgo->mapOfSubShapes());
136 // Store original shapes for naming.
137 anObjects.splice(anObjects.begin(), aTools);
138 anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
140 // Combine result with not used solids from compsolid.
141 if(aShapesToAdd.size() > 0) {
142 aShapesToAdd.push_back(aShape);
143 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
144 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
145 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
150 aShape = aFillerAlgo->shape();
151 aMakeShapeList->appendAlgo(aFillerAlgo);
152 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
154 // workaround: make copy to name edges correctly
156 // Store result and naming.
158 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
159 aResultBody->storeModified(anObjects.front(), aShape);
161 for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
162 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
163 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
164 //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
167 setResult(aResultBody);