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>
27 #include <GeomAPI_ShapeExplorer.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_ResultCompSolid.h>
31 #include <ModelAPI_Tools.h>
33 //=================================================================================================
34 FeaturesPlugin_Union::FeaturesPlugin_Union()
38 //=================================================================================================
39 void FeaturesPlugin_Union::initAttributes()
41 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
44 //=================================================================================================
45 void FeaturesPlugin_Union::execute()
47 ListOfShape anObjects;
48 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
51 AttributeSelectionListPtr anObjectsSelList =
52 selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
53 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
55 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
59 ResultPtr aContext = anObjectAttr->context();
60 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
61 if(aResCompSolidPtr.get()) {
62 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
63 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
64 anIt = aCompSolidsObjects.begin();
65 for(; anIt != aCompSolidsObjects.end(); anIt++) {
66 if(anIt->first->isEqual(aContextShape)) {
67 aCompSolidsObjects[anIt->first].push_back(anObject);
71 if(anIt == aCompSolidsObjects.end()) {
72 aCompSolidsObjects[aContextShape].push_back(anObject);
75 anObjects.push_back(anObject);
79 // Collecting solids from compsolids which will not be modified in
80 // boolean operation and will be added to result.
81 ListOfShape aShapesToAdd;
82 for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
83 anIt = aCompSolidsObjects.begin();
84 anIt != aCompSolidsObjects.end(); anIt++) {
85 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
86 ListOfShape& aUsedInOperationSolids = anIt->second;
87 anObjects.insert(anObjects.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
89 // Collect solids from compsolid which will not be modified in boolean operation.
90 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
91 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
92 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
93 for(; anIt != aUsedInOperationSolids.end(); anIt++) {
94 if(aSolidInCompSolid->isEqual(*anIt)) {
98 if(anIt == aUsedInOperationSolids.end()) {
99 aShapesToAdd.push_back(aSolidInCompSolid);
104 if(anObjects.size() < 2) {
105 setError("Error: Not enough objects for operation. Should be at least 2.");
111 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
112 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
114 GeomAlgoAPI_Boolean::BOOL_FUSE));
116 // Checking that the algorithm worked properly.
117 GeomAlgoAPI_MakeShapeList aMakeShapeList;
118 GeomAPI_DataMapOfShapeShape aMapOfShapes;
119 if(!aFuseAlgo->isDone()) {
120 setError("Error: Boolean algorithm failed.");
123 if(aFuseAlgo->shape()->isNull()) {
124 setError("Error: Resulting shape is Null.");
127 if(!aFuseAlgo->isValid()) {
128 setError("Error: Resulting shape is not valid.");
132 GeomShapePtr aShape = aFuseAlgo->shape();
133 aMakeShapeList.appendAlgo(aFuseAlgo);
134 aMapOfShapes.merge(aFuseAlgo->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(!aFillerAlgo->isDone()) {
146 setError("Error: PaveFiller algorithm failed.");
149 if(aFillerAlgo->shape()->isNull()) {
150 setError("Error: Resulting shape is Null.");
153 if(!aFillerAlgo->isValid()) {
154 setError("Error: Resulting shape is not valid.");
158 aShape = aFillerAlgo->shape();
159 aMakeShapeList.appendAlgo(aFillerAlgo);
160 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
163 // Store result and naming.
164 const int aModifyTag = 1;
165 const int aDeletedTag = 2;
166 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
167 const int aSubsolidsTag = 3;
168 const std::string aModName = "Modified";
170 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
171 aResultBody->storeModified(anObjects.front(), aShape, aSubsolidsTag);
173 for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
174 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE,
175 aModifyTag, aModName, aMapOfShapes);
176 aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
179 setResult(aResultBody);