1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_VersionedBoolean.h"
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>
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>
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)
57 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
58 theBooleanAlgo.reset(new GeomAlgoAPI_Partition(theObjects, theTools));
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));
65 ListOfShape anObjects = theObjects;
67 aTools.splice(aTools.begin(), anObjects, anObjects.begin());
68 theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(anObjects, aTools, theBooleanType, theFuzzy));
72 theBooleanAlgo.reset(new GeomAlgoAPI_Boolean(theObjects, theTools, theBooleanType, theFuzzy));
76 //=================================================================================================
77 void FeaturesPlugin_VersionedBoolean::initVersion(const std::string& theVersion,
78 const AttributePtr theObjectsAttr,
79 const AttributePtr theToolsAttr)
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());
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);
94 //=================================================================================================
95 bool FeaturesPlugin_VersionedBoolean::processAttribute(const std::string& theAttributeName,
96 GeomAPI_ShapeHierarchy& theObjects,
97 ListOfShape& thePlanesList)
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);
114 theObjects.addObject(anObject);
116 ResultPtr aContext = anObjectAttr->context();
117 ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theObjects);
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,
130 std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
131 ListOfShape& theResultShapesList,
132 GeomShapePtr theResultCompound)
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;
141 ListOfShape aToolsWithPlanes = theTools;
142 ListOfShape aPlanesCopy = thePlanes;
143 resizePlanes(aListWithObject, aPlanesCopy, aMakeShapeList);
144 aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
146 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
147 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes, theFuzzy));
149 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
154 // Checking that the algorithm worked properly.
156 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
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())
168 if (aSubResultsNb == 1) {
169 anIt.init(aResShape);
171 aResShape = anIt.current();
175 aMakeShapeList->appendAlgo(aBoolAlgo);
177 GeomAPI_ShapeIterator aShapeIt(aResShape);
178 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
179 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
186 else { // create a separate ResultBody
187 aResultBody = document()->createBody(data(), theResultIndex);
189 // tools should be added to the list to fulfill the correct history of modification
190 aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
192 ListOfShape aUsedTools = theTools;
193 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
195 ModelAPI_Tools::loadModifiedShapes(aResultBody,
200 setResult(aResultBody, theResultIndex);
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);
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,
224 std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
225 ListOfShape& theResultShapesList,
226 GeomShapePtr theResultCompound)
228 ListOfShape aUsedInOperationSolids;
229 ListOfShape aNotUsedSolids;
230 theCompsolidHierarchy.splitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
232 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
235 ListOfShape aToolsWithPlanes = theTools;
236 ListOfShape aPlanesCopy = thePlanes;
237 resizePlanes(aUsedInOperationSolids, aPlanesCopy, aMakeShapeList);
238 aToolsWithPlanes.insert(aToolsWithPlanes.end(), aPlanesCopy.begin(), aPlanesCopy.end());
240 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
241 performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationSolids, aToolsWithPlanes, theFuzzy);
243 // Checking that the algorithm worked properly.
245 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
250 aMakeShapeList->appendAlgo(aBoolAlgo);
251 GeomShapePtr aResultShape = aBoolAlgo->shape();
253 // Add result to not used solids from compsolid.
254 if (!aNotUsedSolids.empty()) {
255 theCompsolidHierarchy.markProcessed(aNotUsedSolids);
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);
267 aMakeShapeList->appendAlgo(aFillerAlgo);
268 aResultShape = aFillerAlgo->shape();
271 GeomAPI_ShapeIterator aShapeIt(aResultShape);
272 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
274 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
281 else { // create a separate ResultBody
282 aResultBody = document()->createBody(data(), theResultIndex);
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());
289 ListOfShape aUsedTools = theTools;
290 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
292 ModelAPI_Tools::loadModifiedShapes(aResultBody,
297 setResult(aResultBody, theResultIndex);
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);
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,
319 std::vector<ModelAPI_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
320 ListOfShape& theResultShapesList,
321 GeomShapePtr theResultCompound)
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();
332 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
333 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
334 performBoolean(theBooleanType, aBoolAlgo, aUsedInOperationShapes, theTools, theFuzzy);
336 // Checking that the algorithm worked properly.
338 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
343 aMakeShapeList->appendAlgo(aBoolAlgo);
344 GeomShapePtr aResultShape = aBoolAlgo->shape();
346 // Add result to not used shape from compound.
347 if (!aNotUsedShapes.empty()) {
348 theCompoundHierarchy.markProcessed(aNotUsedShapes);
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());
357 aShapesForResult.push_back(aResultShape);
360 if (aShapesForResult.size() == 1) {
361 aResultShape = aShapesForResult.front();
364 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
368 GeomAPI_ShapeIterator aShapeIt(aResultShape);
369 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
370 std::shared_ptr<ModelAPI_ResultBody> aResultBody;
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);
377 else { // create a separate ResultBody
378 aResultBody = document()->createBody(data(), theResultIndex);
380 ListOfShape aCompoundList;
381 aCompoundList.push_back(theCompound);
382 ModelAPI_Tools::loadModifiedShapes(aResultBody,
387 setResult(aResultBody, theResultIndex);
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);
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)
408 ListOfShape aCompounds;
409 theObjectsHierarchy.compoundsOfUnusedObjects(aCompounds);
410 theToolsHierarchy.compoundsOfUnusedObjects(aCompounds);
412 GeomShapePtr aResultShape = theResult;
413 if (!aCompounds.empty()) {
414 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aCompounds);
416 std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
417 aBuilder->add(aResultShape, theResult);
418 theMakeShapeList->appendAlgo(aBuilder);
424 //=================================================================================================
425 void FeaturesPlugin_VersionedBoolean::resizePlanes(
426 const ListOfShape& theObjects,
427 ListOfShape& thePlanes,
428 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList)
430 if (thePlanes.empty())
433 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
434 GeomAlgoAPI_ShapeTools::getBoundingBox(theObjects, 1.0);
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);