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)
92 AttributeSelectionListPtr anObjectsSelList = selectionList(theAttributeName);
93 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
94 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
95 GeomShapePtr anObject = anObjectAttr->value();
96 if (!anObject.get()) {
97 // It could be a construction plane.
98 ResultPtr aContext = anObjectAttr->context();
99 anObject = anObjectAttr->context()->shape();
100 if (anObject.get()) {
101 thePlanesList.push_back(anObject);
107 theObjects.AddObject(anObject);
109 ResultPtr aContext = anObjectAttr->context();
110 parentForShape(anObject, aContext, theObjects);
115 //=================================================================================================
116 void FeaturesPlugin_Boolean::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
117 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
118 const ListOfShape& theTools,
119 const std::shared_ptr<GeomAPI_Shape> theResultShape,
120 const GeomMakeShapePtr& theMakeShape)
123 if(theBaseShape->isEqual(theResultShape)) {
124 theResultBody->store(theResultShape, false);
128 theResultBody->storeModified(theBaseShape, theResultShape);
130 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::EDGE);
131 theResultBody->loadModifiedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
133 theResultBody->loadDeletedShapes(theMakeShape, theBaseShape, GeomAPI_Shape::FACE);
135 for (ListOfShape::const_iterator anIter = theTools.begin();
136 anIter != theTools.end();
139 GeomAPI_Shape::ShapeType aShapeType =
140 (*anIter)->shapeType() <= GeomAPI_Shape::FACE ? GeomAPI_Shape::FACE
141 : GeomAPI_Shape::EDGE;
142 theResultBody->loadModifiedShapes(theMakeShape, *anIter, aShapeType);
144 theResultBody->loadDeletedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE);
148 //=================================================================================================
149 bool FeaturesPlugin_Boolean::processObject(
150 const GeomAlgoAPI_Tools::BOPType theBooleanType,
151 const GeomShapePtr& theObject,
152 const ListOfShape& theTools,
153 const ListOfShape& thePlanes,
155 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
156 ListOfShape& theResultShapesList)
158 ListOfShape aListWithObject;
159 aListWithObject.push_back(theObject);
160 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
161 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
162 GeomShapePtr aResShape;
164 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
165 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
168 ListOfShape aToolsWithPlanes = theTools;
169 for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt) {
170 GeomShapePtr aPlane = *anIt;
171 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
172 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
173 new GeomAlgoAPI_MakeShapeCustom);
174 aMkShCustom->addModified(aPlane, aTool);
175 aMakeShapeList->appendAlgo(aMkShCustom);
176 aToolsWithPlanes.push_back(aTool);
179 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
180 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
182 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
186 // Checking that the algorithm worked properly.
188 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
193 aResShape = aBoolAlgo->shape();
194 if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
195 int aSubResultsNb = 0;
196 GeomAPI_ShapeIterator anIt(aResShape);
197 for (; anIt.more(); anIt.next())
200 if (aSubResultsNb == 1) {
201 anIt.init(aResShape);
203 aResShape = anIt.current();
207 aMakeShapeList->appendAlgo(aBoolAlgo);
209 GeomAPI_ShapeIterator aShapeIt(aResShape);
210 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
211 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
212 document()->createBody(data(), theResultIndex);
214 // tools should be added to the list to fulfill the correct history of modification
215 aListWithObject.insert(aListWithObject.end(), theTools.begin(), theTools.end());
217 ListOfShape aUsedTools = theTools;
218 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
220 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
225 setResult(aResultBody, theResultIndex);
228 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
229 aRBA.resultBody = aResultBody;
230 aRBA.baseShape = theObject;
231 aRBA.makeShape = aMakeShapeList;
232 theResultBaseAlgoList.push_back(aRBA);
233 theResultShapesList.push_back(aResShape);
238 //=================================================================================================
239 bool FeaturesPlugin_Boolean::processCompsolid(
240 const GeomAlgoAPI_Tools::BOPType theBooleanType,
241 const ObjectHierarchy& theCompsolidHierarchy,
242 const GeomShapePtr& theCompsolid,
243 const ListOfShape& theTools,
244 const ListOfShape& thePlanes,
246 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
247 ListOfShape& theResultShapesList)
249 ListOfShape aUsedInOperationSolids;
250 ListOfShape aNotUsedSolids;
251 theCompsolidHierarchy.SplitCompound(theCompsolid, aUsedInOperationSolids, aNotUsedSolids);
253 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
255 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
256 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
259 ListOfShape aToolsWithPlanes = theTools;
260 for (ListOfShape::const_iterator anIt = thePlanes.begin(); anIt != thePlanes.end(); ++anIt)
262 GeomShapePtr aPlane = *anIt;
263 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
264 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
265 new GeomAlgoAPI_MakeShapeCustom);
266 aMkShCustom->addModified(aPlane, aTool);
267 aMakeShapeList->appendAlgo(aMkShCustom);
268 aToolsWithPlanes.push_back(aTool);
271 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
272 if (theBooleanType == GeomAlgoAPI_Tools::BOOL_PARTITION)
273 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
275 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
279 // Checking that the algorithm worked properly.
281 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
286 aMakeShapeList->appendAlgo(aBoolAlgo);
287 GeomShapePtr aResultShape = aBoolAlgo->shape();
289 // Add result to not used solids from compsolid.
290 if (!aNotUsedSolids.empty()) {
291 ListOfShape aShapesToAdd = aNotUsedSolids;
292 aShapesToAdd.push_back(aBoolAlgo->shape());
293 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
294 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
295 if (!aFillerAlgo->isDone()) {
296 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
297 setError(aFeatureError);
301 aMakeShapeList->appendAlgo(aFillerAlgo);
302 aResultShape = aFillerAlgo->shape();
305 GeomAPI_ShapeIterator aShapeIt(aResultShape);
306 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
308 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
309 document()->createBody(data(), theResultIndex);
311 ListOfShape aCompSolidList;
312 aCompSolidList.push_back(theCompsolid);
313 // tools should be added to the list to fulfill the correct history of modification
314 aCompSolidList.insert(aCompSolidList.end(), theTools.begin(), theTools.end());
316 ListOfShape aUsedTools = theTools;
317 aUsedTools.insert(aUsedTools.end(), thePlanes.begin(), thePlanes.end());
319 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
324 setResult(aResultBody, theResultIndex);
327 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
328 aRBA.resultBody = aResultBody;
329 aRBA.baseShape = theCompsolid;
330 aRBA.makeShape = aMakeShapeList;
331 theResultBaseAlgoList.push_back(aRBA);
332 theResultShapesList.push_back(aResultShape);
337 //=================================================================================================
338 bool FeaturesPlugin_Boolean::processCompound(
339 const GeomAlgoAPI_Tools::BOPType theBooleanType,
340 const ObjectHierarchy& theCompoundHierarchy,
341 const GeomShapePtr& theCompound,
342 const ListOfShape& theTools,
344 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
345 ListOfShape& theResultShapesList)
347 ListOfShape aUsedInOperationShapes;
348 ListOfShape aNotUsedShapes;
349 theCompoundHierarchy.SplitCompound(theCompound, aUsedInOperationShapes, aNotUsedShapes);
351 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
352 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
353 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
357 // Checking that the algorithm worked properly.
359 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
364 aMakeShapeList->appendAlgo(aBoolAlgo);
365 GeomShapePtr aResultShape = aBoolAlgo->shape();
367 // Add result to not used shape from compound.
368 if (!aNotUsedShapes.empty()) {
369 ListOfShape aShapesForResult = aNotUsedShapes;
370 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
371 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
372 aShapesForResult.push_back(aResultIt.current());
376 aShapesForResult.push_back(aResultShape);
379 if (aShapesForResult.size() == 1) {
380 aResultShape = aShapesForResult.front();
383 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
387 GeomAPI_ShapeIterator aShapeIt(aResultShape);
388 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
389 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
390 document()->createBody(data(), theResultIndex);
392 ListOfShape aCompoundList;
393 aCompoundList.push_back(theCompound);
394 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
399 setResult(aResultBody, theResultIndex);
402 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
403 aRBA.resultBody = aResultBody;
404 aRBA.baseShape = theCompound;
405 aRBA.makeShape = aMakeShapeList;
406 theResultBaseAlgoList.push_back(aRBA);
407 theResultShapesList.push_back(aResultShape);
412 //=================================================================================================
414 void FeaturesPlugin_Boolean::ObjectHierarchy::AddObject(const GeomShapePtr& theObject)
416 myObjects.push_back(theObject);
419 void FeaturesPlugin_Boolean::ObjectHierarchy::AddParent(const GeomShapePtr& theShape,
420 const GeomShapePtr& theParent)
422 myParent[theShape] = theParent;
423 mySubshapes[theParent].push_back(theShape);
426 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Parent(const GeomShapePtr& theShape,
427 bool theMarkProcessed)
429 MapShapeToParent::const_iterator aFound = myParent.find(theShape);
430 GeomShapePtr aParent;
431 if (aFound != myParent.end()) {
432 aParent = aFound->second;
433 if (theMarkProcessed) {
434 // mark the parent and all its subs as processed by Boolean algorithm
435 myProcessedObjects.insert(aParent);
436 const ListOfShape& aSubs = mySubshapes[aParent];
437 for (ListOfShape::const_iterator anIt = aSubs.begin(); anIt != aSubs.end(); ++anIt)
438 myProcessedObjects.insert(*anIt);
444 void FeaturesPlugin_Boolean::ObjectHierarchy::SplitCompound(const GeomShapePtr& theCompShape,
445 ListOfShape& theUsed,
446 ListOfShape& theNotUsed) const
451 const ListOfShape& aSubs = mySubshapes.find(theCompShape)->second;
453 aSubsSet.insert(aSubs.begin(), aSubs.end());
455 for (GeomAPI_ShapeExplorer anExp(theCompShape, GeomAPI_Shape::SOLID);
456 anExp.more(); anExp.next()) {
457 GeomShapePtr aCurrent = anExp.current();
458 if (aSubsSet.find(aCurrent) == aSubsSet.end())
459 theNotUsed.push_back(aCurrent);
461 theUsed.push_back(aCurrent);
465 bool FeaturesPlugin_Boolean::ObjectHierarchy::IsEmpty() const
467 return myObjects.empty();
470 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::Begin()
472 return Iterator(this);
475 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator FeaturesPlugin_Boolean::ObjectHierarchy::End()
477 return Iterator(this, false);
480 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::Iterator(
481 FeaturesPlugin_Boolean::ObjectHierarchy* theHierarchy, bool isBegin)
482 : myHierarchy(theHierarchy)
485 myObject = myHierarchy->myObjects.begin();
486 SkipAlreadyProcessed();
488 myObject = myHierarchy->myObjects.end();
491 void FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::SkipAlreadyProcessed()
493 while (myObject != myHierarchy->myObjects.end() &&
494 myHierarchy->myProcessedObjects.find(*myObject) != myHierarchy->myProcessedObjects.end())
498 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator==(const Iterator& theOther) const
500 return myObject == theOther.myObject;
503 bool FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator!=(const Iterator& theOther) const
505 return !operator==(theOther);
508 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator&
509 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++()
512 SkipAlreadyProcessed();
516 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator
517 FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator++(int)
520 aCurrent.myHierarchy = myHierarchy;
521 aCurrent.myObject = myObject;
529 GeomShapePtr FeaturesPlugin_Boolean::ObjectHierarchy::Iterator::operator*() const
531 myHierarchy->myProcessedObjects.insert(*myObject);