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_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.");
112 std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
114 if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
115 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
116 anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
118 GeomAlgoAPI_Boolean::BOOL_FUSE));
120 anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
123 // Checking that the algorithm worked properly.
124 GeomAlgoAPI_MakeShapeList aMakeShapeList;
125 GeomAPI_DataMapOfShapeShape aMapOfShapes;
126 if(!anAlgo->isDone()) {
127 setError("Error: Boolean algorithm failed.");
130 if(anAlgo->shape()->isNull()) {
131 setError("Error: Resulting shape is Null.");
134 if(!anAlgo->isValid()) {
135 setError("Error: Resulting shape is not valid.");
139 GeomShapePtr aShape = anAlgo->shape();
140 aMakeShapeList.appendAlgo(anAlgo);
141 aMapOfShapes.merge(anAlgo->mapOfSubShapes());
143 // Store original shapes for naming.
144 anObjects.splice(anObjects.begin(), aTools);
145 anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
147 // Combine result with not used solids from compsolid.
148 if(aShapesToAdd.size() > 0) {
149 aShapesToAdd.push_back(aShape);
150 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
151 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
152 if(!aFillerAlgo->isDone()) {
153 setError("Error: PaveFiller algorithm failed.");
156 if(aFillerAlgo->shape()->isNull()) {
157 setError("Error: Resulting shape is Null.");
160 if(!aFillerAlgo->isValid()) {
161 setError("Error: Resulting shape is not valid.");
165 aShape = aFillerAlgo->shape();
166 aMakeShapeList.appendAlgo(aFillerAlgo);
167 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
169 // workaround: make copy to name edges correctly
171 // Store result and naming.
172 const int aModifyEdgeTag = 1;
173 const int aModifyFaceTag = 2;
174 const int aDeletedTag = 3;
175 /// sub solids will be placed at labels 4, 5 etc. if result is compound of solids
176 const int aSubsolidsTag = 4;
177 const std::string aModEName = "Modified_Edge";
178 const std::string aModFName = "Modified_Face";
180 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
181 aResultBody->storeModified(anObjects.front(), aShape, aSubsolidsTag);
183 for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
184 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::EDGE,
185 aModifyEdgeTag, aModEName, aMapOfShapes,
187 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE,
188 aModifyFaceTag, aModFName, aMapOfShapes,
190 //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
193 setResult(aResultBody);