Salome HOME
Movement of examples to CVS EXAMPLES SAMPLES_SRC.
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupBase.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_GroupBase.hxx"
28 #include "SMESHDS_Mesh.hxx"
29
30 #include "utilities.h"
31
32 using namespace std;
33
34 //=============================================================================
35 /*!
36  *  
37  */
38 //=============================================================================
39
40 SMESHDS_GroupBase::SMESHDS_GroupBase (const int                 theID,
41                                       const SMESHDS_Mesh*       theMesh,
42                                       const SMDSAbs_ElementType theType):
43        myID(theID), myMesh(theMesh), myType(theType), myStoreName(""),
44        myCurIndex(0), myCurID(-1)
45 {
46 }
47
48 //=============================================================================
49 /*!
50  *  
51  */
52 //=============================================================================
53
54 int SMESHDS_GroupBase::GetID (const int theIndex)
55 {
56   if (myCurIndex < 1 || myCurIndex > theIndex) {
57     myIterator = GetElements();
58     myCurIndex = 0;
59     myCurID = -1;
60   }
61   while (myCurIndex < theIndex && myIterator->more()) {
62     myCurIndex++;
63     myCurID = myIterator->next()->GetID();
64   }
65   return myCurIndex == theIndex ? myCurID : -1;
66 }
67
68 //=============================================================================
69 /*!
70  *  
71  */
72 //=============================================================================
73
74 const SMDS_MeshElement* SMESHDS_GroupBase::findInMesh (const int theID) const
75 {
76   SMDSAbs_ElementType aType = GetType();
77   const SMDS_MeshElement* aElem = NULL;
78   if (aType == SMDSAbs_Node) {
79     aElem = GetMesh()->FindNode(theID);
80   }
81   else if (aType != SMDSAbs_All) {
82     aElem = GetMesh()->FindElement(theID);
83     if (aElem && aType != aElem->GetType())
84       aElem = NULL;
85   }
86   return aElem;
87 }
88
89 //=============================================================================
90 /*!
91  *  Internal method: resets cached iterator, should be called by ancestors
92  *  when they are modified (ex: Add() or Remove() )
93  */
94 //=============================================================================
95 void SMESHDS_GroupBase::resetIterator()
96 {
97   myCurIndex = 0;
98   myCurID = -1;
99 }
100
101 //=======================================================================
102 //function : Extent
103 //purpose  : 
104 //=======================================================================
105
106 int SMESHDS_GroupBase::Extent()
107 {
108   SMDS_ElemIteratorPtr it = GetElements();
109   int nb = 0;
110   if ( it )
111     for ( ; it->more(); it->next() ) 
112       nb++;
113   return nb;
114 }
115
116 //=======================================================================
117 //function : IsEmpty
118 //purpose  : 
119 //=======================================================================
120
121 bool SMESHDS_GroupBase::IsEmpty()
122 {
123   SMDS_ElemIteratorPtr it = GetElements();
124   return ( !it || !it->more() );
125 }
126
127 //=======================================================================
128 //function : Contains
129 //purpose  : 
130 //=======================================================================
131
132 bool SMESHDS_GroupBase::Contains (const int theID)
133 {
134   SMDS_ElemIteratorPtr it = GetElements();
135   bool contains = false;
136   if ( it )
137     while ( !contains && it->more() )
138       contains = ( it->next()->GetID() == theID );
139   return contains;
140 }
141
142 //=======================================================================
143 //function : SetType
144 //purpose  : 
145 //=======================================================================
146
147 void SMESHDS_GroupBase::SetType(SMDSAbs_ElementType theType)
148 {
149   myType = theType;
150 }