Salome HOME
f74b06ad4963c2b2630e64773cf262916587fa5b
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_GroupIntersection.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CollectionPlugin_GroupIntersection.h"
21
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>
28
29 #include <GeomAPI_ShapeIterator.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32
33 typedef std::set<GeomShapePtr, GeomAPI_Shape::ComparatorWithOri> SetOfShape;
34
35 CollectionPlugin_GroupIntersection::CollectionPlugin_GroupIntersection()
36 {
37 }
38
39 void CollectionPlugin_GroupIntersection::initAttributes()
40 {
41   data()->addAttribute(CollectionPlugin_GroupIntersection::LIST_ID(),
42                        ModelAPI_AttributeSelectionList::typeId());
43 }
44
45 static void explodeCompound(const GeomShapePtr& theCompound, SetOfShape& theSet)
46 {
47   for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
48     theSet.insert(anIt.current());
49 }
50
51 static void keepCommonShapes(const GeomShapePtr& theCompound, ListOfShape& theShapes)
52 {
53   if (theShapes.empty()) {
54     for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
55       theShapes.push_back(anIt.current());
56   }
57   else {
58     SetOfShape aSubs;
59     explodeCompound(theCompound, aSubs);
60
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);
67       }
68       else
69         ++anIt;
70     }
71   }
72 }
73
74 void CollectionPlugin_GroupIntersection::execute()
75 {
76   ResultGroupPtr aGroup = document()->createGroup(data());
77   // clean the result of the operation
78   aGroup->store(GeomShapePtr());
79
80   // shapes containing in each group
81   ListOfShape aCommon;
82
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);
90     if (aCommon.empty())
91       break;
92   }
93
94   if (aCommon.empty()) {
95     setError("Error: Empty result.");
96     removeResults(0);
97   }
98   else {
99     // update the type of selection
100     updateGroupType(LIST_ID());
101
102     GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aCommon);
103     aGroup->store(aCompound);
104     setResult(aGroup);
105   }
106 }