Salome HOME
Fix of IPAL19448 (Qt4 porting: application hungs up at several Import MED file cycles).
[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.com
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   // akl: removeGroup() is called from SMESH_Mesh_i() destructor
84   // if ( myMeshServant )
85   //   myMeshServant->removeGroup(myLocalID);
86 }
87
88 //=======================================================================
89 //function : GetSmeshGroup
90 //purpose  : 
91 //=======================================================================
92
93 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
94 {
95   if ( myMeshServant ) {
96     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
97     return aMesh.GetGroup(myLocalID);
98   }
99   return 0;
100 }
101
102 //=======================================================================
103 //function : GetGroupDS
104 //purpose  : 
105 //=======================================================================
106
107 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
108 {
109   ::SMESH_Group* aGroup = GetSmeshGroup();
110   if ( aGroup )
111     return aGroup->GetGroupDS();
112   return 0;
113 }
114
115 //=============================================================================
116 /*!
117  *  
118  */
119 //=============================================================================
120
121 void SMESH_GroupBase_i::SetName( const char* theName )
122 {
123   // Update Python script
124   TPythonDump() <<  _this() << ".SetName( '" << theName << "' )";
125
126   // Perform renaming
127   ::SMESH_Group* aGroup = GetSmeshGroup();
128   if (aGroup) {
129     aGroup->SetName(theName);
130
131     // Update group name in a study
132     SMESH_Gen_i* aGen = myMeshServant->GetGen();
133     aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
134     return;
135   }
136   MESSAGE("can't set name of a vague group");
137 }
138
139 //=============================================================================
140 /*!
141  *  
142  */
143 //=============================================================================
144
145 char* SMESH_GroupBase_i::GetName()
146 {
147   ::SMESH_Group* aGroup = GetSmeshGroup();
148   if (aGroup)
149     return CORBA::string_dup (aGroup->GetName());
150   MESSAGE("get name of a vague group");
151   return CORBA::string_dup( "NO_NAME" );
152 }
153
154 //=============================================================================
155 /*!
156  *  
157  */
158 //=============================================================================
159
160 SMESH::ElementType SMESH_GroupBase_i::GetType()
161 {
162   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
163   if (aGroupDS) {
164     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
165     SMESH::ElementType aType;
166     switch (aSMDSType) {
167     case SMDSAbs_Node:   aType = SMESH::NODE; break;
168     case SMDSAbs_Edge:   aType = SMESH::EDGE; break;
169     case SMDSAbs_Face:   aType = SMESH::FACE; break;
170     case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
171     default:             aType = SMESH::ALL; break;
172     }
173     return aType;
174   }
175   MESSAGE("get type of a vague group");
176   return SMESH::ALL;
177 }
178
179
180 //=============================================================================
181 /*!
182  *  
183  */
184 //=============================================================================
185
186 CORBA::Long SMESH_GroupBase_i::Size()
187 {
188   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
189   if (aGroupDS)
190     return aGroupDS->Extent();
191   MESSAGE("get size of a vague group");
192   return 0;
193 }
194
195 //=============================================================================
196 /*!
197  *  
198  */
199 //=============================================================================
200
201 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
202 {
203   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
204   if (aGroupDS)
205     return aGroupDS->IsEmpty();
206   MESSAGE("checking IsEmpty of a vague group");
207   return true;
208 }
209
210 //=============================================================================
211 /*!
212  *  
213  */
214 //=============================================================================
215
216 void SMESH_Group_i::Clear()
217 {
218   // Update Python script
219   TPythonDump() << _this() << ".Clear()";
220
221   // Clear the group
222   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
223   if (aGroupDS) {
224     aGroupDS->Clear();
225     return;
226   }
227   MESSAGE("attempt to clear a vague group");
228 }
229
230 //=============================================================================
231 /*!
232  *  
233  */
234 //=============================================================================
235
236 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
237 {
238   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
239   if (aGroupDS)
240     return aGroupDS->Contains(theID);
241   MESSAGE("attempt to check contents of a vague group");
242   return false;
243 }
244
245 //=============================================================================
246 /*!
247  *  
248  */
249 //=============================================================================
250
251 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
252 {
253   // Update Python script
254   TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
255
256   // Add elements to the group
257   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
258   if (aGroupDS) {
259     int nbAdd = 0;
260     for (int i = 0; i < theIDs.length(); i++) {
261       int anID = (int) theIDs[i];
262       if (aGroupDS->Add(anID))
263         nbAdd++;
264     }
265     return nbAdd;
266   }
267   MESSAGE("attempt to add elements to a vague group");
268   return 0;
269 }
270
271 //=============================================================================
272 /*!
273  *  
274  */
275 //=============================================================================
276
277 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
278 {
279   // Update Python script
280   TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
281
282   // Remove elements from the group
283   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
284   if (aGroupDS) {
285     int nbDel = 0;
286     for (int i = 0; i < theIDs.length(); i++) {
287       int anID = (int) theIDs[i];
288       if (aGroupDS->Remove(anID))
289         nbDel++;
290     }
291     return nbDel;
292   }
293   MESSAGE("attempt to remove elements from a vague group");
294   return 0;
295 }
296
297 //=============================================================================
298 /*!
299  *  
300  */
301 //=============================================================================
302
303 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
304
305 CORBA::Long 
306 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
307                    SMESHDS_GroupBase* theGroupBase,
308                    TFunChangeGroup theFun)
309 {
310   CORBA::Long aNb = 0;
311   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
312     SMESH::Controls::Filter::TIdSequence aSequence;
313     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
314     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
315     
316     CORBA::Long i = 0, iEnd = aSequence.size();
317     for(; i < iEnd; i++)
318       if((aGroupDS->*theFun)(aSequence[i]))
319         aNb++;
320     return aNb;
321   }
322   return aNb;
323 }
324
325 CORBA::Long 
326 SMESH_Group_i::
327 AddByPredicate( SMESH::Predicate_ptr thePredicate )
328 {
329   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
330     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
331     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
332   }
333   return 0;
334 }
335
336 CORBA::Long 
337 SMESH_Group_i::
338 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
339 {
340   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
341     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
342     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
343   }
344   return 0;
345 }
346
347 //=============================================================================
348 /*!
349  *  
350  */
351 //=============================================================================
352
353 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
354 {
355   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
356   if (aGroupDS)
357     return aGroupDS->GetID(theIndex);
358   MESSAGE("attempt to iterate on a vague group");
359   return -1;
360 }
361
362 //=============================================================================
363 /*!
364  *  
365  */
366 //=============================================================================
367
368 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
369 {
370   SMESH::long_array_var aRes = new SMESH::long_array();
371   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
372   if (aGroupDS) {
373     int aSize = aGroupDS->Extent();
374     aRes->length(aSize);
375     for (int i = 0; i < aSize; i++)
376       aRes[i] = aGroupDS->GetID(i+1);
377     return aRes._retn();
378   }
379   MESSAGE("get list of IDs of a vague group");
380   return aRes._retn();
381 }
382
383 //=============================================================================
384 /*!
385  *  
386  */
387 //=============================================================================
388 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
389 {
390   SMESH::SMESH_Mesh_var aMesh;
391   if ( myMeshServant )
392     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
393   return aMesh._retn();
394 }
395
396 //=============================================================================
397 /*!
398  *  
399  */
400 //=============================================================================
401 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
402 {
403   SMESH::long_array_var aResult = GetListOfID();
404   return aResult._retn();
405 }
406
407 //=======================================================================
408 //function : GetShape
409 //purpose  : 
410 //=======================================================================
411
412 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
413 {
414   GEOM::GEOM_Object_var aGeomObj;
415   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
416   if ( aGroupDS ) {
417     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
418     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
419   }
420   return aGeomObj._retn();
421 }
422
423 //=============================================================================
424 /*!
425  *
426  */
427 //=============================================================================
428 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
429 {
430   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
431   if (aGroupDS)
432   {
433     Quantity_Color aQColor = aGroupDS->GetColor();
434     SALOMEDS::Color aColor;
435     aColor.R = aQColor.Red();
436     aColor.G = aQColor.Green();
437     aColor.B = aQColor.Blue();
438
439     return aColor;
440   }
441   MESSAGE("get color of a group");
442   return SALOMEDS::Color();
443 }
444
445 //=============================================================================
446 /*!
447  *
448  */
449 //=============================================================================
450 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
451 {
452   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
453   if (aGroupDS)
454   {
455     Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
456     return aGroupDS->SetColor(aQColor);
457   }
458   MESSAGE("set color of a group");
459   return ;
460 }
461
462 //=============================================================================
463 /*!
464  *
465  */
466 //=============================================================================
467 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
468 {
469   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
470   if (aGroupDS)
471     return aGroupDS->GetColorGroup();
472   MESSAGE("get color number of a group");
473   return 0;
474 }
475
476 //=============================================================================
477 /*!
478  *
479  */
480 //=============================================================================
481 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
482 {
483   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
484   if (aGroupDS)
485     return aGroupDS->SetColorGroup(color);
486   MESSAGE("set color number of a group");
487   return ;
488 }