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 "CollectionPlugin_GroupIntersection.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::ComparatorWithOri> SetOfShape;
35 CollectionPlugin_GroupIntersection::CollectionPlugin_GroupIntersection()
39 void CollectionPlugin_GroupIntersection::initAttributes()
41 data()->addAttribute(CollectionPlugin_GroupIntersection::LIST_ID(),
42 ModelAPI_AttributeSelectionList::typeId());
45 static void explodeCompound(const GeomShapePtr& theCompound, SetOfShape& theSet)
47 for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
48 theSet.insert(anIt.current());
51 static void keepCommonShapes(const GeomShapePtr& theCompound, ListOfShape& theShapes)
53 if (theShapes.empty()) {
54 for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
55 theShapes.push_back(anIt.current());
59 explodeCompound(theCompound, aSubs);
61 ListOfShape::iterator anIt = theShapes.begin();
62 while (anIt != theShapes.end()) {
63 if (aSubs.find(*anIt) == aSubs.end()) {
64 // the shape is not found
65 ListOfShape::iterator aRemoveIt = anIt++;
66 theShapes.erase(aRemoveIt);
74 void CollectionPlugin_GroupIntersection::execute()
76 ResultGroupPtr aGroup = document()->createGroup(data());
77 // clean the result of the operation
78 aGroup->store(GeomShapePtr());
80 // shapes containing in each group
83 // collect shapes containing in all groups
84 AttributeSelectionListPtr aSelectedGroups = selectionList(LIST_ID());
85 for (int anIndex = 0; anIndex < aSelectedGroups->size(); ++anIndex) {
86 AttributeSelectionPtr aCurSelection = aSelectedGroups->value(anIndex);
87 ResultGroupPtr aCurGroup =
88 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(aCurSelection->context());
89 keepCommonShapes(aCurGroup->shape(), aCommon);
94 if (aCommon.empty()) {
95 setError("Error: Empty result.");
99 // update the type of selection
100 updateGroupType(LIST_ID());
102 GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aCommon);
103 aGroup->store(aCompound);