Salome HOME
IPAL 0051561: Hexotic algorithm fail
[modules/smesh.git] / src / SMESH_I / SMESH_Group_i.cxx
1 // Copyright (C) 2007-2012  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
30 #include "SMDSAbs_ElementType.hxx"
31 #include "SMESHDS_Group.hxx"
32 #include "SMESHDS_GroupOnFilter.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_Filter_i.hxx"
36 #include "SMESH_Gen_i.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_Mesh_i.hxx"
39 #include "SMESH_PythonDump.hxx"
40 #include "SMESH_PreMeshInfo.hxx"
41
42 #include CORBA_SERVER_HEADER(SMESH_Filter)
43
44 #include "utilities.h"
45
46 using namespace SMESH;
47
48 //=============================================================================
49 /*!
50  *  
51  */
52 //=============================================================================
53
54 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA,
55                                       SMESH_Mesh_i*           theMeshServant,
56                                       const int               theLocalID )
57 : SALOME::GenericObj_i( thePOA ),
58   myMeshServant( theMeshServant ), 
59   myLocalID( theLocalID ),
60   myNbNodes(-1),
61   myGroupDSTic(0),
62   myPreMeshInfo(NULL)
63 {
64   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
65   // servant activation is performed by SMESH_Mesh_i::createGroup()
66   // thePOA->activate_object( this );
67 }
68
69 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA,
70                               SMESH_Mesh_i*           theMeshServant,
71                               const int               theLocalID )
72      : SALOME::GenericObj_i( thePOA ),
73        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
74 {
75   //MESSAGE("SMESH_Group_i; this = "<<this );
76 }
77
78 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA,
79                                           SMESH_Mesh_i*           theMeshServant,
80                                           const int               theLocalID )
81      : SALOME::GenericObj_i( thePOA ),
82        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
83 {
84   //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
85 }
86
87 SMESH_GroupOnFilter_i::SMESH_GroupOnFilter_i( PortableServer::POA_ptr thePOA,
88                                               SMESH_Mesh_i*           theMeshServant,
89                                               const int               theLocalID )
90   : SALOME::GenericObj_i( thePOA ),
91     SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
92 {
93   //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
94 }
95
96 //=============================================================================
97 /*!
98  *  
99  */
100 //=============================================================================
101
102 SMESH_GroupBase_i::~SMESH_GroupBase_i()
103 {
104   MESSAGE("~SMESH_GroupBase_i; this = "<<this );
105   if ( myMeshServant )
106     myMeshServant->removeGroup(myLocalID);
107
108   if ( myPreMeshInfo ) delete myPreMeshInfo; myPreMeshInfo = NULL;
109 }
110
111 //=======================================================================
112 //function : GetSmeshGroup
113 //purpose  : 
114 //=======================================================================
115
116 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
117 {
118   if ( myMeshServant ) {
119     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
120     return aMesh.GetGroup(myLocalID);
121   }
122   return 0;
123 }
124
125 //=======================================================================
126 //function : GetGroupDS
127 //purpose  : 
128 //=======================================================================
129
130 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
131 {
132   ::SMESH_Group* aGroup = GetSmeshGroup();
133   if ( aGroup )
134     return aGroup->GetGroupDS();
135   return 0;
136 }
137
138 //=============================================================================
139 /*!
140  *  
141  */
142 //=============================================================================
143
144 void SMESH_GroupBase_i::SetName( const char* theName )
145 {
146   // Perform renaming
147   ::SMESH_Group* aGroup = GetSmeshGroup();
148   if (!aGroup) {
149     MESSAGE("can't set name of a vague group");
150     return;
151   }
152
153   if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
154     return; // nothing to rename
155
156   aGroup->SetName(theName);
157
158   // Update group name in a study
159   SMESH_Gen_i* aGen = myMeshServant->GetGen();
160   aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
161   
162   // Update Python script
163   TPythonDump() <<  _this() << ".SetName( '" << theName << "' )";
164 }
165
166 //=============================================================================
167 /*!
168  *  
169  */
170 //=============================================================================
171
172 char* SMESH_GroupBase_i::GetName()
173 {
174   ::SMESH_Group* aGroup = GetSmeshGroup();
175   if (aGroup)
176     return CORBA::string_dup (aGroup->GetName());
177   MESSAGE("get name of a vague group");
178   return CORBA::string_dup( "NO_NAME" );
179 }
180
181 //=============================================================================
182 /*!
183  *  
184  */
185 //=============================================================================
186
187 SMESH::ElementType SMESH_GroupBase_i::GetType()
188 {
189   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
190   if (aGroupDS) {
191     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
192     SMESH::ElementType aType;
193     switch (aSMDSType) {
194     case SMDSAbs_Node:      aType = SMESH::NODE;   break;
195     case SMDSAbs_Edge:      aType = SMESH::EDGE;   break;
196     case SMDSAbs_Face:      aType = SMESH::FACE;   break;
197     case SMDSAbs_Volume:    aType = SMESH::VOLUME; break;
198     case SMDSAbs_0DElement: aType = SMESH::ELEM0D; break;
199     case SMDSAbs_Ball:      aType = SMESH::BALL;   break;
200     default:                aType = SMESH::ALL;    break;
201     }
202     return aType;
203   }
204   MESSAGE("get type of a vague group");
205   return SMESH::ALL;
206 }
207
208
209 //=============================================================================
210 /*!
211  *  
212  */
213 //=============================================================================
214
215 CORBA::Long SMESH_GroupBase_i::Size()
216 {
217   if ( myPreMeshInfo )
218     return GetType() == SMESH::NODE ? myPreMeshInfo->NbNodes() : myPreMeshInfo->NbElements();
219
220   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
221   if (aGroupDS)
222     return aGroupDS->Extent();
223   MESSAGE("get size of a vague group");
224   return 0;
225 }
226
227 //=============================================================================
228 /*!
229  *  
230  */
231 //=============================================================================
232
233 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
234 {
235   if ( myPreMeshInfo )
236     return Size() == 0;
237
238   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
239   if (aGroupDS)
240     return aGroupDS->IsEmpty();
241   MESSAGE("checking IsEmpty of a vague group");
242   return true;
243 }
244
245 //=============================================================================
246 /*!
247  *  
248  */
249 //=============================================================================
250
251 void SMESH_Group_i::Clear()
252 {
253   if ( myPreMeshInfo )
254     myPreMeshInfo->FullLoadFromFile();
255
256   // Update Python script
257   TPythonDump() << _this() << ".Clear()";
258
259   // Clear the group
260   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
261   if (aGroupDS) {
262     aGroupDS->Clear();
263     return;
264   }
265   MESSAGE("attempt to clear a vague group");
266 }
267
268 //=============================================================================
269 /*!
270  *  
271  */
272 //=============================================================================
273
274 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
275 {
276   if ( myPreMeshInfo )
277     myPreMeshInfo->FullLoadFromFile();
278
279   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
280   if (aGroupDS)
281     return aGroupDS->Contains(theID);
282   MESSAGE("attempt to check contents of a vague group");
283   return false;
284 }
285
286 //=============================================================================
287 /*!
288  *  
289  */
290 //=============================================================================
291
292 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
293 {
294   if ( myPreMeshInfo )
295     myPreMeshInfo->FullLoadFromFile();
296
297   // Update Python script
298   TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
299
300   // Add elements to the group
301   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
302   if (aGroupDS) {
303     int nbAdd = 0;
304     for (int i = 0; i < theIDs.length(); i++) {
305       int anID = (int) theIDs[i];
306       if (aGroupDS->Add(anID))
307         nbAdd++;
308     }
309     return nbAdd;
310   }
311   MESSAGE("attempt to add elements to a vague group");
312   return 0;
313 }
314
315 //=============================================================================
316 /*!
317  *  
318  */
319 //=============================================================================
320
321 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
322 {
323   if ( myPreMeshInfo )
324     myPreMeshInfo->FullLoadFromFile();
325
326   // Update Python script
327   TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
328
329   // Remove elements from the group
330   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
331   if (aGroupDS) {
332     int nbDel = 0;
333     for (int i = 0; i < theIDs.length(); i++) {
334       int anID = (int) theIDs[i];
335       if (aGroupDS->Remove(anID))
336         nbDel++;
337     }
338     return nbDel;
339   }
340   MESSAGE("attempt to remove elements from a vague group");
341   return 0;
342 }
343
344 //=============================================================================
345 /*!
346  *  
347  */
348 //=============================================================================
349
350 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
351
352 CORBA::Long 
353 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
354                    SMESHDS_GroupBase*  theGroupBase,
355                    TFunChangeGroup     theFun)
356 {
357   CORBA::Long aNb = 0;
358   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
359     SMESH::Controls::Filter::TIdSequence aSequence;
360     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
361     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
362     
363     CORBA::Long i = 0, iEnd = aSequence.size();
364     for(; i < iEnd; i++)
365       if((aGroupDS->*theFun)(aSequence[i]))
366         aNb++;
367     return aNb;
368   }
369   return aNb;
370 }
371
372 CORBA::Long 
373 SMESH_Group_i::
374 AddByPredicate( SMESH::Predicate_ptr thePredicate )
375 {
376   if ( myPreMeshInfo )
377     myPreMeshInfo->FullLoadFromFile();
378
379   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
380     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
381     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
382   }
383   return 0;
384 }
385
386 CORBA::Long 
387 SMESH_Group_i::
388 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
389 {
390   if ( myPreMeshInfo )
391     myPreMeshInfo->FullLoadFromFile();
392
393   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
394     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
395     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
396   }
397   return 0;
398 }
399
400 CORBA::Long SMESH_Group_i::AddFrom( SMESH::SMESH_IDSource_ptr theSource )
401 {
402   if ( myPreMeshInfo )
403     myPreMeshInfo->FullLoadFromFile();
404
405   TPythonDump pd;
406   long nbAdd = 0;
407   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
408   if (aGroupDS) {
409     SMESH::long_array_var anIds;
410     SMESH::SMESH_GroupBase_var group = SMESH::SMESH_GroupBase::_narrow(theSource);
411     SMESH::SMESH_Mesh_var mesh       = SMESH::SMESH_Mesh::_narrow(theSource);
412     SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow(theSource);
413     SMESH::Filter_var filter         = SMESH::Filter::_narrow(theSource);
414     if ( !group->_is_nil())
415       anIds = group->GetType()==GetType() ? theSource->GetIDs() :  new SMESH::long_array();
416     else if ( !mesh->_is_nil() )
417       anIds = mesh->GetElementsByType( GetType() );
418     else if ( !submesh->_is_nil())
419       anIds = submesh->GetElementsByType( GetType() );
420     else if ( !filter->_is_nil() ) {
421       filter->SetMesh( GetMeshServant()->_this() );
422       anIds = filter->GetElementType()==GetType() ? theSource->GetIDs() : new SMESH::long_array();
423     }
424     else 
425       anIds = theSource->GetIDs();
426     for ( int i = 0, total = anIds->length(); i < total; i++ ) {
427       if ( aGroupDS->Add((int)anIds[i]) ) nbAdd++;
428     }
429   }
430
431   // Update Python script
432   pd << "nbAdd = " << _this() << ".AddFrom( " << theSource << " )";
433
434   return nbAdd;
435 }
436
437 //=============================================================================
438 /*!
439  *  
440  */
441 //=============================================================================
442
443 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
444 {
445   if ( myPreMeshInfo )
446     myPreMeshInfo->FullLoadFromFile();
447
448   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
449   if (aGroupDS)
450     return aGroupDS->GetID(theIndex);
451   MESSAGE("attempt to iterate on a vague group");
452   return -1;
453 }
454
455 //=============================================================================
456 /*!
457  *  
458  */
459 //=============================================================================
460
461 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
462 {
463   if ( myPreMeshInfo )
464     myPreMeshInfo->FullLoadFromFile();
465
466   SMESH::long_array_var aRes = new SMESH::long_array();
467   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
468   if (aGroupDS)
469   {
470     int aSize = aGroupDS->Extent();
471     aRes->length(aSize);
472     for (int i = 0; i < aSize; i++)
473       aRes[i] = aGroupDS->GetID(i+1);
474
475     if ( 0 < aSize && aSize < 100 ) // for comfortable testing ;)
476       std::sort( &aRes[0], &aRes[0]+aSize );
477   }
478   MESSAGE("get list of IDs of a vague group");
479   return aRes._retn();
480 }
481
482 namespace
483 {
484   //================================================================================
485   /*!
486    * \brief return nodes of elements pointered by iterator
487    */
488   //================================================================================
489
490   void getNodesOfElements(SMDS_ElemIteratorPtr        elemIt,
491                           set<const SMDS_MeshNode* >& nodes)
492   {
493     while ( elemIt->more() )
494     {
495       const SMDS_MeshElement* e = elemIt->next();
496       nodes.insert( e->begin_nodes(), e->end_nodes() );
497     }
498   }
499 }
500   
501 //================================================================================
502 /*!
503  * \brief return the number of nodes of cells included to the group
504  */
505 //================================================================================
506
507 CORBA::Long SMESH_GroupBase_i::GetNumberOfNodes()
508 {
509   if ( GetType() == SMESH::NODE )
510     return Size();
511
512   if ( myPreMeshInfo )
513     myPreMeshInfo->FullLoadFromFile();
514
515   if ( SMESHDS_GroupBase* g = GetGroupDS())
516   {
517     if ( myNbNodes < 0 || g->GetTic() != myGroupDSTic )
518     {      
519       set<const SMDS_MeshNode* > nodes;
520       getNodesOfElements( g->GetElements(), nodes );
521       myNbNodes = nodes.size();
522       myGroupDSTic = g->GetTic();
523     }
524   }
525   return myNbNodes;
526 }
527
528 //================================================================================
529 /*!
530  * \brief Return true if GetNumberOfNodes() won't take a long time for computation
531  */
532 //================================================================================
533
534 CORBA::Boolean SMESH_GroupBase_i::IsNodeInfoAvailable()
535 {
536   if ( GetType() == SMESH::NODE/* || Size() < 100000 */)
537     return true;
538   if ( myPreMeshInfo )
539     return false;
540   if ( SMESHDS_GroupBase* g = GetGroupDS())
541     return ( myNbNodes > -1 && g->GetTic() == myGroupDSTic);
542   return false;
543 }
544
545 //================================================================================
546 /*!
547  * \brief Return IDs of nodes of cells included to the group
548  */
549 //================================================================================
550
551 SMESH::long_array* SMESH_GroupBase_i::GetNodeIDs()
552 {
553   if ( GetType() == SMESH::NODE )
554     return GetListOfID();
555
556   if ( myPreMeshInfo )
557     myPreMeshInfo->FullLoadFromFile();
558
559   SMESH::long_array_var aRes = new SMESH::long_array();
560   if ( SMESHDS_GroupBase* g = GetGroupDS())
561   {
562     set<const SMDS_MeshNode* > nodes;
563     getNodesOfElements( g->GetElements(), nodes );
564     aRes->length( nodes.size() );
565     set<const SMDS_MeshNode*>::iterator nIt = nodes.begin(), nEnd = nodes.end();
566     for ( int i = 0; nIt != nEnd; ++nIt, ++i )
567       aRes[i] = (*nIt)->GetID();
568   }
569   return aRes._retn();
570 }
571
572 //=============================================================================
573 /*!
574  *  
575  */
576 //=============================================================================
577 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
578 {
579   SMESH::SMESH_Mesh_var aMesh;
580   if ( myMeshServant )
581     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
582   return aMesh._retn();
583 }
584
585 //=======================================================================
586 //function : GetShape
587 //purpose  : 
588 //=======================================================================
589
590 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
591 {
592   GEOM::GEOM_Object_var aGeomObj;
593   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
594   if ( aGroupDS ) {
595     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
596     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
597   }
598   return aGeomObj._retn();
599 }
600
601 //=============================================================================
602 /*!
603  *
604  */
605 //=============================================================================
606 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
607 {
608   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
609   if (aGroupDS)
610   {
611     Quantity_Color aQColor = aGroupDS->GetColor();
612     SALOMEDS::Color aColor;
613     aColor.R = aQColor.Red();
614     aColor.G = aQColor.Green();
615     aColor.B = aQColor.Blue();
616
617     return aColor;
618   }
619   MESSAGE("get color of a group");
620   return SALOMEDS::Color();
621 }
622
623 //=============================================================================
624 /*!
625  *
626  */
627 //=============================================================================
628 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
629 {
630   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
631   if (aGroupDS)
632   {
633     Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
634     Quantity_Color oldColor = aGroupDS->GetColor();
635     if ( oldColor != aQColor )
636     {
637       aGroupDS->SetColor(aQColor);
638       TPythonDump()<<_this()<<".SetColor( SALOMEDS.Color( "<<color.R<<", "<<color.G<<", "<<color.B<<" ))";
639     }
640   }
641 }
642
643 //=============================================================================
644 /*!
645  *
646  */
647 //=============================================================================
648 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
649 {
650   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
651   if (aGroupDS)
652     return aGroupDS->GetColorGroup();
653   MESSAGE("get color number of a group");
654   return 0;
655 }
656
657 //=============================================================================
658 /*!
659  *
660  */
661 //=============================================================================
662 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
663 {
664   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
665   if (aGroupDS)
666   {
667     aGroupDS->SetColorGroup(color);
668     TPythonDump()<<_this()<<".SetColorNumber( "<<color<<" )";
669   }
670   MESSAGE("set color number of a group");
671   return ;
672 }
673
674 //=============================================================================
675 /*!
676  * Returns statistic of mesh elements
677  * Result array of number enityties
678  * Inherited from SMESH_IDSource
679  */
680 //=============================================================================
681 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
682 {
683   if ( myPreMeshInfo )
684     return myPreMeshInfo->GetMeshInfo();
685
686   SMESH::long_array_var aRes = new SMESH::long_array();
687   aRes->length(SMESH::Entity_Last);
688   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
689     aRes[i] = 0;
690
691   if ( SMESHDS_GroupBase* g = GetGroupDS())
692   {
693     if ( g->GetType() == SMDSAbs_Node || ( myNbNodes > -1 && g->GetTic() == myGroupDSTic))
694       aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
695
696     if ( g->GetType() != SMDSAbs_Node )
697       SMESH_Mesh_i::CollectMeshInfo( g->GetElements(), aRes);
698   }
699
700   return aRes._retn();
701 }
702
703 //=======================================================================
704 //function : GetIDs
705 //purpose  : Returns ids of members
706 //=======================================================================
707
708 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
709 {
710   return GetListOfID();
711 }
712
713 //=======================================================================
714 //function : GetTypes
715 //purpose  : Returns types of elements it contains
716 //=======================================================================
717
718 SMESH::array_of_ElementType* SMESH_GroupBase_i::GetTypes()
719 {
720   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
721   if ( !IsEmpty() )
722   {
723     types->length( 1 );
724     types[0] = GetType();
725   }
726   return types._retn();
727 }
728
729 //=======================================================================
730 //function : IsMeshInfoCorrect
731 //purpose  : * Returns false if GetMeshInfo() returns incorrect information that may
732 //           * happen if mesh data is not yet fully loaded from the file of study.
733 //=======================================================================
734
735 bool SMESH_GroupBase_i::IsMeshInfoCorrect()
736 {
737   return myPreMeshInfo ? myPreMeshInfo->IsMeshInfoCorrect() : true;
738 }
739
740 //================================================================================
741 /*!
742  * \brief Retrieves the predicate from the filter
743  */
744 //================================================================================
745
746 SMESH_PredicatePtr SMESH_GroupOnFilter_i::GetPredicate( SMESH::Filter_ptr filter )
747 {
748   SMESH_PredicatePtr predicate;
749
750   if ( SMESH::Filter_i* filt_i = SMESH::DownCast< SMESH::Filter_i* >( filter ))
751     if ( SMESH::Predicate_i* predic_i= filt_i->GetPredicate_i() )
752       predicate = predic_i->GetPredicate();
753
754   return predicate;
755 }
756
757 //================================================================================
758 /*!
759  * \brief Sets the filter defining group contents
760  */
761 //================================================================================
762
763 void SMESH_GroupOnFilter_i::SetFilter(SMESH::Filter_ptr theFilter)
764 {
765   if ( myPreMeshInfo )
766     myPreMeshInfo->FullLoadFromFile();
767
768   if ( ! myFilter->_is_nil() )
769     myFilter->UnRegister();
770
771   myFilter = SMESH::Filter::_duplicate( theFilter );
772
773   if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
774     grDS->SetPredicate( GetPredicate( myFilter ));
775
776   TPythonDump()<< _this() <<".SetFilter( "<<theFilter<<" )";
777
778   if ( myFilter )
779   {
780     myFilter->Register();
781     SMESH::DownCast< SMESH::Filter_i* >( myFilter )->AddWaiter( this );
782   }
783 }
784
785 //================================================================================
786 /*!
787  * \brief Returns the filter defining group contents
788  */
789 //================================================================================
790
791 SMESH::Filter_ptr SMESH_GroupOnFilter_i::GetFilter()
792 {
793   SMESH::Filter_var f = myFilter;
794   TPythonDump() << f << " = " << _this() << ".GetFilter()";
795   return f._retn();
796 }
797
798 #define SEPAR '^'
799
800 //================================================================================
801 /*!
802  * \brief Return a string to be used to store group definition in the study
803  */
804 //================================================================================
805
806 std::string SMESH_GroupOnFilter_i::FilterToString() const
807 {
808   SMESH_Comment result;
809   SMESH::Filter::Criteria_var criteria;
810   if ( !myFilter->_is_nil() && myFilter->GetCriteria( criteria.out() ))
811   {
812     result << criteria->length() << SEPAR;
813     for ( unsigned i = 0; i < criteria->length(); ++i )
814     {
815       // write FunctorType as string but not as number to assure correct
816       // persistence if enum FunctorType is modified by insertion in the middle
817       SMESH::Filter::Criterion& crit = criteria[ i ];
818       result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Type ))    << SEPAR;
819       result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Compare )) << SEPAR;
820       result << crit.Threshold                                                  << SEPAR;
821       result << crit.ThresholdStr                                               << SEPAR;
822       result << crit.ThresholdID                                                << SEPAR;
823       result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.UnaryOp )) << SEPAR;
824       result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.BinaryOp ))<< SEPAR;
825       result << crit.Tolerance                                                  << SEPAR;
826       result << crit.TypeOfElement                                              << SEPAR;
827       result << crit.Precision                                                  << SEPAR;
828     }
829   }
830   return result;
831 }
832
833 //================================================================================
834 /*!
835  * \brief Restore the filter by the persistent string
836  */
837 //================================================================================
838
839 SMESH::Filter_ptr SMESH_GroupOnFilter_i::StringToFilter(const std::string& thePersistStr )
840 {
841   SMESH::Filter_var filter;
842
843   // divide thePersistStr into sub-strings
844   std::vector< std::string > strVec;
845   std::string::size_type from = 0, to;
846   while ( from < thePersistStr.size() )
847   {
848     to = thePersistStr.find( SEPAR, from );
849     if ( to == std::string::npos )
850       break;
851     strVec.push_back( thePersistStr.substr( from, to-from ));
852     from = to+1;
853   }
854   if ( strVec.empty() || strVec[0] == "0" )
855     return filter._retn();
856 #undef SEPAR
857
858   // create Criteria
859   int nbCrit = atoi( strVec[0].c_str() );
860   SMESH::Filter::Criteria_var criteria = new SMESH::Filter::Criteria;
861   criteria->length( nbCrit );
862   int nbStrPerCrit = ( strVec.size() - 1 ) / nbCrit;
863   for ( int i = 0; i < nbCrit; ++i )
864   {
865     SMESH::Filter::Criterion& crit = criteria[ i ];
866     int iStr = 1 + i * nbStrPerCrit;
867     crit.Type         = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
868     crit.Compare      = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
869     crit.Threshold    = atof(                       strVec[ iStr++ ].c_str() );
870     crit.ThresholdStr =                             strVec[ iStr++ ].c_str();
871     crit.ThresholdID  =                             strVec[ iStr++ ].c_str();
872     crit.UnaryOp      = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
873     crit.BinaryOp     = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
874     crit.Tolerance    = atof(                       strVec[ iStr++ ].c_str() );
875     crit.TypeOfElement= SMESH::ElementType( atoi(   strVec[ iStr++ ].c_str() ));
876     crit.Precision    = atoi(                       strVec[ iStr++ ].c_str() );
877   }
878
879   // create a filter
880   TPythonDump pd;
881   SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
882   filter = aFilterMgr->CreateFilter();
883   filter->SetCriteria( criteria.inout() );
884   
885   aFilterMgr->UnRegister();
886
887   pd << ""; // to avoid optimizing pd out
888
889   return filter._retn();
890 }
891
892 //================================================================================
893 /*!
894  * \brief Destructor of SMESH_GroupOnFilter_i
895  */
896 //================================================================================
897
898 SMESH_GroupOnFilter_i::~SMESH_GroupOnFilter_i()
899 {
900   if ( ! myFilter->_is_nil() )
901   {
902     SMESH::DownCast< SMESH::Filter_i* >( myFilter )->RemoveWaiter( this );
903     myFilter->UnRegister();
904   }
905 }
906
907 //================================================================================
908 /*!
909  * \brief Method calleds when a predicate of myFilter changes
910  */
911 //================================================================================
912
913 void SMESH_GroupOnFilter_i::PredicateChanged()
914 {
915   if ( myPreMeshInfo )
916     myPreMeshInfo->FullLoadFromFile();
917
918   if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
919     grDS->SetPredicate( GetPredicate( myFilter ));
920 }