1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 ),
61 myMeshServant( theMeshServant ),
62 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 //=============================================================================
249 * Returns \c true if \c this group depends on the \a other via
250 * FT_BelongToMeshGroup predicate or vice versa
252 //=============================================================================
254 bool SMESH_GroupBase_i::IsInDependency( SMESH::SMESH_GroupBase_ptr other )
256 if ( NotifyerAndWaiter* nw = SMESH::DownCast< NotifyerAndWaiter* >( other ))
257 return ( nw->ContainModifWaiter( this ) || this->ContainModifWaiter( nw ));
262 //=============================================================================
266 //=============================================================================
268 void SMESH_Group_i::Clear()
271 myPreMeshInfo->FullLoadFromFile();
273 // Update Python script
274 TPythonDump() << SMESH::SMESH_Group_var(_this()) << ".Clear()";
277 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
282 Modified(); // notify dependent Filter with FT_BelongToMeshGroup criterion
284 MESSAGE("attempt to clear a vague group");
287 //=============================================================================
291 //=============================================================================
293 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
296 myPreMeshInfo->FullLoadFromFile();
298 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
300 return aGroupDS->Contains(theID);
301 MESSAGE("attempt to check contents of a vague group");
305 //=============================================================================
309 //=============================================================================
311 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
314 myPreMeshInfo->FullLoadFromFile();
316 // Update Python script
317 TPythonDump() << "nbAdd = " << SMESH::SMESH_Group_var(_this()) << ".Add( " << theIDs << " )";
319 // Add elements to the group
320 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
323 for ( CORBA::ULong i = 0; i < theIDs.length(); i++) {
324 int anID = (int) theIDs[i];
325 if ( aGroupDS->Add( anID ))
329 Modified(); // notify dependent Filter with FT_BelongToMeshGroup criterion
332 MESSAGE("attempt to add elements to a vague group");
336 //=============================================================================
340 //=============================================================================
342 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
345 myPreMeshInfo->FullLoadFromFile();
347 // Update Python script
348 TPythonDump() << "nbDel = " << SMESH::SMESH_Group_var(_this())
349 << ".Remove( " << theIDs << " )";
351 // Remove elements from the group
352 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
355 for ( CORBA::ULong i = 0; i < theIDs.length(); i++ ) {
356 int anID = (int) theIDs[i];
357 if ( aGroupDS->Remove( anID ))
361 Modified(); // notify dependent Filter with FT_BelongToMeshGroup criterion
364 MESSAGE("attempt to remove elements from a vague group");
368 //=============================================================================
372 //=============================================================================
374 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
377 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
378 SMESHDS_GroupBase* theGroupBase,
379 NotifyerAndWaiter* theGroupImpl,
380 TFunChangeGroup theFun)
383 if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
384 SMESH::Controls::Filter::TIdSequence aSequence;
385 const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
386 SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
388 CORBA::Long i = 0, iEnd = aSequence.size();
390 if((aGroupDS->*theFun)(aSequence[i]))
393 theGroupImpl->Modified();
401 AddByPredicate( SMESH::Predicate_ptr thePredicate )
404 myPreMeshInfo->FullLoadFromFile();
406 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
407 TPythonDump() << SMESH::SMESH_Group_var(_this())
408 << ".AddByPredicate( " << aPredicate << " )";
409 return ChangeByPredicate( aPredicate, GetGroupDS(), this, &SMESHDS_Group::Add );
416 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
419 myPreMeshInfo->FullLoadFromFile();
421 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
422 TPythonDump() << SMESH::SMESH_Group_var(_this())
423 << ".RemoveByPredicate( " << aPredicate << " )";
424 return ChangeByPredicate(aPredicate,GetGroupDS(),this, &SMESHDS_Group::Remove);
429 CORBA::Long SMESH_Group_i::AddFrom( SMESH::SMESH_IDSource_ptr theSource )
432 myPreMeshInfo->FullLoadFromFile();
435 long prevNb = Size();
436 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
438 if ( SMDS_ElemIteratorPtr elemIt = SMESH_Mesh_i::GetElements( theSource, GetType() ))
439 while ( elemIt->more() )
440 aGroupDS->SMDSGroup().Add( elemIt->next() );
443 // Update Python script
444 pd << "nbAdd = " << SMESH::SMESH_Group_var(_this()) << ".AddFrom( " << theSource << " )";
446 if ( prevNb != Size() )
447 Modified(); // notify dependent Filter with FT_BelongToMeshGroup criterion
449 return Size() - prevNb;
452 //=============================================================================
456 //=============================================================================
458 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
461 myPreMeshInfo->FullLoadFromFile();
463 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
465 return aGroupDS->GetID(theIndex);
466 MESSAGE("attempt to iterate on a vague group");
470 //=============================================================================
474 //=============================================================================
476 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
479 myPreMeshInfo->FullLoadFromFile();
481 SMESH::long_array_var aRes = new SMESH::long_array();
482 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
485 int aSize = aGroupDS->Extent();
487 for (int i = 0; i < aSize; i++)
488 aRes[i] = aGroupDS->GetID(i+1);
490 if ( 0 < aSize && aSize < 100 ) // for comfortable testing ;)
491 std::sort( &aRes[0], &aRes[0]+aSize );
493 MESSAGE("get list of IDs of a vague group");
499 //================================================================================
501 * \brief return nodes of elements pointered by iterator
503 //================================================================================
505 void getNodesOfElements(SMDS_ElemIteratorPtr elemIt,
506 std::set<const SMDS_MeshNode* >& nodes)
508 while ( elemIt->more() )
510 const SMDS_MeshElement* e = elemIt->next();
511 nodes.insert( e->begin_nodes(), e->end_nodes() );
516 //================================================================================
518 * \brief return the number of nodes of cells included to the group
520 //================================================================================
522 CORBA::Long SMESH_GroupBase_i::GetNumberOfNodes()
524 if ( GetType() == SMESH::NODE )
528 myPreMeshInfo->FullLoadFromFile();
530 if ( SMESHDS_GroupBase* g = GetGroupDS())
532 if ( myNbNodes < 0 || g->GetTic() != myGroupDSTic )
534 std::set<const SMDS_MeshNode* > nodes;
535 getNodesOfElements( g->GetElements(), nodes );
536 myNbNodes = nodes.size();
537 myGroupDSTic = g->GetTic();
543 //================================================================================
545 * \brief Return true if GetNumberOfNodes() won't take a long time for computation
547 //================================================================================
549 CORBA::Boolean SMESH_GroupBase_i::IsNodeInfoAvailable()
551 if ( GetType() == SMESH::NODE/* || Size() < 100000 */)
555 if ( SMESHDS_GroupBase* g = GetGroupDS())
556 return ( myNbNodes > -1 && g->GetTic() == myGroupDSTic);
560 //================================================================================
562 * \brief Return IDs of nodes of cells included to the group
564 //================================================================================
566 SMESH::long_array* SMESH_GroupBase_i::GetNodeIDs()
568 if ( GetType() == SMESH::NODE )
569 return GetListOfID();
572 myPreMeshInfo->FullLoadFromFile();
574 SMESH::long_array_var aRes = new SMESH::long_array();
575 if ( SMESHDS_GroupBase* g = GetGroupDS())
577 std::set<const SMDS_MeshNode* > nodes;
578 getNodesOfElements( g->GetElements(), nodes );
579 aRes->length( nodes.size() );
580 std::set<const SMDS_MeshNode*>::iterator nIt = nodes.begin(), nEnd = nodes.end();
581 for ( int i = 0; nIt != nEnd; ++nIt, ++i )
582 aRes[i] = (*nIt)->GetID();
587 //=============================================================================
591 //=============================================================================
592 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
594 SMESH::SMESH_Mesh_var aMesh;
596 aMesh = myMeshServant->_this();
597 return aMesh._retn();
600 //=======================================================================
601 //function : GetShape
603 //=======================================================================
605 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
607 GEOM::GEOM_Object_var aGeomObj;
608 SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
610 SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
611 aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
613 return aGeomObj._retn();
616 //=============================================================================
620 //=============================================================================
621 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
623 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
626 Quantity_Color aQColor = aGroupDS->GetColor();
627 SALOMEDS::Color aColor;
628 aColor.R = aQColor.Red();
629 aColor.G = aQColor.Green();
630 aColor.B = aQColor.Blue();
634 MESSAGE("get color of a group");
635 return SALOMEDS::Color();
638 //=============================================================================
642 //=============================================================================
643 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
645 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
648 Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
649 Quantity_Color oldColor = aGroupDS->GetColor();
650 if ( oldColor != aQColor )
652 aGroupDS->SetColor(aQColor);
653 TPythonDump()<< SMESH::SMESH_GroupBase_var(_this())
654 << ".SetColor( SALOMEDS.Color( "
655 <<color.R<<", "<<color.G<<", "<<color.B<<" ))";
660 //=============================================================================
664 //=============================================================================
665 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
667 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
669 return aGroupDS->GetColorGroup();
670 MESSAGE("get color number of a group");
674 //=============================================================================
678 //=============================================================================
679 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
681 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
684 aGroupDS->SetColorGroup(color);
685 TPythonDump()<<SMESH::SMESH_GroupBase_var(_this())<<".SetColorNumber( "<<color<<" )";
687 MESSAGE("set color number of a group");
691 //=============================================================================
693 * Returns number of mesh elements of each \a SMESH::EntityType
694 * Result array of number of elements per \a SMESH::EntityType
695 * Inherited from SMESH_IDSource
697 //=============================================================================
699 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
702 return myPreMeshInfo->GetMeshInfo();
704 SMESH::long_array_var aRes = new SMESH::long_array();
705 aRes->length(SMESH::Entity_Last);
706 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
709 if ( SMESHDS_GroupBase* g = GetGroupDS())
711 if ( g->GetType() == SMDSAbs_Node /*|| ( myNbNodes > -1 && g->GetTic() == myGroupDSTic)*/)
712 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
714 if ( g->GetType() != SMDSAbs_Node )
715 SMESH_Mesh_i::CollectMeshInfo( g->GetElements(), aRes);
721 //=============================================================================
723 * Returns number of mesh elements of each \a ElementType
725 //=============================================================================
727 SMESH::long_array* SMESH_GroupBase_i::GetNbElementsByType()
729 SMESH::long_array_var aRes = new SMESH::long_array();
730 aRes->length(SMESH::NB_ELEMENT_TYPES);
731 for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
735 aRes[ GetType() ] = myPreMeshInfo->NbElements( SMDSAbs_ElementType( GetType() ));
737 aRes[ GetType() ] = Size();
742 //=======================================================================
744 //purpose : Returns ids of members
745 //=======================================================================
747 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
749 return GetListOfID();
752 //=======================================================================
753 //function : GetTypes
754 //purpose : Returns types of elements it contains
755 //=======================================================================
757 SMESH::array_of_ElementType* SMESH_GroupBase_i::GetTypes()
759 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
763 types[0] = GetType();
765 return types._retn();
768 //=======================================================================
769 //function : IsMeshInfoCorrect
770 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
771 // * happen if mesh data is not yet fully loaded from the file of study.
772 //=======================================================================
774 bool SMESH_GroupBase_i::IsMeshInfoCorrect()
776 return myPreMeshInfo ? myPreMeshInfo->IsMeshInfoCorrect() : true;
779 //=======================================================================
780 //function : GetVtkUgStream
781 //purpose : Return data vtk unstructured grid (not implemented)
782 //=======================================================================
784 SALOMEDS::TMPFile* SMESH_GroupBase_i::GetVtkUgStream()
786 SALOMEDS::TMPFile_var SeqFile;
787 return SeqFile._retn();
790 //================================================================================
792 * \brief Retrieves the predicate from the filter
794 //================================================================================
796 SMESH_PredicatePtr SMESH_GroupOnFilter_i::GetPredicate( SMESH::Filter_ptr filter )
798 SMESH_PredicatePtr predicate;
800 if ( SMESH::Filter_i* filt_i = SMESH::DownCast< SMESH::Filter_i* >( filter ))
801 if ( SMESH::Predicate_i* predic_i= filt_i->GetPredicate_i() )
802 predicate = predic_i->GetPredicate();
807 //================================================================================
809 * \brief Sets the filter defining group contents
811 //================================================================================
813 void SMESH_GroupOnFilter_i::SetFilter(SMESH::Filter_ptr theFilter)
814 throw (SALOME::SALOME_Exception)
816 if ( myFilter->_is_equivalent( theFilter ))
820 myPreMeshInfo->FullLoadFromFile();
822 if ( ! myFilter->_is_nil() )
823 myFilter->UnRegister();
825 myFilter = SMESH::Filter::_duplicate( theFilter );
827 if ( !myFilter->_is_nil() )
829 myFilter->Register();
831 if ( SMESH::Filter_i* f = SMESH::DownCast< SMESH::Filter_i* >( myFilter ))
833 // make filter notify me about change of either a predicate or a base group
834 f->FindBaseObjects();
836 if ( f->ContainModifWaiter( this ) ||
837 this->ContainModifWaiter( f ))
839 SetFilter( SMESH::Filter::_nil() );
840 THROW_SALOME_CORBA_EXCEPTION( "Cyclic dependency between Groups on Filter",
843 f->AddModifWaiter( this );
845 myFilter->SetMesh( SMESH::SMESH_Mesh::_nil() ); // to UnRegister() the mesh
848 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
850 grDS->SetPredicate( GetPredicate( myFilter ));
851 Modified(); // notify dependent Filter with FT_BelongToMeshGroup criterion
854 TPythonDump()<< SMESH::SMESH_GroupOnFilter_var(_this()) <<".SetFilter( "<<theFilter<<" )";
857 //================================================================================
859 * \brief Returns the filter defining group contents
861 //================================================================================
863 SMESH::Filter_ptr SMESH_GroupOnFilter_i::GetFilter()
865 SMESH::Filter_var f = myFilter;
866 TPythonDump() << f << " = " << SMESH::SMESH_GroupOnFilter_var(_this()) << ".GetFilter()";
870 //=======================================================================
872 //purpose : Returns ids of members
873 //=======================================================================
875 SMESH::long_array* SMESH_GroupOnFilter_i::GetListOfID()
878 myPreMeshInfo->FullLoadFromFile();
880 SMESH::long_array_var aRes = new SMESH::long_array();
881 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
882 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
884 const SMDS_MeshInfo& meshInfo = aGroupDS->GetMesh()->GetMeshInfo();
885 aRes->length( meshInfo.NbElements( aGroupDS->GetType() ));
886 if ( aRes->length() ) // else aRes[0] -> SIGSEGV
887 aRes->length( grDS->GetElementIds( &aRes[0] ));
889 if ( 0 < aRes->length() && aRes->length() < 100 ) // for comfortable testing ;)
890 std::sort( &aRes[0], &aRes[0] + aRes->length() );
892 MESSAGE("get list of IDs of a vague group");
896 //=============================================================================
898 * Returns statistic of mesh elements
899 * Result array of number enityties
900 * Inherited from SMESH_IDSource
902 //=============================================================================
904 SMESH::long_array* SMESH_GroupOnFilter_i::GetMeshInfo()
907 return myPreMeshInfo->GetMeshInfo();
909 SMESH::long_array_var aRes = new SMESH::long_array();
910 aRes->length(SMESH::Entity_Last);
911 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
914 if ( SMESHDS_GroupBase* g = GetGroupDS())
916 if ( g->GetType() == SMDSAbs_Node /*|| ( myNbNodes > -1 && g->GetTic() == myGroupDSTic)*/)
917 aRes[ SMDSEntity_Node ] = GetNumberOfNodes();
919 if ( g->GetType() != SMDSAbs_Node )
921 std::vector< int > nbElems = static_cast< SMESHDS_GroupOnFilter* >( g )->GetMeshInfo();
922 for ( size_t i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
923 if ( i < nbElems.size() )
924 aRes[i] = nbElems[ i ];
933 //================================================================================
935 * \brief Return a string to be used to store group definition in the study
937 //================================================================================
939 std::string SMESH_GroupOnFilter_i::FilterToString() const
941 SMESH_Comment result;
942 SMESH::Filter::Criteria_var criteria;
943 if ( !myFilter->_is_nil() && myFilter->GetCriteria( criteria.out() ))
945 result << criteria->length() << SEPAR;
946 for ( unsigned i = 0; i < criteria->length(); ++i )
948 SMESH::Filter::Criterion& crit = criteria[ i ];
950 if ( SMESH::FunctorType( crit.Type ) == SMESH::FT_BelongToMeshGroup &&
951 crit.ThresholdID.in() && crit.ThresholdID.in()[0] )
953 CORBA::Object_var obj = SMESH_Gen_i::GetORB()->string_to_object( crit.ThresholdID );
954 if ( SMESH_GroupBase_i * g = SMESH::DownCast< SMESH_GroupBase_i*>( obj ))
955 if ( SMESHDS_GroupBase* gDS = g->GetGroupDS() )
956 crit.ThresholdID = gDS->GetStoreName();
958 // write FunctorType as string but not as number to assure correct
959 // persistence if enum FunctorType is modified by insertion in the middle
960 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Type )) << SEPAR;
961 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.Compare )) << SEPAR;
962 result << crit.Threshold << SEPAR;
963 result << crit.ThresholdStr << SEPAR;
964 result << crit.ThresholdID << SEPAR;
965 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.UnaryOp )) << SEPAR;
966 result << SMESH::FunctorTypeToString( SMESH::FunctorType( crit.BinaryOp ))<< SEPAR;
967 result << crit.Tolerance << SEPAR;
968 result << crit.TypeOfElement << SEPAR;
969 result << crit.Precision << SEPAR;
975 //================================================================================
977 * \brief Restore the filter by the persistent string
979 //================================================================================
981 SMESH::Filter_ptr SMESH_GroupOnFilter_i::StringToFilter(const std::string& thePersistStr )
983 SMESH::Filter_var filter;
985 // divide thePersistStr into sub-strings
986 std::vector< std::string > strVec;
987 std::string::size_type from = 0, to;
988 while ( from < thePersistStr.size() )
990 to = thePersistStr.find( SEPAR, from );
991 if ( to == std::string::npos )
993 strVec.push_back( thePersistStr.substr( from, to-from ));
996 if ( strVec.empty() || strVec[0] == "0" )
997 return filter._retn();
1001 int nbCrit = atoi( strVec[0].c_str() );
1002 SMESH::Filter::Criteria_var criteria = new SMESH::Filter::Criteria;
1003 criteria->length( nbCrit );
1004 int nbStrPerCrit = ( strVec.size() - 1 ) / nbCrit;
1005 for ( int i = 0; i < nbCrit; ++i )
1007 SMESH::Filter::Criterion& crit = criteria[ i ];
1008 int iStr = 1 + i * nbStrPerCrit;
1009 crit.Type = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
1010 crit.Compare = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
1011 crit.Threshold = atof( strVec[ iStr++ ].c_str() );
1012 crit.ThresholdStr = strVec[ iStr++ ].c_str();
1013 crit.ThresholdID = strVec[ iStr++ ].c_str();
1014 crit.UnaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
1015 crit.BinaryOp = SMESH::StringToFunctorType( strVec[ iStr++ ].c_str() );
1016 crit.Tolerance = atof( strVec[ iStr++ ].c_str() );
1017 crit.TypeOfElement= SMESH::ElementType( atoi( strVec[ iStr++ ].c_str() ));
1018 crit.Precision = atoi( strVec[ iStr++ ].c_str() );
1023 SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
1024 filter = aFilterMgr->CreateFilter();
1025 filter->SetCriteria( criteria.inout() );
1027 aFilterMgr->UnRegister();
1029 pd << ""; // to avoid optimizing pd out
1031 return filter._retn();
1034 //================================================================================
1036 * \brief Destructor of SMESH_GroupOnFilter_i
1038 //================================================================================
1040 SMESH_GroupOnFilter_i::~SMESH_GroupOnFilter_i()
1042 if ( ! myFilter->_is_nil() )
1044 SMESH::DownCast< SMESH::Filter_i* >( myFilter )->RemoveModifWaiter( this );
1045 myFilter->UnRegister();
1049 //================================================================================
1051 * \brief Method called when a predicate of myFilter changes
1053 //================================================================================
1055 void SMESH_GroupOnFilter_i::OnBaseObjModified(NotifyerAndWaiter* filter, bool /*removed*/)
1057 if ( myPreMeshInfo )
1058 myPreMeshInfo->FullLoadFromFile();
1060 if ( SMESHDS_GroupOnFilter* grDS = dynamic_cast< SMESHDS_GroupOnFilter*>( GetGroupDS() ))
1061 grDS->SetPredicate( GetPredicate( myFilter )); // group resets its cache