1 // Copyright (C) 2014-2019 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_Boolean.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_ShapeTools.h>
39 #include <GeomAlgoAPI_Tools.h>
40 #include <GeomAPI_Face.h>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_ShapeIterator.h>
47 //=================================================================================================
48 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean(const OperationType theOperationType)
49 : myOperationType(theOperationType)
53 //=================================================================================================
54 void FeaturesPlugin_Boolean::initAttributes()
56 AttributeSelectionListPtr aSelection =
57 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
58 FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
60 aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
61 FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
67 //=================================================================================================
68 FeaturesPlugin_Boolean::OperationType FeaturesPlugin_Boolean::operationType()
70 return myOperationType;
73 //=================================================================================================
74 void FeaturesPlugin_Boolean::parentForShape(const GeomShapePtr& theShape,
75 const ResultPtr& theContext,
76 ObjectHierarchy& theShapesHierarchy)
78 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(theContext);
79 if (aResCompSolidPtr.get()) {
80 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
81 if (aContextShape->shapeType() <= GeomAPI_Shape::COMPSOLID) {
82 theShapesHierarchy.AddParent(theShape, aContextShape);
83 parentForShape(aContextShape, aResCompSolidPtr, theShapesHierarchy);
88 bool FeaturesPlugin_Boolean::processAttribute(const std::string& theAttributeName,
89 ObjectHierarchy& theObjects,
90 ListOfShape& thePlanesList,
91 ListOfShape& theEdgesAndFaces)
93 AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
94 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
95 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
96 GeomShapePtr anObject = anObjectAttr->value();
97 if (!anObject.get()) {
98 // It could be a construction plane.
99 ResultPtr aContext = anObjectAttr->context();
100 anObject = anObjectAttr->context()->shape();
101 if (anObject.get()) {
102 thePlanesList.push_back(anObject);
108 if (anObject->shapeType() == GeomAPI_Shape::EDGE ||
109 anObject->shapeType() == GeomAPI_Shape::FACE) {
110 theEdgesAndFaces.push_back(anObject);
114 theObjects.AddObject(anObject);
116 ResultPtr aContext = anObjectAttr->context();
117 parentForShape(anObject, aContext, theObjects);
122 //=================================================================================================
123 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
124 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
125 const ListOfShape& theTools,
126 const std::shared_ptr<GeomAPI_Shape> theResultShape,
127 const GeomMakeShapePtr& theMakeShape)
130 if(theBaseShape->isEqual(theResultShape)) {
131 theResultBody->store(theResultShape, false);
135 theResultBody->storeModified(theBaseShape, theResultShape);
137 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
138 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
140 theResultBody->loadDeletedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
142 for (ListOfShape::const_iterator anIter = theTools.begin();
143 anIter != theTools.end();
146 GeomAPI_Shape::ShapeType aShapeType =
147 (*anIter)->shapeType() <= GeomAPI_Shape::FACE ? GeomAPI_Shape::FACE
148 : GeomAPI_Shape::EDGE;
149 theResultBody->loadModifiedShapes(theMakeShape, *anIter, aShapeType);
151 theResultBody->loadDeletedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE);
155 //=================================================================================================
156 bool FeaturesPlugin_Boolean::processObject(
157 const GeomAlgoAPI_Tools::BOPType theBooleanType,
158 const GeomShapePtr& theObject,
159 const ListOfShape& theTools,
160 const ListOfShape& thePlanes,
162 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
163 ListOfShape& theResultShapesList)
165 ListOfShape aListWithObject;
166 aListWithObject.push_back(theObject);
167 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
168 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
169 GeomShapePtr aResShape;
171 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
172 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
175 ListOfShape aToolsWithPlanes = theTools;
176 for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt) {
177 GeomShapePtr aPlane = *anIt;
178 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
179 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
180 new GeomAlgoAPI_MakeShapeCustom);
181 aMkShCustom->addModified(aPlane, aTool);
182 aMakeShapeList->appendAlgo(aMkShCustom);
183 aToolsWithPlanes.push_back(aTool);
186 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
187 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
189 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
193 // Checking that the algorithm worked properly.
195 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
200 aResShape = aBoolAlgo->shape();
201 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
202 int aSubResultsNb = 0;
203 GeomAPI_ShapeIterator anIt(aResShape);
204 for (; anIt.more(); anIt.next())
207 if (aSubResultsNb == 1) {
208 anIt.init(aResShape);
210 aResShape = anIt.current();
214 aMakeShapeList->appendAlgo(aBoolAlgo);
216 GeomAPI_ShapeIterator aShapeIt(aResShape);
217 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
218 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
219 document()->createBody(data(), theResultIndex);
221 // tools should be added to the list to fulfill the correct history of modification
222 aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
224 ListOfShape aUsedTools = theTools;
225 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
227 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
232 setResult(aResultBody, theResultIndex);
235 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
236 aRBA.resultBody = aResultBody;
237 aRBA.baseShape = theObject;
238 aRBA.makeShape = aMakeShapeList;
239 theResultBaseAlgoList.push_back(aRBA);
240 theResultShapesList.push_back(aResShape);
245 //=================================================================================================
246 bool FeaturesPlugin_Boolean::processCompsolid(
247 const GeomAlgoAPI_Tools::BOPType theBooleanType,
248 const ObjectHierarchy& theCompsolidHierarchy,
249 const GeomShapePtr& theCompsolid,
250 const ListOfShape& theTools,
251 const ListOfShape& thePlanes,
253 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
254 ListOfShape& theResultShapesList)
256 ListOfShape aUsedInOperationSolids;
257 ListOfShape aNotUsedSolids;
258 theCompsolidHierarchy.SplitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
260 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
262 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
263 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
266 ListOfShape aToolsWithPlanes = theTools;
267 for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt)
269 GeomShapePtr aPlane = *anIt;
270 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
271 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
272 new GeomAlgoAPI_MakeShapeCustom);
273 aMkShCustom->addModified(aPlane, aTool);
274 aMakeShapeList->appendAlgo(aMkShCustom);
275 aToolsWithPlanes.push_back(aTool);
278 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
279 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
280 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
282 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
286 // Checking that the algorithm worked properly.
288 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
293 aMakeShapeList->appendAlgo(aBoolAlgo);
294 GeomShapePtr aResultShape = aBoolAlgo->shape();
296 // Add result to not used solids from compsolid.
297 if (!aNotUsedSolids.empty()) {
298 ListOfShape aShapesToAdd = aNotUsedSolids;
299 aShapesToAdd.push_back(aBoolAlgo->shape());
300 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
301 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
302 if (!aFillerAlgo->isDone()) {
303 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
304 setError(aFeatureError);
308 aMakeShapeList->appendAlgo(aFillerAlgo);
309 aResultShape = aFillerAlgo->shape();
312 GeomAPI_ShapeIterator aShapeIt(aResultShape);
313 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
315 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
316 document()->createBody(data(), theResultIndex);
318 ListOfShape aCompSolidList;
319 aCompSolidList.push_back(theCompsolid);
320 // tools should be added to the list to fulfill the correct history of modification
321 aCompSolidList.insert(aCompSolidList.end(), theTools.begin(), theTools.end());
323 ListOfShape aUsedTools = theTools;
324 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
326 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
331 setResult(aResultBody, theResultIndex);
334 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
335 aRBA.resultBody = aResultBody;
336 aRBA.baseShape = theCompsolid;
337 aRBA.makeShape = aMakeShapeList;
338 theResultBaseAlgoList.push_back(aRBA);
339 theResultShapesList.push_back(aResultShape);
344 //=================================================================================================
345 bool FeaturesPlugin_Boolean::processCompound(
346 const GeomAlgoAPI_Tools::BOPType theBooleanType,
347 const ObjectHierarchy& theCompoundHierarchy,
348 const GeomShapePtr& theCompound,
349 const ListOfShape& theTools,
351 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
352 ListOfShape& theResultShapesList)
354 ListOfShape aUsedInOperationShapes;
355 ListOfShape aNotUsedShapes;
356 theCompoundHierarchy.SplitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
358 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
359 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
360 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
364 // Checking that the algorithm worked properly.
366 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
371 aMakeShapeList->appendAlgo(aBoolAlgo);
372 GeomShapePtr aResultShape = aBoolAlgo->shape();
374 // Add result to not used shape from compound.
375 if (!aNotUsedShapes.empty()) {
376 ListOfShape aShapesForResult = aNotUsedShapes;
377 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
378 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
379 aShapesForResult.push_back(aResultIt.current());
383 aShapesForResult.push_back(aResultShape);
386 if (aShapesForResult.size() == 1) {
387 aResultShape = aShapesForResult.front();
390 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
394 GeomAPI_ShapeIterator aShapeIt(aResultShape);
395 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
396 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
397 document()->createBody(data(), theResultIndex);
399 ListOfShape aCompoundList;
400 aCompoundList.push_back(theCompound);
401 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
406 setResult(aResultBody, theResultIndex);
409 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
410 aRBA.resultBody = aResultBody;
411 aRBA.baseShape = theCompound;
412 aRBA.makeShape = aMakeShapeList;
413 theResultBaseAlgoList.push_back(aRBA);
414 theResultShapesList.push_back(aResultShape);
419 //=================================================================================================
421 void FeaturesPlugin_Boolean::ObjectHierarchy::AddObject(const GeomShapePtr& theObject)
423 myObjects.push_back(theObject);
426 void FeaturesPlugin_Boolean::ObjectHierarchy::AddParent(const GeomShapePtr& theShape,
427 const GeomShapePtr& theParent)
429 myParent[theShape] = theParent;
430 mySubshapes[theParent].push_back(theShape);
433 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Parent(const GeomShapePtr& theShape,
434 bool theMarkProcessed)
436 MapShapeToParent::const_iterator aFound = myParent.find(theShape);
437 GeomShapePtr aParent;
438 if (aFound != myParent.end()) {
439 aParent = aFound->second;
440 if (theMarkProcessed) {
441 // mark the parent and all its subs as processed by Boolean algorithm
442 myProcessedObjects.insert(aParent);
443 const ListOfShape& aSubs = mySubshapes[aParent];
444 for (ListOfShape::const_iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt)
445 myProcessedObjects.insert(*anIt);
451 void FeaturesPlugin_Boolean::ObjectHierarchy::SplitCompound(const GeomShapePtr& theCompShape,
452 ListOfShape& theUsed,
453 ListOfShape& theNotUsed) const
458 const ListOfShape& aSubs = mySubshapes.find(theCompShape)->second;
460 aSubsSet.insert(aSubs.begin(), aSubs.end());
462 for (GeomAPI_ShapeExplorer anExp(theCompShape, GeomAPI_Shape::SOLID);
463 anExp.more(); anExp.next()) {
464 GeomShapePtr aCurrent = anExp.current();
465 if (aSubsSet.find(aCurrent) == aSubsSet.end())
466 theNotUsed.push_back(aCurrent);
468 theUsed.push_back(aCurrent);
472 bool FeaturesPlugin_Boolean::ObjectHierarchy::IsEmpty() const
474 return myObjects.empty();
477 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::Begin()
479 return Iterator(this);
482 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::End()
484 return Iterator(this, false);
487 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::Iterator(
488 FeaturesPlugin_Boolean::ObjectHierarchy* theHierarchy, bool isBegin)
489 : myHierarchy(theHierarchy)
492 myObject = myHierarchy->myObjects.begin();
493 SkipAlreadyProcessed();
495 myObject = myHierarchy->myObjects.end();
498 void FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::SkipAlreadyProcessed()
500 while (myObject != myHierarchy->myObjects.end() &&
501 myHierarchy->myProcessedObjects.find(*myObject) != myHierarchy->myProcessedObjects.end())
505 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator==(const Iterator& theOther) const
507 return myObject == theOther.myObject;
510 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator!=(const Iterator& theOther) const
512 return !operator==(theOther);
515 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator&
516 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++()
519 SkipAlreadyProcessed();
523 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator
524 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++(int)
527 aCurrent.myHierarchy = myHierarchy;
528 aCurrent.myObject = myObject;
536 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator*() const
538 myHierarchy->myProcessedObjects.insert(*myObject);