Salome HOME
5221743a9a6e3087c2ce4c3cd49af071ac2dff53
[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.com
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   myColor = Quantity_Color( 0.0, 0.0, 0.0, Quantity_TOC_RGB );
47 }
48
49 //=============================================================================
50 /*!
51  *  
52  */
53 //=============================================================================
54
55 int SMESHDS_GroupBase::GetID (const int theIndex)
56 {
57   if (myCurIndex < 1 || myCurIndex > theIndex) {
58     myIterator = GetElements();
59     myCurIndex = 0;
60     myCurID = -1;
61   }
62   while (myCurIndex < theIndex && myIterator->more()) {
63     myCurIndex++;
64     myCurID = myIterator->next()->GetID();
65   }
66   return myCurIndex == theIndex ? myCurID : -1;
67 }
68
69 //=============================================================================
70 /*!
71  *  
72  */
73 //=============================================================================
74
75 const SMDS_MeshElement* SMESHDS_GroupBase::findInMesh (const int theID) const
76 {
77   SMDSAbs_ElementType aType = GetType();
78   const SMDS_MeshElement* aElem = NULL;
79   if (aType == SMDSAbs_Node) {
80     aElem = GetMesh()->FindNode(theID);
81   }
82   else if (aType != SMDSAbs_All) {
83     aElem = GetMesh()->FindElement(theID);
84     if (aElem && aType != aElem->GetType())
85       aElem = NULL;
86   }
87   return aElem;
88 }
89
90 //=============================================================================
91 /*!
92  *  Internal method: resets cached iterator, should be called by ancestors
93  *  when they are modified (ex: Add() or Remove() )
94  */
95 //=============================================================================
96 void SMESHDS_GroupBase::resetIterator()
97 {
98   myCurIndex = 0;
99   myCurID = -1;
100 }
101
102 //=======================================================================
103 //function : Extent
104 //purpose  : 
105 //=======================================================================
106
107 int SMESHDS_GroupBase::Extent()
108 {
109   SMDS_ElemIteratorPtr it = GetElements();
110   int nb = 0;
111   if ( it )
112     for ( ; it->more(); it->next() ) 
113       nb++;
114   return nb;
115 }
116
117 //=======================================================================
118 //function : IsEmpty
119 //purpose  : 
120 //=======================================================================
121
122 bool SMESHDS_GroupBase::IsEmpty()
123 {
124   SMDS_ElemIteratorPtr it = GetElements();
125   return ( !it || !it->more() );
126 }
127
128 //=======================================================================
129 //function : Contains
130 //purpose  : 
131 //=======================================================================
132
133 bool SMESHDS_GroupBase::Contains (const int theID)
134 {
135   SMDS_ElemIteratorPtr it = GetElements();
136   bool contains = false;
137   if ( it )
138     while ( !contains && it->more() )
139       contains = ( it->next()->GetID() == theID );
140   return contains;
141 }
142
143 //=======================================================================
144 //function : SetType
145 //purpose  : 
146 //=======================================================================
147
148 void SMESHDS_GroupBase::SetType(SMDSAbs_ElementType theType)
149 {
150   myType = theType;
151 }
152
153 //=======================================================================
154 //function : SetType
155 //purpose  : 
156 //=======================================================================
157
158 void SMESHDS_GroupBase::SetColorGroup(int theColorGroup)
159 {
160   if( theColorGroup < 0 || theColorGroup > 360 )
161   {
162     MESSAGE("SMESHDS_GroupBase::SetColorGroup : Value must be in range [0,360]");
163     return;
164   }
165
166   Quantity_Color aColor( (double)theColorGroup, 1.0, 1.0, Quantity_TOC_HLS );
167   SetColor( aColor );
168 }
169   
170 //=======================================================================
171 //function : SetType
172 //purpose  : 
173 //=======================================================================
174
175 int SMESHDS_GroupBase::GetColorGroup() const
176 {
177   Quantity_Color aColor = GetColor();
178   double aHue = aColor.Hue();
179   if( aHue < 0 )
180     return 0;
181   return (int)( aHue );
182 }
183