Salome HOME
ecd6efb8ce998c2fc5c2048e21bb56e7bfd1a2d5
[modules/smesh.git] / src / SMESHDS / SMESHDS_Group.cxx
1 //  SMESH SMESHDS : idl implementation based on 'SMESH' unit's classes
2 //
3 //  Copyright (C) 2004  CEA
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org 
20 //
21 //
22 //
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 /*!
82  *  
83  */
84 //=============================================================================
85
86 bool SMESHDS_Group::Add (const int theID)
87 {
88   const SMDS_MeshElement* aElem = findInMesh (theID);
89   if (!aElem || myGroup.Contains(aElem))
90     return false;
91
92   if (myGroup.IsEmpty())
93     SetType( aElem->GetType() );
94
95   myGroup.Add (aElem);
96   return true;
97 }
98
99 //=============================================================================
100 /*!
101  *  
102  */
103 //=============================================================================
104
105 bool SMESHDS_Group::Remove (const int theID)
106 {
107   const SMDS_MeshElement* aElem = findInMesh (theID);
108   if (!aElem || !myGroup.Contains(aElem))
109     return false;
110   myGroup.Remove (aElem);
111   return true;
112 }
113
114 //=======================================================================
115 //function : Clear
116 //purpose  : 
117 //=======================================================================
118
119 void SMESHDS_Group::Clear()
120 {
121   myGroup.Clear();
122 }
123
124 // =====================
125 // class MyGroupIterator
126 // =====================
127
128 class MyGroupIterator: public SMDS_ElemIterator
129 {
130   const SMDS_MeshGroup& myGroup;
131  public:
132   MyGroupIterator(const SMDS_MeshGroup& group): myGroup(group) { myGroup.InitIterator(); }
133   bool more() { return myGroup.More(); }
134   const SMDS_MeshElement* next() { return myGroup.Next(); }
135 };
136    
137 //=======================================================================
138 //function : GetElements
139 //purpose  : 
140 //=======================================================================
141
142 SMDS_ElemIteratorPtr SMESHDS_Group::GetElements()
143 {
144   return SMDS_ElemIteratorPtr( new MyGroupIterator ( myGroup ));
145 }
146
147 //=======================================================================
148 //function : SetType
149 //purpose  : 
150 //=======================================================================
151
152 void SMESHDS_Group::SetType(SMDSAbs_ElementType theType)
153 {
154   if ( myGroup.IsEmpty() || GetType() == SMDSAbs_All ) {
155     SMESHDS_GroupBase::SetType( theType );
156     myGroup.SetType ( theType );
157   }
158   else
159     SMESHDS_GroupBase::SetType( myGroup.GetType() );
160 }
161