1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_Union.h"
23 #include <GeomAlgoAPI_Boolean.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_PaveFiller.h>
26 #include <GeomAlgoAPI_Tools.h>
27 #include <GeomAlgoAPI_UnifySameDomain.h>
29 #include <GeomAPI_ShapeExplorer.h>
30 #include <GeomAPI_ShapeIterator.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_Tools.h>
36 //=================================================================================================
37 FeaturesPlugin_Union::FeaturesPlugin_Union()
41 //=================================================================================================
42 void FeaturesPlugin_Union::initAttributes()
44 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
47 //=================================================================================================
48 void FeaturesPlugin_Union::execute()
50 ListOfShape anObjects;
51 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
54 AttributeSelectionListPtr anObjectsSelList =
55 selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
56 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
57 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
58 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
62 ResultPtr aContext = anObjectAttr->context();
63 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
64 if(aResCompSolidPtr.get()) {
65 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
66 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
67 anIt = aCompSolidsObjects.begin();
68 for(; anIt != aCompSolidsObjects.end(); anIt++) {
69 if(anIt->first->isEqual(aContextShape)) {
70 aCompSolidsObjects[anIt->first].push_back(anObject);
74 if(anIt == aCompSolidsObjects.end()) {
75 aCompSolidsObjects[aContextShape].push_back(anObject);
78 anObjects.push_back(anObject);
82 // Collecting solids from compsolids which will not be modified in
83 // boolean operation and will be added to result.
84 ListOfShape aShapesToAdd;
85 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
86 anIt = aCompSolidsObjects.begin();
87 anIt != aCompSolidsObjects.end(); anIt++) {
88 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
89 ListOfShape& aUsedInOperationSolids = anIt->second;
90 anObjects.insert(anObjects.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
92 // Collect solids from compsolid which will not be modified in boolean operation.
93 for(GeomAPI_ShapeIterator anExp(aCompSolid); anExp.more(); anExp.next()) {
94 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
95 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
96 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
97 if(aSolidInCompSolid->isEqual(*anIt)) {
101 if(anIt == aUsedInOperationSolids.end()) {
102 aShapesToAdd.push_back(aSolidInCompSolid);
107 if(anObjects.size() < 2) {
108 setError("Error: Not enough objects for operation. Should be at least 2.");
114 std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
116 if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
117 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
118 anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
120 GeomAlgoAPI_Boolean::BOOL_FUSE));
122 anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
125 // Checking that the algorithm worked properly.
126 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
127 GeomAPI_DataMapOfShapeShape aMapOfShapes;
128 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(anAlgo, getKind(), anError)) {
133 GeomShapePtr aShape = anAlgo->shape();
134 aMakeShapeList->appendAlgo(anAlgo);
135 aMapOfShapes.merge(anAlgo->mapOfSubShapes());
137 // Store original shapes for naming.
138 anObjects.splice(anObjects.begin(), aTools);
139 anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
141 // Combine result with not used solids from compsolid.
142 if(aShapesToAdd.size() > 0) {
143 aShapesToAdd.push_back(aShape);
144 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
145 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
146 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
151 aShape = aFillerAlgo->shape();
152 aMakeShapeList->appendAlgo(aFillerAlgo);
153 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
155 // workaround: make copy to name edges correctly
157 // Store result and naming.
159 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
160 aResultBody->storeModified(anObjects.front(), aShape);
162 for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
163 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
164 aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
165 //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
168 setResult(aResultBody);