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