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