1 // Copyright (C) 2014-2024 CEA, EDF
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 "CollectionPlugin_GroupSubstraction.h"
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_ResultGroup.h>
29 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
33 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
35 CollectionPlugin_GroupSubstraction::CollectionPlugin_GroupSubstraction()
39 void CollectionPlugin_GroupSubstraction::initAttributes()
41 data()->addAttribute(CollectionPlugin_GroupSubstraction::LIST_ID(),
42 ModelAPI_AttributeSelectionList::typeId());
43 data()->addAttribute(CollectionPlugin_GroupSubstraction::TOOLS_ID(),
44 ModelAPI_AttributeSelectionList::typeId());
47 static void explodeCompound(const GeomShapePtr& theCompound, SetOfShape& theSet)
49 for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
50 theSet.insert(anIt.current());
53 static void subtractLists(const GeomShapePtr& theCompound,
54 const SetOfShape& theExclude,
55 ListOfShape& theResult)
57 for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next()) {
58 GeomShapePtr aCurrent = anIt.current();
59 if (theExclude.find(aCurrent) != theExclude.end())
60 continue; // shape has to be excluded
61 // check the shape is already in the list
62 ListOfShape::iterator anIt2 = theResult.begin();
63 for (; anIt2 != theResult.end(); ++anIt2)
64 if (aCurrent->isEqual(*anIt2))
66 if (anIt2 == theResult.end())
67 theResult.push_back(aCurrent);
71 void CollectionPlugin_GroupSubstraction::execute()
73 ResultGroupPtr aGroup = document()->createGroup(data());
74 // clean the result of the operation
75 aGroup->store(GeomShapePtr());
77 // collect shapes to be excluded (tools)
78 SetOfShape aShapesToExclude;
79 AttributeSelectionListPtr aTools = selectionList(TOOLS_ID());
80 for (int anIndex = 0; anIndex < aTools->size(); ++anIndex) {
81 AttributeSelectionPtr aCurSelection = aTools->value(anIndex);
82 ResultGroupPtr aCurGroup =
83 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aCurSelection->context());
84 explodeCompound(aCurGroup->shape(), aShapesToExclude);
87 // keep only shapes that should not be excluded
89 AttributeSelectionListPtr anObjects = selectionList(LIST_ID());
90 for (int anIndex = 0; anIndex < anObjects->size(); ++anIndex) {
91 AttributeSelectionPtr aCurSelection = anObjects->value(anIndex);
92 ResultGroupPtr aCurGroup =
93 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aCurSelection->context());
94 subtractLists(aCurGroup->shape(), aShapesToExclude, aCut);
100 setError("Error: Empty result.");
104 // update the type of selection
105 updateGroupType(LIST_ID());
107 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aCut);
108 aGroup->store(aCompound);