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