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