1 // Copyright (C) 2007-2013 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 if ( myPreMeshInfo ) delete myPreMeshInfo; myPreMeshInfo = NULL;
107 //=======================================================================
108 //function : GetSmeshGroup
110 //=======================================================================
112 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
114 if ( myMeshServant ) {
115 ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
116 return aMesh.GetGroup(myLocalID);
121 //=======================================================================
122 //function : GetGroupDS
124 //=======================================================================
126 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
128 ::SMESH_Group* aGroup = GetSmeshGroup();
130 return aGroup->GetGroupDS();
134 //=============================================================================
138 //=============================================================================
140 void SMESH_GroupBase_i::SetName( const char* theName )
143 ::SMESH_Group* aGroup = GetSmeshGroup();
145 MESSAGE("can't set name of a vague group");
149 if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
150 return; // nothing to rename
152 aGroup->SetName(theName);
154 // Update group name in a study
155 SMESH_Gen_i* aGen = myMeshServant->GetGen();
156 SALOMEDS::Study_var aStudy = aGen->GetCurrentStudy();
157 SMESH::SMESH_GroupBase_var aGrp = _this();
158 SALOMEDS::SObject_var anSO = aGen->ObjectToSObject( aStudy, aGrp );
159 if ( !anSO->_is_nil() )
161 aGen->SetName( anSO, theName );
163 // Update Python script
164 TPythonDump() << anSO << ".SetName( '" << theName << "' )";
168 //=============================================================================
172 //=============================================================================
174 char* SMESH_GroupBase_i::GetName()
176 ::SMESH_Group* aGroup = GetSmeshGroup();
178 return CORBA::string_dup (aGroup->GetName());
179 MESSAGE("get name of a vague group");
180 return CORBA::string_dup( "NO_NAME" );
183 //=============================================================================
187 //=============================================================================
189 SMESH::ElementType SMESH_GroupBase_i::GetType()
191 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
193 SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
194 SMESH::ElementType aType;
196 case SMDSAbs_Node: aType = SMESH::NODE; break;
197 case SMDSAbs_Edge: aType = SMESH::EDGE; break;
198 case SMDSAbs_Face: aType = SMESH::FACE; break;
199 case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
200 case SMDSAbs_0DElement: aType = SMESH::ELEM0D; break;
201 case SMDSAbs_Ball: aType = SMESH::BALL; break;
202 default: aType = SMESH::ALL; break;
206 MESSAGE("get type of a vague group");
211 //=============================================================================
215 //=============================================================================
217 CORBA::Long SMESH_GroupBase_i::Size()
220 return GetType() == SMESH::NODE ? myPreMeshInfo->NbNodes() : myPreMeshInfo->NbElements();
222 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
224 return aGroupDS->Extent();
225 MESSAGE("get size of a vague group");
229 //=============================================================================
233 //=============================================================================
235 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
240 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
242 return aGroupDS->IsEmpty();
243 MESSAGE("checking IsEmpty of a vague group");
247 //=============================================================================
251 //=============================================================================
253 void SMESH_Group_i::Clear()
256 myPreMeshInfo->FullLoadFromFile();
258 // Update Python script
259 TPythonDump() << SMESH::SMESH_Group_var(_this()) << ".Clear()";
262 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
267 MESSAGE("attempt to clear a vague group");
270 //=============================================================================
274 //=============================================================================
276 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
279 myPreMeshInfo->FullLoadFromFile();
281 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
283 return aGroupDS->Contains(theID);
284 MESSAGE("attempt to check contents of a vague group");
288 //=============================================================================
292 //=============================================================================
294 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
297 myPreMeshInfo->FullLoadFromFile();
299 // Update Python script
300 TPythonDump() << "nbAdd = " << SMESH::SMESH_Group_var(_this()) << ".Add( " << theIDs << " )";
302 // Add elements to the group
303 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
306 for (int i = 0; i < theIDs.length(); i++) {
307 int anID = (int) theIDs[i];
308 if (aGroupDS->Add(anID))
313 MESSAGE("attempt to add elements to a vague group");
317 //=============================================================================
321 //=============================================================================
323 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
326 myPreMeshInfo->FullLoadFromFile();
328 // Update Python script
329 TPythonDump() << "nbDel = " << SMESH::SMESH_Group_var(_this())
330 << ".Remove( " << theIDs << " )";
332 // Remove elements from the group
333 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
336 for (int i = 0; i < theIDs.length(); i++) {
337 int anID = (int) theIDs[i];
338 if (aGroupDS->Remove(anID))
343 MESSAGE("attempt to remove elements from a vague group");
347 //=============================================================================
351 //=============================================================================
353 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
356 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
357 SMESHDS_GroupBase* theGroupBase,
358 TFunChangeGroup theFun)
361 if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
362 SMESH::Controls::Filter::TIdSequence aSequence;
363 const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
364 SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
366 CORBA::Long i = 0, iEnd = aSequence.size();
368 if((aGroupDS->*theFun)(aSequence[i]))
377 AddByPredicate( SMESH::Predicate_ptr thePredicate )
380 myPreMeshInfo->FullLoadFromFile();
382 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
383 TPythonDump() << SMESH::SMESH_Group_var(_this())
384 << ".AddByPredicate( " << aPredicate << " )";
385 return ChangeByPredicate( aPredicate, GetGroupDS(), &SMESHDS_Group::Add );
392 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
395 myPreMeshInfo->FullLoadFromFile();
397 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
398 TPythonDump() << SMESH::SMESH_Group_var(_this())
399 << ".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();
411 long prevNb = Size();
412 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
414 SMDS_ElemIteratorPtr elemIt = SMESH_Mesh_i::GetElements( theSource, GetType() );
415 while ( elemIt->more() )
416 aGroupDS->SMDSGroup().Add( elemIt->next() );
419 // Update Python script
420 pd << "nbAdd = " << SMESH::SMESH_Group_var(_this()) << ".AddFrom( " << theSource << " )";
422 return prevNb - Size();
425 //=============================================================================
429 //=============================================================================
431 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
434 myPreMeshInfo->FullLoadFromFile();
436 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
438 return aGroupDS->GetID(theIndex);
439 MESSAGE("attempt to iterate on a vague group");
443 //=============================================================================
447 //=============================================================================
449 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
452 myPreMeshInfo->FullLoadFromFile();
454 SMESH::long_array_var aRes = new SMESH::long_array();
455 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
458 int aSize = aGroupDS->Extent();
460 for (int i = 0; i < aSize; i++)
461 aRes[i] = aGroupDS->GetID(i+1);
463 if ( 0 < aSize && aSize < 100 ) // for comfortable testing ;)
464 std::sort( &aRes[0], &aRes[0]+aSize );
466 MESSAGE("get list of IDs of a vague group");
472 //================================================================================
474 * \brief return nodes of elements pointered by iterator
476 //================================================================================
478 void getNodesOfElements(SMDS_ElemIteratorPtr elemIt,
479 set<const SMDS_MeshNode* >& nodes)
481 while ( elemIt->more() )
483 const SMDS_MeshElement* e = elemIt->next();
484 nodes.insert( e->begin_nodes(), e->end_nodes() );
489 //================================================================================
491 * \brief return the number of nodes of cells included to the group
493 //================================================================================
495 CORBA::Long SMESH_GroupBase_i::GetNumberOfNodes()
497 if ( GetType() == SMESH::NODE )
501 myPreMeshInfo->FullLoadFromFile();
503 if ( SMESHDS_GroupBase* g = GetGroupDS())
505 if ( myNbNodes < 0 || g->GetTic() != myGroupDSTic )
507 set<const SMDS_MeshNode* > nodes;
508 getNodesOfElements( g->GetElements(), nodes );
509 myNbNodes = nodes.size();
510 myGroupDSTic = g->GetTic();
516 //================================================================================
518 * \brief Return true if GetNumberOfNodes() won't take a long time for computation
520 //================================================================================
522 CORBA::Boolean SMESH_GroupBase_i::IsNodeInfoAvailable()
524 if ( GetType() == SMESH::NODE/* || Size() < 100000 */)
528 if ( SMESHDS_GroupBase* g = GetGroupDS())
529 return ( myNbNodes > -1 && g->GetTic() == myGroupDSTic);
533 //================================================================================
535 * \brief Return IDs of nodes of cells included to the group
537 //================================================================================
539 SMESH::long_array* SMESH_GroupBase_i::GetNodeIDs()
541 if ( GetType() == SMESH::NODE )
542 return GetListOfID();
545 myPreMeshInfo->FullLoadFromFile();
547 SMESH::long_array_var aRes = new SMESH::long_array();
548 if ( SMESHDS_GroupBase* g = GetGroupDS())
550 set<const SMDS_MeshNode* > nodes;
551 getNodesOfElements( g->GetElements(), nodes );
552 aRes->length( nodes.size() );
553 set<const SMDS_MeshNode*>::iterator nIt = nodes.begin(), nEnd = nodes.end();
554 for ( int i = 0; nIt != nEnd; ++nIt, ++i )
555 aRes[i] = (*nIt)->GetID();
560 //=============================================================================
564 //=============================================================================
565 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
567 SMESH::SMESH_Mesh_var aMesh;
569 aMesh = myMeshServant->_this();
570 return aMesh._retn();
573 //=======================================================================
574 //function : GetShape
576 //=======================================================================
578 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
580 GEOM::GEOM_Object_var aGeomObj;
581 SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
583 SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
584 aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
586 return aGeomObj._retn();
589 //=============================================================================
593 //=============================================================================
594 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
596 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
599 Quantity_Color aQColor = aGroupDS->GetColor();
600 SALOMEDS::Color aColor;
601 aColor.R = aQColor.Red();
602 aColor.G = aQColor.Green();
603 aColor.B = aQColor.Blue();
607 MESSAGE("get color of a group");
608 return SALOMEDS::Color();
611 //=============================================================================
615 //=============================================================================
616 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
618 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
621 Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
622 Quantity_Color oldColor = aGroupDS->GetColor();
623 if ( oldColor != aQColor )
625 aGroupDS->SetColor(aQColor);
626 TPythonDump()<< SMESH::SMESH_GroupBase_var(_this())
627 << ".SetColor( SALOMEDS.Color( "
628 <<color.R<<", "<<color.G<<", "<<color.B<<" ))";
633 //=============================================================================
637 //=============================================================================
638 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
640 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
642 return aGroupDS->GetColorGroup();
643 MESSAGE("get color number of a group");
647 //=============================================================================
651 //=============================================================================
652 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
654 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
657 aGroupDS->SetColorGroup(color);
658 TPythonDump()<<SMESH::SMESH_GroupBase_var(_this())<<".SetColorNumber( "<<color<<" )";
660 MESSAGE("set color number of a group");
664 //=============================================================================
666 * Returns number of mesh elements of each \a SMESH::EntityType
667 * Result array of number of elements per \a SMESH::EntityType
668 * Inherited from SMESH_IDSource
670 //=============================================================================
672 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
675 return myPreMeshInfo->GetMeshInfo();
677 SMESH::long_array_var aRes = new SMESH::long_array();
678 aRes->length(SMESH::Entity_Last);
679 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
682 if ( SMESHDS_GroupBase* g = GetGroupDS())
684 if ( g->GetType() == SMDSAbs_Node || ( myNbNodes > -1 && g->GetTic() == myGroupDSTic))
685 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
687 if ( g->GetType() != SMDSAbs_Node )
688 SMESH_Mesh_i::CollectMeshInfo( g->GetElements(), aRes);
694 //=============================================================================
696 * Returns number of mesh elements of each \a ElementType
698 //=============================================================================
700 SMESH::long_array* SMESH_GroupBase_i::GetNbElementsByType()
702 SMESH::long_array_var aRes = new SMESH::long_array();
703 aRes->length(SMESH::NB_ELEMENT_TYPES);
704 for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
708 aRes[ GetType() ] = myPreMeshInfo->NbElements( SMDSAbs_ElementType( GetType() ));
710 aRes[ GetType() ] = Size();
715 //=======================================================================
717 //purpose : Returns ids of members
718 //=======================================================================
720 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
722 return GetListOfID();
725 //=======================================================================
726 //function : GetTypes
727 //purpose : Returns types of elements it contains
728 //=======================================================================
730 SMESH::array_of_ElementType* SMESH_GroupBase_i::GetTypes()
732 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
736 types[0] = GetType();
738 return types._retn();
741 //=======================================================================
742 //function : IsMeshInfoCorrect
743 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
744 // * happen if mesh data is not yet fully loaded from the file of study.
745 //=======================================================================
747 bool SMESH_GroupBase_i::IsMeshInfoCorrect()
749 return myPreMeshInfo ? myPreMeshInfo->IsMeshInfoCorrect() : true;
752 //================================================================================
754 * \brief Retrieves the predicate from the filter
756 //================================================================================
758 SMESH_PredicatePtr SMESH_GroupOnFilter_i::GetPredicate( SMESH::Filter_ptr filter )
760 SMESH_PredicatePtr predicate;
762 if ( SMESH::Filter_i* filt_i = SMESH::DownCast< SMESH::Filter_i* >( filter ))
763 if ( SMESH::Predicate_i* predic_i= filt_i->GetPredicate_i() )
764 predicate = predic_i->GetPredicate();
769 //================================================================================
771 * \brief Sets the filter defining group contents
773 //================================================================================
775 void SMESH_GroupOnFilter_i::SetFilter(SMESH::Filter_ptr theFilter)
778 myPreMeshInfo->FullLoadFromFile();
780 if ( ! myFilter->_is_nil() )
781 myFilter->UnRegister();
783 myFilter = SMESH::Filter::_duplicate( theFilter );
785 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
786 grDS->SetPredicate( GetPredicate( myFilter ));
788 TPythonDump()<< SMESH::SMESH_GroupOnFilter_var(_this()) <<".SetFilter( "<<theFilter<<" )";
792 myFilter->SetMesh( SMESH::SMESH_Mesh::_nil() ); // to UnRegister() the mesh
793 myFilter->Register();
794 SMESH::DownCast< SMESH::Filter_i* >( myFilter )->AddWaiter( this );
798 //================================================================================
800 * \brief Returns the filter defining group contents
802 //================================================================================
804 SMESH::Filter_ptr SMESH_GroupOnFilter_i::GetFilter()
806 SMESH::Filter_var f = myFilter;
807 TPythonDump() << f << " = " << SMESH::SMESH_GroupOnFilter_var(_this()) << ".GetFilter()";
811 //=======================================================================
813 //purpose : Returns ids of members
814 //=======================================================================
816 SMESH::long_array* SMESH_GroupOnFilter_i::GetListOfID()
819 myPreMeshInfo->FullLoadFromFile();
821 SMESH::long_array_var aRes = new SMESH::long_array();
822 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
823 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
825 const SMDS_MeshInfo& meshInfo = aGroupDS->GetMesh()->GetMeshInfo();
826 aRes->length( meshInfo.NbElements( aGroupDS->GetType() ));
827 if ( aRes->length() ) // else aRes[0] -> SIGSEGV
828 aRes->length( grDS->GetElementIds( &aRes[0] ));
830 if ( 0 < aRes->length() && aRes->length() < 100 ) // for comfortable testing ;)
831 std::sort( &aRes[0], &aRes[0] + aRes->length() );
833 MESSAGE("get list of IDs of a vague group");
837 //=============================================================================
839 * Returns statistic of mesh elements
840 * Result array of number enityties
841 * Inherited from SMESH_IDSource
843 //=============================================================================
845 SMESH::long_array* SMESH_GroupOnFilter_i::GetMeshInfo()
848 return myPreMeshInfo->GetMeshInfo();
850 SMESH::long_array_var aRes = new SMESH::long_array();
851 aRes->length(SMESH::Entity_Last);
852 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
855 if ( SMESHDS_GroupBase* g = GetGroupDS())
857 if ( g->GetType() == SMDSAbs_Node || ( myNbNodes > -1 && g->GetTic() == myGroupDSTic))
858 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
860 if ( g->GetType() != SMDSAbs_Node )
862 vector< int > nbElems = static_cast< SMESHDS_GroupOnFilter* >( g )->GetMeshInfo();
863 for ( size_t i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
864 if ( i < nbElems.size() )
865 aRes[i] = nbElems[ i ];
874 //================================================================================
876 * \brief Return a string to be used to store group definition in the study
878 //================================================================================
880 std::string SMESH_GroupOnFilter_i::FilterToString() const
882 SMESH_Comment result;
883 SMESH::Filter::Criteria_var criteria;
884 if ( !myFilter->_is_nil() && myFilter->GetCriteria( criteria.out() ))
886 result << criteria->length() << SEPAR;
887 for ( unsigned i = 0; i < criteria->length(); ++i )
889 // write FunctorType as string but not as number to assure correct
890 // persistence if enum FunctorType is modified by insertion in the middle
891 SMESH::Filter::Criterion& crit = criteria[ i ];
892 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Type )) << SEPAR;
893 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Compare )) << SEPAR;
894 result << crit.Threshold << SEPAR;
895 result << crit.ThresholdStr << SEPAR;
896 result << crit.ThresholdID << SEPAR;
897 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.UnaryOp )) << SEPAR;
898 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.BinaryOp ))<< SEPAR;
899 result << crit.Tolerance << SEPAR;
900 result << crit.TypeOfElement << SEPAR;
901 result << crit.Precision << SEPAR;
907 //================================================================================
909 * \brief Restore the filter by the persistent string
911 //================================================================================
913 SMESH::Filter_ptr SMESH_GroupOnFilter_i::StringToFilter(const std::string& thePersistStr )
915 SMESH::Filter_var filter;
917 // divide thePersistStr into sub-strings
918 std::vector< std::string > strVec;
919 std::string::size_type from = 0, to;
920 while ( from < thePersistStr.size() )
922 to = thePersistStr.find( SEPAR, from );
923 if ( to == std::string::npos )
925 strVec.push_back( thePersistStr.substr( from, to-from ));
928 if ( strVec.empty() || strVec[0] == "0" )
929 return filter._retn();
933 int nbCrit = atoi( strVec[0].c_str() );
934 SMESH::Filter::Criteria_var criteria = new SMESH::Filter::Criteria;
935 criteria->length( nbCrit );
936 int nbStrPerCrit = ( strVec.size() - 1 ) / nbCrit;
937 for ( int i = 0; i < nbCrit; ++i )
939 SMESH::Filter::Criterion& crit = criteria[ i ];
940 int iStr = 1 + i * nbStrPerCrit;
941 crit.Type = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
942 crit.Compare = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
943 crit.Threshold = atof( strVec[ iStr++ ].c_str() );
944 crit.ThresholdStr = strVec[ iStr++ ].c_str();
945 crit.ThresholdID = strVec[ iStr++ ].c_str();
946 crit.UnaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
947 crit.BinaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
948 crit.Tolerance = atof( strVec[ iStr++ ].c_str() );
949 crit.TypeOfElement= SMESH::ElementType( atoi( strVec[ iStr++ ].c_str() ));
950 crit.Precision = atoi( strVec[ iStr++ ].c_str() );
955 SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
956 filter = aFilterMgr->CreateFilter();
957 filter->SetCriteria( criteria.inout() );
959 aFilterMgr->UnRegister();
961 pd << ""; // to avoid optimizing pd out
963 return filter._retn();
966 //================================================================================
968 * \brief Destructor of SMESH_GroupOnFilter_i
970 //================================================================================
972 SMESH_GroupOnFilter_i::~SMESH_GroupOnFilter_i()
974 if ( ! myFilter->_is_nil() )
976 SMESH::DownCast< SMESH::Filter_i* >( myFilter )->RemoveWaiter( this );
977 myFilter->UnRegister();
981 //================================================================================
983 * \brief Method calleds when a predicate of myFilter changes
985 //================================================================================
987 void SMESH_GroupOnFilter_i::PredicateChanged()
990 myPreMeshInfo->FullLoadFromFile();
992 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
993 grDS->SetPredicate( GetPredicate( myFilter ));