Salome HOME
Merge branch 'master' into pre/penta18
[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  *  Don't use it!
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   if ( myMesh->GetMeshInfo().NbElements( myType ) == 0 )
128     // avoid long iteration over sub-meshes of a complex sub-mesh of a group on geometry
129     return false;
130   SMDS_ElemIteratorPtr it = GetElements();
131   return ( !it || !it->more() );
132 }
133
134 //=======================================================================
135 //function : Contains
136 //purpose  : 
137 //=======================================================================
138
139 bool SMESHDS_GroupBase::Contains (const int theID)
140 {
141   if ( SMDS_ElemIteratorPtr it = GetElements() ) {
142     while ( it->more() )
143       if ( it->next()->GetID() == theID )
144         return true;
145   }
146   return false;
147 }
148
149 //=======================================================================
150 //function : Contains
151 //purpose  : 
152 //=======================================================================
153
154 bool SMESHDS_GroupBase::Contains (const SMDS_MeshElement* elem)
155 {
156   if ( elem )
157     return Contains( elem->GetID() );
158   return false;
159 }
160
161 //=======================================================================
162 //function : SetType
163 //purpose  : 
164 //=======================================================================
165
166 void SMESHDS_GroupBase::SetType(SMDSAbs_ElementType theType)
167 {
168   myType = theType;
169 }
170
171 //=======================================================================
172 //function : SetType
173 //purpose  : 
174 //=======================================================================
175
176 void SMESHDS_GroupBase::SetColorGroup(int theColorGroup)
177 {
178   int aRed = ( theColorGroup/1000000 );
179   int aGreen = ( theColorGroup -aRed*1000000)/1000;
180   int aBlue = ( theColorGroup - aRed*1000000 - aGreen*1000 );
181   double aR = aRed/255.0;
182   double aG = aGreen/255.0;
183   double aB = aBlue/255.0;
184   if ( aR < 0. || aR > 1. || // PAL19395
185        aG < 0. || aG > 1. ||
186        aB < 0. || aB > 1. )
187 // #ifdef _DEBUG_
188 //     cout << "SMESHDS_GroupBase::SetColorGroup("<<theColorGroup<<"), invalid color ignored"<<endl;
189 // #endif
190     return;
191   Quantity_Color aColor( aR, aG, aB, Quantity_TOC_RGB );
192   SetColor( aColor );
193 }
194   
195 //=======================================================================
196 //function : SetType
197 //purpose  : 
198 //=======================================================================
199
200 int SMESHDS_GroupBase::GetColorGroup() const
201 {
202   Quantity_Color aColor = GetColor();
203   double aRed = aColor.Red();
204   double aGreen = aColor.Green();
205   double aBlue = aColor.Blue();
206   int aR = int( aRed  *255 );
207   int aG = int( aGreen*255 );
208   int aB = int( aBlue *255 );
209   int aRet = (int)(aR*1000000 + aG*1000 + aB);
210
211   return aRet;
212 }
213