Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESH_I / SMESH_Group_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
23 //  File   : SMESH_Group_i.cxx
24 //  Author : Sergey ANIKIN, OCC
25 //  Module : SMESH
26 //
27 #include "SMESH_Group_i.hxx"
28 #include "SMESH_Mesh_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include "SMESH_Group.hxx"
31 #include "SMESHDS_Group.hxx"
32 #include "SMESHDS_GroupOnGeom.hxx"
33 #include "SMDSAbs_ElementType.hxx"
34
35 #include "SMESH_Filter_i.hxx"
36 #include "SMESH_PythonDump.hxx"
37
38 #include "utilities.h"
39
40 using namespace SMESH;
41
42 //=============================================================================
43 /*!
44  *  
45  */
46 //=============================================================================
47
48 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
49 : SALOME::GenericObj_i( thePOA ),
50   myMeshServant( theMeshServant ), 
51   myLocalID( theLocalID )
52 {
53   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
54   // servant activation is performed by SMESH_Mesh_i::createGroup()
55   // thePOA->activate_object( this );
56 }
57
58 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
59      : SALOME::GenericObj_i( thePOA ),
60        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
61 {
62   //MESSAGE("SMESH_Group_i; this = "<<this );
63 }
64
65 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
66      : SALOME::GenericObj_i( thePOA ),
67        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
68 {
69   //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
70 }
71
72 //=============================================================================
73 /*!
74  *  
75  */
76 //=============================================================================
77
78 SMESH_GroupBase_i::~SMESH_GroupBase_i()
79 {
80   MESSAGE("~SMESH_GroupBase_i; this = "<<this );
81   if ( myMeshServant )
82     myMeshServant->removeGroup(myLocalID);
83 }
84
85 //=======================================================================
86 //function : GetSmeshGroup
87 //purpose  : 
88 //=======================================================================
89
90 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
91 {
92   if ( myMeshServant ) {
93     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
94     return aMesh.GetGroup(myLocalID);
95   }
96   return 0;
97 }
98
99 //=======================================================================
100 //function : GetGroupDS
101 //purpose  : 
102 //=======================================================================
103
104 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
105 {
106   ::SMESH_Group* aGroup = GetSmeshGroup();
107   if ( aGroup )
108     return aGroup->GetGroupDS();
109   return 0;
110 }
111
112 //=============================================================================
113 /*!
114  *  
115  */
116 //=============================================================================
117
118 void SMESH_GroupBase_i::SetName( const char* theName )
119 {
120   // Perform renaming
121   ::SMESH_Group* aGroup = GetSmeshGroup();
122   if (!aGroup) {
123     MESSAGE("can't set name of a vague group");
124     return;
125   }
126
127   if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
128     return; // nothing to rename
129
130   aGroup->SetName(theName);
131
132   // Update group name in a study
133   SMESH_Gen_i* aGen = myMeshServant->GetGen();
134   aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
135   
136   // Update Python script
137   TPythonDump() <<  _this() << ".SetName( '" << theName << "' )";
138 }
139
140 //=============================================================================
141 /*!
142  *  
143  */
144 //=============================================================================
145
146 char* SMESH_GroupBase_i::GetName()
147 {
148   ::SMESH_Group* aGroup = GetSmeshGroup();
149   if (aGroup)
150     return CORBA::string_dup (aGroup->GetName());
151   MESSAGE("get name of a vague group");
152   return CORBA::string_dup( "NO_NAME" );
153 }
154
155 //=============================================================================
156 /*!
157  *  
158  */
159 //=============================================================================
160
161 SMESH::ElementType SMESH_GroupBase_i::GetType()
162 {
163   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
164   if (aGroupDS) {
165     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
166     SMESH::ElementType aType;
167     switch (aSMDSType) {
168     case SMDSAbs_Node:   aType = SMESH::NODE; break;
169     case SMDSAbs_Edge:   aType = SMESH::EDGE; break;
170     case SMDSAbs_Face:   aType = SMESH::FACE; break;
171     case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
172     default:             aType = SMESH::ALL; break;
173     }
174     return aType;
175   }
176   MESSAGE("get type of a vague group");
177   return SMESH::ALL;
178 }
179
180
181 //=============================================================================
182 /*!
183  *  
184  */
185 //=============================================================================
186
187 CORBA::Long SMESH_GroupBase_i::Size()
188 {
189   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
190   if (aGroupDS)
191     return aGroupDS->Extent();
192   MESSAGE("get size of a vague group");
193   return 0;
194 }
195
196 //=============================================================================
197 /*!
198  *  
199  */
200 //=============================================================================
201
202 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
203 {
204   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
205   if (aGroupDS)
206     return aGroupDS->IsEmpty();
207   MESSAGE("checking IsEmpty of a vague group");
208   return true;
209 }
210
211 //=============================================================================
212 /*!
213  *  
214  */
215 //=============================================================================
216
217 void SMESH_Group_i::Clear()
218 {
219   // Update Python script
220   TPythonDump() << _this() << ".Clear()";
221
222   // Clear the group
223   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
224   if (aGroupDS) {
225     aGroupDS->Clear();
226     return;
227   }
228   MESSAGE("attempt to clear a vague group");
229 }
230
231 //=============================================================================
232 /*!
233  *  
234  */
235 //=============================================================================
236
237 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
238 {
239   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
240   if (aGroupDS)
241     return aGroupDS->Contains(theID);
242   MESSAGE("attempt to check contents of a vague group");
243   return false;
244 }
245
246 //=============================================================================
247 /*!
248  *  
249  */
250 //=============================================================================
251
252 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
253 {
254   // Update Python script
255   TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
256
257   // Add elements to the group
258   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
259   if (aGroupDS) {
260     int nbAdd = 0;
261     for (int i = 0; i < theIDs.length(); i++) {
262       int anID = (int) theIDs[i];
263       if (aGroupDS->Add(anID))
264         nbAdd++;
265     }
266     return nbAdd;
267   }
268   MESSAGE("attempt to add elements to a vague group");
269   return 0;
270 }
271
272 //=============================================================================
273 /*!
274  *  
275  */
276 //=============================================================================
277
278 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
279 {
280   // Update Python script
281   TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
282
283   // Remove elements from the group
284   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
285   if (aGroupDS) {
286     int nbDel = 0;
287     for (int i = 0; i < theIDs.length(); i++) {
288       int anID = (int) theIDs[i];
289       if (aGroupDS->Remove(anID))
290         nbDel++;
291     }
292     return nbDel;
293   }
294   MESSAGE("attempt to remove elements from a vague group");
295   return 0;
296 }
297
298 //=============================================================================
299 /*!
300  *  
301  */
302 //=============================================================================
303
304 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
305
306 CORBA::Long 
307 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
308                    SMESHDS_GroupBase* theGroupBase,
309                    TFunChangeGroup theFun)
310 {
311   CORBA::Long aNb = 0;
312   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
313     SMESH::Controls::Filter::TIdSequence aSequence;
314     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
315     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
316     
317     CORBA::Long i = 0, iEnd = aSequence.size();
318     for(; i < iEnd; i++)
319       if((aGroupDS->*theFun)(aSequence[i]))
320         aNb++;
321     return aNb;
322   }
323   return aNb;
324 }
325
326 CORBA::Long 
327 SMESH_Group_i::
328 AddByPredicate( SMESH::Predicate_ptr thePredicate )
329 {
330   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
331     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
332     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
333   }
334   return 0;
335 }
336
337 CORBA::Long 
338 SMESH_Group_i::
339 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
340 {
341   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
342     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
343     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
344   }
345   return 0;
346 }
347
348 //=============================================================================
349 /*!
350  *  
351  */
352 //=============================================================================
353
354 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
355 {
356   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
357   if (aGroupDS)
358     return aGroupDS->GetID(theIndex);
359   MESSAGE("attempt to iterate on a vague group");
360   return -1;
361 }
362
363 //=============================================================================
364 /*!
365  *  
366  */
367 //=============================================================================
368
369 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
370 {
371   SMESH::long_array_var aRes = new SMESH::long_array();
372   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
373   if (aGroupDS) {
374     int aSize = aGroupDS->Extent();
375     aRes->length(aSize);
376     for (int i = 0; i < aSize; i++)
377       aRes[i] = aGroupDS->GetID(i+1);
378     return aRes._retn();
379   }
380   MESSAGE("get list of IDs of a vague group");
381   return aRes._retn();
382 }
383
384 //=============================================================================
385 /*!
386  *  
387  */
388 //=============================================================================
389 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
390 {
391   SMESH::SMESH_Mesh_var aMesh;
392   if ( myMeshServant )
393     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
394   return aMesh._retn();
395 }
396
397 //=============================================================================
398 /*!
399  *  
400  */
401 //=============================================================================
402 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
403 {
404   SMESH::long_array_var aResult = GetListOfID();
405   return aResult._retn();
406 }
407
408 //=======================================================================
409 //function : GetShape
410 //purpose  : 
411 //=======================================================================
412
413 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
414 {
415   GEOM::GEOM_Object_var aGeomObj;
416   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
417   if ( aGroupDS ) {
418     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
419     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
420   }
421   return aGeomObj._retn();
422 }
423
424 //=============================================================================
425 /*!
426  *
427  */
428 //=============================================================================
429 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
430 {
431   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
432   if (aGroupDS)
433   {
434     Quantity_Color aQColor = aGroupDS->GetColor();
435     SALOMEDS::Color aColor;
436     aColor.R = aQColor.Red();
437     aColor.G = aQColor.Green();
438     aColor.B = aQColor.Blue();
439
440     return aColor;
441   }
442   MESSAGE("get color of a group");
443   return SALOMEDS::Color();
444 }
445
446 //=============================================================================
447 /*!
448  *
449  */
450 //=============================================================================
451 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
452 {
453   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
454   if (aGroupDS)
455   {
456     Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
457     return aGroupDS->SetColor(aQColor);
458   }
459   MESSAGE("set color of a group");
460   return ;
461 }
462
463 //=============================================================================
464 /*!
465  *
466  */
467 //=============================================================================
468 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
469 {
470   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
471   if (aGroupDS)
472     return aGroupDS->GetColorGroup();
473   MESSAGE("get color number of a group");
474   return 0;
475 }
476
477 //=============================================================================
478 /*!
479  *
480  */
481 //=============================================================================
482 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
483 {
484   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
485   if (aGroupDS)
486     return aGroupDS->SetColorGroup(color);
487   MESSAGE("set color number of a group");
488   return ;
489 }