Salome HOME
0020511: EDF 1101 SMESH : Add CGNS to Mesh Format Supported
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupOnFilter.cxx
1 // Copyright (C) 2007-2011  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.
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 //  File   : SMESHDS_GroupOnFilter.cxx
24 //  Module : SMESH
25 //
26 #include "SMESHDS_GroupOnFilter.hxx"
27
28 #include "SMESHDS_Mesh.hxx"
29 #include "SMDS_SetIterator.hxx"
30
31 using namespace std;
32
33 //=============================================================================
34 /*!
35  * Creates a group based on thePredicate
36  */
37 //=============================================================================
38
39 SMESHDS_GroupOnFilter::SMESHDS_GroupOnFilter (const int                 theID,
40                                               const SMESHDS_Mesh*       theMesh,
41                                               const SMDSAbs_ElementType theType,
42                                               const SMESH_PredicatePtr& thePredicate)
43   : SMESHDS_GroupBase(theID,theMesh,theType), myMeshModifTime(0)
44 {
45   setChanged();
46   SetPredicate( thePredicate );
47 }
48
49 //================================================================================
50 /*!
51  * \brief Sets a new predicate
52  */
53 //================================================================================
54
55 void SMESHDS_GroupOnFilter::SetPredicate( const SMESH_PredicatePtr& thePredicate)
56 {
57   myPredicate = thePredicate;
58   setChanged();
59   if ( myPredicate )
60     myPredicate->SetMesh( GetMesh() );
61 }
62
63 //================================================================================
64 /*!
65  * \brief Returns nb of elements
66  */
67 //================================================================================
68
69 int SMESHDS_GroupOnFilter::Extent() const
70 {
71   update();
72   return myElements.size();
73 }
74
75 //================================================================================
76 /*!
77  * \brief Checks if the element belongs to the group
78  */
79 //================================================================================
80
81 bool SMESHDS_GroupOnFilter::Contains (const int theID)
82 {
83   return myPredicate ? myPredicate->IsSatisfy( theID ) : false;
84 }
85
86 //================================================================================
87 /*!
88  * \brief Checks if the element belongs to the group
89  */
90 //================================================================================
91
92 bool SMESHDS_GroupOnFilter::Contains (const SMDS_MeshElement* elem)
93 {
94   return myPredicate ? myPredicate->IsSatisfy( elem->GetID() ) : false;
95 }
96
97 //================================================================================
98 /*!
99  * \brief Return iterator on all elements
100  */
101 //================================================================================
102
103 SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const
104 {
105   update();
106   return SMDS_ElemIteratorPtr
107     ( new SMDS_ElementVectorIterator( myElements.begin(), myElements.end() ));
108 }
109
110 //================================================================================
111 /*!
112  * \brief return ID of theIndex-th element
113  *  \param theIndex - index countered from 1
114  *  \retval int - element ID
115  */
116 //================================================================================
117
118 int SMESHDS_GroupOnFilter::GetID (const int theIndex)
119 {
120   update();
121   if ( theIndex < 1 || theIndex > myElements.size() )
122     return -1;
123   return myElements[ theIndex-1 ]->GetID();
124 }
125
126 //================================================================================
127 /*!
128  * \brief Updates myElements if necessary
129  */
130 //================================================================================
131
132 void SMESHDS_GroupOnFilter::update() const
133 {
134   if ( myMeshModifTime < GetMesh()->GetMTime() )
135   {
136     SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
137     me->myElements.clear();
138     if ( myPredicate )
139     {
140       me->myElements.reserve( GetMesh()->GetMeshInfo().NbElements(GetType()));
141       SMDS_ElemIteratorPtr elIt = GetMesh()->elementsIterator(GetType());
142       while ( elIt->more() )
143       {
144         const SMDS_MeshElement* e = elIt->next();
145         if ( myPredicate->IsSatisfy( e->GetID() ))
146           me->myElements.push_back( e );
147       }
148       me->myElements.resize( myElements.size() );
149     }
150     me->setChanged( false );
151   }
152 }
153
154 //================================================================================
155 /*!
156  * \brief Sets myMeshModifTime according to modification state
157  */
158 //================================================================================
159
160 void SMESHDS_GroupOnFilter::setChanged(bool changed)
161 {
162   myMeshModifTime = GetMesh()->GetMTime();
163   if ( changed && myMeshModifTime != 0 )
164     --myMeshModifTime;
165 }