Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[modules/smesh.git] / src / SMESH_I / SMESH_Group_i.cxx
1 //  SMESH SMESH_I : 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   : SMESH_Group_i.cxx
24 //  Author : Sergey ANIKIN, OCC
25 //  Module : SMESH
26 //  $Header$
27
28 #include "SMESH_Group_i.hxx"
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include <SMESH_Group.hxx>
32 #include <SMESHDS_Group.hxx>
33 #include <SMDSAbs_ElementType.hxx>
34 #include <utilities.h>
35
36 //=============================================================================
37 /*!
38  *  
39  */
40 //=============================================================================
41
42 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
43 : SALOME::GenericObj_i( thePOA ),
44   myMeshServant( theMeshServant ), 
45   myLocalID( theLocalID )
46 {
47   thePOA->activate_object( this );
48 }
49
50
51 //=============================================================================
52 /*!
53  *  
54  */
55 //=============================================================================
56
57 SMESH_Group_i::~SMESH_Group_i()
58 {
59   MESSAGE("~SMESH_Group_i;" );
60   if ( myMeshServant )
61     myMeshServant->removeGroup(myLocalID);
62 }
63
64
65 //=============================================================================
66 /*!
67  *  
68  */
69 //=============================================================================
70
71 void SMESH_Group_i::SetName( const char* theName )
72 {
73   if ( myMeshServant ) {
74     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
75     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
76     if (aGroup) {
77       aGroup->SetName(theName);
78
79       // Update group name in a study
80       SALOMEDS::Study_var aStudy = myMeshServant->GetGen()->GetCurrentStudy();
81       if ( !aStudy->_is_nil() ) {
82         SALOMEDS::SObject_var aGroupSO = aStudy->FindObjectIOR( SMESH_Gen_i::GetORB()->object_to_string( _this() ) );
83         if ( !aGroupSO->_is_nil() ) {
84           SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
85           aBuilder->SetName( aGroupSO, theName );
86         }
87       }
88       return;
89     }
90   }
91   MESSAGE("can't set name of a vague group");
92 }
93
94
95 //=============================================================================
96 /*!
97  *  
98  */
99 //=============================================================================
100
101 char* SMESH_Group_i::GetName()
102 {
103   if ( myMeshServant ) {
104     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
105     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
106     if (aGroup)
107       return CORBA::string_dup (aGroup->GetName());
108   }
109   MESSAGE("get name of a vague group");
110   return CORBA::string_dup( "NO_NAME" );
111 }
112
113
114 //=============================================================================
115 /*!
116  *  
117  */
118 //=============================================================================
119
120 SMESH::ElementType SMESH_Group_i::GetType()
121 {
122   if ( myMeshServant ) {
123     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
124     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
125     if (aGroup) {
126       SMDSAbs_ElementType aSMDSType = aGroup->GetGroupDS()->GetType();
127       SMESH::ElementType aType;
128       switch (aSMDSType) {
129       case SMDSAbs_Node:   aType = SMESH::NODE; break;
130       case SMDSAbs_Edge:   aType = SMESH::EDGE; break;
131       case SMDSAbs_Face:   aType = SMESH::FACE; break;
132       case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
133       default:             aType = SMESH::ALL; break;
134       }
135       return aType;
136     }
137   }
138   MESSAGE("get type of a vague group");
139   return SMESH::ALL;
140 }
141
142
143 //=============================================================================
144 /*!
145  *  
146  */
147 //=============================================================================
148
149 CORBA::Long SMESH_Group_i::Size()
150 {
151   if ( myMeshServant ) {
152     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
153     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
154     if (aGroup) {
155       int aSize = aGroup->GetGroupDS()->Extent();
156       return aSize;
157     }
158   }
159   MESSAGE("get size of a vague group");
160   return 0;
161 }
162
163
164 //=============================================================================
165 /*!
166  *  
167  */
168 //=============================================================================
169
170 CORBA::Boolean SMESH_Group_i::IsEmpty()
171 {
172   if ( myMeshServant ) {
173     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
174     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
175     if (aGroup) {
176       bool isEmpty = aGroup->GetGroupDS()->IsEmpty();
177       return isEmpty;
178     }
179   }
180   MESSAGE("checking IsEmpty of a vague group");
181   return true;
182 }
183
184
185 //=============================================================================
186 /*!
187  *  
188  */
189 //=============================================================================
190
191 void SMESH_Group_i::Clear()
192 {
193   if ( myMeshServant ) {
194     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
195     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
196     if (aGroup) {
197       // a SMDS group forgets its type after clearing, so we must re-set it
198       SMDSAbs_ElementType aSMDSType = aGroup->GetGroupDS()->GetType();
199       aGroup->GetGroupDS()->Clear();
200       aGroup->GetGroupDS()->SetType(aSMDSType);
201       return;
202     }
203   }
204   MESSAGE("attempt to clear a vague group");
205 }
206
207
208 //=============================================================================
209 /*!
210  *  
211  */
212 //=============================================================================
213
214 CORBA::Boolean SMESH_Group_i::Contains( CORBA::Long theID )
215 {
216   if ( myMeshServant ) {
217     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
218     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
219     if (aGroup) {
220       bool res = aGroup->GetGroupDS()->Contains(theID);
221       return res;
222     }
223   }
224   MESSAGE("attempt to check contents of a vague group");
225   return false;
226 }
227
228
229 //=============================================================================
230 /*!
231  *  
232  */
233 //=============================================================================
234
235 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
236 {
237   if ( myMeshServant ) {
238     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
239     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
240     if (aGroup) {
241       SMESHDS_Group* aGroupDS = aGroup->GetGroupDS();
242       int nbAdd = 0;
243       for (int i = 0; i < theIDs.length(); i++) {
244         int anID = (int) theIDs[i];
245         if (aGroupDS->Add(anID))
246           nbAdd++;
247       }
248       return nbAdd;
249     }
250   }
251   MESSAGE("attempt to add elements to a vague group");
252   return 0;
253 }
254
255
256 //=============================================================================
257 /*!
258  *  
259  */
260 //=============================================================================
261
262 CORBA::Long SMESH_Group_i::GetID( CORBA::Long theIndex )
263 {
264   if ( myMeshServant ) {
265     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
266     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
267     if (aGroup) {
268       int anID = aGroup->GetGroupDS()->GetID(theIndex);
269       return anID;
270     }
271   }
272   MESSAGE("attempt to iterate on a vague group");
273   return -1;
274 }
275
276
277 //=============================================================================
278 /*!
279  *  
280  */
281 //=============================================================================
282
283 SMESH::long_array* SMESH_Group_i::GetListOfID()
284 {
285   SMESH::long_array_var aRes = new SMESH::long_array();
286   if ( myMeshServant ) {
287     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
288     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
289     if (aGroup) {
290       SMESHDS_Group* aGroupDS = aGroup->GetGroupDS();
291       int aSize = aGroupDS->Extent();
292       aRes->length(aSize);
293       for (int i = 0; i < aSize; i++)
294         aRes[i] = aGroupDS->GetID(i+1);
295       return aRes._retn();
296     }
297   }
298   MESSAGE("get list of IDs of a vague group");
299   return aRes._retn();
300 }
301
302
303 //=============================================================================
304 /*!
305  *  
306  */
307 //=============================================================================
308
309 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
310 {
311   if ( myMeshServant ) {
312     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
313     ::SMESH_Group* aGroup = aMesh.GetGroup(myLocalID);
314     if (aGroup) {
315       // a SMDS group forgets its type after clearing, so we must re-set it
316       // if the group becomes empty
317       SMDSAbs_ElementType aSMDSType = aGroup->GetGroupDS()->GetType();
318       SMESHDS_Group* aGroupDS = aGroup->GetGroupDS();
319       int nbDel = 0;
320       for (int i = 0; i < theIDs.length(); i++) {
321         int anID = (int) theIDs[i];
322         if (aGroupDS->Remove(anID))
323           nbDel++;
324       }
325       if (aGroupDS->IsEmpty())
326         aGroupDS->SetType(aSMDSType);
327       return nbDel;
328     }
329   }
330   MESSAGE("attempt to remove elements from a vague group");
331   return 0;
332 }
333
334
335 //=============================================================================
336 /*!
337  *  
338  */
339 //=============================================================================
340 SMESH::SMESH_Mesh_ptr SMESH_Group_i::GetMesh()
341 {
342   MESSAGE("SMESH_Group_i::GetMesh(): mesh servant = " << myMeshServant );
343   SMESH::SMESH_Mesh_var aMesh;
344   if ( myMeshServant )
345     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
346   return aMesh._retn();
347 }
348
349
350 //=============================================================================
351 /*!
352  *  
353  */
354 //=============================================================================
355 int SMESH_Group_i::GetLocalID()
356 {
357   return myLocalID;
358 }