+
+
+LogicalOR_ptr FilterManager_i::CreateLogicalOR()
+{
+ SMESH::LogicalOR_i* aServant = new SMESH::LogicalOR_i();
+ SMESH::LogicalOR_var anObj = aServant->_this();
+ return anObj._retn();
+}
+
+Filter_ptr FilterManager_i::CreateFilter()
+{
+ SMESH::Filter_i* aServant = new SMESH::Filter_i();
+ SMESH::Filter_var anObj = aServant->_this();
+ return anObj._retn();
+}
+
+FilterLibrary_ptr FilterManager_i::LoadLibrary( const char* aFileName )
+{
+ SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i( aFileName );
+ SMESH::FilterLibrary_var anObj = aServant->_this();
+ return anObj._retn();
+}
+
+FilterLibrary_ptr FilterManager_i::CreateLibrary()
+{
+ SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i();
+ SMESH::FilterLibrary_var anObj = aServant->_this();
+ return anObj._retn();
+}
+
+CORBA::Boolean FilterManager_i::DeleteLibrary( const char* aFileName )
+{
+ return remove( aFileName ) ? false : true;
+}
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::CreateFilterManager
+ *
+ * Create filter manager
+ */
+//=============================================================================
+
+SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
+{
+ SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
+ SMESH::FilterManager_var anObj = aFilter->_this();
+ return anObj._retn();
+}
+
+
+/*
+ FILTER
+*/
+
+//=======================================================================
+// name : Filter_i::Filter_i
+// Purpose : Constructor
+//=======================================================================
+Filter_i::Filter_i()
+: myPredicate( NULL )
+{}
+
+//=======================================================================
+// name : Filter_i::~Filter_i
+// Purpose : Destructor
+//=======================================================================
+Filter_i::~Filter_i()
+{
+ if ( myPredicate )
+ myPredicate->Destroy();
+}
+
+//=======================================================================
+// name : Filter_i::SetPredicate
+// Purpose : Set predicate
+//=======================================================================
+void Filter_i::SetPredicate( Predicate_ptr thePredicate )
+{
+ if ( myPredicate )
+ myPredicate->Destroy();
+
+ myPredicate = dynamic_cast<Predicate_i*>( SMESH_Gen_i::GetServant( thePredicate ).in() );
+
+ if ( myPredicate )
+ {
+ myFilter.SetPredicate( myPredicate->GetPredicate() );
+ myPredicate->Register();
+ }
+}
+
+//=======================================================================
+// name : Filter_i::GetElementType
+// Purpose : Get entity type
+//=======================================================================
+SMESH::ElementType Filter_i::GetElementType()
+{
+ return myPredicate != 0 ? myPredicate->GetElementType() : SMESH::ALL;
+}
+
+//=======================================================================
+// name : Filter_i::SetMesh
+// Purpose : Set mesh
+//=======================================================================
+void Filter_i::SetMesh( SMESH_Mesh_ptr theMesh )
+{
+ if ( myPredicate )
+ myPredicate->SetMesh( theMesh );
+}
+
+//=======================================================================
+// name : Filter_i::GetElementsId
+// Purpose : Get ids of entities
+//=======================================================================
+SMESH::long_array* Filter_i::GetElementsId( SMESH_Mesh_ptr theMesh )
+{
+ SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh);
+ Controls::Filter::TIdSequence aSequence = myFilter.GetElementsId(aMesh);
+
+ SMESH::long_array_var anArray = new SMESH::long_array;
+ long i = 0, iEnd = aSequence.size();
+
+ anArray->length( iEnd );
+ for ( ; i < iEnd; i++ )
+ anArray[ i ] = aSequence[i];
+
+ return anArray._retn();
+}
+
+//=======================================================================
+// name : getCriteria
+// Purpose : Retrieve criterions from predicate
+//=======================================================================
+static inline bool getCriteria( Predicate_i* thePred,
+ SMESH::Filter::Criteria_out theCriteria )
+{
+ int aFType = thePred->GetFunctorType();
+
+ switch ( aFType )
+ {
+ case FT_FreeBorders:
+ case FT_FreeEdges:
+ {
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = aFType;
+ theCriteria[ i ].TypeOfElement = thePred->GetElementType();
+ return true;
+ }
+ case FT_BelongToGeom:
+ {
+ BelongToGeom_i* aPred = dynamic_cast<BelongToGeom_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = FT_BelongToGeom;
+ theCriteria[ i ].ThresholdStr = aPred->GetShapeName();
+ theCriteria[ i ].TypeOfElement = aPred->GetElementType();
+
+ return true;
+ }
+ case FT_BelongToPlane:
+ case FT_BelongToCylinder:
+ {
+ BelongToSurface_i* aPred = dynamic_cast<BelongToSurface_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = aFType;
+ theCriteria[ i ].ThresholdStr = aPred->GetShapeName();
+ theCriteria[ i ].TypeOfElement = aPred->GetElementType();
+ theCriteria[ i ].Tolerance = aPred->GetTolerance();
+
+ return true;
+ }
+ case FT_LyingOnGeom:
+ {
+ LyingOnGeom_i* aPred = dynamic_cast<LyingOnGeom_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = FT_LyingOnGeom;
+ theCriteria[ i ].ThresholdStr = aPred->GetShapeName();
+ theCriteria[ i ].TypeOfElement = aPred->GetElementType();
+
+ return true;
+ }
+ case FT_RangeOfIds:
+ {
+ RangeOfIds_i* aPred = dynamic_cast<RangeOfIds_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = FT_RangeOfIds;
+ theCriteria[ i ].ThresholdStr = aPred->GetRangeStr();
+ theCriteria[ i ].TypeOfElement = aPred->GetElementType();
+
+ return true;
+ }
+ case FT_BadOrientedVolume:
+ {
+ BadOrientedVolume_i* aPred = dynamic_cast<BadOrientedVolume_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = FT_BadOrientedVolume;
+ theCriteria[ i ].TypeOfElement = aPred->GetElementType();
+
+ return true;
+ }
+ case FT_LessThan:
+ case FT_MoreThan:
+ case FT_EqualTo:
+ {
+ Comparator_i* aCompar = dynamic_cast<Comparator_i*>( thePred );
+
+ CORBA::ULong i = theCriteria->length();
+ theCriteria->length( i + 1 );
+
+ theCriteria[ i ] = createCriterion();
+
+ theCriteria[ i ].Type = aCompar->GetNumFunctor_i()->GetFunctorType();
+ theCriteria[ i ].Compare = aFType;
+ theCriteria[ i ].Threshold = aCompar->GetMargin();
+ theCriteria[ i ].TypeOfElement = aCompar->GetElementType();
+
+ if ( aFType == FT_EqualTo )
+ {
+ EqualTo_i* aCompar = dynamic_cast<EqualTo_i*>( thePred );
+ theCriteria[ i ].Tolerance = aCompar->GetTolerance();
+ }
+ }
+ return true;
+
+ case FT_LogicalNOT:
+ {
+ Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
+ getCriteria( aPred, theCriteria );
+ theCriteria[ theCriteria->length() - 1 ].UnaryOp = FT_LogicalNOT;
+ }
+ return true;
+
+ case FT_LogicalAND:
+ case FT_LogicalOR:
+ {
+ Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
+ Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
+ if ( !getCriteria( aPred1, theCriteria ) )
+ return false;
+ theCriteria[ theCriteria->length() - 1 ].BinaryOp = aFType;
+ return getCriteria( aPred2, theCriteria );
+ }
+
+ case FT_Undefined:
+ return false;
+ default:
+ return false;
+ }
+}
+
+//=======================================================================
+// name : Filter_i::GetCriteria
+// Purpose : Retrieve criterions from predicate
+//=======================================================================
+CORBA::Boolean Filter_i::GetCriteria( SMESH::Filter::Criteria_out theCriteria )
+{
+ theCriteria = new SMESH::Filter::Criteria;
+ return myPredicate != 0 ? getCriteria( myPredicate, theCriteria ) : true;
+}
+
+//=======================================================================
+// name : Filter_i::SetCriteria
+// Purpose : Create new predicate and set criterions in it
+//=======================================================================
+CORBA::Boolean Filter_i::SetCriteria( const SMESH::Filter::Criteria& theCriteria )
+{
+ if ( myPredicate != 0 )
+ myPredicate->Destroy();
+
+ SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
+ FilterManager_ptr aFilterMgr = aFilter->_this();
+
+ // CREATE two lists ( PREDICATES and LOG OP )
+
+ // Criterion
+ std::list<SMESH::Predicate_ptr> aPredicates;
+ std::list<int> aBinaries;
+ for ( int i = 0, n = theCriteria.length(); i < n; i++ )
+ {
+ int aCriterion = theCriteria[ i ].Type;
+ int aCompare = theCriteria[ i ].Compare;
+ double aThreshold = theCriteria[ i ].Threshold;
+ int aUnary = theCriteria[ i ].UnaryOp;
+ int aBinary = theCriteria[ i ].BinaryOp;
+ double aTolerance = theCriteria[ i ].Tolerance;
+ const char* aThresholdStr = theCriteria[ i ].ThresholdStr;
+ ElementType aTypeOfElem = theCriteria[ i ].TypeOfElement;
+ long aPrecision = theCriteria[ i ].Precision;
+
+ SMESH::Predicate_ptr aPredicate = SMESH::Predicate::_nil();
+ SMESH::NumericalFunctor_ptr aFunctor = SMESH::NumericalFunctor::_nil();
+
+ switch ( aCriterion )
+ {
+ // Functors
+
+ case SMESH::FT_MultiConnection:
+ aFunctor = aFilterMgr->CreateMultiConnection();
+ break;
+ case SMESH::FT_MultiConnection2D:
+ aFunctor = aFilterMgr->CreateMultiConnection2D();
+ break;
+ case SMESH::FT_Length:
+ aFunctor = aFilterMgr->CreateLength();
+ break;
+ case SMESH::FT_Length2D:
+ aFunctor = aFilterMgr->CreateLength2D();
+ break;
+ case SMESH::FT_AspectRatio:
+ aFunctor = aFilterMgr->CreateAspectRatio();
+ break;
+ case SMESH::FT_AspectRatio3D:
+ aFunctor = aFilterMgr->CreateAspectRatio3D();
+ break;
+ case SMESH::FT_Warping:
+ aFunctor = aFilterMgr->CreateWarping();
+ break;
+ case SMESH::FT_MinimumAngle:
+ aFunctor = aFilterMgr->CreateMinimumAngle();
+ break;
+ case SMESH::FT_Taper:
+ aFunctor = aFilterMgr->CreateTaper();
+ break;
+ case SMESH::FT_Skew:
+ aFunctor = aFilterMgr->CreateSkew();
+ break;
+ case SMESH::FT_Area:
+ aFunctor = aFilterMgr->CreateArea();
+ break;
+
+ // Predicates
+
+ case SMESH::FT_FreeBorders:
+ aPredicate = aFilterMgr->CreateFreeBorders();
+ break;
+ case SMESH::FT_FreeEdges:
+ aPredicate = aFilterMgr->CreateFreeEdges();
+ break;
+ case SMESH::FT_BelongToGeom:
+ {
+ SMESH::BelongToGeom_ptr tmpPred = aFilterMgr->CreateBelongToGeom();
+ tmpPred->SetElementType( aTypeOfElem );
+ tmpPred->SetShapeName( aThresholdStr );
+ aPredicate = tmpPred;
+ }
+ break;
+ case SMESH::FT_BelongToPlane:
+ case SMESH::FT_BelongToCylinder:
+ {
+ SMESH::BelongToSurface_ptr tmpPred;
+ if ( aCriterion == SMESH::FT_BelongToPlane )
+ tmpPred = aFilterMgr->CreateBelongToPlane();
+ else
+ tmpPred = aFilterMgr->CreateBelongToCylinder();
+ tmpPred->SetShapeName( aThresholdStr, aTypeOfElem );
+ tmpPred->SetTolerance( aTolerance );
+ aPredicate = tmpPred;
+ }
+ break;
+ case SMESH::FT_LyingOnGeom:
+ {
+ SMESH::LyingOnGeom_ptr tmpPred = aFilterMgr->CreateLyingOnGeom();
+ tmpPred->SetElementType( aTypeOfElem );
+ tmpPred->SetShapeName( aThresholdStr );
+ aPredicate = tmpPred;
+ }
+ break;
+ case SMESH::FT_RangeOfIds:
+ {
+ SMESH::RangeOfIds_ptr tmpPred = aFilterMgr->CreateRangeOfIds();
+ tmpPred->SetRangeStr( aThresholdStr );
+ tmpPred->SetElementType( aTypeOfElem );
+ aPredicate = tmpPred;
+ }
+ break;
+ case SMESH::FT_BadOrientedVolume:
+ {
+ aPredicate = aFilterMgr->CreateBadOrientedVolume();
+ }
+ break;
+
+ default:
+ continue;
+ }
+
+ // Comparator
+ if ( !aFunctor->_is_nil() && aPredicate->_is_nil() )
+ {
+ SMESH::Comparator_ptr aComparator = SMESH::Comparator::_nil();
+
+ if ( aCompare == SMESH::FT_LessThan )
+ aComparator = aFilterMgr->CreateLessThan();
+ else if ( aCompare == SMESH::FT_MoreThan )
+ aComparator = aFilterMgr->CreateMoreThan();
+ else if ( aCompare == SMESH::FT_EqualTo )
+ aComparator = aFilterMgr->CreateEqualTo();
+ else
+ continue;
+
+ aComparator->SetNumFunctor( aFunctor );
+ aComparator->SetMargin( aThreshold );
+
+ if ( aCompare == FT_EqualTo )
+ {
+ SMESH::EqualTo_var anEqualTo = SMESH::EqualTo::_narrow( aComparator );
+ anEqualTo->SetTolerance( aTolerance );
+ }
+
+ aPredicate = aComparator;
+
+ aFunctor->SetPrecision( aPrecision );
+ }
+
+ // Logical not
+ if ( aUnary == FT_LogicalNOT )
+ {
+ SMESH::LogicalNOT_ptr aNotPred = aFilterMgr->CreateLogicalNOT();
+ aNotPred->SetPredicate( aPredicate );
+ aPredicate = aNotPred;
+ }
+
+ // logical op
+ aPredicates.push_back( aPredicate );
+ aBinaries.push_back( aBinary );
+
+ } // end of for
+
+ // CREATE ONE PREDICATE FROM PREVIOUSLY CREATED MAP
+
+ // combine all "AND" operations
+
+ std::list<SMESH::Predicate_ptr> aResList;
+
+ std::list<SMESH::Predicate_ptr>::iterator aPredIter;
+ std::list<int>::iterator aBinaryIter;
+
+ SMESH::Predicate_ptr aPrevPredicate = SMESH::Predicate::_nil();
+ int aPrevBinary = SMESH::FT_Undefined;
+
+ for ( aPredIter = aPredicates.begin(), aBinaryIter = aBinaries.begin();
+ aPredIter != aPredicates.end() && aBinaryIter != aBinaries.end();
+ ++aPredIter, ++aBinaryIter )
+ {
+ int aCurrBinary = *aBinaryIter;
+
+ SMESH::Predicate_ptr aCurrPred = SMESH::Predicate::_nil();
+
+ if ( aPrevBinary == SMESH::FT_LogicalAND )
+ {
+
+ SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalAND();
+ aBinaryPred->SetPredicate1( aPrevPredicate );
+ aBinaryPred->SetPredicate2( *aPredIter );
+ aCurrPred = aBinaryPred;
+ }
+ else
+ aCurrPred = *aPredIter;
+
+ if ( aCurrBinary != SMESH::FT_LogicalAND )
+ aResList.push_back( aCurrPred );
+
+ aPrevPredicate = aCurrPred;
+ aPrevBinary = aCurrBinary;
+ }
+
+ // combine all "OR" operations
+
+ SMESH::Predicate_ptr aResPredicate = SMESH::Predicate::_nil();
+
+ if ( aResList.size() == 1 )
+ aResPredicate = *aResList.begin();
+ else if ( aResList.size() > 1 )
+ {
+ std::list<SMESH::Predicate_ptr>::iterator anIter = aResList.begin();
+ aResPredicate = *anIter;
+ anIter++;
+ for ( ; anIter != aResList.end(); ++anIter )
+ {
+ SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalOR();
+ aBinaryPred->SetPredicate1( aResPredicate );
+ aBinaryPred->SetPredicate2( *anIter );
+ aResPredicate = aBinaryPred;
+ }
+ }
+
+ SetPredicate( aResPredicate );
+
+ return !aResPredicate->_is_nil();
+}
+
+//=======================================================================
+// name : Filter_i::GetPredicate_i
+// Purpose : Get implementation of predicate
+//=======================================================================
+Predicate_i* Filter_i::GetPredicate_i()
+{
+ return myPredicate;
+}
+
+//=======================================================================
+// name : Filter_i::GetPredicate
+// Purpose : Get predicate
+//=======================================================================
+Predicate_ptr Filter_i::GetPredicate()
+{
+ if ( myPredicate == 0 )
+ return SMESH::Predicate::_nil();
+ else
+ {
+ SMESH::Predicate_var anObj = myPredicate->_this();
+ return anObj._retn();
+ }
+}
+
+/*
+ FILTER LIBRARY
+*/
+
+#define ATTR_TYPE "type"
+#define ATTR_COMPARE "compare"
+#define ATTR_THRESHOLD "threshold"
+#define ATTR_UNARY "unary"
+#define ATTR_BINARY "binary"
+#define ATTR_THRESHOLD_STR "threshold_str"
+#define ATTR_TOLERANCE "tolerance"
+#define ATTR_ELEMENT_TYPE "ElementType"
+
+//=======================================================================
+// name : toString
+// Purpose : Convert bool to LDOMString
+//=======================================================================
+static inline LDOMString toString( const bool val )
+{
+ return val ? "logical not" : "";
+}
+
+//=======================================================================
+// name : toBool
+// Purpose : Convert LDOMString to bool
+//=======================================================================
+static inline bool toBool( const LDOMString& theStr )
+{
+ return theStr.equals( "logical not" );
+}
+
+//=======================================================================
+// name : toString
+// Purpose : Convert double to LDOMString
+//=======================================================================
+static inline LDOMString toString( const double val )
+{
+ char a[ 255 ];
+ sprintf( a, "%e", val );
+ return LDOMString( a );
+}
+
+//=======================================================================
+// name : toDouble
+// Purpose : Convert LDOMString to double
+//=======================================================================
+static inline double toDouble( const LDOMString& theStr )
+{
+ return atof( theStr.GetString() );
+}
+
+//=======================================================================
+// name : toString
+// Purpose : Convert functor type to LDOMString
+//=======================================================================
+static inline LDOMString toString( const long theType )
+{
+ switch ( theType )
+ {
+ case FT_AspectRatio : return "Aspect ratio";
+ case FT_Warping : return "Warping";
+ case FT_MinimumAngle : return "Minimum angle";
+ case FT_Taper : return "Taper";
+ case FT_Skew : return "Skew";
+ case FT_Area : return "Area";
+ case FT_BelongToGeom : return "Belong to Geom";
+ case FT_BelongToPlane : return "Belong to Plane";
+ case FT_BelongToCylinder: return "Belong to Cylinder";
+ case FT_LyingOnGeom : return "Lying on Geom";
+ case FT_BadOrientedVolume: return "Bad Oriented Volume";
+ case FT_RangeOfIds : return "Range of IDs";
+ case FT_FreeBorders : return "Free borders";
+ case FT_FreeEdges : return "Free edges";
+ case FT_MultiConnection : return "Borders at multi-connections";
+ case FT_MultiConnection2D: return "Borders at multi-connections 2D";
+ case FT_Length : return "Length";
+ case FT_Length2D : return "Length2D";
+ case FT_LessThan : return "Less than";
+ case FT_MoreThan : return "More than";
+ case FT_EqualTo : return "Equal to";
+ case FT_LogicalNOT : return "Not";
+ case FT_LogicalAND : return "And";
+ case FT_LogicalOR : return "Or";
+ case FT_Undefined : return "";
+ default : return "";
+ }
+}
+
+//=======================================================================
+// name : toFunctorType
+// Purpose : Convert LDOMString to functor type
+//=======================================================================
+static inline SMESH::FunctorType toFunctorType( const LDOMString& theStr )
+{
+ if ( theStr.equals( "Aspect ratio" ) ) return FT_AspectRatio;
+ else if ( theStr.equals( "Warping" ) ) return FT_Warping;
+ else if ( theStr.equals( "Minimum angle" ) ) return FT_MinimumAngle;
+ else if ( theStr.equals( "Taper" ) ) return FT_Taper;
+ else if ( theStr.equals( "Skew" ) ) return FT_Skew;
+ else if ( theStr.equals( "Area" ) ) return FT_Area;
+ else if ( theStr.equals( "Belong to Geom" ) ) return FT_BelongToGeom;
+ else if ( theStr.equals( "Belong to Plane" ) ) return FT_BelongToPlane;
+ else if ( theStr.equals( "Belong to Cylinder" ) ) return FT_BelongToCylinder;
+ else if ( theStr.equals( "Lying on Geom" ) ) return FT_LyingOnGeom;
+ else if ( theStr.equals( "Free borders" ) ) return FT_FreeBorders;
+ else if ( theStr.equals( "Free edges" ) ) return FT_FreeEdges;
+ else if ( theStr.equals( "Borders at multi-connections" ) ) return FT_MultiConnection;
+ // else if ( theStr.equals( "Borders at multi-connections 2D" ) ) return FT_MultiConnection2D;
+ else if ( theStr.equals( "Length" ) ) return FT_Length;
+ // else if ( theStr.equals( "Length2D" ) ) return FT_Length2D;
+ else if ( theStr.equals( "Range of IDs" ) ) return FT_RangeOfIds;
+ else if ( theStr.equals( "Bad Oriented Volume" ) ) return FT_BadOrientedVolume;
+ else if ( theStr.equals( "Less than" ) ) return FT_LessThan;
+ else if ( theStr.equals( "More than" ) ) return FT_MoreThan;
+ else if ( theStr.equals( "Equal to" ) ) return FT_EqualTo;
+ else if ( theStr.equals( "Not" ) ) return FT_LogicalNOT;
+ else if ( theStr.equals( "And" ) ) return FT_LogicalAND;
+ else if ( theStr.equals( "Or" ) ) return FT_LogicalOR;
+ else if ( theStr.equals( "" ) ) return FT_Undefined;
+ else return FT_Undefined;
+}
+
+//=======================================================================
+// name : toFunctorType
+// Purpose : Convert LDOMString to value of ElementType enumeration
+//=======================================================================
+static inline SMESH::ElementType toElementType( const LDOMString& theStr )
+{
+ if ( theStr.equals( "NODE" ) ) return SMESH::NODE;
+ else if ( theStr.equals( "EDGE" ) ) return SMESH::EDGE;
+ else if ( theStr.equals( "FACE" ) ) return SMESH::FACE;
+ else if ( theStr.equals( "VOLUME" ) ) return SMESH::VOLUME;
+ else return SMESH::ALL;
+}
+
+//=======================================================================
+// name : toString
+// Purpose : Convert ElementType to string
+//=======================================================================
+static inline LDOMString toString( const SMESH::ElementType theType )
+{
+ switch ( theType )
+ {
+ case SMESH::NODE : return "NODE";
+ case SMESH::EDGE : return "EDGE";
+ case SMESH::FACE : return "FACE";
+ case SMESH::VOLUME : return "VOLUME";
+ case SMESH::ALL : return "ALL";
+ default : return "";
+ }
+}
+
+//=======================================================================
+// name : findFilter
+// Purpose : Find filter in document
+//=======================================================================
+static LDOM_Element findFilter( const char* theFilterName,
+ const LDOM_Document& theDoc,
+ LDOM_Node* theParent = 0 )
+{
+ LDOM_Element aRootElement = theDoc.getDocumentElement();
+ if ( aRootElement.isNull() || !aRootElement.hasChildNodes() )
+ return LDOM_Element();
+
+ for ( LDOM_Node aTypeNode = aRootElement.getFirstChild();
+ !aTypeNode.isNull(); aTypeNode = aTypeNode.getNextSibling() )
+ {
+ for ( LDOM_Node aFilter = aTypeNode.getFirstChild();
+ !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
+ {
+ LDOM_Element* anElem = ( LDOM_Element* )&aFilter;
+ if ( anElem->getTagName().equals( LDOMString( "filter" ) ) &&
+ anElem->getAttribute( "name" ).equals( LDOMString( theFilterName ) ) )
+ {
+ if ( theParent != 0 )
+ *theParent = aTypeNode;
+ return (LDOM_Element&)aFilter;
+ }
+ }
+ }
+ return LDOM_Element();
+}
+
+//=======================================================================
+// name : getSectionName
+// Purpose : Get name of section of filters
+//=======================================================================
+static const char* getSectionName( const ElementType theType )
+{
+ switch ( theType )
+ {
+ case SMESH::NODE : return "Filters for nodes";
+ case SMESH::EDGE : return "Filters for edges";
+ case SMESH::FACE : return "Filters for faces";
+ case SMESH::VOLUME : return "Filters for volumes";
+ case SMESH::ALL : return "Filters for elements";
+ default : return "";
+ }
+}
+
+//=======================================================================
+// name : getSection
+// Purpose : Create section for filters corresponding to the entity type
+//=======================================================================
+static LDOM_Node getSection( const ElementType theType,
+ LDOM_Document& theDoc,
+ const bool toCreate = false )
+{
+ LDOM_Element aRootElement = theDoc.getDocumentElement();
+ if ( aRootElement.isNull() )
+ return LDOM_Node();
+
+ // Find section
+ bool anExist = false;
+ const char* aSectionName = getSectionName( theType );
+ if ( strcmp( aSectionName, "" ) == 0 )
+ return LDOM_Node();
+
+ LDOM_NodeList aSections = theDoc.getElementsByTagName( "section" );
+ LDOM_Node aNode;
+ for ( int i = 0, n = aSections.getLength(); i < n; i++ )
+ {
+ aNode = aSections.item( i );
+ LDOM_Element& anItem = ( LDOM_Element& )aNode;
+ if ( anItem.getAttribute( "name" ).equals( LDOMString( aSectionName ) ) )
+ {
+ anExist = true;
+ break;
+ }
+ }
+
+ // Create new section if necessary
+ if ( !anExist )
+ {
+ if ( toCreate )
+ {
+ LDOM_Element aNewItem = theDoc.createElement( "section" );
+ aNewItem.setAttribute( "name", aSectionName );
+ aRootElement.appendChild( aNewItem );
+ return aNewItem;
+ }
+ else
+ return LDOM_Node();
+ }
+ return
+ aNode;
+}
+
+//=======================================================================
+// name : createFilterItem
+// Purpose : Create filter item or LDOM document
+//=======================================================================
+static LDOM_Element createFilterItem( const char* theName,
+ SMESH::Filter_ptr theFilter,
+ LDOM_Document& theDoc )
+{
+ // create new filter in document
+ LDOM_Element aFilterItem = theDoc.createElement( "filter" );
+ aFilterItem.setAttribute( "name", theName );
+
+ // save filter criterions
+ SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
+
+ if ( !theFilter->GetCriteria( aCriteria ) )
+ return LDOM_Element();
+
+ for ( CORBA::ULong i = 0, n = aCriteria->length(); i < n; i++ )
+ {
+ LDOM_Element aCriterionItem = theDoc.createElement( "criterion" );
+
+ aCriterionItem.setAttribute( ATTR_TYPE , toString( aCriteria[ i ].Type ) );
+ aCriterionItem.setAttribute( ATTR_COMPARE , toString( aCriteria[ i ].Compare ) );
+ aCriterionItem.setAttribute( ATTR_THRESHOLD , toString( aCriteria[ i ].Threshold ) );
+ aCriterionItem.setAttribute( ATTR_UNARY , toString( aCriteria[ i ].UnaryOp ) );
+ aCriterionItem.setAttribute( ATTR_BINARY , toString( aCriteria[ i ].BinaryOp ) );
+
+ aCriterionItem.setAttribute( ATTR_THRESHOLD_STR, (const char*)aCriteria[ i ].ThresholdStr );
+ aCriterionItem.setAttribute( ATTR_TOLERANCE , toString( aCriteria[ i ].Tolerance ) );
+ aCriterionItem.setAttribute( ATTR_ELEMENT_TYPE ,
+ toString( (SMESH::ElementType)aCriteria[ i ].TypeOfElement ) );
+
+ aFilterItem.appendChild( aCriterionItem );
+ }
+
+ return aFilterItem;
+}
+
+//=======================================================================
+// name : FilterLibrary_i::FilterLibrary_i
+// Purpose : Constructor
+//=======================================================================
+FilterLibrary_i::FilterLibrary_i( const char* theFileName )
+{
+ myFileName = strdup( theFileName );
+ SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
+ myFilterMgr = aFilterMgr->_this();
+
+ LDOMParser aParser;
+
+ // Try to use existing library file
+ bool anExists = false;
+ if ( !aParser.parse( myFileName ) )
+ {
+ myDoc = aParser.getDocument();
+ anExists = true;
+ }
+ // Create a new XML document if it doesn't exist
+ else
+ myDoc = LDOM_Document::createDocument( LDOMString() );
+
+ LDOM_Element aRootElement = myDoc.getDocumentElement();
+ if ( aRootElement.isNull() )
+ {
+ // If the existing document is empty --> try to create a new one
+ if ( anExists )
+ myDoc = LDOM_Document::createDocument( LDOMString() );
+ }
+}
+
+//=======================================================================
+// name : FilterLibrary_i::FilterLibrary_i
+// Purpose : Constructor
+//=======================================================================
+FilterLibrary_i::FilterLibrary_i()
+{
+ myFileName = 0;
+ SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
+ myFilterMgr = aFilter->_this();
+
+ myDoc = LDOM_Document::createDocument( LDOMString() );
+}
+
+FilterLibrary_i::~FilterLibrary_i()
+{
+ delete myFileName;
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Copy
+// Purpose : Create filter and initialize it with values from library
+//=======================================================================
+Filter_ptr FilterLibrary_i::Copy( const char* theFilterName )
+{
+ Filter_ptr aRes;
+ LDOM_Node aFilter = findFilter( theFilterName, myDoc );
+
+ if ( aFilter.isNull() )
+ return aRes;
+
+ std::list<SMESH::Filter::Criterion> aCriteria;
+
+ for ( LDOM_Node aCritNode = aFilter.getFirstChild();
+ !aCritNode.isNull() ; aCritNode = aCritNode.getNextSibling() )
+ {
+ LDOM_Element* aCrit = (LDOM_Element*)&aCritNode;
+
+ const char* aTypeStr = aCrit->getAttribute( ATTR_TYPE ).GetString();
+ const char* aCompareStr = aCrit->getAttribute( ATTR_COMPARE ).GetString();
+ const char* aUnaryStr = aCrit->getAttribute( ATTR_UNARY ).GetString();
+ const char* aBinaryStr = aCrit->getAttribute( ATTR_BINARY ).GetString();
+ const char* anElemTypeStr = aCrit->getAttribute( ATTR_ELEMENT_TYPE ).GetString();
+
+ SMESH::Filter::Criterion aCriterion = createCriterion();
+
+ aCriterion.Type = toFunctorType( aTypeStr );
+ aCriterion.Compare = toFunctorType( aCompareStr );
+ aCriterion.UnaryOp = toFunctorType( aUnaryStr );
+ aCriterion.BinaryOp = toFunctorType( aBinaryStr );
+
+ aCriterion.TypeOfElement = toElementType( anElemTypeStr );
+
+ LDOMString str = aCrit->getAttribute( ATTR_THRESHOLD );
+ int val = 0;
+ aCriterion.Threshold = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
+ ? val : atof( str.GetString() );
+
+ str = aCrit->getAttribute( ATTR_TOLERANCE );
+ aCriterion.Tolerance = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
+ ? val : atof( str.GetString() );
+
+ str = aCrit->getAttribute( ATTR_THRESHOLD_STR );
+ if ( str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val ) )
+ {
+ char a[ 255 ];
+ sprintf( a, "%d", val );
+ aCriterion.ThresholdStr = strdup( a );
+ }
+ else
+ aCriterion.ThresholdStr = str.GetString();
+
+ aCriteria.push_back( aCriterion );
+ }
+
+ SMESH::Filter::Criteria_var aCriteriaVar = new SMESH::Filter::Criteria;
+ aCriteriaVar->length( aCriteria.size() );
+
+ CORBA::ULong i = 0;
+ std::list<SMESH::Filter::Criterion>::iterator anIter = aCriteria.begin();
+
+ for( ; anIter != aCriteria.end(); ++anIter )
+ aCriteriaVar[ i++ ] = *anIter;
+
+ aRes = myFilterMgr->CreateFilter();
+ aRes->SetCriteria( aCriteriaVar.inout() );
+
+ return aRes;
+}
+
+//=======================================================================
+// name : FilterLibrary_i::SetFileName
+// Purpose : Set file name for library
+//=======================================================================
+void FilterLibrary_i::SetFileName( const char* theFileName )
+{
+ delete myFileName;
+ myFileName = strdup( theFileName );
+}
+
+//=======================================================================
+// name : FilterLibrary_i::GetFileName
+// Purpose : Get file name of library
+//=======================================================================
+char* FilterLibrary_i::GetFileName()
+{
+ return CORBA::string_dup( myFileName );
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Add
+// Purpose : Add new filter to library
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::Add( const char* theFilterName, Filter_ptr theFilter )
+{
+ // if filter already in library or entry filter is null do nothing
+ LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
+ if ( !aFilterNode.isNull() || theFilter->_is_nil() )
+ return false;
+
+ // get section corresponding to the filter type
+ ElementType anEntType = theFilter->GetElementType();
+
+ LDOM_Node aSection = getSection( anEntType, myDoc, true );
+ if ( aSection.isNull() )
+ return false;
+
+ // create filter item
+ LDOM_Element aFilterItem = createFilterItem( theFilterName, theFilter, myDoc );
+ if ( aFilterItem.isNull() )
+ return false;
+ else
+ {
+ aSection.appendChild( aFilterItem );
+ return true;
+ }
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Add
+// Purpose : Add new filter to library
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::AddEmpty( const char* theFilterName, ElementType theType )
+{
+ // if filter already in library or entry filter is null do nothing
+ LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
+ if ( !aFilterNode.isNull() )
+ return false;
+
+ LDOM_Node aSection = getSection( theType, myDoc, true );
+ if ( aSection.isNull() )
+ return false;
+
+ // create filter item
+ Filter_var aFilter = myFilterMgr->CreateFilter();
+
+ LDOM_Element aFilterItem = createFilterItem( theFilterName, aFilter, myDoc );
+ if ( aFilterItem.isNull() )
+ return false;
+ else
+ {
+ aSection.appendChild( aFilterItem );
+ return true;
+ }
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Delete
+// Purpose : Delete filter from library
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::Delete ( const char* theFilterName )
+{
+ LDOM_Node aParentNode;
+ LDOM_Node aFilterNode = findFilter( theFilterName, myDoc, &aParentNode );
+ if ( aFilterNode.isNull() || aParentNode.isNull() )
+ return false;
+
+ aParentNode.removeChild( aFilterNode );
+ return true;
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Replace
+// Purpose : Replace existing filter with entry filter.
+// IMPORTANT : If filter does not exist it is not created
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::Replace( const char* theFilterName,
+ const char* theNewName,
+ Filter_ptr theFilter )
+{
+ LDOM_Element aFilterItem = findFilter( theFilterName, myDoc );
+ if ( aFilterItem.isNull() || theFilter->_is_nil() )
+ return false;
+
+ LDOM_Element aNewItem = createFilterItem( theNewName, theFilter, myDoc );
+ if ( aNewItem.isNull() )
+ return false;
+ else
+ {
+ aFilterItem.ReplaceElement( aNewItem );
+ return true;
+ }
+}
+
+//=======================================================================
+// name : FilterLibrary_i::Save
+// Purpose : Save library on disk
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::Save()
+{
+ if ( myFileName == 0 || strlen( myFileName ) == 0 )
+ return false;
+
+ FILE* aOutFile = fopen( myFileName, "wt" );
+ if ( !aOutFile )
+ return false;
+
+ LDOM_XmlWriter aWriter( aOutFile );
+ aWriter.SetIndentation( 2 );
+ aWriter << myDoc;
+ fclose( aOutFile );
+
+ return true;
+}
+
+//=======================================================================
+// name : FilterLibrary_i::SaveAs
+// Purpose : Save library on disk
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::SaveAs( const char* aFileName )
+{
+ myFileName = strdup ( aFileName );
+ return Save();
+}
+
+//=======================================================================
+// name : FilterLibrary_i::IsPresent
+// Purpose : Verify whether filter is in library
+//=======================================================================
+CORBA::Boolean FilterLibrary_i::IsPresent( const char* theFilterName )
+{
+ return !findFilter( theFilterName, myDoc ).isNull();
+}
+
+//=======================================================================
+// name : FilterLibrary_i::NbFilters
+// Purpose : Return amount of filters in library
+//=======================================================================
+CORBA::Long FilterLibrary_i::NbFilters( ElementType theType )
+{
+ string_array_var aNames = GetNames( theType );
+ return aNames->length();
+}
+
+//=======================================================================
+// name : FilterLibrary_i::GetNames
+// Purpose : Get names of filters from library
+//=======================================================================
+string_array* FilterLibrary_i::GetNames( ElementType theType )
+{
+ string_array_var anArray = new string_array;
+ TColStd_SequenceOfHAsciiString aSeq;
+
+ LDOM_Node aSection = getSection( theType, myDoc, false );
+
+ if ( !aSection.isNull() )
+ {
+ for ( LDOM_Node aFilter = aSection.getFirstChild();
+ !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
+ {
+ LDOM_Element& anElem = ( LDOM_Element& )aFilter;
+ aSeq.Append( new TCollection_HAsciiString(
+ (Standard_CString)anElem.getAttribute( "name" ).GetString() ) );
+ }
+ }
+
+ anArray->length( aSeq.Length() );
+ for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
+ anArray[ i - 1 ] = CORBA::string_dup( aSeq( i )->ToCString() );
+
+ return anArray._retn();
+}
+
+//=======================================================================
+// name : FilterLibrary_i::GetAllNames
+// Purpose : Get names of filters from library
+//=======================================================================
+string_array* FilterLibrary_i::GetAllNames()
+{
+ string_array_var aResArray = new string_array;
+ for ( int type = SMESH::ALL; type <= SMESH::VOLUME; type++ )
+ {
+ SMESH::string_array_var aNames = GetNames( (SMESH::ElementType)type );
+
+ int aPrevLength = aResArray->length();
+ aResArray->length( aPrevLength + aNames->length() );
+ for ( int i = 0, n = aNames->length(); i < n; i++ )
+ aResArray[ aPrevLength + i ] = aNames[ i ];
+ }
+
+ return aResArray._retn();
+}