Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.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_Partition.h"
21
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Tools.h>
32 #include <ModelAPI_Validator.h>
33
34 #include <GeomAlgoAPI_Boolean.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_MakeShapeCustom.h>
37 #include <GeomAlgoAPI_MakeShapeList.h>
38 #include <GeomAlgoAPI_Partition.h>
39 #include <GeomAlgoAPI_ShapeBuilder.h>
40 #include <GeomAlgoAPI_ShapeTools.h>
41 #include <GeomAlgoAPI_Tools.h>
42
43 #include <GeomAPI_Face.h>
44 #include <GeomAPI_ShapeExplorer.h>
45 #include <GeomAPI_ShapeIterator.h>
46
47 #include <iostream>
48 #include <list>
49 #include <sstream>
50
51
52 //=================================================================================================
53 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
54 {
55 }
56
57 //=================================================================================================
58 void FeaturesPlugin_Partition::initAttributes()
59 {
60   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
61   initVersion(THE_VERSION_1, selectionList(BASE_OBJECTS_ID()));
62 }
63
64 //=================================================================================================
65 void FeaturesPlugin_Partition::execute()
66 {
67   ObjectHierarchy anObjects;
68   ListOfShape aPlanes;
69
70   // Getting objects.
71   processAttribute(BASE_OBJECTS_ID(), anObjects, aPlanes);
72   if(anObjects.IsEmpty()) {
73     static const std::string aFeatureError = "Error: No objects for partition.";
74     setError(aFeatureError);
75     return;
76   }
77
78   ListOfShape aBaseObjects = anObjects.Objects();
79   aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
80
81   // resize planes to the bounding box of operated shapes
82   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
83   resizePlanes(anObjects.Objects(), aPlanes, aMakeShapeList);
84
85   // cut unused solids of composolids from the objects of partition
86   ListOfShape aTargetObjects, anUnusedSubs;
87   std::string aError;
88   if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aMakeShapeList, aError)) {
89     setError(aError);
90     return;
91   }
92   aBaseObjects.insert(aBaseObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
93
94   // perform partition first time to split target solids by planes
95   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
96       new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
97
98   // Checking that the algorithm worked properly.
99   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
100     setError(aError);
101     return;
102   }
103
104   aMakeShapeList->appendAlgo(aPartitionAlgo);
105   GeomShapePtr aResultShape = aPartitionAlgo->shape();
106
107   if (!anUnusedSubs.empty()) {
108     // second pass of a partition to split shared faces of compsolids
109     aTargetObjects.clear();
110     aTargetObjects.push_back(aResultShape);
111     aTargetObjects.insert(aTargetObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
112
113     aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
114
115     // Checking that the algorithm worked properly.
116     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
117       setError(aError);
118       return;
119     }
120
121     aMakeShapeList->appendAlgo(aPartitionAlgo);
122     aResultShape = aPartitionAlgo->shape();
123   }
124
125   int aResultIndex = 0;
126
127   int aPartitionVersion = version();
128   if (aPartitionVersion < THE_VERSION_1) {
129     // default behaviors of Partition
130     if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
131       for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
132         storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
133         ++aResultIndex;
134       }
135     } else {
136       storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
137       ++aResultIndex;
138     }
139   }
140   else {
141     // merge hierarchies of compounds containing objects and tools
142     GeomShapePtr aFirstShape = aResultShape;
143     GeomAPI_ShapeIterator anIt;
144     if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
145       anIt = GeomAPI_ShapeIterator(aResultShape);
146       aFirstShape = anIt.current();
147       anIt.next();
148     }
149
150     GeomShapePtr aResultCompound =
151       keepUnusedSubsOfCompound(aFirstShape, anObjects, ObjectHierarchy(), aMakeShapeList);
152
153     if (anIt.more()) {
154       if (aResultCompound->shapeType() != GeomAPI_Shape::COMPOUND) {
155         // put the shape into compound
156         ListOfShape aShapes;
157         aShapes.push_back(aResultCompound);
158         aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
159       }
160       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
161       for (; anIt.more(); anIt.next())
162         aBuilder->add(aResultCompound, anIt.current());
163       aMakeShapeList->appendAlgo(aBuilder);
164     }
165
166     storeResult(aBaseObjects, aPlanes, aResultCompound, aMakeShapeList, aResultIndex);
167     ++aResultIndex;
168   }
169
170   // Remove the rest results if there were produced in the previous pass.
171   removeResults(aResultIndex);
172 }
173
174 //=================================================================================================
175 void FeaturesPlugin_Partition::storeResult(
176   ListOfShape& theObjects, ListOfShape& thePlanes,
177   const GeomShapePtr theResultShape,
178   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
179   const int theIndex)
180 {
181   // Create result body.
182   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
183
184   // if result is same as one of the base object, no modification was performed
185   for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
186   {
187     if (anObj->get() && (*anObj)->isSame(theResultShape)) {
188       aResultBody->store(theResultShape, false);
189       setResult(aResultBody, theIndex);
190       return;
191     }
192   }
193
194   aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
195
196   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
197   theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
198   for (ListOfShape::const_iterator anIt = theObjects.cbegin();
199        anIt != theObjects.cend();
200        ++anIt)
201   {
202     GeomShapePtr aShape = *anIt;
203     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
204     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
205     aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
206   }
207
208   setResult(aResultBody, theIndex);
209 }
210
211
212 //=================================================================================================
213
214 static bool cutSubs(ListOfShape& theSubsToCut,
215                     const ListOfShape& theTools,
216                     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
217                     std::string& theError)
218 {
219   if (theSubsToCut.empty() || theTools.empty())
220     return true;
221
222   // cut from current list of solids
223   std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo(
224       new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
225   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
226     return false;
227   theMakeShapeList->appendAlgo(aCutAlgo);
228
229   // update list of un-selected objects of the partition
230   GeomAPI_Shape::ShapeType aType = theSubsToCut.front()->shapeType();
231   theSubsToCut.clear();
232   for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
233     theSubsToCut.push_back(anExp.current());
234   return true;
235 }
236
237 bool FeaturesPlugin_Partition::cutSubs(
238     FeaturesPlugin_Partition::ObjectHierarchy& theHierarchy,
239     ListOfShape& theUsed,
240     ListOfShape& theNotUsed,
241     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
242     std::string& theError)
243 {
244   theUsed.clear();
245   theNotUsed.clear();
246
247   ObjectHierarchy::Iterator anIt = theHierarchy.Begin();
248
249   // compose a set of tools for the CUT operation:
250   // find the list of unused subs of the first argument or use itself
251   ListOfShape aToolsForUsed, aToolsForUnused;
252   GeomShapePtr aFirstArgument = theHierarchy.Parent(*anIt, false);
253   if (aFirstArgument && aFirstArgument->shapeType() == GeomAPI_Shape::COMPSOLID) {
254     theHierarchy.SplitCompound(aFirstArgument, theUsed, aToolsForUsed);
255     theNotUsed = aToolsForUsed;
256   }
257   else {
258     aFirstArgument = *anIt;
259     theUsed.push_back(aFirstArgument);
260   }
261   aToolsForUnused.push_back(aFirstArgument);
262
263   // cut subs
264   bool isOk = true;
265   for (++anIt; anIt != theHierarchy.End() && isOk; ++anIt) {
266     ListOfShape aUsed, aNotUsed;
267
268     GeomShapePtr aParent = theHierarchy.Parent(*anIt, false);
269     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
270       aParent = theHierarchy.Parent(*anIt); // get parent once again to mark its subs as processed
271       theHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
272     }
273     else
274       aUsed.push_back(*anIt);
275
276     isOk = ::cutSubs(aUsed, aToolsForUsed, theMakeShapeList, theError)
277         && ::cutSubs(aNotUsed, aToolsForUnused, theMakeShapeList, theError);
278     if (isOk) {
279       theUsed.insert(theUsed.end(), aUsed.begin(), aUsed.end());
280       theNotUsed.insert(theNotUsed.end(), aNotUsed.begin(), aNotUsed.end());
281     }
282   }
283
284   return isOk;
285 }