Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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
37 #include "SMESH_Filter_i.hxx"
38 #include "SMESH_PythonDump.hxx"
39
40 #include "utilities.h"
41
42 using namespace SMESH;
43
44 //=============================================================================
45 /*!
46  *  
47  */
48 //=============================================================================
49
50 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
51 : SALOME::GenericObj_i( thePOA ),
52   myMeshServant( theMeshServant ), 
53   myLocalID( theLocalID )
54 {
55   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
56   // servant activation is performed by SMESH_Mesh_i::createGroup()
57   // thePOA->activate_object( this );
58 }
59
60 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
61      : SALOME::GenericObj_i( thePOA ),
62        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
63 {
64   MESSAGE("SMESH_Group_i; this = "<<this );
65 }
66
67 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
68      : SALOME::GenericObj_i( thePOA ),
69        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
70 {
71   MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
72 }
73
74 //=============================================================================
75 /*!
76  *  
77  */
78 //=============================================================================
79
80 SMESH_GroupBase_i::~SMESH_GroupBase_i()
81 {
82   MESSAGE("~SMESH_GroupBase_i; this = "<<this );
83   if ( myMeshServant )
84     myMeshServant->removeGroup(myLocalID);
85 }
86
87 //=======================================================================
88 //function : GetSmeshGroup
89 //purpose  : 
90 //=======================================================================
91
92 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
93 {
94   if ( myMeshServant ) {
95     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
96     return aMesh.GetGroup(myLocalID);
97   }
98   return 0;
99 }
100
101 //=======================================================================
102 //function : GetGroupDS
103 //purpose  : 
104 //=======================================================================
105
106 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
107 {
108   ::SMESH_Group* aGroup = GetSmeshGroup();
109   if ( aGroup )
110     return aGroup->GetGroupDS();
111   return 0;
112 }
113
114 //=============================================================================
115 /*!
116  *  
117  */
118 //=============================================================================
119
120 void SMESH_GroupBase_i::SetName( const char* theName )
121 {
122   // Update Python script
123   TCollection_AsciiString aStr, aStrName ((char*)theName);
124   SMESH_Gen_i::AddObject(aStr, _this()) += ".SetName(\"";
125   aStr += aStrName + "\")";
126
127   SMESH_Gen_i::AddToCurrentPyScript(aStr);
128
129   // Perform renaming
130   ::SMESH_Group* aGroup = GetSmeshGroup();
131   if (aGroup) {
132     aGroup->SetName(theName);
133
134     // Update group name in a study
135     SMESH_Gen_i* aGen = myMeshServant->GetGen();
136     aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
137     return;
138   }
139   MESSAGE("can't set name of a vague group");
140 }
141
142 //=============================================================================
143 /*!
144  *  
145  */
146 //=============================================================================
147
148 char* SMESH_GroupBase_i::GetName()
149 {
150   ::SMESH_Group* aGroup = GetSmeshGroup();
151   if (aGroup)
152     return CORBA::string_dup (aGroup->GetName());
153   MESSAGE("get name of a vague group");
154   return CORBA::string_dup( "NO_NAME" );
155 }
156
157 //=============================================================================
158 /*!
159  *  
160  */
161 //=============================================================================
162
163 SMESH::ElementType SMESH_GroupBase_i::GetType()
164 {
165   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
166   if (aGroupDS) {
167     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
168     SMESH::ElementType aType;
169     switch (aSMDSType) {
170     case SMDSAbs_Node:   aType = SMESH::NODE; break;
171     case SMDSAbs_Edge:   aType = SMESH::EDGE; break;
172     case SMDSAbs_Face:   aType = SMESH::FACE; break;
173     case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
174     default:             aType = SMESH::ALL; break;
175     }
176     return aType;
177   }
178   MESSAGE("get type of a vague group");
179   return SMESH::ALL;
180 }
181
182
183 //=============================================================================
184 /*!
185  *  
186  */
187 //=============================================================================
188
189 CORBA::Long SMESH_GroupBase_i::Size()
190 {
191   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
192   if (aGroupDS)
193     return aGroupDS->Extent();
194   MESSAGE("get size of a vague group");
195   return 0;
196 }
197
198 //=============================================================================
199 /*!
200  *  
201  */
202 //=============================================================================
203
204 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
205 {
206   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
207   if (aGroupDS)
208     return aGroupDS->IsEmpty();
209   MESSAGE("checking IsEmpty of a vague group");
210   return true;
211 }
212
213 //=============================================================================
214 /*!
215  *  
216  */
217 //=============================================================================
218
219 void SMESH_Group_i::Clear()
220 {
221   // Update Python script
222   TCollection_AsciiString aStr;
223   SMESH_Gen_i::AddObject(aStr, _this()) += ".Clear()";
224
225   SMESH_Gen_i::AddToCurrentPyScript(aStr);
226
227   // Clear the group
228   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
229   if (aGroupDS) {
230     aGroupDS->Clear();
231     return;
232   }
233   MESSAGE("attempt to clear a vague group");
234 }
235
236 //=============================================================================
237 /*!
238  *  
239  */
240 //=============================================================================
241
242 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
243 {
244   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
245   if (aGroupDS)
246     return aGroupDS->Contains(theID);
247   MESSAGE("attempt to check contents of a vague group");
248   return false;
249 }
250
251 //=============================================================================
252 /*!
253  *  
254  */
255 //=============================================================================
256
257 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
258 {
259   // Update Python script
260   TCollection_AsciiString aStr ("nbAdd = ");
261   SMESH_Gen_i::AddObject(aStr, _this()) += ".Add(";
262   SMESH_Gen_i::AddArray(aStr, theIDs) += ")";
263
264   SMESH_Gen_i::AddToCurrentPyScript(aStr);
265
266   // Add elements to the group
267   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
268   if (aGroupDS) {
269     int nbAdd = 0;
270     for (int i = 0; i < theIDs.length(); i++) {
271       int anID = (int) theIDs[i];
272       if (aGroupDS->Add(anID))
273         nbAdd++;
274     }
275     return nbAdd;
276   }
277   MESSAGE("attempt to add elements to a vague group");
278   return 0;
279 }
280
281 //=============================================================================
282 /*!
283  *  
284  */
285 //=============================================================================
286
287 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
288 {
289   // Update Python script
290   TCollection_AsciiString aStr ("nbDel = ");
291   SMESH_Gen_i::AddObject(aStr, _this()) += ".Remove(";
292   SMESH_Gen_i::AddArray(aStr, theIDs) += ")";
293
294   SMESH_Gen_i::AddToCurrentPyScript(aStr);
295
296   // Remove elements from the group
297   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
298   if (aGroupDS) {
299     int nbDel = 0;
300     for (int i = 0; i < theIDs.length(); i++) {
301       int anID = (int) theIDs[i];
302       if (aGroupDS->Remove(anID))
303         nbDel++;
304     }
305     return nbDel;
306   }
307   MESSAGE("attempt to remove elements from a vague group");
308   return 0;
309 }
310
311 //=============================================================================
312 /*!
313  *  
314  */
315 //=============================================================================
316
317 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
318
319 CORBA::Long 
320 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
321                    SMESHDS_GroupBase* theGroupBase,
322                    TFunChangeGroup theFun)
323 {
324   CORBA::Long aNb = 0;
325   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
326     SMESH::Controls::Filter::TIdSequence aSequence;
327     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
328     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
329     
330     CORBA::Long i = 0, iEnd = aSequence.size();
331     for(; i < iEnd; i++)
332       if((aGroupDS->*theFun)(aSequence[i]))
333         aNb++;
334     return aNb;
335   }
336   return aNb;
337 }
338
339 CORBA::Long 
340 SMESH_Group_i::
341 AddByPredicate( SMESH::Predicate_ptr thePredicate )
342 {
343   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
344     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
345     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
346   }
347   return 0;
348 }
349
350 CORBA::Long 
351 SMESH_Group_i::
352 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
353 {
354   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
355     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
356     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
357   }
358   return 0;
359 }
360
361 //=============================================================================
362 /*!
363  *  
364  */
365 //=============================================================================
366
367 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
368 {
369   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
370   if (aGroupDS)
371     return aGroupDS->GetID(theIndex);
372   MESSAGE("attempt to iterate on a vague group");
373   return -1;
374 }
375
376 //=============================================================================
377 /*!
378  *  
379  */
380 //=============================================================================
381
382 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
383 {
384   SMESH::long_array_var aRes = new SMESH::long_array();
385   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
386   if (aGroupDS) {
387     int aSize = aGroupDS->Extent();
388     aRes->length(aSize);
389     for (int i = 0; i < aSize; i++)
390       aRes[i] = aGroupDS->GetID(i+1);
391     return aRes._retn();
392   }
393   MESSAGE("get list of IDs of a vague group");
394   return aRes._retn();
395 }
396
397 //=============================================================================
398 /*!
399  *  
400  */
401 //=============================================================================
402 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
403 {
404   SMESH::SMESH_Mesh_var aMesh;
405   if ( myMeshServant )
406     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
407   return aMesh._retn();
408 }
409
410 //=============================================================================
411 /*!
412  *  
413  */
414 //=============================================================================
415 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
416 {
417   SMESH::long_array_var aResult = GetListOfID();
418   return aResult._retn();
419 }
420
421 //=======================================================================
422 //function : GetShape
423 //purpose  : 
424 //=======================================================================
425
426 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
427 {
428   GEOM::GEOM_Object_var aGeomObj;
429   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
430   if ( aGroupDS ) {
431     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
432     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
433   }
434   return aGeomObj._retn();
435 }
436