Salome HOME
Updated modified shapes storing.
[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
42 #include <GeomAPI_Face.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_ShapeIterator.h>
45
46 #include <iostream>
47 #include <list>
48 #include <sstream>
49
50 typedef std::list<std::pair<GeomShapePtr, ListOfShape> > CompsolidSubs;
51
52 static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
53                              const GeomShapePtr theResultShape,
54                              const GeomAPI_Shape::ShapeType theShapeType,
55                              const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
56
57 static void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
58                                  CompsolidSubs& theObjects, ListOfShape& thePlanes);
59
60 static void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
61                          std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
62
63 static void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed);
64
65 static bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
66                           std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
67                           std::string& theError);
68
69 static bool isAlgoFailed(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo,
70                          std::string& theError);
71
72
73 //=================================================================================================
74 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
75 {
76 }
77
78 //=================================================================================================
79 void FeaturesPlugin_Partition::initAttributes()
80 {
81   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
82 }
83
84 //=================================================================================================
85 void FeaturesPlugin_Partition::execute()
86 {
87   CompsolidSubs anObjects;
88   ListOfShape aPlanes;
89
90   // Getting objects.
91   pullObjectsAndPlanes(selectionList(BASE_OBJECTS_ID()), anObjects, aPlanes);
92   if(anObjects.empty()) {
93     static const std::string aFeatureError = "Error: No objects for partition.";
94     setError(aFeatureError);
95     return;
96   }
97
98   ListOfShape aBaseObjects;
99   for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
100     aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
101   aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
102
103   // resize planes to the bounding box of operated shapes
104   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
105   resizePlanes(anObjects, aPlanes, aMakeShapeList);
106
107   // cut unused solids of composolids from the objects of partition
108   CompsolidSubs anUnusedSubs;
109   unusedSubsOfComposolid(anObjects, anUnusedSubs);
110   for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
111     aBaseObjects.insert(aBaseObjects.end(), anIt->second.begin(), anIt->second.end());
112
113   std::string aError;
114   if (!cutUnusedSubs(anObjects, anUnusedSubs, aMakeShapeList, aError)) {
115     setError(aError);
116     return;
117   }
118
119   // perform partition first time to split target solids
120   ListOfShape aTargetObjects;
121   for (CompsolidSubs::iterator anIt = anObjects.begin(); anIt != anObjects.end(); ++anIt)
122     aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
123
124   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
125     new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
126
127   // Checking that the algorithm worked properly.
128   if (isAlgoFailed(aPartitionAlgo, aError)) {
129     setError(aError);
130     return;
131   }
132
133   aMakeShapeList->appendAlgo(aPartitionAlgo);
134   GeomShapePtr aResultShape = aPartitionAlgo->shape();
135
136   if (!anUnusedSubs.empty()) {
137     // second pass of a partition to split shared faces of compsolids
138     aTargetObjects.clear();
139     aTargetObjects.push_back(aResultShape);
140     for (CompsolidSubs::iterator anIt = anUnusedSubs.begin(); anIt != anUnusedSubs.end(); ++anIt)
141       aTargetObjects.insert(aTargetObjects.end(), anIt->second.begin(), anIt->second.end());
142
143     aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
144
145     // Checking that the algorithm worked properly.
146     if (isAlgoFailed(aPartitionAlgo, aError)) {
147       setError(aError);
148       return;
149     }
150
151     aMakeShapeList->appendAlgo(aPartitionAlgo);
152     aResultShape = aPartitionAlgo->shape();
153   }
154
155   int aResultIndex = 0;
156   if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
157     for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
158       storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
159       ++aResultIndex;
160     }
161   } else {
162     storeResult(aBaseObjects, aPlanes, aResultShape, 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   // Find base. The most complicated is the real modified object (#1799 if box is partitioned by
178   // two planes the box is the base, not planes, independently on the order in the list).
179   GeomShapePtr aBaseShape;
180   for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
181     GeomShapePtr anObjectShape = *anIt;
182     GeomShapePtr aCandidate =
183       findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
184     if(!aCandidate.get()) {
185       aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
186     }
187     if (!aCandidate.get())
188       aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
189
190     if(aCandidate.get()) {
191       if (!aBaseShape.get() || aBaseShape->shapeType() > aCandidate->shapeType()) {
192         aBaseShape = aCandidate;
193       }
194     }
195   }
196
197   // Create result body.
198   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
199
200   // Store modified shape.
201   if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
202     aResultBody->store(theResultShape, false);
203     setResult(aResultBody, theIndex);
204     return;
205   }
206
207   aResultBody->storeModified(aBaseShape, theResultShape);
208
209   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
210   theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
211   for (ListOfShape::const_iterator anIt = theObjects.cbegin();
212        anIt != theObjects.cend();
213        ++anIt)
214   {
215     GeomShapePtr aShape = *anIt;
216     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE, "Modified_Edge");
217     aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE, "Modified_Face");
218     aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
219   }
220
221   setResult(aResultBody, theIndex);
222 }
223
224
225 //=================     Auxiliary functions     ===================================================
226
227 GeomShapePtr findBase(const GeomShapePtr theObjectShape,
228                       const GeomShapePtr theResultShape,
229                       const GeomAPI_Shape::ShapeType theShapeType,
230                       const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
231 {
232   GeomShapePtr aBaseShape;
233   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
234   for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
235       anObjectSubShapesExp.more();
236       anObjectSubShapesExp.next()) {
237     GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
238     ListOfShape aModifiedShapes;
239     theMakeShape->modified(anObjectSubShape, aModifiedShapes);
240     for(ListOfShape::const_iterator
241         aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
242       GeomShapePtr aModShape = *aModIt;
243       if(aMapOfSubShapes->isBound(aModShape)) {
244         aModShape = aMapOfSubShapes->find(aModShape);
245       }
246       if(theResultShape->isSubShape(aModShape)) {
247         aBaseShape = theObjectShape;
248         break;
249       }
250     }
251     if(aBaseShape.get()) {
252       break;
253     }
254   }
255
256   return aBaseShape;
257 }
258
259 static CompsolidSubs::iterator findOrAdd(CompsolidSubs& theList, const GeomShapePtr& theCompsolid)
260 {
261   CompsolidSubs::iterator aFound = theList.begin();
262   for (; aFound != theList.end(); ++aFound)
263     if (aFound->first == theCompsolid)
264       break;
265   if (aFound == theList.end()) {
266     theList.push_back(std::pair<GeomShapePtr, ListOfShape>(theCompsolid, ListOfShape()));
267     aFound = --theList.end();
268   }
269   return aFound;
270 }
271
272 void pullObjectsAndPlanes(const AttributeSelectionListPtr& theSelectedList,
273                           CompsolidSubs& theObjects, ListOfShape& thePlanes)
274 {
275   std::map<ResultBodyPtr, GeomShapePtr> aMapCompsolidShape;
276
277   int aSize = theSelectedList->size();
278   for (int anIndex = 0; anIndex < aSize; ++anIndex) {
279     AttributeSelectionPtr anObjectAttr = theSelectedList->value(anIndex);
280     ResultPtr aContext = anObjectAttr->context();
281     GeomShapePtr anObject = anObjectAttr->value();
282     if (anObject) {
283       GeomShapePtr anOwnerShape = anObject;
284       // check the result is a compsolid and store all used subs in a single list
285       ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
286       if (aResCompSolidPtr && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
287         std::map<ResultBodyPtr, GeomShapePtr>::const_iterator
288             aFound = aMapCompsolidShape.find(aResCompSolidPtr);
289         if (aFound != aMapCompsolidShape.end())
290           anOwnerShape = aFound->second;
291         else {
292           anOwnerShape = aResCompSolidPtr->shape();
293           aMapCompsolidShape[aResCompSolidPtr] = anOwnerShape;
294         }
295       }
296
297       CompsolidSubs::iterator aFound = findOrAdd(theObjects, anOwnerShape);
298       aFound->second.push_back(anObject);
299     }
300     else {
301       // It could be a construction plane.
302       thePlanes.push_back(anObjectAttr->context()->shape());
303     }
304   }
305 }
306
307 void resizePlanes(const CompsolidSubs& theObjects, ListOfShape& thePlanes,
308                   std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
309 {
310   ListOfShape aSolidsInOperation;
311   for (CompsolidSubs::const_iterator anIt = theObjects.begin(); anIt != theObjects.end(); ++anIt)
312     aSolidsInOperation.insert(aSolidsInOperation.end(), anIt->second.begin(), anIt->second.end());
313
314   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
315       GeomAlgoAPI_ShapeTools::getBoundingBox(aSolidsInOperation, 1.0);
316
317   ListOfShape aPlanesCopy = thePlanes;
318   thePlanes.clear();
319
320   // Resize planes to fit in bounding box
321   for (ListOfShape::const_iterator anIt = aPlanesCopy.begin(); anIt != aPlanesCopy.end(); ++anIt) {
322     GeomShapePtr aPlane = *anIt;
323     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
324     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
325     aMkShCustom->addModified(aPlane, aTool);
326     theMakeShapeList->appendAlgo(aMkShCustom);
327     thePlanes.push_back(aTool);
328   }
329 }
330
331 void unusedSubsOfComposolid(const CompsolidSubs& theObjects, CompsolidSubs& theNotUsed)
332 {
333   for (CompsolidSubs::const_iterator aCSIt = theObjects.begin();
334        aCSIt != theObjects.end(); ++aCSIt) {
335     if (aCSIt->first->shapeType() != GeomAPI_Shape::COMPSOLID)
336       continue;
337
338     // check the compsolid is selected
339     if (aCSIt->second.size() == 1 && aCSIt->first->isEqual(aCSIt->second.front()))
340       continue;
341
342     // process all sub-solids of compsolid
343     ListOfShape aNotUsedSolids;
344     for (GeomAPI_ShapeExplorer anExp(aCSIt->first, GeomAPI_Shape::SOLID);
345          anExp.more(); anExp.next()) {
346       GeomShapePtr aSolidInCompSolid = anExp.current();
347       ListOfShape::const_iterator anIt = aCSIt->second.begin();
348       for (; anIt != aCSIt->second.end(); ++anIt)
349         if (aSolidInCompSolid->isEqual(*anIt))
350           break;
351
352       if (anIt == aCSIt->second.end())
353         aNotUsedSolids.push_back(aSolidInCompSolid);
354     }
355
356     if (!aNotUsedSolids.empty())
357       theNotUsed.push_back(std::pair<GeomShapePtr, ListOfShape>(aCSIt->first, aNotUsedSolids));
358   }
359 }
360
361 static bool cutSubs(const GeomShapePtr& theFirstArgument,
362                     CompsolidSubs& theSubsToCut,
363                     const ListOfShape& theTools,
364                     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
365                     std::string& theError)
366 {
367   if (theTools.empty())
368     return true;
369
370   std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo;
371
372   for (CompsolidSubs::iterator aUIt= theSubsToCut.begin(); aUIt != theSubsToCut.end(); ++aUIt) {
373     if (aUIt->first == theFirstArgument)
374       continue; // no need to split unused subs of the first compsolid
375
376     // cut from current list of solids
377     aCutAlgo.reset(
378         new GeomAlgoAPI_Boolean(aUIt->second, theTools, GeomAlgoAPI_Boolean::BOOL_CUT));
379     if (isAlgoFailed(aCutAlgo, theError))
380       return false;
381     theMakeShapeList->appendAlgo(aCutAlgo);
382
383     // update list of un-selected objects of the partition
384     GeomAPI_Shape::ShapeType aType = aUIt->second.front()->shapeType();
385     aUIt->second.clear();
386     for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
387       aUIt->second.push_back(anExp.current());
388   }
389   return true;
390 }
391
392 bool cutUnusedSubs(CompsolidSubs& theObjects, CompsolidSubs& theNotUsed,
393                    std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
394                    std::string& theError)
395 {
396   GeomShapePtr aFirstArgument = theObjects.front().first;
397
398   // compose a set of tools for the CUT operation:
399   // find the list of unused subs of the first argument or use itself
400   ListOfShape aToolsForUsed;
401   CompsolidSubs::iterator aUIt = theNotUsed.begin();
402   for (; aUIt != theNotUsed.end(); ++aUIt)
403     if (aUIt->first == aFirstArgument) {
404       aToolsForUsed.insert(aToolsForUsed.end(), aUIt->second.begin(), aUIt->second.end());
405       break;
406     }
407   ListOfShape aToolsForUnused;
408   aToolsForUnused.push_back(aFirstArgument);
409
410   // cut subs
411   return cutSubs(aFirstArgument, theObjects, aToolsForUsed, theMakeShapeList, theError)
412       && cutSubs(aFirstArgument, theNotUsed, aToolsForUnused, theMakeShapeList, theError);
413 }
414
415 bool isAlgoFailed(const std::shared_ptr<GeomAlgoAPI_MakeShape>& theAlgo, std::string& theError)
416 {
417   if (!theAlgo->isDone()) {
418     theError = "Error: Partition algorithm failed.";
419     return true;
420   }
421   if (theAlgo->shape()->isNull()) {
422     theError = "Error: Resulting shape is Null.";
423     return true;
424   }
425   if (!theAlgo->isValid()) {
426     theError = "Error: Resulting shape is not valid.";
427     return true;
428   }
429
430   theError.clear();
431   return false;
432 }