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