1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 // File : SMESH_Group_i.cxx
25 // Author : Sergey ANIKIN, OCC
28 #include "SMESH_Group_i.hxx"
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"
42 #include CORBA_SERVER_HEADER(SMESH_Filter)
44 #include "utilities.h"
46 using namespace SMESH;
48 //=============================================================================
52 //=============================================================================
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 ),
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 );
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 )
75 //MESSAGE("SMESH_Group_i; this = "<<this );
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 )
84 //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
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 )
93 //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
96 //=============================================================================
100 //=============================================================================
102 SMESH_GroupBase_i::~SMESH_GroupBase_i()
104 MESSAGE("~SMESH_GroupBase_i; this = "<<this );
106 myMeshServant->removeGroup(myLocalID);
108 if ( myPreMeshInfo ) delete myPreMeshInfo; myPreMeshInfo = NULL;
111 //=======================================================================
112 //function : GetSmeshGroup
114 //=======================================================================
116 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
118 if ( myMeshServant ) {
119 ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
120 return aMesh.GetGroup(myLocalID);
125 //=======================================================================
126 //function : GetGroupDS
128 //=======================================================================
130 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
132 ::SMESH_Group* aGroup = GetSmeshGroup();
134 return aGroup->GetGroupDS();
138 //=============================================================================
142 //=============================================================================
144 void SMESH_GroupBase_i::SetName( const char* theName )
147 ::SMESH_Group* aGroup = GetSmeshGroup();
149 MESSAGE("can't set name of a vague group");
153 if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
154 return; // nothing to rename
156 aGroup->SetName(theName);
158 // Update group name in a study
159 SMESH_Gen_i* aGen = myMeshServant->GetGen();
160 SALOMEDS::Study_var aStudy = aGen->GetCurrentStudy();
161 SALOMEDS::SObject_var anSO = aGen->ObjectToSObject( aStudy, _this() );
162 if ( !anSO->_is_nil() )
164 aGen->SetName( anSO, theName );
166 // Update Python script
167 TPythonDump() << _this() << ".SetName( '" << theName << "' )";
171 //=============================================================================
175 //=============================================================================
177 char* SMESH_GroupBase_i::GetName()
179 ::SMESH_Group* aGroup = GetSmeshGroup();
181 return CORBA::string_dup (aGroup->GetName());
182 MESSAGE("get name of a vague group");
183 return CORBA::string_dup( "NO_NAME" );
186 //=============================================================================
190 //=============================================================================
192 SMESH::ElementType SMESH_GroupBase_i::GetType()
194 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
196 SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
197 SMESH::ElementType aType;
199 case SMDSAbs_Node: aType = SMESH::NODE; break;
200 case SMDSAbs_Edge: aType = SMESH::EDGE; break;
201 case SMDSAbs_Face: aType = SMESH::FACE; break;
202 case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
203 case SMDSAbs_0DElement: aType = SMESH::ELEM0D; break;
204 case SMDSAbs_Ball: aType = SMESH::BALL; break;
205 default: aType = SMESH::ALL; break;
209 MESSAGE("get type of a vague group");
214 //=============================================================================
218 //=============================================================================
220 CORBA::Long SMESH_GroupBase_i::Size()
223 return GetType() == SMESH::NODE ? myPreMeshInfo->NbNodes() : myPreMeshInfo->NbElements();
225 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
227 return aGroupDS->Extent();
228 MESSAGE("get size of a vague group");
232 //=============================================================================
236 //=============================================================================
238 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
243 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
245 return aGroupDS->IsEmpty();
246 MESSAGE("checking IsEmpty of a vague group");
250 //=============================================================================
254 //=============================================================================
256 void SMESH_Group_i::Clear()
259 myPreMeshInfo->FullLoadFromFile();
261 // Update Python script
262 TPythonDump() << _this() << ".Clear()";
265 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
270 MESSAGE("attempt to clear a vague group");
273 //=============================================================================
277 //=============================================================================
279 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
282 myPreMeshInfo->FullLoadFromFile();
284 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
286 return aGroupDS->Contains(theID);
287 MESSAGE("attempt to check contents of a vague group");
291 //=============================================================================
295 //=============================================================================
297 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
300 myPreMeshInfo->FullLoadFromFile();
302 // Update Python script
303 TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
305 // Add elements to the group
306 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
309 for (int i = 0; i < theIDs.length(); i++) {
310 int anID = (int) theIDs[i];
311 if (aGroupDS->Add(anID))
316 MESSAGE("attempt to add elements to a vague group");
320 //=============================================================================
324 //=============================================================================
326 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
329 myPreMeshInfo->FullLoadFromFile();
331 // Update Python script
332 TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
334 // Remove elements from the group
335 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
338 for (int i = 0; i < theIDs.length(); i++) {
339 int anID = (int) theIDs[i];
340 if (aGroupDS->Remove(anID))
345 MESSAGE("attempt to remove elements from a vague group");
349 //=============================================================================
353 //=============================================================================
355 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
358 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
359 SMESHDS_GroupBase* theGroupBase,
360 TFunChangeGroup theFun)
363 if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
364 SMESH::Controls::Filter::TIdSequence aSequence;
365 const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
366 SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
368 CORBA::Long i = 0, iEnd = aSequence.size();
370 if((aGroupDS->*theFun)(aSequence[i]))
379 AddByPredicate( SMESH::Predicate_ptr thePredicate )
382 myPreMeshInfo->FullLoadFromFile();
384 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
385 TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
386 return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
393 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
396 myPreMeshInfo->FullLoadFromFile();
398 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
399 TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
400 return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
405 CORBA::Long SMESH_Group_i::AddFrom( SMESH::SMESH_IDSource_ptr theSource )
408 myPreMeshInfo->FullLoadFromFile();
412 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
414 SMESH::long_array_var anIds;
415 SMESH::SMESH_GroupBase_var group = SMESH::SMESH_GroupBase::_narrow(theSource);
416 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow(theSource);
417 SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow(theSource);
418 SMESH::Filter_var filter = SMESH::Filter::_narrow(theSource);
419 if ( !group->_is_nil())
420 anIds = group->GetType()==GetType() ? theSource->GetIDs() : new SMESH::long_array();
421 else if ( !mesh->_is_nil() )
422 anIds = mesh->GetElementsByType( GetType() );
423 else if ( !submesh->_is_nil())
424 anIds = submesh->GetElementsByType( GetType() );
425 else if ( !filter->_is_nil() ) {
426 filter->SetMesh( GetMeshServant()->_this() );
427 anIds = filter->GetElementType()==GetType() ? theSource->GetIDs() : new SMESH::long_array();
430 anIds = theSource->GetIDs();
431 for ( int i = 0, total = anIds->length(); i < total; i++ ) {
432 if ( aGroupDS->Add((int)anIds[i]) ) nbAdd++;
436 // Update Python script
437 pd << "nbAdd = " << _this() << ".AddFrom( " << theSource << " )";
442 //=============================================================================
446 //=============================================================================
448 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
451 myPreMeshInfo->FullLoadFromFile();
453 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
455 return aGroupDS->GetID(theIndex);
456 MESSAGE("attempt to iterate on a vague group");
460 //=============================================================================
464 //=============================================================================
466 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
469 myPreMeshInfo->FullLoadFromFile();
471 SMESH::long_array_var aRes = new SMESH::long_array();
472 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
475 int aSize = aGroupDS->Extent();
477 for (int i = 0; i < aSize; i++)
478 aRes[i] = aGroupDS->GetID(i+1);
480 if ( 0 < aSize && aSize < 100 ) // for comfortable testing ;)
481 std::sort( &aRes[0], &aRes[0]+aSize );
483 MESSAGE("get list of IDs of a vague group");
489 //================================================================================
491 * \brief return nodes of elements pointered by iterator
493 //================================================================================
495 void getNodesOfElements(SMDS_ElemIteratorPtr elemIt,
496 set<const SMDS_MeshNode* >& nodes)
498 while ( elemIt->more() )
500 const SMDS_MeshElement* e = elemIt->next();
501 nodes.insert( e->begin_nodes(), e->end_nodes() );
506 //================================================================================
508 * \brief return the number of nodes of cells included to the group
510 //================================================================================
512 CORBA::Long SMESH_GroupBase_i::GetNumberOfNodes()
514 if ( GetType() == SMESH::NODE )
518 myPreMeshInfo->FullLoadFromFile();
520 if ( SMESHDS_GroupBase* g = GetGroupDS())
522 if ( myNbNodes < 0 || g->GetTic() != myGroupDSTic )
524 set<const SMDS_MeshNode* > nodes;
525 getNodesOfElements( g->GetElements(), nodes );
526 myNbNodes = nodes.size();
527 myGroupDSTic = g->GetTic();
533 //================================================================================
535 * \brief Return true if GetNumberOfNodes() won't take a long time for computation
537 //================================================================================
539 CORBA::Boolean SMESH_GroupBase_i::IsNodeInfoAvailable()
541 if ( GetType() == SMESH::NODE/* || Size() < 100000 */)
545 if ( SMESHDS_GroupBase* g = GetGroupDS())
546 return ( myNbNodes > -1 && g->GetTic() == myGroupDSTic);
550 //================================================================================
552 * \brief Return IDs of nodes of cells included to the group
554 //================================================================================
556 SMESH::long_array* SMESH_GroupBase_i::GetNodeIDs()
558 if ( GetType() == SMESH::NODE )
559 return GetListOfID();
562 myPreMeshInfo->FullLoadFromFile();
564 SMESH::long_array_var aRes = new SMESH::long_array();
565 if ( SMESHDS_GroupBase* g = GetGroupDS())
567 set<const SMDS_MeshNode* > nodes;
568 getNodesOfElements( g->GetElements(), nodes );
569 aRes->length( nodes.size() );
570 set<const SMDS_MeshNode*>::iterator nIt = nodes.begin(), nEnd = nodes.end();
571 for ( int i = 0; nIt != nEnd; ++nIt, ++i )
572 aRes[i] = (*nIt)->GetID();
577 //=============================================================================
581 //=============================================================================
582 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
584 SMESH::SMESH_Mesh_var aMesh;
586 aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
587 return aMesh._retn();
590 //=======================================================================
591 //function : GetShape
593 //=======================================================================
595 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
597 GEOM::GEOM_Object_var aGeomObj;
598 SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
600 SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
601 aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
603 return aGeomObj._retn();
606 //=============================================================================
610 //=============================================================================
611 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
613 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
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();
624 MESSAGE("get color of a group");
625 return SALOMEDS::Color();
628 //=============================================================================
632 //=============================================================================
633 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
635 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
638 Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
639 Quantity_Color oldColor = aGroupDS->GetColor();
640 if ( oldColor != aQColor )
642 aGroupDS->SetColor(aQColor);
643 TPythonDump()<<_this()<<".SetColor( SALOMEDS.Color( "<<color.R<<", "<<color.G<<", "<<color.B<<" ))";
648 //=============================================================================
652 //=============================================================================
653 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
655 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
657 return aGroupDS->GetColorGroup();
658 MESSAGE("get color number of a group");
662 //=============================================================================
666 //=============================================================================
667 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
669 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
672 aGroupDS->SetColorGroup(color);
673 TPythonDump()<<_this()<<".SetColorNumber( "<<color<<" )";
675 MESSAGE("set color number of a group");
679 //=============================================================================
681 * Returns statistic of mesh elements
682 * Result array of number enityties
683 * Inherited from SMESH_IDSource
685 //=============================================================================
687 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
690 return myPreMeshInfo->GetMeshInfo();
692 SMESH::long_array_var aRes = new SMESH::long_array();
693 aRes->length(SMESH::Entity_Last);
694 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
697 if ( SMESHDS_GroupBase* g = GetGroupDS())
699 if ( g->GetType() == SMDSAbs_Node || ( myNbNodes > -1 && g->GetTic() == myGroupDSTic))
700 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
702 if ( g->GetType() != SMDSAbs_Node )
703 SMESH_Mesh_i::CollectMeshInfo( g->GetElements(), aRes);
709 //=======================================================================
711 //purpose : Returns ids of members
712 //=======================================================================
714 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
716 return GetListOfID();
719 //=======================================================================
720 //function : GetTypes
721 //purpose : Returns types of elements it contains
722 //=======================================================================
724 SMESH::array_of_ElementType* SMESH_GroupBase_i::GetTypes()
726 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
730 types[0] = GetType();
732 return types._retn();
735 //=======================================================================
736 //function : IsMeshInfoCorrect
737 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
738 // * happen if mesh data is not yet fully loaded from the file of study.
739 //=======================================================================
741 bool SMESH_GroupBase_i::IsMeshInfoCorrect()
743 return myPreMeshInfo ? myPreMeshInfo->IsMeshInfoCorrect() : true;
746 //================================================================================
748 * \brief Retrieves the predicate from the filter
750 //================================================================================
752 SMESH_PredicatePtr SMESH_GroupOnFilter_i::GetPredicate( SMESH::Filter_ptr filter )
754 SMESH_PredicatePtr predicate;
756 if ( SMESH::Filter_i* filt_i = SMESH::DownCast< SMESH::Filter_i* >( filter ))
757 if ( SMESH::Predicate_i* predic_i= filt_i->GetPredicate_i() )
758 predicate = predic_i->GetPredicate();
763 //================================================================================
765 * \brief Sets the filter defining group contents
767 //================================================================================
769 void SMESH_GroupOnFilter_i::SetFilter(SMESH::Filter_ptr theFilter)
772 myPreMeshInfo->FullLoadFromFile();
774 if ( ! myFilter->_is_nil() )
775 myFilter->UnRegister();
777 myFilter = SMESH::Filter::_duplicate( theFilter );
779 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
780 grDS->SetPredicate( GetPredicate( myFilter ));
782 TPythonDump()<< _this() <<".SetFilter( "<<theFilter<<" )";
786 myFilter->SetMesh( SMESH::SMESH_Mesh::_nil() ); // to UnRegister() the mesh
787 myFilter->Register();
788 SMESH::DownCast< SMESH::Filter_i* >( myFilter )->AddWaiter( this );
792 //================================================================================
794 * \brief Returns the filter defining group contents
796 //================================================================================
798 SMESH::Filter_ptr SMESH_GroupOnFilter_i::GetFilter()
800 SMESH::Filter_var f = myFilter;
801 TPythonDump() << f << " = " << _this() << ".GetFilter()";
805 //=======================================================================
807 //purpose : Returns ids of members
808 //=======================================================================
810 SMESH::long_array* SMESH_GroupOnFilter_i::GetListOfID()
813 myPreMeshInfo->FullLoadFromFile();
815 SMESH::long_array_var aRes = new SMESH::long_array();
816 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
817 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
819 const SMDS_MeshInfo& meshInfo = aGroupDS->GetMesh()->GetMeshInfo();
820 aRes->length( meshInfo.NbElements( aGroupDS->GetType() ));
821 if ( aRes->length() ) // else aRes[0] -> SIGSEGV
822 aRes->length( grDS->GetElementIds( &aRes[0] ));
824 if ( 0 < aRes->length() && aRes->length() < 100 ) // for comfortable testing ;)
825 std::sort( &aRes[0], &aRes[0] + aRes->length() );
827 MESSAGE("get list of IDs of a vague group");
831 //=============================================================================
833 * Returns statistic of mesh elements
834 * Result array of number enityties
835 * Inherited from SMESH_IDSource
837 //=============================================================================
839 SMESH::long_array* SMESH_GroupOnFilter_i::GetMeshInfo()
842 return myPreMeshInfo->GetMeshInfo();
844 SMESH::long_array_var aRes = new SMESH::long_array();
845 aRes->length(SMESH::Entity_Last);
846 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
849 if ( SMESHDS_GroupBase* g = GetGroupDS())
851 if ( g->GetType() == SMDSAbs_Node || ( myNbNodes > -1 && g->GetTic() == myGroupDSTic))
852 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
854 if ( g->GetType() != SMDSAbs_Node )
856 vector< int > nbElems = static_cast< SMESHDS_GroupOnFilter* >( g )->GetMeshInfo();
857 for ( size_t i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
858 if ( i < nbElems.size() )
859 aRes[i] = nbElems[ i ];
868 //================================================================================
870 * \brief Return a string to be used to store group definition in the study
872 //================================================================================
874 std::string SMESH_GroupOnFilter_i::FilterToString() const
876 SMESH_Comment result;
877 SMESH::Filter::Criteria_var criteria;
878 if ( !myFilter->_is_nil() && myFilter->GetCriteria( criteria.out() ))
880 result << criteria->length() << SEPAR;
881 for ( unsigned i = 0; i < criteria->length(); ++i )
883 // write FunctorType as string but not as number to assure correct
884 // persistence if enum FunctorType is modified by insertion in the middle
885 SMESH::Filter::Criterion& crit = criteria[ i ];
886 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Type )) << SEPAR;
887 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Compare )) << SEPAR;
888 result << crit.Threshold << SEPAR;
889 result << crit.ThresholdStr << SEPAR;
890 result << crit.ThresholdID << SEPAR;
891 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.UnaryOp )) << SEPAR;
892 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.BinaryOp ))<< SEPAR;
893 result << crit.Tolerance << SEPAR;
894 result << crit.TypeOfElement << SEPAR;
895 result << crit.Precision << SEPAR;
901 //================================================================================
903 * \brief Restore the filter by the persistent string
905 //================================================================================
907 SMESH::Filter_ptr SMESH_GroupOnFilter_i::StringToFilter(const std::string& thePersistStr )
909 SMESH::Filter_var filter;
911 // divide thePersistStr into sub-strings
912 std::vector< std::string > strVec;
913 std::string::size_type from = 0, to;
914 while ( from < thePersistStr.size() )
916 to = thePersistStr.find( SEPAR, from );
917 if ( to == std::string::npos )
919 strVec.push_back( thePersistStr.substr( from, to-from ));
922 if ( strVec.empty() || strVec[0] == "0" )
923 return filter._retn();
927 int nbCrit = atoi( strVec[0].c_str() );
928 SMESH::Filter::Criteria_var criteria = new SMESH::Filter::Criteria;
929 criteria->length( nbCrit );
930 int nbStrPerCrit = ( strVec.size() - 1 ) / nbCrit;
931 for ( int i = 0; i < nbCrit; ++i )
933 SMESH::Filter::Criterion& crit = criteria[ i ];
934 int iStr = 1 + i * nbStrPerCrit;
935 crit.Type = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
936 crit.Compare = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
937 crit.Threshold = atof( strVec[ iStr++ ].c_str() );
938 crit.ThresholdStr = strVec[ iStr++ ].c_str();
939 crit.ThresholdID = strVec[ iStr++ ].c_str();
940 crit.UnaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
941 crit.BinaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
942 crit.Tolerance = atof( strVec[ iStr++ ].c_str() );
943 crit.TypeOfElement= SMESH::ElementType( atoi( strVec[ iStr++ ].c_str() ));
944 crit.Precision = atoi( strVec[ iStr++ ].c_str() );
949 SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
950 filter = aFilterMgr->CreateFilter();
951 filter->SetCriteria( criteria.inout() );
953 aFilterMgr->UnRegister();
955 pd << ""; // to avoid optimizing pd out
957 return filter._retn();
960 //================================================================================
962 * \brief Destructor of SMESH_GroupOnFilter_i
964 //================================================================================
966 SMESH_GroupOnFilter_i::~SMESH_GroupOnFilter_i()
968 if ( ! myFilter->_is_nil() )
970 SMESH::DownCast< SMESH::Filter_i* >( myFilter )->RemoveWaiter( this );
971 myFilter->UnRegister();
975 //================================================================================
977 * \brief Method calleds when a predicate of myFilter changes
979 //================================================================================
981 void SMESH_GroupOnFilter_i::PredicateChanged()
984 myPreMeshInfo->FullLoadFromFile();
986 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
987 grDS->SetPredicate( GetPredicate( myFilter ));