Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHDS / SMESHDS_Group.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESHDS : idl implementation based on 'SMESH' unit's classes
23 //  File   : SMESHDS_Group.cxx
24 //  Module : SMESH
25 //  $Header$
26 //
27 #include "SMESHDS_Group.hxx"
28 #include "SMESHDS_Mesh.hxx"
29
30 using namespace std;
31
32 //=============================================================================
33 /*!
34  *  
35  */
36 //=============================================================================
37
38 SMESHDS_Group::SMESHDS_Group (const int                 theID,
39                               const SMESHDS_Mesh*       theMesh,
40                               const SMDSAbs_ElementType theType)
41      : SMESHDS_GroupBase(theID,theMesh,theType),
42        myGroup(theMesh,theType)
43 {
44 }
45
46 //=======================================================================
47 //function : Extent
48 //purpose  : 
49 //=======================================================================
50
51 int SMESHDS_Group::Extent()
52 {
53   return myGroup.Extent();
54 }
55
56 //=======================================================================
57 //function : IsEmpty
58 //purpose  : 
59 //=======================================================================
60
61 bool SMESHDS_Group::IsEmpty()
62 {
63   return myGroup.IsEmpty();
64 }
65
66 //=============================================================================
67 /*!
68  *  
69  */
70 //=============================================================================
71
72 bool SMESHDS_Group::Contains (const int theID)
73 {
74   const SMDS_MeshElement* aElem = findInMesh (theID);
75   if (aElem)
76     return myGroup.Contains(aElem);
77   return false;
78 }
79
80 //=======================================================================
81 //function : Contains
82 //purpose  : 
83 //=======================================================================
84
85 bool SMESHDS_Group::Contains (const SMDS_MeshElement* elem)
86 {
87   if (elem)
88     return myGroup.Contains(elem);
89   return false;
90 }
91
92 //=============================================================================
93 /*!
94  *  
95  */
96 //=============================================================================
97
98 bool SMESHDS_Group::Add (const int theID)
99 {
100   const SMDS_MeshElement* aElem = findInMesh (theID);
101   if (!aElem || myGroup.Contains(aElem))
102     return false;
103
104   if (myGroup.IsEmpty())
105     SetType( aElem->GetType() );
106
107   myGroup.Add (aElem);
108   resetIterator();
109   return true;
110 }
111
112 //=============================================================================
113 /*!
114  *  
115  */
116 //=============================================================================
117
118 bool SMESHDS_Group::Remove (const int theID)
119 {
120   const SMDS_MeshElement* aElem = findInMesh (theID);
121   if (!aElem || !myGroup.Contains(aElem))
122     return false;
123   myGroup.Remove (aElem);
124   resetIterator();
125   return true;
126 }
127
128
129 //======================================================================
130 //function : Clear
131 //purpose  : 
132 //=======================================================================
133
134 void SMESHDS_Group::Clear()
135 {
136   myGroup.Clear();
137   resetIterator();
138 }
139
140 // =====================
141 // class MyGroupIterator
142 // =====================
143
144 class MyGroupIterator: public SMDS_ElemIterator
145 {
146   const SMDS_MeshGroup& myGroup;
147  public:
148   MyGroupIterator(const SMDS_MeshGroup& group): myGroup(group) { myGroup.InitIterator(); }
149   bool more() { return myGroup.More(); }
150   const SMDS_MeshElement* next() { return myGroup.Next(); }
151 };
152    
153 //=======================================================================
154 //function : GetElements
155 //purpose  : 
156 //=======================================================================
157
158 SMDS_ElemIteratorPtr SMESHDS_Group::GetElements()
159 {
160   return SMDS_ElemIteratorPtr( new MyGroupIterator ( myGroup ));
161 }
162
163 //=======================================================================
164 //function : SetType
165 //purpose  : 
166 //=======================================================================
167
168 void SMESHDS_Group::SetType(SMDSAbs_ElementType theType)
169 {
170   if ( myGroup.IsEmpty() || GetType() == SMDSAbs_All ) {
171     SMESHDS_GroupBase::SetType( theType );
172     myGroup.SetType ( theType );
173   }
174   else
175     SMESHDS_GroupBase::SetType( myGroup.GetType() );
176 }
177