]> SALOME platform Git repositories - modules/shaper.git/blob - src/CollectionPlugin/CollectionPlugin_GroupSubstraction.cpp
Salome HOME
Updated copyright comment
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_GroupSubstraction.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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_GroupSubstraction.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::Comparator> SetOfShape;
34
35 CollectionPlugin_GroupSubstraction::CollectionPlugin_GroupSubstraction()
36 {
37 }
38
39 void CollectionPlugin_GroupSubstraction::initAttributes()
40 {
41   data()->addAttribute(CollectionPlugin_GroupSubstraction::LIST_ID(),
42                        ModelAPI_AttributeSelectionList::typeId());
43   data()->addAttribute(CollectionPlugin_GroupSubstraction::TOOLS_ID(),
44                        ModelAPI_AttributeSelectionList::typeId());
45 }
46
47 static void explodeCompound(const GeomShapePtr& theCompound, SetOfShape& theSet)
48 {
49   for (GeomAPI_ShapeIterator anIt(theCompound); anIt.more(); anIt.next())
50     theSet.insert(anIt.current());
51 }
52
53 static void subtractLists(const GeomShapePtr& theCompound,
54                           const SetOfShape& theExclude,
55                                 ListOfShape& theResult)
56 {
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))
65         break;
66     if (anIt2 == theResult.end())
67       theResult.push_back(aCurrent);
68   }
69 }
70
71 void CollectionPlugin_GroupSubstraction::execute()
72 {
73   ResultGroupPtr aGroup = document()->createGroup(data());
74   // clean the result of the operation
75   aGroup->store(GeomShapePtr());
76
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);
85   }
86
87   // keep only shapes that should not be excluded
88   ListOfShape aCut;
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);
95     if (aCut.empty())
96       break;
97   }
98
99   if (aCut.empty()) {
100     setError("Error: Empty result.");
101     removeResults(0);
102   }
103   else {
104     // update the type of selection
105     updateGroupType(LIST_ID());
106
107     GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aCut);
108     aGroup->store(aCompound);
109     setResult(aGroup);
110   }
111 }