Salome HOME
Fix compilation errors using gcc-5.X relating to explicit stream::operator bool()
[modules/smesh.git] / src / SMESHFiltersSelection / SMESH_TypeFilter.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SMESH_TypeFilter.hxx"
24
25 #include <LightApp_DataOwner.h>
26 #include <SUIT_Session.h>
27 #include <SalomeApp_Application.h>
28 #include <SalomeApp_Study.h>
29
30 #include <SALOMEconfig.h>
31 #include CORBA_CLIENT_HEADER(SMESH_Gen)
32
33 SMESH_TypeFilter::SMESH_TypeFilter (SMESH::MeshObjectType theType)
34 {
35   myType = theType;
36 }
37
38 SMESH_TypeFilter::~SMESH_TypeFilter()
39 {
40 }
41
42 namespace
43 {
44   //================================================================================
45   /*!
46    * \brief Returns true if \a obj is SMESH_IDSource including elements of a given \a type 
47    */
48   //================================================================================
49
50   bool isIDSourceOfType( _PTR(SObject) obj, SMESH::ElementType type )
51   {
52     bool Ok = false;
53     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
54       (SUIT_Session::session()->activeApplication());
55     _PTR(GenericAttribute) anAttr;
56     if ( obj->FindAttribute(anAttr, "AttributeIOR"))
57     {
58       _PTR(AttributeIOR) anIOR = anAttr;
59       std::string aVal = anIOR->Value();
60       if ( aVal.size() > 0 )
61       {
62         CORBA::Object_var corbaObj = app->orb()->string_to_object( aVal.c_str() );
63         SMESH::SMESH_IDSource_var ids = SMESH::SMESH_IDSource::_narrow( corbaObj );
64         if ( ! ids->_is_nil() )
65         {
66           SMESH::array_of_ElementType_var types = ids->GetTypes();
67           for ( int i = 0, nb = types->length(); i < nb && !Ok; ++i )
68             Ok = ( types[i] == type );
69         }
70       }
71     }
72     return Ok;
73   }
74 }
75
76 bool SMESH_TypeFilter::isOk (const SUIT_DataOwner* theDataOwner) const
77 {
78   bool Ok = false, extractReference = true;
79
80   const LightApp_DataOwner* owner =
81     dynamic_cast<const LightApp_DataOwner*>(theDataOwner);
82   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
83     (SUIT_Session::session()->activeApplication()->activeStudy());
84
85   if (owner && appStudy) {
86     _PTR(Study) study = appStudy->studyDS();
87     QString entry = owner->entry();
88
89     _PTR(SObject) obj (study->FindObjectID(entry.toLatin1().data())), aRefSO;
90     if( extractReference && obj && obj->ReferencedObject( aRefSO ) )
91       obj = aRefSO;
92     if (!obj) return false;
93
94     _PTR(SObject) objFather = obj->GetFather();
95     _PTR(SComponent) objComponent = obj->GetFatherComponent();
96
97     if( objComponent->ComponentDataType()!="SMESH" )
98       return false;
99
100     int aLevel = obj->Depth() - objComponent->Depth();
101
102     // Max level under the component is 5:
103     //
104     // 0    Mesh Component
105     // 1    |- Hypotheses
106     // 2    |  |- Regular 1D
107     //      |- Algorithms
108     //      |- Mesh 1
109     //         |- * Main Shape
110     //         |- Applied Hypotheses
111     //         |- Applied Algorithms
112     //         |- Submeshes on Face
113     // 3       |  |- SubmeshFace
114     // 4       |     |- * Face 1
115     // 4       |     |- Applied algorithms ( selectable in Use Case Browser )
116     // 5       |          |- Regular 1D
117     //         |- Group Of Nodes
118     //            |- Group 1
119
120     if (aLevel <= 0)
121       return false;
122
123     switch (myType)
124     {
125       case SMESH::HYPOTHESIS:
126         {
127           if      (aLevel == 2 && (objFather->Tag() == SMESH::Tag_HypothesisRoot))
128             // hypo definition
129             Ok = true;
130           else if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_RefOnAppliedHypothesis))
131             // applied global hypo
132             Ok = true;
133           else if (aLevel == 5 && (objFather->Tag() == SMESH::Tag_RefOnAppliedHypothesis))
134             // applied local hypo
135             Ok = true;
136           break;
137         }
138       case SMESH::ALGORITHM:
139         {
140           if      (aLevel == 2 && (objFather->Tag() == SMESH::Tag_AlgorithmsRoot))
141             // algo definition
142             Ok = true;
143           else if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_RefOnAppliedAlgorithms))
144             // applied global algo
145             Ok = true;
146           else if (aLevel == 5 && (objFather->Tag() == SMESH::Tag_RefOnAppliedAlgorithms))
147             // applied local algo
148             Ok = true;
149           break;
150         }
151       case SMESH::MESH:
152         {
153           if (aLevel == 1 && (obj->Tag() >= SMESH::Tag_FirstMeshRoot))
154             Ok = true;
155           break;
156         }
157       case SMESH::SUBMESH:
158         {
159           // see SMESH_Gen_i.cxx for tag numbers
160           if (aLevel == 3 && (objFather->Tag() >= SMESH::Tag_FirstSubMesh &&
161                               objFather->Tag() <= SMESH::Tag_LastSubMesh))
162             Ok = true;
163           break;
164         }
165       case SMESH::MESHorSUBMESH:
166         {
167           if (aLevel == 1 && (obj->Tag() >= SMESH::Tag_FirstMeshRoot))
168             Ok = true; // mesh
169           else if (aLevel == 3 && (objFather->Tag() >= SMESH::Tag_FirstSubMesh &&
170                                    objFather->Tag() <= SMESH::Tag_LastSubMesh))
171             Ok = true;
172           break;
173         }
174       case SMESH::SUBMESH_VERTEX: // Label "SubMeshes on vertexes"
175         {
176           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_SubMeshOnVertex))
177             Ok = true;
178           break;
179         }
180       case SMESH::SUBMESH_EDGE:
181         {
182           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_SubMeshOnEdge))
183             Ok = true;
184           break;
185         }
186       case SMESH::SUBMESH_FACE:
187         {
188           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_SubMeshOnFace))
189             Ok = true;
190           break;
191         }
192       case SMESH::SUBMESH_SOLID:
193         {
194           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_SubMeshOnSolid))
195             Ok = true;
196           break;
197         }
198       case SMESH::SUBMESH_COMPOUND:
199         {
200           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_SubMeshOnCompound))
201             Ok = true;
202           break;
203         }
204       case SMESH::GROUP:
205         {
206           if (aLevel == 3 && (objFather->Tag() >= SMESH::Tag_FirstGroup))
207             Ok = true;
208           break;
209         }
210       case SMESH::GROUP_NODE:
211         {
212           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_NodeGroups))
213             Ok = true;
214           break;
215         }
216       case SMESH::GROUP_EDGE:
217         {
218           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_EdgeGroups))
219             Ok = true;
220           break;
221         }
222       case SMESH::GROUP_FACE:
223         {
224           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_FaceGroups))
225             Ok = true;
226           break;
227         }
228       case SMESH::GROUP_VOLUME:
229         {
230           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_VolumeGroups))
231             Ok = true;
232           break;
233         }
234       case SMESH::GROUP_0D:
235         {
236           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_0DElementsGroups))
237             Ok = true;
238           break;
239         }
240       case SMESH::GROUP_BALL:
241         {
242           if (aLevel == 3 && (objFather->Tag() == SMESH::Tag_BallElementsGroups))
243             Ok = true;
244           break;
245         }
246       case SMESH::IDSOURCE:
247         {
248           Ok = ( SMESH_TypeFilter(SMESH::MESHorSUBMESH).isOk( theDataOwner ) ||
249                  SMESH_TypeFilter(SMESH::GROUP)        .isOk( theDataOwner ));
250           break;
251         }
252       case SMESH::IDSOURCE_EDGE:
253         {
254           Ok = isIDSourceOfType( obj, SMESH::EDGE );
255           break;
256         }
257       case SMESH::IDSOURCE_FACE:
258         {
259           Ok = isIDSourceOfType( obj, SMESH::FACE );
260           break;
261         }
262       case SMESH::IDSOURCE_VOLUME:
263         {
264           Ok = isIDSourceOfType( obj, SMESH::VOLUME );
265           break;
266         }
267     default:;
268     }
269   }
270   return Ok;
271 }
272
273 SMESH::MeshObjectType SMESH_TypeFilter::type() const
274 {
275   return myType;
276 }