]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_VersionedBoolean.cpp
Salome HOME
La part
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedBoolean.cpp
1 // Copyright (C) 2014-2022  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_VersionedBoolean.h"
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeReference.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Tools.h>
31
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_CompoundBuilder.h>
34 #include <GeomAlgoAPI_MakeShapeCustom.h>
35 #include <GeomAlgoAPI_MakeShapeList.h>
36 #include <GeomAlgoAPI_Partition.h>
37 #include <GeomAlgoAPI_PaveFiller.h>
38 #include <GeomAlgoAPI_ShapeBuilder.h>
39 #include <GeomAlgoAPI_ShapeTools.h>
40 #include <GeomAlgoAPI_Tools.h>
41 #include <GeomAlgoAPI_UnifySameDomain.h>
42 #include <GeomAPI_Face.h>
43 #include <GeomAPI_ShapeExplorer.h>
44 #include <GeomAPI_ShapeIterator.h>
45
46 #include <algorithm>
47 #include <map>
48
49
50 //=================================================================================================
51 static void performBoolean(const GeomAlgoAPI_Tools::BOPType theBooleanType,
52                            GeomMakeShapePtr& theBooleanAlgo,
53                            const ListOfShape& theObjects,
54                            const ListOfShape& theTools,
55                            const double theFuzzy)
56 {
57   if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
58     theBooleanAlgo.reset(new GeomAlgoAPI_Partition(theObjects, theTools));
59   else {
60     // separate processing of FUSE, if only objects are given
61     if (theBooleanType == GeomAlgoAPI_Tools::BOOL_FUSE && theTools.empty()) {
62       if (theObjects.front()->shapeType() == GeomAPI_Shape::FACE)
63         theBooleanAlgo.reset(new GeomAlgoAPI_UnifySameDomain(theObjects));
64       else {
65         ListOfShape anObjects = theObjects;
66         ListOfShape aTools;
67         aTools.splice(aTools.begin(), anObjects, anObjects.begin());
68         theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(anObjects, aTools, theBooleanType, theFuzzy));
69       }
70     }
71     else
72       theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(theObjects, theTools, theBooleanType, theFuzzy));
73   }
74 }
75
76 //=================================================================================================
77 void FeaturesPlugin_VersionedBoolean::initVersion(const std::string& theVersion,
78                                                   const AttributePtr theObjectsAttr,
79                                                   const AttributePtr theToolsAttr)
80 {
81   AttributeIntegerPtr anOldVersionAttr = integer("version");
82   if (anOldVersionAttr && anOldVersionAttr->isInitialized() && data()->version().empty()) {
83     // move value to the common version interface in ModelAPI_Data
84     data()->setVersion(BOP_VERSION_9_4());
85   }
86   else if ((!theObjectsAttr || !theObjectsAttr->isInitialized()) &&
87            (!theToolsAttr || !theToolsAttr->isInitialized())) {
88     // this is a newly created feature (not read from file),
89     // so, initialize the latest version
90     data()->setVersion(theVersion);
91   }
92 }
93
94 //=================================================================================================
95 bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAttributeName,
96                                               GeomAPI_ShapeHierarchy& theObjects,
97                                               ListOfShape& thePlanesList)
98 {
99   AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
100   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
101     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
102     GeomShapePtr anObject = anObjectAttr->value();
103     if (!anObject.get()) {
104       // It could be a construction plane.
105       ResultPtr aContext = anObjectAttr->context();
106       anObject = anObjectAttr->context()->shape();
107       if (anObject.get()) {
108         thePlanesList.push_back(anObject);
109         continue;
110       } else
111         return false;
112     }
113
114     theObjects.addObject(anObject);
115
116     ResultPtr aContext = anObjectAttr->context();
117     ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theObjects);
118   }
119   return true;
120 }
121
122 //=================================================================================================
123 bool FeaturesPlugin_VersionedBoolean::processObject(
124     const GeomAlgoAPI_Tools::BOPType theBooleanType,
125     const GeomShapePtr& theObject,
126     const ListOfShape& theTools,
127     const ListOfShape& thePlanes,
128     const double theFuzzy,
129     int& theResultIndex,
130     std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
131     ListOfShape& theResultShapesList,
132     GeomShapePtr theResultCompound)
133 {
134   ListOfShape aListWithObject;
135   aListWithObject.push_back(theObject);
136   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
137   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
138   GeomShapePtr aResShape;
139
140   // Resize planes.
141   ListOfShape aToolsWithPlanes = theTools;
142   ListOfShape aPlanesCopy = thePlanes;
143   resizePlanes(aListWithObject, aPlanesCopy, aMakeShapeList);
144   aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
145
146   if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
147     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes, theFuzzy));
148   else
149     aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
150                                             aToolsWithPlanes,
151                                             theBooleanType,
152                                             theFuzzy));
153
154   // Checking that the algorithm worked properly.
155   std::string anError;
156   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
157     setError(anError);
158     return false;
159   }
160
161   aResShape = aBoolAlgo->shape();
162   if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
163     int aSubResultsNb = 0;
164     GeomAPI_ShapeIterator anIt(aResShape);
165     for (; anIt.more(); anIt.next())
166       ++aSubResultsNb;
167
168     if (aSubResultsNb == 1) {
169       anIt.init(aResShape);
170       if (anIt.more())
171         aResShape = anIt.current();
172     }
173   }
174
175   aMakeShapeList->appendAlgo(aBoolAlgo);
176
177   GeomAPI_ShapeIterator aShapeIt(aResShape);
178   if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
179     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
180
181     if (theResultCompound) { // store BOP result to the compound
182       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
183       aBuilder->add(theResultCompound, aResShape);
184       aMakeShapeList->appendAlgo(aBuilder);
185     }
186     else { // create a separate ResultBody
187       aResultBody = document()->createBody(data(), theResultIndex);
188
189       // tools should be added to the list to fulfill the correct history of modification
190       aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
191
192       ListOfShape aUsedTools = theTools;
193       aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
194
195       ModelAPI_Tools::loadModifiedShapes(aResultBody,
196                                                aListWithObject,
197                                                aUsedTools,
198                                                aMakeShapeList,
199                                                aResShape);
200       setResult(aResultBody, theResultIndex);
201       ++theResultIndex;
202     }
203
204
205     ModelAPI_Tools::ResultBaseAlgo aRBA;
206     aRBA.resultBody = aResultBody;
207     aRBA.baseShape = theObject;
208     aRBA.makeShape = aMakeShapeList;
209     theResultBaseAlgoList.push_back(aRBA);
210     theResultShapesList.push_back(aResShape);
211   }
212   return true;
213 }
214
215 //=================================================================================================
216 bool FeaturesPlugin_VersionedBoolean::processCompsolid(
217     const GeomAlgoAPI_Tools::BOPType theBooleanType,
218     GeomAPI_ShapeHierarchy& theCompsolidHierarchy,
219     const GeomShapePtr& theCompsolid,
220     const ListOfShape& theTools,
221     const ListOfShape& thePlanes,
222     const double theFuzzy,
223     int& theResultIndex,
224     std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
225     ListOfShape& theResultShapesList,
226     GeomShapePtr theResultCompound)
227 {
228   ListOfShape aUsedInOperationSolids;
229   ListOfShape aNotUsedSolids;
230   theCompsolidHierarchy.splitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
231
232   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
233
234   // Resize planes.
235   ListOfShape aToolsWithPlanes = theTools;
236   ListOfShape aPlanesCopy = thePlanes;
237   resizePlanes(aUsedInOperationSolids, aPlanesCopy, aMakeShapeList);
238   aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
239
240   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
241   performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationSolids, aToolsWithPlanes, theFuzzy);
242
243   // Checking that the algorithm worked properly.
244   std::string anError;
245   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
246     setError(anError);
247     return false;
248   }
249
250   aMakeShapeList->appendAlgo(aBoolAlgo);
251   GeomShapePtr aResultShape = aBoolAlgo->shape();
252
253   // Add result to not used solids from compsolid.
254   if (!aNotUsedSolids.empty()) {
255     theCompsolidHierarchy.markProcessed(aNotUsedSolids);
256
257     ListOfShape aShapesToAdd = aNotUsedSolids;
258     aShapesToAdd.push_back(aBoolAlgo->shape());
259     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
260         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, theFuzzy));
261     if (!aFillerAlgo->isDone()) {
262       std::string aFeatureError = "Error: PaveFiller algorithm failed.";
263       setError(aFeatureError);
264       return false;
265     }
266
267     aMakeShapeList->appendAlgo(aFillerAlgo);
268     aResultShape = aFillerAlgo->shape();
269   }
270
271   GeomAPI_ShapeIterator aShapeIt(aResultShape);
272   if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
273   {
274     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
275
276     if (theResultCompound) { // store BOP result to the compound
277       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
278       aBuilder->add(theResultCompound, aResultShape);
279       aMakeShapeList->appendAlgo(aBuilder);
280     }
281     else { // create a separate ResultBody
282       aResultBody = document()->createBody(data(), theResultIndex);
283
284       ListOfShape aCompSolidList;
285       aCompSolidList.push_back(theCompsolid);
286       // tools should be added to the list to fulfill the correct history of modification
287       aCompSolidList.insert(aCompSolidList.end(), theTools.begin(), theTools.end());
288
289       ListOfShape aUsedTools = theTools;
290       aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
291
292       ModelAPI_Tools::loadModifiedShapes(aResultBody,
293                                                aCompSolidList,
294                                                aUsedTools,
295                                                aMakeShapeList,
296                                                aResultShape);
297       setResult(aResultBody, theResultIndex);
298       ++theResultIndex;
299     }
300
301     ModelAPI_Tools::ResultBaseAlgo aRBA;
302     aRBA.resultBody = aResultBody;
303     aRBA.baseShape = theCompsolid;
304     aRBA.makeShape = aMakeShapeList;
305     theResultBaseAlgoList.push_back(aRBA);
306     theResultShapesList.push_back(aResultShape);
307   }
308   return true;
309 }
310
311 //=================================================================================================
312 bool FeaturesPlugin_VersionedBoolean::processCompound(
313     const GeomAlgoAPI_Tools::BOPType theBooleanType,
314     GeomAPI_ShapeHierarchy& theCompoundHierarchy,
315     const GeomShapePtr& theCompound,
316     const ListOfShape& theTools,
317     const double theFuzzy,
318     int& theResultIndex,
319     std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
320     ListOfShape& theResultShapesList,
321     GeomShapePtr theResultCompound)
322 {
323   ListOfShape aUsedInOperationShapes;
324   ListOfShape aNotUsedShapes;
325   theCompoundHierarchy.splitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
326   if (theResultCompound) {
327     // Not necessary to keep all subs of the current compound,
328     // all unused solids are already stored in the result compound.
329     aNotUsedShapes.clear();
330   }
331
332   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
333   std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
334   performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationShapes, theTools, theFuzzy);
335
336   // Checking that the algorithm worked properly.
337   std::string anError;
338   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
339     setError(anError);
340     return false;
341   }
342
343   aMakeShapeList->appendAlgo(aBoolAlgo);
344   GeomShapePtr aResultShape = aBoolAlgo->shape();
345
346   // Add result to not used shape from compound.
347   if (!aNotUsedShapes.empty()) {
348     theCompoundHierarchy.markProcessed(aNotUsedShapes);
349
350     ListOfShape aShapesForResult = aNotUsedShapes;
351     if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
352       for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
353         aShapesForResult.push_back(aResultIt.current());
354       }
355     }
356     else {
357       aShapesForResult.push_back(aResultShape);
358     }
359
360     if (aShapesForResult.size() == 1) {
361       aResultShape = aShapesForResult.front();
362     }
363     else {
364       aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
365     }
366   }
367
368   GeomAPI_ShapeIterator aShapeIt(aResultShape);
369   if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
370     std::shared_ptr<ModelAPI_ResultBody> aResultBody;
371
372     if (theResultCompound) { // store BOP result to the compound
373       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
374       aBuilder->add(theResultCompound, aResultShape);
375       aMakeShapeList->appendAlgo(aBuilder);
376     }
377     else { // create a separate ResultBody
378       aResultBody = document()->createBody(data(), theResultIndex);
379
380       ListOfShape aCompoundList;
381       aCompoundList.push_back(theCompound);
382       ModelAPI_Tools::loadModifiedShapes(aResultBody,
383                                                aCompoundList,
384                                                theTools,
385                                                aMakeShapeList,
386                                                aResultShape);
387       setResult(aResultBody, theResultIndex);
388       ++theResultIndex;
389     }
390
391     ModelAPI_Tools::ResultBaseAlgo aRBA;
392     aRBA.resultBody = aResultBody;
393     aRBA.baseShape = theCompound;
394     aRBA.makeShape = aMakeShapeList;
395     theResultBaseAlgoList.push_back(aRBA);
396     theResultShapesList.push_back(aResultShape);
397   }
398   return true;
399 }
400
401 //==================================================================================================
402 GeomShapePtr FeaturesPlugin_VersionedBoolean::keepUnusedSubsOfCompound(
403     const GeomShapePtr& theResult,
404     const GeomAPI_ShapeHierarchy& theObjectsHierarchy,
405     const GeomAPI_ShapeHierarchy& theToolsHierarchy,
406     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
407 {
408   ListOfShape aCompounds;
409   theObjectsHierarchy.compoundsOfUnusedObjects(aCompounds);
410   theToolsHierarchy.compoundsOfUnusedObjects(aCompounds);
411
412   GeomShapePtr aResultShape = theResult;
413   if (!aCompounds.empty()) {
414     aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aCompounds);
415     if (theResult) {
416       std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
417       aBuilder->add(aResultShape, theResult);
418       theMakeShapeList->appendAlgo(aBuilder);
419     }
420   }
421   return aResultShape;
422 }
423
424 //=================================================================================================
425 void FeaturesPlugin_VersionedBoolean::resizePlanes(
426     const ListOfShape& theObjects,
427     ListOfShape& thePlanes,
428     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
429 {
430   if (thePlanes.empty())
431     return;
432
433   std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
434       GeomAlgoAPI_ShapeTools::getBoundingBox(theObjects, 1.0);
435
436   // Resize planes to fit in bounding box
437   for (ListOfShape::iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt) {
438     GeomShapePtr aPlane = *anIt;
439     GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
440     std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
441     aMkShCustom->addModified(aPlane, aTool);
442     theMakeShapeList->appendAlgo(aMkShCustom);
443     *anIt = aTool;
444   }
445 }