Salome HOME
Initial implementation of higher level objects history for partition
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.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_Partition.h"
22
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_BodyBuilder.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Validator.h>
34
35 #include <GeomAlgoAPI_Boolean.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
37 #include <GeomAlgoAPI_MakeShapeCustom.h>
38 #include <GeomAlgoAPI_MakeShapeList.h>
39 #include <GeomAlgoAPI_Partition.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 typedef std::list<std::pair<GeomShapePtr, ListOfShape> > CompsolidSubs;
52
53 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
54                              const GeomShapePtr theResultShape,
55                              const GeomAPI_Shape::ShapeType theShapeType,
56                              const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
57
58 static void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
59                                  CompsolidSubs& theObjects, ListOfShape& thePlanes);
60
61 static void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
62                          std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
63
64 static void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed);
65
66 static bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
67                           std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
68                           std::string& theError);
69
70
71 //=================================================================================================
72 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
73 {
74 }
75
76 //=================================================================================================
77 void FeaturesPlugin_Partition::initAttributes()
78 {
79   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
80 }
81
82 //=================================================================================================
83 void FeaturesPlugin_Partition::execute()
84 {
85   CompsolidSubs anObjects;
86   ListOfShape aPlanes;
87
88   // Getting objects.
89   pullObjectsAndPlanes(selectionList(BASE_OBJECTS_ID()), anObjects, aPlanes);
90   if(anObjects.empty()) {
91     static const std::string aFeatureError = "Error: No objects for partition.";
92     setError(aFeatureError);
93     return;
94   }
95
96   ListOfShape aBaseObjects;
97   for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
98     aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
99   aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
100
101   // resize planes to the bounding box of operated shapes
102   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
103   resizePlanes(anObjects, aPlanes, aMakeShapeList);
104
105   // cut unused solids of composolids from the objects of partition
106   CompsolidSubs anUnusedSubs;
107   unusedSubsOfComposolid(anObjects, anUnusedSubs);
108   for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
109     aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
110
111   std::string aError;
112   if (!cutUnusedSubs(anObjects, anUnusedSubs, aMakeShapeList, aError)) {
113     setError(aError);
114     return;
115   }
116
117   // perform partition first time to split target solids
118   ListOfShape aTargetObjects;
119   for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
120     aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
121
122   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
123     new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
124
125   // Checking that the algorithm worked properly.
126   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
127     setError(aError);
128     return;
129   }
130
131   aMakeShapeList->appendAlgo(aPartitionAlgo);
132   GeomShapePtr aResultShape = aPartitionAlgo->shape();
133
134   if (!anUnusedSubs.empty()) {
135     // second pass of a partition to split shared faces of compsolids
136     aTargetObjects.clear();
137     aTargetObjects.push_back(aResultShape);
138     for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
139       aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
140
141     aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
142
143     // Checking that the algorithm worked properly.
144     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
145       setError(aError);
146       return;
147     }
148
149     aMakeShapeList->appendAlgo(aPartitionAlgo);
150     aResultShape = aPartitionAlgo->shape();
151   }
152
153   int aResultIndex = 0;
154   if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
155     for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
156       storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
157       ++aResultIndex;
158     }
159   } else {
160     storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
161     ++aResultIndex;
162   }
163
164   // Remove the rest results if there were produced in the previous pass.
165   removeResults(aResultIndex);
166 }
167
168 //=================================================================================================
169 void FeaturesPlugin_Partition::storeResult(
170   ListOfShape& theObjects, ListOfShape& thePlanes,
171   const GeomShapePtr theResultShape,
172   const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
173   const int theIndex)
174 {
175   // Create result body.
176   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
177
178   // if result is same as one of the base object, no modification was performed
179   for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
180   {
181     if (anObj->get() && (*anObj)->isSame(theResultShape)) {
182       aResultBody->store(theResultShape, false);
183       setResult(aResultBody, theIndex);
184       return;
185     }
186   }
187
188   aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
189
190   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
191   theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
192   for (ListOfShape::const_iterator anIt = theObjects.cbegin();
193        anIt != theObjects.cend();
194        ++anIt)
195   {
196     GeomShapePtr aShape = *anIt;
197     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
198     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
199     aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
200   }
201
202   setResult(aResultBody, theIndex);
203 }
204
205
206 //=================     Auxiliary functions     ===================================================
207
208 static CompsolidSubs::iterator findOrAdd(CompsolidSubs& theList, const GeomShapePtr& theCompsolid)
209 {
210   CompsolidSubs::iterator aFound = theList.begin();
211   for (; aFound != theList.end(); ++aFound)
212     if (aFound->first == theCompsolid)
213       break;
214   if (aFound == theList.end()) {
215     theList.push_back(std::pair<GeomShapePtr, ListOfShape>(theCompsolid, ListOfShape()));
216     aFound = --theList.end();
217   }
218   return aFound;
219 }
220
221 void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
222                           CompsolidSubs& theObjects, ListOfShape& thePlanes)
223 {
224   std::map<ResultBodyPtr, GeomShapePtr> aMapCompsolidShape;
225
226   int aSize = theSelectedList->size();
227   for (int anIndex = 0; anIndex < aSize; ++anIndex) {
228     AttributeSelectionPtr anObjectAttr = theSelectedList->value(anIndex);
229     ResultPtr aContext = anObjectAttr->context();
230     GeomShapePtr anObject = anObjectAttr->value();
231     if (anObject) {
232       GeomShapePtr anOwnerShape = anObject;
233       // check the result is a compsolid and store all used subs in a single list
234       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
235       if (aResCompSolidPtr && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
236         std::map<ResultBodyPtr, GeomShapePtr>::const_iterator
237             aFound = aMapCompsolidShape.find(aResCompSolidPtr);
238         if (aFound != aMapCompsolidShape.end())
239           anOwnerShape = aFound->second;
240         else {
241           anOwnerShape = aResCompSolidPtr->shape();
242           aMapCompsolidShape[aResCompSolidPtr] = anOwnerShape;
243         }
244       }
245
246       CompsolidSubs::iterator aFound = findOrAdd(theObjects, anOwnerShape);
247       aFound->second.push_back(anObject);
248     }
249     else {
250       // It could be a construction plane.
251       thePlanes.push_back(anObjectAttr->context()->shape());
252     }
253   }
254 }
255
256 void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
257                   std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
258 {
259   ListOfShape aSolidsInOperation;
260   for (CompsolidSubs::const_iterator anIt = theObjects.begin(); anIt != theObjects.end(); ++anIt)
261     aSolidsInOperation.insert(aSolidsInOperation.end(), anIt->second.begin(), anIt->second.end());
262
263   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
264       GeomAlgoAPI_ShapeTools::getBoundingBox(aSolidsInOperation, 1.0);
265
266   ListOfShape aPlanesCopy = thePlanes;
267   thePlanes.clear();
268
269   // Resize planes to fit in bounding box
270   for (ListOfShape::const_iterator anIt = aPlanesCopy.begin(); anIt != aPlanesCopy.end(); ++anIt) {
271     GeomShapePtr aPlane = *anIt;
272     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
273     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
274     aMkShCustom->addModified(aPlane, aTool);
275     theMakeShapeList->appendAlgo(aMkShCustom);
276     thePlanes.push_back(aTool);
277   }
278 }
279
280 void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed)
281 {
282   for (CompsolidSubs::const_iterator aCSIt = theObjects.begin();
283        aCSIt != theObjects.end(); ++aCSIt) {
284     if (aCSIt->first->shapeType() != GeomAPI_Shape::COMPSOLID)
285       continue;
286
287     // check the compsolid is selected
288     if (aCSIt->second.size() == 1 && aCSIt->first->isEqual(aCSIt->second.front()))
289       continue;
290
291     // process all sub-solids of compsolid
292     ListOfShape aNotUsedSolids;
293     for (GeomAPI_ShapeExplorer anExp(aCSIt->first, GeomAPI_Shape::SOLID);
294          anExp.more(); anExp.next()) {
295       GeomShapePtr aSolidInCompSolid = anExp.current();
296       ListOfShape::const_iterator anIt = aCSIt->second.begin();
297       for (; anIt != aCSIt->second.end(); ++anIt)
298         if (aSolidInCompSolid->isEqual(*anIt))
299           break;
300
301       if (anIt == aCSIt->second.end())
302         aNotUsedSolids.push_back(aSolidInCompSolid);
303     }
304
305     if (!aNotUsedSolids.empty())
306       theNotUsed.push_back(std::pair<GeomShapePtr, ListOfShape>(aCSIt->first, aNotUsedSolids));
307   }
308 }
309
310 static bool cutSubs(const GeomShapePtr& theFirstArgument,
311                     CompsolidSubs& theSubsToCut,
312                     const ListOfShape& theTools,
313                     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
314                     std::string& theError)
315 {
316   if (theTools.empty())
317     return true;
318
319   std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo;
320
321   for (CompsolidSubs::iterator aUIt= theSubsToCut.begin(); aUIt != theSubsToCut.end(); ++aUIt) {
322     if (aUIt->first == theFirstArgument)
323       continue; // no need to split unused subs of the first compsolid
324
325     // cut from current list of solids
326     aCutAlgo.reset(
327         new GeomAlgoAPI_Boolean(aUIt->second, theTools, GeomAlgoAPI_Boolean::BOOL_CUT));
328     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
329       return false;
330     theMakeShapeList->appendAlgo(aCutAlgo);
331
332     // update list of un-selected objects of the partition
333     GeomAPI_Shape::ShapeType aType = aUIt->second.front()->shapeType();
334     aUIt->second.clear();
335     for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
336       aUIt->second.push_back(anExp.current());
337   }
338   return true;
339 }
340
341 bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
342                    std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
343                    std::string& theError)
344 {
345   GeomShapePtr aFirstArgument = theObjects.front().first;
346
347   // compose a set of tools for the CUT operation:
348   // find the list of unused subs of the first argument or use itself
349   ListOfShape aToolsForUsed;
350   CompsolidSubs::iterator aUIt = theNotUsed.begin();
351   for (; aUIt != theNotUsed.end(); ++aUIt)
352     if (aUIt->first == aFirstArgument) {
353       aToolsForUsed.insert(aToolsForUsed.end(), aUIt->second.begin(), aUIt->second.end());
354       break;
355     }
356   ListOfShape aToolsForUnused;
357   aToolsForUnused.push_back(aFirstArgument);
358
359   // cut subs
360   return cutSubs(aFirstArgument, theObjects, aToolsForUsed, theMakeShapeList, theError)
361       && cutSubs(aFirstArgument, theNotUsed, aToolsForUnused, theMakeShapeList, theError);
362 }