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