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