Salome HOME
Merge remote branch 'origin/V7_dev' into V7_dev
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupBase.cxx
1 // Copyright (C) 2007-2016  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_GroupBase.hxx"
29 #include "SMESHDS_Mesh.hxx"
30
31 #include "utilities.h"
32
33 using namespace std;
34
35 Quantity_Color SMESHDS_GroupBase::myDefaultColor = Quantity_Color( 0.0, 0.0, 0.0, Quantity_TOC_RGB );
36
37 //=============================================================================
38 /*!
39  *  
40  */
41 //=============================================================================
42
43 SMESHDS_GroupBase::SMESHDS_GroupBase (const int                 theID,
44                                       const SMESHDS_Mesh*       theMesh,
45                                       const SMDSAbs_ElementType theType):
46        myID(theID), myMesh(theMesh), myType(theType), myStoreName(""),
47        myCurIndex(0), myCurID(-1)
48 {
49   myColor = myDefaultColor;
50 }
51
52 //=============================================================================
53 /*!
54  *  
55  */
56 //=============================================================================
57
58 int SMESHDS_GroupBase::GetID (const int theIndex)
59 {
60   if (myCurIndex < 1 || myCurIndex > theIndex) {
61     myIterator = GetElements();
62     myCurIndex = 0;
63     myCurID = -1;
64   }
65   while (myCurIndex < theIndex && myIterator->more()) {
66     myCurIndex++;
67     myCurID = myIterator->next()->GetID();
68   }
69   return myCurIndex == theIndex ? myCurID : -1;
70 }
71
72 //=============================================================================
73 /*!
74  *  
75  */
76 //=============================================================================
77
78 const SMDS_MeshElement* SMESHDS_GroupBase::findInMesh (const int theID) const
79 {
80   SMDSAbs_ElementType aType = GetType();
81   const SMDS_MeshElement* aElem = NULL;
82   if (aType == SMDSAbs_Node) {
83     aElem = GetMesh()->FindNode(theID);
84   }
85   else if (aType != SMDSAbs_All) {
86     aElem = GetMesh()->FindElement(theID);
87     if (aElem && aType != aElem->GetType())
88       aElem = NULL;
89   }
90   return aElem;
91 }
92
93 //=============================================================================
94 /*!
95  *  Internal method: resets cached iterator, should be called by ancestors
96  *  when they are modified (ex: Add() or Remove() )
97  */
98 //=============================================================================
99 void SMESHDS_GroupBase::resetIterator()
100 {
101   myCurIndex = 0;
102   myCurID = -1;
103 }
104
105 //=======================================================================
106 //function : Extent
107 //purpose  : 
108 //=======================================================================
109
110 int SMESHDS_GroupBase::Extent() const
111 {
112   SMDS_ElemIteratorPtr it = GetElements();
113   int nb = 0;
114   if ( it )
115     for ( ; it->more(); it->next() ) 
116       nb++;
117   return nb;
118 }
119
120 //=======================================================================
121 //function : IsEmpty
122 //purpose  : 
123 //=======================================================================
124
125 bool SMESHDS_GroupBase::IsEmpty()
126 {
127   SMDS_ElemIteratorPtr it = GetElements();
128   return ( !it || !it->more() );
129 }
130
131 //=======================================================================
132 //function : Contains
133 //purpose  : 
134 //=======================================================================
135
136 bool SMESHDS_GroupBase::Contains (const int theID)
137 {
138   if ( SMDS_ElemIteratorPtr it = GetElements() ) {
139     while ( it->more() )
140       if ( it->next()->GetID() == theID )
141         return true;
142   }
143   return false;
144 }
145
146 //=======================================================================
147 //function : Contains
148 //purpose  : 
149 //=======================================================================
150
151 bool SMESHDS_GroupBase::Contains (const SMDS_MeshElement* elem)
152 {
153   if ( elem )
154     return Contains( elem->GetID() );
155   return false;
156 }
157
158 //=======================================================================
159 //function : SetType
160 //purpose  : 
161 //=======================================================================
162
163 void SMESHDS_GroupBase::SetType(SMDSAbs_ElementType theType)
164 {
165   myType = theType;
166 }
167
168 //=======================================================================
169 //function : SetType
170 //purpose  : 
171 //=======================================================================
172
173 void SMESHDS_GroupBase::SetColorGroup(int theColorGroup)
174 {
175   int aRed = ( theColorGroup/1000000 );
176   int aGreen = ( theColorGroup -aRed*1000000)/1000;
177   int aBlue = ( theColorGroup - aRed*1000000 - aGreen*1000 );
178   double aR = aRed/255.0;
179   double aG = aGreen/255.0;
180   double aB = aBlue/255.0;
181   if ( aR < 0. || aR > 1. || // PAL19395
182        aG < 0. || aG > 1. ||
183        aB < 0. || aB > 1. )
184 // #ifdef _DEBUG_
185 //     cout << "SMESHDS_GroupBase::SetColorGroup("<<theColorGroup<<"), invalid color ignored"<<endl;
186 // #endif
187     return;
188   Quantity_Color aColor( aR, aG, aB, Quantity_TOC_RGB );
189   SetColor( aColor );
190 }
191   
192 //=======================================================================
193 //function : SetType
194 //purpose  : 
195 //=======================================================================
196
197 int SMESHDS_GroupBase::GetColorGroup() const
198 {
199   Quantity_Color aColor = GetColor();
200   double aRed = aColor.Red();
201   double aGreen = aColor.Green();
202   double aBlue = aColor.Blue();
203   int aR = int( aRed  *255 );
204   int aG = int( aGreen*255 );
205   int aB = int( aBlue *255 );
206   int aRet = (int)(aR*1000000 + aG*1000 + aB);
207
208   return aRet;
209 }
210