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