Salome HOME
2b3188ddcbd4d5f4fcc439063fa034cc734974a5
[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
27 #include <GeomAPI_ShapeExplorer.h>
28
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_ResultCompSolid.h>
31 #include <ModelAPI_Tools.h>
32
33 //=================================================================================================
34 FeaturesPlugin_Union::FeaturesPlugin_Union()
35 {
36 }
37
38 //=================================================================================================
39 void FeaturesPlugin_Union::initAttributes()
40 {
41   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
42 }
43
44 //=================================================================================================
45 void FeaturesPlugin_Union::execute()
46 {
47   ListOfShape anObjects;
48   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
49
50   // Getting objects.
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();
56     if(!anObject.get()) {
57       return;
58     }
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);
68           break;
69         }
70       }
71       if(anIt == aCompSolidsObjects.end()) {
72         aCompSolidsObjects[aContextShape].push_back(anObject);
73       }
74     } else {
75       anObjects.push_back(anObject);
76     }
77   }
78
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());
88
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)) {
95           break;
96         }
97       }
98       if(anIt == aUsedInOperationSolids.end()) {
99         aShapesToAdd.push_back(aSolidInCompSolid);
100       }
101     }
102   }
103
104   if(anObjects.size() < 2) {
105     setError("Error: Not enough objects for operation. Should be at least 2.");
106     return;
107   }
108
109   // Fuse objects.
110   ListOfShape aTools;
111   aTools.splice(aTools.begin(), anObjects, anObjects.begin());
112   std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
113                                                             aTools,
114                                                             GeomAlgoAPI_Boolean::BOOL_FUSE));
115
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.");
121     return;
122   }
123   if(aFuseAlgo->shape()->isNull()) {
124     setError("Error: Resulting shape is Null.");
125     return;
126   }
127   if(!aFuseAlgo->isValid()) {
128     setError("Error: Resulting shape is not valid.");
129     return;
130   }
131
132   GeomShapePtr aShape = aFuseAlgo->shape();
133   aMakeShapeList.appendAlgo(aFuseAlgo);
134   aMapOfShapes.merge(aFuseAlgo->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(!aFillerAlgo->isDone()) {
146       setError("Error: PaveFiller algorithm failed.");
147       return;
148     }
149     if(aFillerAlgo->shape()->isNull()) {
150       setError("Error: Resulting shape is Null.");
151       return;
152     }
153     if(!aFillerAlgo->isValid()) {
154       setError("Error: Resulting shape is not valid.");
155       return;
156     }
157
158     aShape = aFillerAlgo->shape();
159     aMakeShapeList.appendAlgo(aFillerAlgo);
160     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
161   }
162
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";
169
170   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
171   aResultBody->storeModified(anObjects.front(), aShape, aSubsolidsTag);
172
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);
177   }
178
179   setResult(aResultBody);
180 }