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 <Selector_Container.h>
22 #include <Selector_NameGenerator.h>
23 #include <Selector_NExplode.h>
25 #include <TNaming_NamedShape.hxx>
26 #include <TDataStd_Name.hxx>
27 #include <TDataStd_Integer.hxx>
28 #include <TDF_ChildIterator.hxx>
29 #include <TopTools_MapOfShape.hxx>
30 #include <TopExp_Explorer.hxx>
31 #include <BRep_Tool.hxx>
33 #include <TopoDS_Builder.hxx>
34 #include <TopoDS_Compound.hxx>
36 Selector_Container::Selector_Container() : Selector_AlgoWithSubs()
39 bool Selector_Container::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue)
41 myShapeType = theValue.ShapeType();
43 // iterate all sub-shapes and select them on sublabels
44 for(TopoDS_Iterator aSubIter(theValue); aSubIter.More(); aSubIter.Next()) {
45 Selector_Algo* aSubAlgo = Selector_Algo::select(theContext, aSubIter.Value(),
46 newSubLabel(), baseDocument(),
47 false, useNeighbors(), useIntersections()); //for subs no geometrical naming allowed
48 if (!append(aSubAlgo))
54 void Selector_Container::store()
56 storeType(Selector_Algo::SELTYPE_CONTAINER);
57 // store all sub-selectors
58 TDataStd_Integer::Set(label(), shapeTypeID(), (int)myShapeType);
59 std::list<Selector_Algo*>::const_iterator aSubSel = list().cbegin();
60 for(; aSubSel != list().cend(); aSubSel++) {
63 TDataStd_Integer::Set(label(), shapeTypeID(), (int)myShapeType);
66 bool Selector_Container::restore()
68 Handle(TDataStd_Integer) aShapeTypeAttr;
69 if (!label().FindAttribute(shapeTypeID(), aShapeTypeAttr))
71 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
72 // restore sub-selectors
73 bool aSubResult = true;
74 for(TDF_ChildIterator aSub(label(), false); aSub.More(); aSub.Next()) {
75 Selector_Algo* aSubSel = restoreByLab(aSub.Value(), baseDocument());
76 if (!append(aSubSel, false)) {
77 break; // some empty label left in the end
83 TDF_Label Selector_Container::restoreByName(std::string theName,
84 const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
86 myShapeType = theShapeType;
88 for(size_t aStart = 0; aStart != std::string::npos; aStart = theName.find('[', aStart + 1)) {
89 size_t anEndPos = theName.find(']', aStart + 1);
90 if (anEndPos != std::string::npos) {
91 // there could be sub-intersections, so, [[...]] case; searching for other open-bracket
92 size_t aNextStart = theName.find('[', aStart + 1);
93 while(aNextStart != std::string::npos && aNextStart < anEndPos) {
94 anEndPos = theName.find(']', anEndPos + 1);
95 if (anEndPos == std::string::npos) {
96 return TDF_Label(); // invalid parentheses
98 aNextStart = theName.find('[', aNextStart + 1);
100 if (anEndPos == std::string::npos)
101 return TDF_Label(); // invalid parentheses
102 std::string aSubStr = theName.substr(aStart + 1, anEndPos - aStart - 1);
103 TopAbs_ShapeEnum aSubShapeType = TopAbs_FACE;
104 switch (myShapeType) {
105 case TopAbs_COMPSOLID: aSubShapeType = TopAbs_SOLID; break;
106 case TopAbs_WIRE: aSubShapeType = TopAbs_EDGE; break;
109 TDF_Label aSubContext;
110 Selector_Algo* aSubSel =
111 Selector_Algo::restoreByName(newSubLabel(), baseDocument(), aSubStr, aSubShapeType,
112 geometricalNaming(), theNameGenerator, aSubContext);
113 if (!append(aSubSel))
116 if (aSubContext.IsNull()) {
121 if (!aContext.IsNull() && !aContext.IsEqual(aSubContext)) {
122 if (!theNameGenerator->isLater(aContext, aSubContext))
123 aContext = aSubContext;
125 aContext = aSubContext;
128 return TDF_Label(); // invalid parentheses
129 aStart = anEndPos; // for recursive parenthesis set start on the current end
134 bool Selector_Container::solve(const TopoDS_Shape& theContext)
136 TopoDS_Shape aResult;
138 TopoDS_Builder aBuilder;
139 switch(myShapeType) {
140 case TopAbs_COMPOUND: {
141 TopoDS_Compound aComp;
142 aBuilder.MakeCompound(aComp);
146 case TopAbs_COMPSOLID: {
147 TopoDS_CompSolid aComp;
148 aBuilder.MakeCompSolid(aComp);
154 aBuilder.MakeShell(aShell);
160 aBuilder.MakeWire(aWire);
165 TopoDS_ListOfShape aSubSelectorShapes;
166 std::list<Selector_Algo*>::const_iterator aSubSel = list().cbegin();
167 for(; aSubSel != list().cend(); aSubSel++) {
168 if (!(*aSubSel)->solve(theContext)) {
171 aBuilder.Add(aResult, (*aSubSel)->value());
173 if (!aResult.IsNull()) {
174 Selector_Algo::store(aResult);
180 std::string Selector_Container::name(Selector_NameGenerator* theNameGenerator)
183 // add names of sub-components one by one in "[]"
184 std::list<Selector_Algo*>::const_iterator aSubSel = list().cbegin();
185 for(; aSubSel != list().cend(); aSubSel++) {
187 aResult += (*aSubSel)->name(theNameGenerator);