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