]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_BooleanCut.cpp
Salome HOME
Reverted some changes in salome build/run scripts.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCut.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_BooleanCut.h"
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAPI_ShapeIterator.h>
34
35 //==================================================================================================
36 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
37 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
38 {
39 }
40
41 //==================================================================================================
42 void FeaturesPlugin_BooleanCut::execute()
43 {
44   ListOfShape anObjects, aTools;
45   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
46   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
47
48   // Getting objects.
49   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
50   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
51     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
52     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
53     if(!anObject.get()) {
54       return;
55     }
56     ResultPtr aContext = anObjectAttr->context();
57     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
58     if (aResCompSolidPtr.get())
59     {
60       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
61       GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
62       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
63         aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
64
65         std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
66           anIt = aMap.begin();
67         for (; anIt != aMap.end(); anIt++) {
68           if (anIt->first->isEqual(aContextShape)) {
69             aMap[anIt->first].push_back(anObject);
70             break;
71           }
72         }
73         if (anIt == aMap.end()) {
74           aMap[aContextShape].push_back(anObject);
75         }
76
77     } else {
78       anObjects.push_back(anObject);
79     }
80   }
81
82   // Getting tools.
83   AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
84   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
85     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
86     GeomShapePtr aTool = aToolAttr->value();
87     if(!aTool.get()) {
88       return;
89     }
90     aTools.push_back(aTool);
91   }
92
93   int aResultIndex = 0;
94
95   if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
96      || aTools.empty()) {
97     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
98     setError(aFeatureError);
99     return;
100   }
101
102   // For solids cut each object with all tools.
103   for(ListOfShape::iterator anObjectsIt = anObjects.begin();
104       anObjectsIt != anObjects.end();
105       ++anObjectsIt) {
106     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
107     GeomAlgoAPI_MakeShapeList aMakeShapeList;
108     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
109       new GeomAlgoAPI_Boolean(anObject,
110                               aTools,
111                               GeomAlgoAPI_Boolean::BOOL_CUT));
112     GeomShapePtr aResShape = aCutAlgo->shape();
113
114     // Checking that the algorithm worked properly.
115     if (!aCutAlgo->isDone()) {
116       static const std::string aFeatureError = "Error: Boolean algorithm failed.";
117       setError(aFeatureError);
118       return;
119     }
120     if(aResShape->isNull()) {
121       static const std::string aShapeError = "Error: Resulting shape is Null.";
122       setError(aShapeError);
123       return;
124     }
125     if (!aCutAlgo->isValid()) {
126       std::string aFeatureError = "Error: Resulting shape is not valid.";
127       setError(aFeatureError);
128       return;
129     }
130
131     aMakeShapeList.appendAlgo(aCutAlgo);
132
133     GeomAPI_ShapeIterator aShapeIt(aResShape);
134     if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
135     {
136       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
137         document()->createBody(data(), aResultIndex);
138
139       loadNamingDS(aResultBody, anObject, aTools, aResShape,
140                    aMakeShapeList, *(aCutAlgo->mapOfSubShapes()),
141                    false);
142       setResult(aResultBody, aResultIndex);
143       aResultIndex++;
144     }
145   }
146
147   // Compsolids handling
148   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
149        anIt = aCompSolidsObjects.begin();
150        anIt != aCompSolidsObjects.end();
151        ++anIt)
152   {
153     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
154     ListOfShape& aUsedInOperationSolids = anIt->second;
155
156     // Collecting solids from compsolids which will not be modified in boolean operation.
157     ListOfShape aNotUsedSolids;
158     for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
159         anExp.more();
160         anExp.next())
161     {
162       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
163       ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
164       for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
165         if (aSolidInCompSolid->isEqual(*aUsedIt)) {
166           break;
167         }
168       }
169       if (aUsedIt == aUsedInOperationSolids.end()) {
170         aNotUsedSolids.push_back(aSolidInCompSolid);
171       }
172     }
173
174     GeomAlgoAPI_MakeShapeList aMakeShapeList;
175     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
176       new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
177                               aTools,
178                               GeomAlgoAPI_Boolean::BOOL_CUT));
179
180     // Checking that the algorithm worked properly.
181     if (!aCutAlgo->isDone()) {
182       static const std::string aFeatureError = "Error: Boolean algorithm failed.";
183       setError(aFeatureError);
184       return;
185     }
186     if (aCutAlgo->shape()->isNull()) {
187       static const std::string aShapeError = "Error: Resulting shape is Null.";
188       setError(aShapeError);
189       return;
190     }
191     if (!aCutAlgo->isValid()) {
192       std::string aFeatureError = "Error: Resulting shape is not valid.";
193       setError(aFeatureError);
194       return;
195     }
196
197     aMakeShapeList.appendAlgo(aCutAlgo);
198     GeomAPI_DataMapOfShapeShape aMapOfShapes;
199     aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
200     GeomShapePtr aResultShape = aCutAlgo->shape();
201
202     // Add result to not used solids from compsolid.
203     if(!aNotUsedSolids.empty()) {
204       ListOfShape aShapesToAdd = aNotUsedSolids;
205       aShapesToAdd.push_back(aCutAlgo->shape());
206       std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
207         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
208       if(!aFillerAlgo->isDone()) {
209         std::string aFeatureError = "Error: PaveFiller algorithm failed.";
210         setError(aFeatureError);
211         return;
212       }
213
214       aMakeShapeList.appendAlgo(aFillerAlgo);
215       aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
216       aResultShape = aFillerAlgo->shape();
217     }
218
219     GeomAPI_ShapeIterator aShapeIt(aResultShape);
220     if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
221     {
222       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
223         document()->createBody(data(), aResultIndex);
224
225       loadNamingDS(aResultBody,
226                    aCompSolid,
227                    aTools,
228                    aResultShape,
229                    aMakeShapeList,
230                    aMapOfShapes,
231                    false);
232       setResult(aResultBody, aResultIndex);
233       aResultIndex++;
234     }
235   }
236
237   // Compounds handling
238   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
239        anIt = aCompoundObjects.begin();
240        anIt != aCompoundObjects.end();
241        ++anIt)
242   {
243     std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
244     ListOfShape& aUsedInOperationShapes = anIt->second;
245
246     // Collecting shapes from compound which will not be modified in boolean operation.
247     ListOfShape aNotUsedShapes;
248     for (GeomAPI_ShapeIterator aCompIt(aCompound);
249          aCompIt.more();
250          aCompIt.next())
251     {
252       std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
253       ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
254       for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
255         if (aShapeInCompound->isEqual(*aUsedIt)) {
256           break;
257         }
258       }
259       if (aUsedIt == aUsedInOperationShapes.end()) {
260         aNotUsedShapes.push_back(aShapeInCompound);
261       }
262     }
263
264     GeomAlgoAPI_MakeShapeList aMakeShapeList;
265     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
266       new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
267                               aTools,
268                               GeomAlgoAPI_Boolean::BOOL_CUT));
269
270     // Checking that the algorithm worked properly.
271     if (!aCutAlgo->isDone()) {
272       static const std::string aFeatureError = "Error: Boolean algorithm failed.";
273       setError(aFeatureError);
274       return;
275     }
276     if (aCutAlgo->shape()->isNull()) {
277       static const std::string aShapeError = "Error: Resulting shape is Null.";
278       setError(aShapeError);
279       return;
280     }
281     if (!aCutAlgo->isValid()) {
282       std::string aFeatureError = "Error: Resulting shape is not valid.";
283       setError(aFeatureError);
284       return;
285     }
286
287     aMakeShapeList.appendAlgo(aCutAlgo);
288     GeomAPI_DataMapOfShapeShape aMapOfShapes;
289     aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
290     GeomShapePtr aResultShape = aCutAlgo->shape();
291
292     // Add result to not used shape from compound.
293     if (!aNotUsedShapes.empty()) {
294       ListOfShape aShapesForResult = aNotUsedShapes;
295       if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
296         for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
297           aShapesForResult.push_back(aResultIt.current());
298         }
299       } else {
300         aShapesForResult.push_back(aResultShape);
301       }
302
303       if (aShapesForResult.size() == 1) {
304         aResultShape = aShapesForResult.front();
305       } else {
306         aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
307       }
308     }
309
310     GeomAPI_ShapeIterator aShapeIt(aResultShape);
311     if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
312       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
313         document()->createBody(data(), aResultIndex);
314
315       loadNamingDS(aResultBody,
316                    aCompound,
317                    aTools,
318                    aResultShape,
319                    aMakeShapeList,
320                    aMapOfShapes,
321                    false);
322       setResult(aResultBody, aResultIndex);
323       aResultIndex++;
324     }
325   }
326
327   // remove the rest results if there were produced in the previous pass
328   removeResults(aResultIndex);
329 }
330
331 //==================================================================================================
332 void FeaturesPlugin_BooleanCut::loadNamingDS(ResultBodyPtr theResultBody,
333                                              const GeomShapePtr theBaseShape,
334                                              const ListOfShape& theTools,
335                                              const GeomShapePtr theResultShape,
336                                              GeomAlgoAPI_MakeShape& theMakeShape,
337                                              GeomAPI_DataMapOfShapeShape& theMapOfShapes,
338                                              const bool theIsStoreAsGenerated)
339 {
340   //load result
341   if(theBaseShape->isEqual(theResultShape)) {
342     theResultBody->store(theResultShape, false);
343   } else {
344     const int aModifyVTag = 1;
345     const int aModifyETag = 2;
346     const int aModifyFTag = 3;
347     const int aDeletedTag = 4;
348     /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
349     const int aSubsolidsTag = 5;
350
351     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
352
353     const std::string aModVName = "Modified_Vertex";
354     const std::string aModEName = "Modified_Edge";
355     const std::string aModFName = "Modified_Face";
356
357     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
358                                                aModifyVTag, aModVName, theMapOfShapes, false,
359                                                theIsStoreAsGenerated, true);
360     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
361                                                aModifyETag, aModEName, theMapOfShapes, false,
362                                                theIsStoreAsGenerated, true);
363     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
364                                                aModifyFTag, aModFName, theMapOfShapes, false,
365                                                theIsStoreAsGenerated, true);
366
367     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
368                                      GeomAPI_Shape::VERTEX, aDeletedTag);
369     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
370                                      GeomAPI_Shape::EDGE, aDeletedTag);
371     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
372                                      GeomAPI_Shape::FACE, aDeletedTag);
373
374     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
375     {
376       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
377                                                  aModifyVTag, aModVName, theMapOfShapes, false,
378                                                  theIsStoreAsGenerated, true);
379
380       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
381                                                  aModifyETag, aModEName, theMapOfShapes, false,
382                                                  theIsStoreAsGenerated, true);
383
384       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
385                                                  aModifyFTag, aModFName, theMapOfShapes, false,
386                                                  theIsStoreAsGenerated, true);
387
388       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
389       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
390       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
391     }
392   }
393 }