Salome HOME
untabify
[modules/smesh.git] / src / SMESH_I / SMESH_Group_i.cxx
1 //  Copyright (C) 2007-2010  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
23 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 //  File   : SMESH_Group_i.cxx
25 //  Author : Sergey ANIKIN, OCC
26 //  Module : SMESH
27 //
28 #include "SMESH_Group_i.hxx"
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Group.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMDSAbs_ElementType.hxx"
35
36 #include "SMESH_Filter_i.hxx"
37 #include "SMESH_PythonDump.hxx"
38
39 #include "utilities.h"
40
41 using namespace SMESH;
42
43 //=============================================================================
44 /*!
45  *  
46  */
47 //=============================================================================
48
49 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
50 : SALOME::GenericObj_i( thePOA ),
51   myMeshServant( theMeshServant ), 
52   myLocalID( theLocalID )
53 {
54   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
55   // servant activation is performed by SMESH_Mesh_i::createGroup()
56   // thePOA->activate_object( this );
57 }
58
59 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
60      : SALOME::GenericObj_i( thePOA ),
61        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
62 {
63   //MESSAGE("SMESH_Group_i; this = "<<this );
64 }
65
66 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
67      : SALOME::GenericObj_i( thePOA ),
68        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
69 {
70   //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
71 }
72
73 //=============================================================================
74 /*!
75  *  
76  */
77 //=============================================================================
78
79 SMESH_GroupBase_i::~SMESH_GroupBase_i()
80 {
81   MESSAGE("~SMESH_GroupBase_i; this = "<<this );
82   if ( myMeshServant )
83     myMeshServant->removeGroup(myLocalID);
84 }
85
86 //=======================================================================
87 //function : GetSmeshGroup
88 //purpose  : 
89 //=======================================================================
90
91 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
92 {
93   if ( myMeshServant ) {
94     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
95     return aMesh.GetGroup(myLocalID);
96   }
97   return 0;
98 }
99
100 //=======================================================================
101 //function : GetGroupDS
102 //purpose  : 
103 //=======================================================================
104
105 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
106 {
107   ::SMESH_Group* aGroup = GetSmeshGroup();
108   if ( aGroup )
109     return aGroup->GetGroupDS();
110   return 0;
111 }
112
113 //=============================================================================
114 /*!
115  *  
116  */
117 //=============================================================================
118
119 void SMESH_GroupBase_i::SetName( const char* theName )
120 {
121   // Perform renaming
122   ::SMESH_Group* aGroup = GetSmeshGroup();
123   if (!aGroup) {
124     MESSAGE("can't set name of a vague group");
125     return;
126   }
127
128   if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
129     return; // nothing to rename
130
131   aGroup->SetName(theName);
132
133   // Update group name in a study
134   SMESH_Gen_i* aGen = myMeshServant->GetGen();
135   aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
136   
137   // Update Python script
138   TPythonDump() <<  _this() << ".SetName( '" << theName << "' )";
139 }
140
141 //=============================================================================
142 /*!
143  *  
144  */
145 //=============================================================================
146
147 char* SMESH_GroupBase_i::GetName()
148 {
149   ::SMESH_Group* aGroup = GetSmeshGroup();
150   if (aGroup)
151     return CORBA::string_dup (aGroup->GetName());
152   MESSAGE("get name of a vague group");
153   return CORBA::string_dup( "NO_NAME" );
154 }
155
156 //=============================================================================
157 /*!
158  *  
159  */
160 //=============================================================================
161
162 SMESH::ElementType SMESH_GroupBase_i::GetType()
163 {
164   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
165   if (aGroupDS) {
166     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
167     SMESH::ElementType aType;
168     switch (aSMDSType) {
169     case SMDSAbs_Node:      aType = SMESH::NODE;   break;
170     case SMDSAbs_Edge:      aType = SMESH::EDGE;   break;
171     case SMDSAbs_Face:      aType = SMESH::FACE;   break;
172     case SMDSAbs_Volume:    aType = SMESH::VOLUME; break;
173     case SMDSAbs_0DElement: aType = SMESH::ELEM0D; 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   TPythonDump() << _this() << ".Clear()";
223
224   // Clear the group
225   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
226   if (aGroupDS) {
227     aGroupDS->Clear();
228     return;
229   }
230   MESSAGE("attempt to clear a vague group");
231 }
232
233 //=============================================================================
234 /*!
235  *  
236  */
237 //=============================================================================
238
239 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
240 {
241   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
242   if (aGroupDS)
243     return aGroupDS->Contains(theID);
244   MESSAGE("attempt to check contents of a vague group");
245   return false;
246 }
247
248 //=============================================================================
249 /*!
250  *  
251  */
252 //=============================================================================
253
254 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
255 {
256   // Update Python script
257   TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
258
259   // Add elements to the group
260   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
261   if (aGroupDS) {
262     int nbAdd = 0;
263     for (int i = 0; i < theIDs.length(); i++) {
264       int anID = (int) theIDs[i];
265       if (aGroupDS->Add(anID))
266         nbAdd++;
267     }
268     return nbAdd;
269   }
270   MESSAGE("attempt to add elements to a vague group");
271   return 0;
272 }
273
274 //=============================================================================
275 /*!
276  *  
277  */
278 //=============================================================================
279
280 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
281 {
282   // Update Python script
283   TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
284
285   // Remove elements from the group
286   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
287   if (aGroupDS) {
288     int nbDel = 0;
289     for (int i = 0; i < theIDs.length(); i++) {
290       int anID = (int) theIDs[i];
291       if (aGroupDS->Remove(anID))
292         nbDel++;
293     }
294     return nbDel;
295   }
296   MESSAGE("attempt to remove elements from a vague group");
297   return 0;
298 }
299
300 //=============================================================================
301 /*!
302  *  
303  */
304 //=============================================================================
305
306 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
307
308 CORBA::Long 
309 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
310                    SMESHDS_GroupBase* theGroupBase,
311                    TFunChangeGroup theFun)
312 {
313   CORBA::Long aNb = 0;
314   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
315     SMESH::Controls::Filter::TIdSequence aSequence;
316     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
317     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
318     
319     CORBA::Long i = 0, iEnd = aSequence.size();
320     for(; i < iEnd; i++)
321       if((aGroupDS->*theFun)(aSequence[i]))
322         aNb++;
323     return aNb;
324   }
325   return aNb;
326 }
327
328 CORBA::Long 
329 SMESH_Group_i::
330 AddByPredicate( SMESH::Predicate_ptr thePredicate )
331 {
332   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
333     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
334     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
335   }
336   return 0;
337 }
338
339 CORBA::Long 
340 SMESH_Group_i::
341 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
342 {
343   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
344     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
345     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
346   }
347   return 0;
348 }
349
350 CORBA::Long SMESH_Group_i::AddFrom( SMESH::SMESH_IDSource_ptr theSource )
351 {
352   long nbAdd = 0;
353   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
354   if (aGroupDS) {
355     SMESH::long_array_var anIds;
356     if ( !CORBA::is_nil(SMESH::SMESH_GroupBase::_narrow(theSource)) &&
357          SMESH::SMESH_GroupBase::_narrow(theSource)->GetType() == GetType() ) {
358       anIds = theSource->GetIDs();
359     }
360     else if ( !CORBA::is_nil(SMESH::SMESH_Mesh::_narrow(theSource)) ) {
361       anIds = SMESH::SMESH_Mesh::_narrow(theSource)->GetElementsByType( GetType() );
362     }
363     else if ( !CORBA::is_nil(SMESH::SMESH_subMesh::_narrow(theSource)) ) {
364       anIds = SMESH::SMESH_subMesh::_narrow(theSource)->GetElementsByType( GetType() );
365     }
366     else {
367       anIds->length( 0 );
368     }
369     for ( int i = 0, total = anIds->length(); i < total; i++ ) {
370       if ( aGroupDS->Add((int)anIds[i]) ) nbAdd++;
371     }
372   }
373
374   // Update Python script
375   TPythonDump() << "nbAdd = " << _this() << ".AddFrom( " << theSource << " )";
376
377   return nbAdd;
378 }
379
380 //=============================================================================
381 /*!
382  *  
383  */
384 //=============================================================================
385
386 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
387 {
388   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
389   if (aGroupDS)
390     return aGroupDS->GetID(theIndex);
391   MESSAGE("attempt to iterate on a vague group");
392   return -1;
393 }
394
395 //=============================================================================
396 /*!
397  *  
398  */
399 //=============================================================================
400
401 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
402 {
403   SMESH::long_array_var aRes = new SMESH::long_array();
404   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
405   if (aGroupDS) {
406     int aSize = aGroupDS->Extent();
407     aRes->length(aSize);
408     for (int i = 0; i < aSize; i++)
409       aRes[i] = aGroupDS->GetID(i+1);
410     return aRes._retn();
411   }
412   MESSAGE("get list of IDs of a vague group");
413   return aRes._retn();
414 }
415
416 //=============================================================================
417 /*!
418  *  
419  */
420 //=============================================================================
421 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
422 {
423   SMESH::SMESH_Mesh_var aMesh;
424   if ( myMeshServant )
425     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
426   return aMesh._retn();
427 }
428
429 //=============================================================================
430 /*!
431  *  
432  */
433 //=============================================================================
434 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
435 {
436   SMESH::long_array_var aResult = GetListOfID();
437   return aResult._retn();
438 }
439
440 //=======================================================================
441 //function : GetShape
442 //purpose  : 
443 //=======================================================================
444
445 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
446 {
447   GEOM::GEOM_Object_var aGeomObj;
448   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
449   if ( aGroupDS ) {
450     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
451     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
452   }
453   return aGeomObj._retn();
454 }
455
456 //=============================================================================
457 /*!
458  *
459  */
460 //=============================================================================
461 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
462 {
463   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
464   if (aGroupDS)
465   {
466     Quantity_Color aQColor = aGroupDS->GetColor();
467     SALOMEDS::Color aColor;
468     aColor.R = aQColor.Red();
469     aColor.G = aQColor.Green();
470     aColor.B = aQColor.Blue();
471
472     return aColor;
473   }
474   MESSAGE("get color of a group");
475   return SALOMEDS::Color();
476 }
477
478 //=============================================================================
479 /*!
480  *
481  */
482 //=============================================================================
483 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
484 {
485   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
486   if (aGroupDS)
487   {
488     Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
489     return aGroupDS->SetColor(aQColor);
490   }
491   MESSAGE("set color of a group");
492   return ;
493 }
494
495 //=============================================================================
496 /*!
497  *
498  */
499 //=============================================================================
500 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
501 {
502   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
503   if (aGroupDS)
504     return aGroupDS->GetColorGroup();
505   MESSAGE("get color number of a group");
506   return 0;
507 }
508
509 //=============================================================================
510 /*!
511  *
512  */
513 //=============================================================================
514 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
515 {
516   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
517   if (aGroupDS)
518     return aGroupDS->SetColorGroup(color);
519   MESSAGE("set color number of a group");
520   return ;
521 }
522
523 //=============================================================================
524 /*!
525  * Returns statistic of mesh elements
526  * Result array of number enityties
527  * Inherited from SMESH_IDSource
528  */
529 //=============================================================================
530 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
531 {
532   SMESH::long_array_var aRes = new SMESH::long_array();
533   aRes->length(SMESH::Entity_Last);
534   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
535     aRes[i] = 0;
536
537   SMESHDS_GroupBase* aGrpDS = GetGroupDS();
538   if ( !aGrpDS )
539     return aRes._retn();
540   if ( GetType() == NODE )
541     aRes[ SMESH::Entity_Node ] = aGrpDS->Extent();
542   else
543     SMESH_Mesh_i::CollectMeshInfo( aGrpDS->GetElements(), aRes);
544   return aRes._retn();
545 }