Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_Union.h"
21
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>
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::string anError;
113   std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
114   ListOfShape aTools;
115   if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
116     aTools.splice(aTools.begin(), anObjects, anObjects.begin());
117     anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
118                  aTools,
119                  GeomAlgoAPI_Boolean::BOOL_FUSE));
120   } else {
121     anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
122   }
123
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)) {
128     setError(anError);
129     return;
130   }
131
132   GeomShapePtr aShape = anAlgo->shape();
133   aMakeShapeList->appendAlgo(anAlgo);
134   aMapOfShapes.merge(anAlgo->mapOfSubShapes());
135
136   // Store original shapes for naming.
137   anObjects.splice(anObjects.begin(), aTools);
138   anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
139
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)) {
146       setError(anError);
147       return;
148     }
149
150     aShape = aFillerAlgo->shape();
151     aMakeShapeList->appendAlgo(aFillerAlgo);
152     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
153   }
154   // workaround: make copy to name edges correctly
155
156   // Store result and naming.
157
158   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
159   aResultBody->storeModified(anObjects.front(), aShape);
160
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);
165   }
166
167   setResult(aResultBody);
168 }