Salome HOME
Removed some redundant names.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Union.h"
22
23 #include <GeomAlgoAPI_Boolean.h>
24 #include <GeomAlgoAPI_MakeShapeList.h>
25 #include <GeomAlgoAPI_PaveFiller.h>
26 #include <GeomAlgoAPI_UnifySameDomain.h>
27
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
30
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_ResultBody.h>
33 #include <ModelAPI_Tools.h>
34
35 //=================================================================================================
36 FeaturesPlugin_Union::FeaturesPlugin_Union()
37 {
38 }
39
40 //=================================================================================================
41 void FeaturesPlugin_Union::initAttributes()
42 {
43   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
44 }
45
46 //=================================================================================================
47 void FeaturesPlugin_Union::execute()
48 {
49   ListOfShape anObjects;
50   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
51
52   // Getting objects.
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();
58     if(!anObject.get()) {
59       return;
60     }
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);
70           break;
71         }
72       }
73       if(anIt == aCompSolidsObjects.end()) {
74         aCompSolidsObjects[aContextShape].push_back(anObject);
75       }
76     } else {
77       anObjects.push_back(anObject);
78     }
79   }
80
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());
90
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)) {
97           break;
98         }
99       }
100       if(anIt == aUsedInOperationSolids.end()) {
101         aShapesToAdd.push_back(aSolidInCompSolid);
102       }
103     }
104   }
105
106   if(anObjects.size() < 2) {
107     setError("Error: Not enough objects for operation. Should be at least 2.");
108     return;
109   }
110
111   // Fuse objects.
112   std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
113   ListOfShape aTools;
114   if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
115     aTools.splice(aTools.begin(), anObjects, anObjects.begin());
116     anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
117                  aTools,
118                  GeomAlgoAPI_Boolean::BOOL_FUSE));
119   } else {
120     anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
121   }
122
123   // Checking that the algorithm worked properly.
124   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
125   GeomAPI_DataMapOfShapeShape aMapOfShapes;
126   if(!anAlgo->isDone()) {
127     setError("Error: Boolean algorithm failed.");
128     return;
129   }
130   if(anAlgo->shape()->isNull()) {
131     setError("Error: Resulting shape is Null.");
132     return;
133   }
134   if(!anAlgo->isValid()) {
135     setError("Error: Resulting shape is not valid.");
136     return;
137   }
138
139   GeomShapePtr aShape = anAlgo->shape();
140   aMakeShapeList->appendAlgo(anAlgo);
141   aMapOfShapes.merge(anAlgo->mapOfSubShapes());
142
143   // Store original shapes for naming.
144   anObjects.splice(anObjects.begin(), aTools);
145   anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
146
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.");
154       return;
155     }
156     if(aFillerAlgo->shape()->isNull()) {
157       setError("Error: Resulting shape is Null.");
158       return;
159     }
160     if(!aFillerAlgo->isValid()) {
161       setError("Error: Resulting shape is not valid.");
162       return;
163     }
164
165     aShape = aFillerAlgo->shape();
166     aMakeShapeList->appendAlgo(aFillerAlgo);
167     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
168   }
169   // workaround: make copy to name edges correctly
170
171   // Store result and naming.
172
173   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
174   aResultBody->storeModified(anObjects.front(), aShape);
175
176   for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
177     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
178     aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
179     //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
180   }
181
182   setResult(aResultBody);
183 }