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