Salome HOME
Fix on Bug PAL7358
[modules/smesh.git] / src / SMESH_I / SMESH_Filter_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESH_Filter_i.cxx
25 //  Author : Alexey Petrov, OCC
26 //  Module : SMESH
27
28
29 #include "SMESH_Filter_i.hxx"
30
31 #include "SMESH_Gen_i.hxx"
32
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMDS_MeshElement.hxx"
36
37 #include "SMESHDS_Mesh.hxx"
38
39 #include <LDOM_Document.hxx>
40 #include <LDOM_Element.hxx>
41 #include <LDOM_Node.hxx>
42 #include <LDOMString.hxx>
43 #include <LDOMParser.hxx>
44 #include <LDOM_XmlWriter.hxx>
45 #include <TCollection_HAsciiString.hxx>
46 #include <TColStd_ListIteratorOfListOfInteger.hxx>
47 #include <TColStd_ListOfInteger.hxx>
48 #include <TColStd_ListOfReal.hxx>
49 #include <TColStd_MapOfInteger.hxx>
50 #include <TColStd_SequenceOfHAsciiString.hxx>
51 #include <TColStd_ListIteratorOfListOfReal.hxx>
52 #include <Precision.hxx>
53 #include <BRep_Tool.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Face.hxx>
57 #include <Geom_Plane.hxx>
58 #include <Geom_CylindricalSurface.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <OSD_Path.hxx>
61 #include <OSD_File.hxx>
62
63 using namespace SMESH;
64 using namespace SMESH::Controls;
65
66 /*
67   Class       : BelongToGeom
68   Description : Predicate for verifying whether entiy belong to
69                 specified geometrical support
70 */
71
72 Controls::BelongToGeom::BelongToGeom()
73 : myMeshDS(NULL),
74   myType(SMDSAbs_All)
75 {}
76
77 void Controls::BelongToGeom::SetMesh( SMDS_Mesh* theMesh )
78 {
79   myMeshDS = dynamic_cast<SMESHDS_Mesh*>(theMesh);
80 }
81
82 void Controls::BelongToGeom::SetGeom( const TopoDS_Shape& theShape )
83 {
84   myShape = theShape;
85 }
86
87 static bool IsContains( SMESHDS_Mesh*           theMeshDS,
88                         const TopoDS_Shape&     theShape,
89                         const SMDS_MeshElement* theElem,
90                         TopAbs_ShapeEnum        theFindShapeEnum,
91                         TopAbs_ShapeEnum        theAvoidShapeEnum = TopAbs_SHAPE )
92 {
93   TopExp_Explorer anExp( theShape,theFindShapeEnum,theAvoidShapeEnum );
94
95   while( anExp.More() )
96   {
97     const TopoDS_Shape& aShape = anExp.Current();
98     if( SMESHDS_SubMesh* aSubMesh = theMeshDS->MeshElements( aShape ) ){
99       if( aSubMesh->Contains( theElem ) )
100         return true;
101     }
102     anExp.Next();
103   }
104   return false;
105 }
106
107 bool Controls::BelongToGeom::IsSatisfy( long theId )
108 {
109   if ( myMeshDS == 0 || myShape.IsNull() )
110     return false;
111
112   if( myType == SMDSAbs_Node )
113   {
114     if( const SMDS_MeshNode* aNode = myMeshDS->FindNode( theId ) )
115     {
116       const SMDS_PositionPtr& aPosition = aNode->GetPosition();
117       SMDS_TypeOfPosition aTypeOfPosition = aPosition->GetTypeOfPosition();
118       switch( aTypeOfPosition )
119       {
120       case SMDS_TOP_VERTEX : return IsContains( myMeshDS,myShape,aNode,TopAbs_VERTEX );
121       case SMDS_TOP_EDGE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_EDGE );
122       case SMDS_TOP_FACE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_FACE );
123       case SMDS_TOP_3DSPACE: return IsContains( myMeshDS,myShape,aNode,TopAbs_SHELL );
124       }
125     }
126   }
127   else
128   {
129     if( const SMDS_MeshElement* anElem = myMeshDS->FindElement( theId ) )
130     {
131       if( myType == SMDSAbs_All )
132       {
133         return IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE ) ||
134                IsContains( myMeshDS,myShape,anElem,TopAbs_FACE ) ||
135                IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
136                IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID );
137       }
138       else if( myType == anElem->GetType() )
139       {
140         switch( myType )
141         {
142         case SMDSAbs_Edge  : return IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE );
143         case SMDSAbs_Face  : return IsContains( myMeshDS,myShape,anElem,TopAbs_FACE );
144         case SMDSAbs_Volume: return IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
145                                     IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID );
146         }
147       }
148     }
149   }
150     
151   return false;
152 }
153
154 void Controls::BelongToGeom::SetType( SMDSAbs_ElementType theType )
155 {
156   myType = theType;
157 }
158
159 SMDSAbs_ElementType Controls::BelongToGeom::GetType() const
160 {
161   return myType;
162 }
163
164 TopoDS_Shape Controls::BelongToGeom::GetShape()
165 {
166   return myShape;
167 }
168
169 SMESHDS_Mesh* Controls::BelongToGeom::GetMeshDS()
170 {
171   return myMeshDS;
172 }
173
174
175 /*
176                             AUXILIARY METHODS
177 */
178
179 static inline SMDS_Mesh* MeshPtr2SMDSMesh( SMESH_Mesh_ptr theMesh )
180 {
181   SMESH_Mesh_i* anImplPtr = 
182     dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
183   return anImplPtr ? anImplPtr->GetImpl().GetMeshDS() : 0;
184 }
185
186 static inline SMESH::long_array* toArray( const TColStd_ListOfInteger& aList )
187 {
188   SMESH::long_array_var anArray = new SMESH::long_array;
189   anArray->length( aList.Extent() );
190   TColStd_ListIteratorOfListOfInteger anIter( aList );
191   int i = 0;
192   for( ; anIter.More(); anIter.Next() )
193     anArray[ i++ ] = anIter.Value();
194
195   return anArray._retn();
196 }
197
198 static inline SMESH::double_array* toArray( const TColStd_ListOfReal& aList )
199 {
200   SMESH::double_array_var anArray = new SMESH::double_array;
201   anArray->length( aList.Extent() );
202   TColStd_ListIteratorOfListOfReal anIter( aList );
203   int i = 0;
204   for( ; anIter.More(); anIter.Next() )
205     anArray[ i++ ] = anIter.Value();
206
207   return anArray._retn();
208 }
209
210 static SMESH::Filter::Criterion createCriterion()
211 {
212   SMESH::Filter::Criterion aCriterion;
213
214   aCriterion.Type          = FT_Undefined;
215   aCriterion.Compare       = FT_Undefined;
216   aCriterion.Threshold     = 0;
217   aCriterion.UnaryOp       = FT_Undefined;
218   aCriterion.BinaryOp      = FT_Undefined;
219   aCriterion.ThresholdStr  = "";
220   aCriterion.Tolerance     = Precision::Confusion();
221   aCriterion.TypeOfElement = SMESH::ALL;
222   aCriterion.Precision     = -1;
223
224   return aCriterion;
225 }
226
227 static TopoDS_Shape getShapeByName( const char* theName )
228 {
229   if ( theName != 0 )
230   {
231     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
232     SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
233     if ( aStudy != 0 )
234     {
235       SALOMEDS::Study::ListOfSObject_var aList =
236         aStudy->FindObjectByName( theName, "GEOM" );
237       if ( aList->length() > 0 )
238       {
239         GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aList[ 0 ]->GetObject() );
240         if ( !aGeomObj->_is_nil() )
241         {
242           GEOM::GEOM_Gen_var aGEOMGen = SMESH_Gen_i::GetGeomEngine();
243           TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, aGeomObj );
244           return aLocShape;
245         }
246       }
247     }
248   }
249   return TopoDS_Shape();
250 }
251
252
253
254 /*
255                                 FUNCTORS
256 */
257
258 /*
259   Class       : Functor_i
260   Description : An abstact class for all functors 
261 */
262 Functor_i::Functor_i(): 
263   SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
264 {
265   SMESH_Gen_i::GetPOA()->activate_object( this );
266 }
267
268 void Functor_i::SetMesh( SMESH_Mesh_ptr theMesh )
269 {
270   myFunctorPtr->SetMesh( MeshPtr2SMDSMesh( theMesh ) );
271   INFOS("Functor_i::SetMesh~");
272 }
273
274 ElementType Functor_i::GetElementType()
275 {
276   return ( ElementType )myFunctorPtr->GetType();
277 }
278
279
280 /*
281   Class       : NumericalFunctor_i
282   Description : Base class for numerical functors
283 */
284 CORBA::Double NumericalFunctor_i::GetValue( CORBA::Long theId )
285 {
286   return myNumericalFunctorPtr->GetValue( theId );
287 }
288
289 void NumericalFunctor_i::SetPrecision( CORBA::Long thePrecision )
290 {
291   myNumericalFunctorPtr->SetPrecision( thePrecision );
292 }
293
294 CORBA::Long NumericalFunctor_i::GetPrecision()
295 {
296  return myNumericalFunctorPtr->GetPrecision();
297 }
298
299 Controls::NumericalFunctorPtr NumericalFunctor_i::GetNumericalFunctor()
300 {
301   return myNumericalFunctorPtr;
302 }
303
304
305 /*
306   Class       : SMESH_MinimumAngle
307   Description : Functor for calculation of minimum angle
308 */
309 MinimumAngle_i::MinimumAngle_i()
310 {
311   myNumericalFunctorPtr.reset( new Controls::MinimumAngle() );
312   myFunctorPtr = myNumericalFunctorPtr;
313 }
314
315 FunctorType MinimumAngle_i::GetFunctorType()
316 {
317   return SMESH::FT_MinimumAngle;
318 }
319
320
321 /*
322   Class       : AspectRatio
323   Description : Functor for calculating aspect ratio
324 */
325 AspectRatio_i::AspectRatio_i()
326 {
327   myNumericalFunctorPtr.reset( new Controls::AspectRatio() );
328   myFunctorPtr = myNumericalFunctorPtr;
329 }
330
331 FunctorType AspectRatio_i::GetFunctorType()
332 {
333   return SMESH::FT_AspectRatio;
334 }
335
336
337 /*
338   Class       : AspectRatio3D
339   Description : Functor for calculating aspect ratio 3D
340 */
341 AspectRatio3D_i::AspectRatio3D_i()
342 {
343   myNumericalFunctorPtr.reset( new Controls::AspectRatio3D() );
344   myFunctorPtr = myNumericalFunctorPtr;
345 }
346
347 FunctorType AspectRatio3D_i::GetFunctorType()
348 {
349   return SMESH::FT_AspectRatio3D;
350 }
351
352
353 /*
354   Class       : Warping_i
355   Description : Functor for calculating warping
356 */
357 Warping_i::Warping_i()
358 {
359   myNumericalFunctorPtr.reset( new Controls::Warping() );
360   myFunctorPtr = myNumericalFunctorPtr;
361 }
362
363 FunctorType Warping_i::GetFunctorType()
364 {
365   return SMESH::FT_Warping;
366 }
367
368
369 /*
370   Class       : Taper_i
371   Description : Functor for calculating taper
372 */
373 Taper_i::Taper_i()
374 {
375   myNumericalFunctorPtr.reset( new Controls::Taper() );
376   myFunctorPtr = myNumericalFunctorPtr;
377 }
378
379 FunctorType Taper_i::GetFunctorType()
380 {
381   return SMESH::FT_Taper;
382 }
383
384
385 /*
386   Class       : Skew_i
387   Description : Functor for calculating skew in degrees
388 */
389 Skew_i::Skew_i()
390 {
391   myNumericalFunctorPtr.reset( new Controls::Skew() );
392   myFunctorPtr = myNumericalFunctorPtr;
393 }
394
395 FunctorType Skew_i::GetFunctorType()
396 {
397   return SMESH::FT_Skew;
398 }
399
400 /*
401   Class       : Area_i
402   Description : Functor for calculating area
403 */
404 Area_i::Area_i()
405 {
406   myNumericalFunctorPtr.reset( new Controls::Area() );
407   myFunctorPtr = myNumericalFunctorPtr;
408 }
409
410 FunctorType Area_i::GetFunctorType()
411 {
412   return SMESH::FT_Area;
413 }
414
415 /*
416   Class       : Length_i
417   Description : Functor for calculating length off edge
418 */
419 Length_i::Length_i()
420 {
421   myNumericalFunctorPtr.reset( new Controls::Length() );
422   myFunctorPtr = myNumericalFunctorPtr;
423 }
424
425 FunctorType Length_i::GetFunctorType()
426 {
427   return SMESH::FT_Length;
428 }
429
430 /*
431   Class       : Length2D_i
432   Description : Functor for calculating length of edge
433 */
434 Length2D_i::Length2D_i()
435 {
436   myNumericalFunctorPtr.reset( new Controls::Length2D() );
437   myFunctorPtr = myNumericalFunctorPtr;
438 }
439
440 FunctorType Length2D_i::GetFunctorType()
441 {
442   return SMESH::FT_Length2D;
443 }
444
445 SMESH::Length2D::Values* Length2D_i::GetValues()
446 {
447   INFOS("Length2D_i::GetValues");
448   SMESH::Controls::Length2D::TValues aValues;
449   myLength2DPtr->GetValues( aValues );
450   
451   long i = 0, iEnd = aValues.size();
452
453   SMESH::Length2D::Values_var aResult = new SMESH::Length2D::Values(iEnd);
454
455   SMESH::Controls::Length2D::TValues::const_iterator anIter;
456   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
457   {
458     const SMESH::Controls::Length2D::Value&  aVal = *anIter;
459     SMESH::Length2D::Value &aValue = aResult[ i ];
460     
461     aValue.myLength = aVal.myLength;
462     aValue.myPnt1 = aVal.myPntId[ 0 ];
463     aValue.myPnt2 = aVal.myPntId[ 1 ];
464    
465   }
466
467   INFOS("Length2D_i::GetValuess~");
468   return aResult._retn();
469 }
470
471 /*
472   Class       : MultiConnection_i
473   Description : Functor for calculating number of faces conneted to the edge
474 */
475 MultiConnection_i::MultiConnection_i()
476 {
477   myNumericalFunctorPtr.reset( new Controls::MultiConnection() );
478   myFunctorPtr = myNumericalFunctorPtr;
479 }
480
481 FunctorType MultiConnection_i::GetFunctorType()
482 {
483   return SMESH::FT_MultiConnection;
484 }
485
486
487 /*
488                             PREDICATES
489 */
490
491
492 /*
493   Class       : Predicate_i
494   Description : Base class for all predicates
495 */
496 CORBA::Boolean Predicate_i::IsSatisfy( CORBA::Long theId )
497 {
498   return myPredicatePtr->IsSatisfy( theId );
499 }
500
501 Controls::PredicatePtr Predicate_i::GetPredicate()
502 {
503   return myPredicatePtr;
504 }
505
506
507 /*
508   Class       : BelongToGeom_i
509   Description : Predicate for selection on geometrical support
510 */
511 BelongToGeom_i::BelongToGeom_i()
512 {
513   myBelongToGeomPtr.reset( new Controls::BelongToGeom() );
514   myFunctorPtr = myPredicatePtr = myBelongToGeomPtr;
515   myShapeName = 0;
516 }
517
518 BelongToGeom_i::~BelongToGeom_i()
519 {
520   delete myShapeName;
521 }
522
523 void BelongToGeom_i::SetGeom( GEOM::GEOM_Object_ptr theGeom )
524 {
525   if ( theGeom->_is_nil() )
526     return;
527   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
528   GEOM::GEOM_Gen_var aGEOMGen = SMESH_Gen_i::GetGeomEngine();
529   TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, theGeom );
530   myBelongToGeomPtr->SetGeom( aLocShape );
531 }
532
533 void BelongToGeom_i::SetGeom( const TopoDS_Shape& theShape )
534 {
535   myBelongToGeomPtr->SetGeom( theShape );
536 }
537
538 void BelongToGeom_i::SetElementType(ElementType theType){
539   myBelongToGeomPtr->SetType(SMDSAbs_ElementType(theType));
540 }
541
542 FunctorType BelongToGeom_i::GetFunctorType()
543 {
544   return SMESH::FT_BelongToGeom;
545 }
546
547 void BelongToGeom_i::SetShapeName( const char* theName )
548 {
549   delete myShapeName;
550   myShapeName = strdup( theName );
551   myBelongToGeomPtr->SetGeom( getShapeByName( myShapeName ) );
552 }
553
554 char* BelongToGeom_i::GetShapeName()
555 {
556   return CORBA::string_dup( myShapeName );
557 }
558
559 /*
560   Class       : BelongToSurface_i
561   Description : Predicate for selection on geometrical support
562 */
563 BelongToSurface_i::BelongToSurface_i( const Handle(Standard_Type)& theSurfaceType )
564 {
565   myElementsOnSurfacePtr.reset( new Controls::ElementsOnSurface() );
566   myFunctorPtr = myPredicatePtr = myElementsOnSurfacePtr;
567   myShapeName = 0;
568   mySurfaceType = theSurfaceType;
569 }
570
571 BelongToSurface_i::~BelongToSurface_i()
572 {
573   delete myShapeName;
574 }
575
576 void BelongToSurface_i::SetSurface( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
577 {
578   if ( theGeom->_is_nil() )
579     return;
580   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
581   GEOM::GEOM_Gen_var aGEOMGen = SMESH_Gen_i::GetGeomEngine();
582   TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, theGeom );
583
584   if ( aLocShape.ShapeType() == TopAbs_FACE )
585   {
586     Handle(Geom_Surface) aSurf = BRep_Tool::Surface( TopoDS::Face( aLocShape ) );
587     if ( !aSurf.IsNull() && aSurf->DynamicType() == mySurfaceType )
588     {
589       myElementsOnSurfacePtr->SetSurface( aLocShape, (SMDSAbs_ElementType)theType );
590       return;
591     }
592   }
593
594   myElementsOnSurfacePtr->SetSurface( TopoDS_Shape(), (SMDSAbs_ElementType)theType );
595 }
596
597 void BelongToSurface_i::SetShapeName( const char* theName, ElementType theType )
598 {
599   delete myShapeName;
600   myShapeName = strdup( theName );
601   myElementsOnSurfacePtr->SetSurface( getShapeByName( myShapeName ), (SMDSAbs_ElementType)theType );
602 }
603
604 char* BelongToSurface_i::GetShapeName()
605 {
606   return CORBA::string_dup( myShapeName );
607 }
608
609 void BelongToSurface_i::SetTolerance( CORBA::Double theToler )
610 {
611   myElementsOnSurfacePtr->SetTolerance( theToler );
612 }
613
614 CORBA::Double BelongToSurface_i::GetTolerance()
615 {
616   return myElementsOnSurfacePtr->GetTolerance();
617 }
618
619 /*
620   Class       : BelongToPlane_i
621   Description : Verify whether mesh element lie in pointed Geom planar object
622 */
623
624 BelongToPlane_i::BelongToPlane_i()
625 : BelongToSurface_i( STANDARD_TYPE( Geom_Plane ) )
626 {
627 }
628
629 void BelongToPlane_i::SetPlane( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
630 {
631   BelongToSurface_i::SetSurface( theGeom, theType );
632 }
633
634 FunctorType BelongToPlane_i::GetFunctorType()
635 {
636   return FT_BelongToPlane;
637 }
638
639 /*
640   Class       : BelongToCylinder_i
641   Description : Verify whether mesh element lie in pointed Geom planar object
642 */
643
644 BelongToCylinder_i::BelongToCylinder_i()
645 : BelongToSurface_i( STANDARD_TYPE( Geom_CylindricalSurface ) )
646 {
647 }
648
649 void BelongToCylinder_i::SetCylinder( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
650 {
651   BelongToSurface_i::SetSurface( theGeom, theType );
652 }
653
654 FunctorType BelongToCylinder_i::GetFunctorType()
655 {
656   return FT_BelongToCylinder;
657 }
658
659
660
661 /*
662   Class       : FreeBorders_i
663   Description : Predicate for free borders
664 */
665 FreeBorders_i::FreeBorders_i()
666 {
667   myPredicatePtr.reset(new Controls::FreeBorders());
668   myFunctorPtr = myPredicatePtr;
669 }
670
671 FunctorType FreeBorders_i::GetFunctorType()
672 {
673   return SMESH::FT_FreeBorders;
674 }
675
676 /*
677   Class       : FreeEdges_i
678   Description : Predicate for free borders
679 */
680 FreeEdges_i::FreeEdges_i()
681 : myFreeEdgesPtr( new Controls::FreeEdges() )
682 {
683   myFunctorPtr = myPredicatePtr = myFreeEdgesPtr;
684 }
685
686 SMESH::FreeEdges::Borders* FreeEdges_i::GetBorders()
687 {
688   INFOS("FreeEdges_i::GetBorders");
689   SMESH::Controls::FreeEdges::TBorders aBorders;
690   myFreeEdgesPtr->GetBoreders( aBorders );
691   
692   long i = 0, iEnd = aBorders.size();
693
694   SMESH::FreeEdges::Borders_var aResult = new SMESH::FreeEdges::Borders(iEnd);
695
696   SMESH::Controls::FreeEdges::TBorders::const_iterator anIter;
697   for ( anIter = aBorders.begin() ; anIter != aBorders.end(); anIter++, i++ )
698   {
699     const SMESH::Controls::FreeEdges::Border&  aBord = *anIter;
700     SMESH::FreeEdges::Border &aBorder = aResult[ i ];
701     
702     aBorder.myElemId = aBord.myElemId;
703     aBorder.myPnt1 = aBord.myPntId[ 0 ];
704     aBorder.myPnt2 = aBord.myPntId[ 1 ];
705   }
706
707   INFOS("FreeEdges_i::GetBorders~");
708   return aResult._retn();
709 }
710
711 FunctorType FreeEdges_i::GetFunctorType()
712 {
713   return SMESH::FT_FreeEdges;
714 }
715
716 /*
717   Class       : RangeOfIds_i
718   Description : Predicate for Range of Ids.
719                 Range may be specified with two ways.
720                 1. Using AddToRange method
721                 2. With SetRangeStr method. Parameter of this method is a string
722                    like as "1,2,3,50-60,63,67,70-"
723 */
724
725 RangeOfIds_i::RangeOfIds_i()
726 {
727   myRangeOfIdsPtr.reset( new Controls::RangeOfIds() );
728   myFunctorPtr = myPredicatePtr = myRangeOfIdsPtr;
729 }
730
731 void RangeOfIds_i::SetRange( const SMESH::long_array& theIds )
732 {
733   CORBA::Long iEnd = theIds.length();
734   for ( CORBA::Long i = 0; i < iEnd; i++ )
735     myRangeOfIdsPtr->AddToRange( theIds[ i ] );
736 }
737
738 CORBA::Boolean RangeOfIds_i::SetRangeStr( const char* theRange )
739 {
740   return myRangeOfIdsPtr->SetRangeStr(
741     TCollection_AsciiString( (Standard_CString)theRange ) );
742 }
743
744 char* RangeOfIds_i::GetRangeStr()
745 {
746   TCollection_AsciiString aStr;
747   myRangeOfIdsPtr->GetRangeStr( aStr );
748   return CORBA::string_dup( aStr.ToCString() );
749 }
750
751 void RangeOfIds_i::SetElementType( ElementType theType )
752 {
753   myRangeOfIdsPtr->SetType( SMDSAbs_ElementType( theType ) );
754 }
755
756 FunctorType RangeOfIds_i::GetFunctorType()
757 {
758   return SMESH::FT_RangeOfIds;
759 }
760
761 /*
762   Class       : Comparator_i
763   Description : Base class for comparators
764 */
765 Comparator_i::Comparator_i():
766   myNumericalFunctor( NULL )
767 {}
768
769 Comparator_i::~Comparator_i()
770 {
771   if ( myNumericalFunctor )
772     myNumericalFunctor->Destroy();
773 }
774
775 void Comparator_i::SetMargin( CORBA::Double theValue )
776 {
777   myComparatorPtr->SetMargin( theValue );
778 }
779
780 CORBA::Double Comparator_i::GetMargin()
781 {
782   return myComparatorPtr->GetMargin();
783 }
784
785 void Comparator_i::SetNumFunctor( NumericalFunctor_ptr theFunct )
786 {
787   if ( myNumericalFunctor )
788     myNumericalFunctor->Destroy();
789
790   myNumericalFunctor = dynamic_cast<NumericalFunctor_i*>( SMESH_Gen_i::GetServant( theFunct ).in() );
791
792   if ( myNumericalFunctor )
793   {
794     myComparatorPtr->SetNumFunctor( myNumericalFunctor->GetNumericalFunctor() );
795     myNumericalFunctor->Register();
796   }
797 }
798
799 Controls::ComparatorPtr Comparator_i::GetComparator()
800 {
801   return myComparatorPtr;
802 }
803
804 NumericalFunctor_i* Comparator_i::GetNumFunctor_i()
805 {
806   return myNumericalFunctor;
807 }
808
809
810 /*
811   Class       : LessThan_i
812   Description : Comparator "<"
813 */
814 LessThan_i::LessThan_i()
815 {
816   myComparatorPtr.reset( new Controls::LessThan() );
817   myFunctorPtr = myPredicatePtr = myComparatorPtr;
818 }
819
820 FunctorType LessThan_i::GetFunctorType()
821 {
822   return SMESH::FT_LessThan;
823 }
824
825
826 /*
827   Class       : MoreThan_i
828   Description : Comparator ">"
829 */
830 MoreThan_i::MoreThan_i()
831 {
832   myComparatorPtr.reset( new Controls::MoreThan() );
833   myFunctorPtr = myPredicatePtr = myComparatorPtr;
834 }
835
836 FunctorType MoreThan_i::GetFunctorType()
837 {
838   return SMESH::FT_MoreThan;
839 }
840
841
842 /*
843   Class       : EqualTo_i
844   Description : Comparator "="
845 */
846 EqualTo_i::EqualTo_i()
847 : myEqualToPtr( new Controls::EqualTo() )
848 {
849   myFunctorPtr = myPredicatePtr = myComparatorPtr = myEqualToPtr;
850 }
851
852 void EqualTo_i::SetTolerance( CORBA::Double theToler )
853 {
854   myEqualToPtr->SetTolerance( theToler );
855 }
856
857 CORBA::Double EqualTo_i::GetTolerance()
858 {
859   return myEqualToPtr->GetTolerance();
860 }
861
862 FunctorType EqualTo_i::GetFunctorType()
863 {
864   return SMESH::FT_EqualTo;
865 }
866
867 /*
868   Class       : LogicalNOT_i
869   Description : Logical NOT predicate
870 */
871 LogicalNOT_i::LogicalNOT_i()
872 : myPredicate( NULL ),
873   myLogicalNOTPtr( new Controls::LogicalNOT() )
874 {
875   myFunctorPtr = myPredicatePtr = myLogicalNOTPtr;
876 }
877
878 LogicalNOT_i::~LogicalNOT_i()
879 {
880   if ( myPredicate )
881     myPredicate->Destroy();
882 }
883
884 void LogicalNOT_i::SetPredicate( Predicate_ptr thePred )
885 {
886   if ( myPredicate )
887     myPredicate->Destroy();
888
889   myPredicate = dynamic_cast<Predicate_i*>( SMESH_Gen_i::GetServant( thePred ).in() );
890
891   if ( myPredicate ){
892     myLogicalNOTPtr->SetPredicate(myPredicate->GetPredicate());
893     myPredicate->Register();
894   }
895 }
896
897 FunctorType LogicalNOT_i::GetFunctorType()
898 {
899   return SMESH::FT_LogicalNOT;
900 }
901
902 Predicate_i* LogicalNOT_i::GetPredicate_i()
903 {
904   return myPredicate;
905 }
906
907
908 /*
909   Class       : LogicalBinary_i
910   Description : Base class for binary logical predicate
911 */
912 LogicalBinary_i::LogicalBinary_i()
913 : myPredicate1( NULL ),
914   myPredicate2( NULL )
915 {}
916
917 LogicalBinary_i::~LogicalBinary_i()
918 {
919   if ( myPredicate1 )
920     myPredicate1->Destroy();
921
922   if ( myPredicate2 )
923     myPredicate2->Destroy();
924 }
925
926 void LogicalBinary_i::SetMesh( SMESH_Mesh_ptr theMesh )
927 {
928   if ( myPredicate1 )
929     myPredicate1->SetMesh( theMesh );
930
931   if ( myPredicate2 )
932     myPredicate2->SetMesh( theMesh );
933 }
934
935 void LogicalBinary_i::SetPredicate1( Predicate_ptr thePredicate )
936 {
937   if ( myPredicate1 )
938     myPredicate1->Destroy();
939
940   myPredicate1 = dynamic_cast<Predicate_i*>( SMESH_Gen_i::GetServant( thePredicate ).in() );
941
942   if ( myPredicate1 ){
943     myLogicalBinaryPtr->SetPredicate1(myPredicate1->GetPredicate());
944     myPredicate1->Register();
945   }
946 }
947
948 void LogicalBinary_i::SetPredicate2( Predicate_ptr thePredicate )
949 {
950   if ( myPredicate2 )
951     myPredicate2->Destroy();
952
953   myPredicate2 = dynamic_cast<Predicate_i*>( SMESH_Gen_i::GetServant( thePredicate ).in() );
954
955   if ( myPredicate2 ){
956     myLogicalBinaryPtr->SetPredicate2(myPredicate2->GetPredicate());
957     myPredicate2->Register();
958   }
959 }
960
961 Controls::LogicalBinaryPtr LogicalBinary_i::GetLogicalBinary()
962 {
963   return myLogicalBinaryPtr;
964 }
965
966 Predicate_i* LogicalBinary_i::GetPredicate1_i()
967 {
968   return myPredicate1;
969 }
970 Predicate_i* LogicalBinary_i::GetPredicate2_i()
971 {
972   return myPredicate2;
973 }
974
975
976 /*
977   Class       : LogicalAND_i
978   Description : Logical AND
979 */
980 LogicalAND_i::LogicalAND_i()
981 {
982   myLogicalBinaryPtr.reset( new Controls::LogicalAND() );
983   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
984 }
985
986 FunctorType LogicalAND_i::GetFunctorType()
987 {
988   return SMESH::FT_LogicalAND;
989 }
990
991
992 /*
993   Class       : LogicalOR_i
994   Description : Logical OR
995 */
996 LogicalOR_i::LogicalOR_i()
997 {
998   myLogicalBinaryPtr.reset( new Controls::LogicalOR() );
999   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
1000 }
1001
1002 FunctorType LogicalOR_i::GetFunctorType()
1003 {
1004   return SMESH::FT_LogicalOR;
1005 }
1006
1007
1008 /*
1009                             FILTER MANAGER
1010 */
1011
1012 FilterManager_i::FilterManager_i()
1013 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
1014 {
1015   SMESH_Gen_i::GetPOA()->activate_object( this );
1016 }
1017
1018 MinimumAngle_ptr FilterManager_i::CreateMinimumAngle()
1019 {
1020   SMESH::MinimumAngle_i* aServant = new SMESH::MinimumAngle_i();
1021   SMESH::MinimumAngle_var anObj = aServant->_this();
1022   return anObj._retn();
1023 }
1024
1025
1026 AspectRatio_ptr FilterManager_i::CreateAspectRatio()
1027 {
1028   SMESH::AspectRatio_i* aServant = new SMESH::AspectRatio_i();
1029   SMESH::AspectRatio_var anObj = aServant->_this();
1030   return anObj._retn();
1031 }
1032
1033
1034 AspectRatio3D_ptr FilterManager_i::CreateAspectRatio3D()
1035 {
1036   SMESH::AspectRatio3D_i* aServant = new SMESH::AspectRatio3D_i();
1037   SMESH::AspectRatio3D_var anObj = aServant->_this();
1038   return anObj._retn();
1039 }
1040
1041
1042 Warping_ptr FilterManager_i::CreateWarping()
1043 {
1044   SMESH::Warping_i* aServant = new SMESH::Warping_i();
1045   SMESH::Warping_var anObj = aServant->_this();
1046   return anObj._retn();
1047 }
1048
1049
1050 Taper_ptr FilterManager_i::CreateTaper()
1051 {
1052   SMESH::Taper_i* aServant = new SMESH::Taper_i();
1053   SMESH::Taper_var anObj = aServant->_this();
1054   return anObj._retn();
1055 }
1056
1057
1058 Skew_ptr FilterManager_i::CreateSkew()
1059 {
1060   SMESH::Skew_i* aServant = new SMESH::Skew_i();
1061   SMESH::Skew_var anObj = aServant->_this();
1062   return anObj._retn();
1063 }
1064
1065
1066 Area_ptr FilterManager_i::CreateArea()
1067 {
1068   SMESH::Area_i* aServant = new SMESH::Area_i();
1069   SMESH::Area_var anObj = aServant->_this();
1070   return anObj._retn();
1071 }
1072
1073
1074 Length_ptr FilterManager_i::CreateLength()
1075 {
1076   SMESH::Length_i* aServant = new SMESH::Length_i();
1077   SMESH::Length_var anObj = aServant->_this();
1078   return anObj._retn();
1079 }
1080
1081 Length2D_ptr FilterManager_i::CreateLength2D()
1082 {
1083   SMESH::Length2D_i* aServant = new SMESH::Length2D_i();
1084   SMESH::Length2D_var anObj = aServant->_this();
1085   return anObj._retn();
1086 }
1087
1088 MultiConnection_ptr FilterManager_i::CreateMultiConnection()
1089 {
1090   SMESH::MultiConnection_i* aServant = new SMESH::MultiConnection_i();
1091   SMESH::MultiConnection_var anObj = aServant->_this();
1092   return anObj._retn();
1093 }
1094
1095
1096 BelongToGeom_ptr FilterManager_i::CreateBelongToGeom()
1097 {
1098   SMESH::BelongToGeom_i* aServant = new SMESH::BelongToGeom_i();
1099   SMESH::BelongToGeom_var anObj = aServant->_this();
1100   return anObj._retn();
1101 }
1102
1103 BelongToPlane_ptr FilterManager_i::CreateBelongToPlane()
1104 {
1105   SMESH::BelongToPlane_i* aServant = new SMESH::BelongToPlane_i();
1106   SMESH::BelongToPlane_var anObj = aServant->_this();
1107   return anObj._retn();
1108 }
1109
1110 BelongToCylinder_ptr FilterManager_i::CreateBelongToCylinder()
1111 {
1112   SMESH::BelongToCylinder_i* aServant = new SMESH::BelongToCylinder_i();
1113   SMESH::BelongToCylinder_var anObj = aServant->_this();
1114   return anObj._retn();
1115 }
1116
1117 FreeBorders_ptr FilterManager_i::CreateFreeBorders()
1118 {
1119   SMESH::FreeBorders_i* aServant = new SMESH::FreeBorders_i();
1120   SMESH::FreeBorders_var anObj = aServant->_this();
1121   return anObj._retn();
1122 }
1123
1124 FreeEdges_ptr FilterManager_i::CreateFreeEdges()
1125 {
1126   SMESH::FreeEdges_i* aServant = new SMESH::FreeEdges_i();
1127   SMESH::FreeEdges_var anObj = aServant->_this();
1128   return anObj._retn();
1129 }
1130
1131 RangeOfIds_ptr FilterManager_i::CreateRangeOfIds()
1132 {
1133   SMESH::RangeOfIds_i* aServant = new SMESH::RangeOfIds_i();
1134   SMESH::RangeOfIds_var anObj = aServant->_this();
1135   return anObj._retn();
1136 }
1137
1138 LessThan_ptr FilterManager_i::CreateLessThan()
1139 {
1140   SMESH::LessThan_i* aServant = new SMESH::LessThan_i();
1141   SMESH::LessThan_var anObj = aServant->_this();
1142   return anObj._retn();
1143 }
1144
1145
1146 MoreThan_ptr FilterManager_i::CreateMoreThan()
1147 {
1148   SMESH::MoreThan_i* aServant = new SMESH::MoreThan_i();
1149   SMESH::MoreThan_var anObj = aServant->_this();
1150   return anObj._retn();
1151 }
1152
1153 EqualTo_ptr FilterManager_i::CreateEqualTo()
1154 {
1155   SMESH::EqualTo_i* aServant = new SMESH::EqualTo_i();
1156   SMESH::EqualTo_var anObj = aServant->_this();
1157   return anObj._retn();
1158 }
1159
1160
1161 LogicalNOT_ptr FilterManager_i::CreateLogicalNOT()
1162 {
1163   SMESH::LogicalNOT_i* aServant = new SMESH::LogicalNOT_i();
1164   SMESH::LogicalNOT_var anObj = aServant->_this();
1165   return anObj._retn();
1166 }
1167
1168
1169 LogicalAND_ptr FilterManager_i::CreateLogicalAND()
1170 {
1171   SMESH::LogicalAND_i* aServant = new SMESH::LogicalAND_i();
1172   SMESH::LogicalAND_var anObj = aServant->_this();
1173   return anObj._retn();
1174 }
1175
1176
1177 LogicalOR_ptr FilterManager_i::CreateLogicalOR()
1178 {
1179   SMESH::LogicalOR_i* aServant = new SMESH::LogicalOR_i();
1180   SMESH::LogicalOR_var anObj = aServant->_this();
1181   return anObj._retn();
1182 }
1183
1184 Filter_ptr FilterManager_i::CreateFilter()
1185 {
1186   SMESH::Filter_i* aServant = new SMESH::Filter_i();
1187   SMESH::Filter_var anObj = aServant->_this();
1188   return anObj._retn();
1189 }
1190
1191 FilterLibrary_ptr FilterManager_i::LoadLibrary( const char* aFileName )
1192 {
1193   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i( aFileName );
1194   SMESH::FilterLibrary_var anObj = aServant->_this();
1195   return anObj._retn();
1196 }
1197
1198 FilterLibrary_ptr FilterManager_i::CreateLibrary()
1199 {
1200   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i();
1201   SMESH::FilterLibrary_var anObj = aServant->_this();
1202   return anObj._retn();
1203 }
1204
1205 CORBA::Boolean FilterManager_i::DeleteLibrary( const char* aFileName )
1206 {
1207   return remove( aFileName ) ? false : true;
1208 }
1209
1210 //=============================================================================
1211 /*!
1212  *  SMESH_Gen_i::CreateFilterManager
1213  *
1214  *  Create filter manager
1215  */
1216 //=============================================================================
1217
1218 SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
1219 {
1220   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
1221   SMESH::FilterManager_var anObj = aFilter->_this();
1222   return anObj._retn();
1223 }
1224
1225
1226 /*
1227                               FILTER
1228 */
1229
1230 //=======================================================================
1231 // name    : Filter_i::Filter_i
1232 // Purpose : Constructor
1233 //=======================================================================
1234 Filter_i::Filter_i()
1235 : myPredicate( NULL )
1236 {}
1237
1238 //=======================================================================
1239 // name    : Filter_i::~Filter_i
1240 // Purpose : Destructor
1241 //=======================================================================
1242 Filter_i::~Filter_i()
1243 {
1244   if ( myPredicate )
1245     myPredicate->Destroy();
1246 }
1247
1248 //=======================================================================
1249 // name    : Filter_i::SetPredicate
1250 // Purpose : Set predicate
1251 //=======================================================================
1252 void Filter_i::SetPredicate( Predicate_ptr thePredicate )
1253 {
1254   if ( myPredicate )
1255     myPredicate->Destroy();
1256
1257   myPredicate = dynamic_cast<Predicate_i*>( SMESH_Gen_i::GetServant( thePredicate ).in() );
1258
1259   if ( myPredicate )
1260   {
1261     myFilter.SetPredicate( myPredicate->GetPredicate() );
1262     myPredicate->Register();
1263   }
1264 }
1265
1266 //=======================================================================
1267 // name    : Filter_i::GetElementType
1268 // Purpose : Get entity type
1269 //=======================================================================
1270 SMESH::ElementType Filter_i::GetElementType()
1271 {
1272   return myPredicate != 0 ? myPredicate->GetElementType() : SMESH::ALL;
1273 }
1274
1275 //=======================================================================
1276 // name    : Filter_i::SetMesh
1277 // Purpose : Set mesh
1278 //=======================================================================
1279 void Filter_i::SetMesh( SMESH_Mesh_ptr theMesh )
1280 {
1281   if ( myPredicate )
1282     myPredicate->SetMesh( theMesh );
1283 }
1284
1285 //=======================================================================
1286 // name    : Filter_i::GetElementsId
1287 // Purpose : Get ids of entities
1288 //=======================================================================
1289 SMESH::long_array* Filter_i::GetElementsId( SMESH_Mesh_ptr theMesh )
1290 {
1291   SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh);
1292   Controls::Filter::TIdSequence aSequence = myFilter.GetElementsId(aMesh);
1293
1294   SMESH::long_array_var anArray = new SMESH::long_array;
1295   long i = 0, iEnd = aSequence.size();
1296
1297   anArray->length( iEnd );
1298   for ( ; i < iEnd; i++ )
1299     anArray[ i ] = aSequence[i];
1300
1301   return anArray._retn();
1302 }
1303
1304 //=======================================================================
1305 // name    : getCriteria
1306 // Purpose : Retrieve criterions from predicate
1307 //=======================================================================
1308 static inline bool getCriteria( Predicate_i*                thePred,
1309                                 SMESH::Filter::Criteria_out theCriteria )
1310 {
1311   int aFType = thePred->GetFunctorType();
1312
1313   switch ( aFType )
1314   {
1315   case FT_FreeBorders:
1316   case FT_FreeEdges:
1317     {
1318       CORBA::ULong i = theCriteria->length();
1319       theCriteria->length( i + 1 );
1320
1321       theCriteria[ i ] = createCriterion();
1322
1323       theCriteria[ i ].Type = aFType;
1324       theCriteria[ i ].TypeOfElement = thePred->GetElementType();
1325       return true;
1326     }
1327   case FT_BelongToGeom:
1328     {
1329       BelongToGeom_i* aPred = dynamic_cast<BelongToGeom_i*>( thePred );
1330
1331       CORBA::ULong i = theCriteria->length();
1332       theCriteria->length( i + 1 );
1333
1334       theCriteria[ i ] = createCriterion();
1335
1336       theCriteria[ i ].Type          = FT_BelongToGeom;
1337       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
1338       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
1339
1340       return true;
1341     }
1342   case FT_BelongToPlane:
1343   case FT_BelongToCylinder:
1344     {
1345       BelongToSurface_i* aPred = dynamic_cast<BelongToSurface_i*>( thePred );
1346
1347       CORBA::ULong i = theCriteria->length();
1348       theCriteria->length( i + 1 );
1349
1350       theCriteria[ i ] = createCriterion();
1351
1352       theCriteria[ i ].Type          = aFType;
1353       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
1354       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
1355       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
1356
1357       return true;
1358     }
1359   case FT_RangeOfIds:
1360     {
1361       RangeOfIds_i* aPred = dynamic_cast<RangeOfIds_i*>( thePred );
1362
1363       CORBA::ULong i = theCriteria->length();
1364       theCriteria->length( i + 1 );
1365
1366       theCriteria[ i ] = createCriterion();
1367
1368       theCriteria[ i ].Type          = FT_RangeOfIds;
1369       theCriteria[ i ].ThresholdStr  = aPred->GetRangeStr();
1370       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
1371
1372       return true;
1373     }
1374   case FT_LessThan:
1375   case FT_MoreThan:
1376   case FT_EqualTo:
1377     {
1378       Comparator_i* aCompar = dynamic_cast<Comparator_i*>( thePred );
1379
1380       CORBA::ULong i = theCriteria->length();
1381       theCriteria->length( i + 1 );
1382
1383       theCriteria[ i ] = createCriterion();
1384
1385       theCriteria[ i ].Type      = aCompar->GetNumFunctor_i()->GetFunctorType();
1386       theCriteria[ i ].Compare   = aFType;
1387       theCriteria[ i ].Threshold = aCompar->GetMargin();
1388       theCriteria[ i ].TypeOfElement = aCompar->GetElementType();
1389
1390       if ( aFType == FT_EqualTo )
1391       {
1392         EqualTo_i* aCompar = dynamic_cast<EqualTo_i*>( thePred );
1393         theCriteria[ i ].Tolerance = aCompar->GetTolerance();
1394       }
1395     }
1396     return true;
1397
1398   case FT_LogicalNOT:
1399     {
1400       Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
1401       getCriteria( aPred, theCriteria );
1402       theCriteria[ theCriteria->length() - 1 ].UnaryOp = FT_LogicalNOT;
1403     }
1404     return true;
1405
1406   case FT_LogicalAND:
1407   case FT_LogicalOR:
1408     {
1409       Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
1410       Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
1411       if ( !getCriteria( aPred1, theCriteria ) )
1412         return false;
1413       theCriteria[ theCriteria->length() - 1 ].BinaryOp = aFType;
1414       return getCriteria( aPred2, theCriteria );
1415     }
1416
1417   case FT_Undefined:
1418     return false;
1419   default:
1420     return false;
1421   }
1422 }
1423
1424 //=======================================================================
1425 // name    : Filter_i::GetCriteria
1426 // Purpose : Retrieve criterions from predicate
1427 //=======================================================================
1428 CORBA::Boolean Filter_i::GetCriteria( SMESH::Filter::Criteria_out theCriteria )
1429 {
1430   theCriteria = new SMESH::Filter::Criteria;
1431   return myPredicate != 0 ? getCriteria( myPredicate, theCriteria ) : true;
1432 }
1433
1434 //=======================================================================
1435 // name    : Filter_i::SetCriteria
1436 // Purpose : Create new predicate and set criterions in it
1437 //=======================================================================
1438 CORBA::Boolean Filter_i::SetCriteria( const SMESH::Filter::Criteria& theCriteria )
1439 {
1440   if ( myPredicate != 0 )
1441     myPredicate->Destroy();
1442
1443     SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
1444     FilterManager_ptr aFilterMgr = aFilter->_this();
1445
1446   // CREATE two lists ( PREDICATES  and LOG OP )
1447
1448   // Criterion
1449   std::list<SMESH::Predicate_ptr> aPredicates;
1450   std::list<int>                  aBinaries;
1451   for ( int i = 0, n = theCriteria.length(); i < n; i++ )
1452   {
1453     int         aCriterion    = theCriteria[ i ].Type;
1454     int         aCompare      = theCriteria[ i ].Compare;
1455     double      aThreshold    = theCriteria[ i ].Threshold;
1456     int         aUnary        = theCriteria[ i ].UnaryOp;
1457     int         aBinary       = theCriteria[ i ].BinaryOp;
1458     double      aTolerance    = theCriteria[ i ].Tolerance;
1459     const char* aThresholdStr = theCriteria[ i ].ThresholdStr;
1460     ElementType aTypeOfElem   = theCriteria[ i ].TypeOfElement;
1461     long        aPrecision    = theCriteria[ i ].Precision;
1462     
1463     SMESH::Predicate_ptr aPredicate = SMESH::Predicate::_nil();
1464     SMESH::NumericalFunctor_ptr aFunctor = SMESH::NumericalFunctor::_nil();
1465
1466     switch ( aCriterion )
1467     {
1468       // Functors
1469       
1470       case SMESH::FT_MultiConnection:
1471         aFunctor = aFilterMgr->CreateMultiConnection();
1472         break;
1473       case SMESH::FT_Length:
1474         aFunctor = aFilterMgr->CreateLength();
1475         break;
1476       case SMESH::FT_Length2D:
1477         aFunctor = aFilterMgr->CreateLength2D();
1478         break;
1479       case SMESH::FT_AspectRatio:
1480         aFunctor = aFilterMgr->CreateAspectRatio();
1481         break;
1482       case SMESH::FT_AspectRatio3D:
1483         aFunctor = aFilterMgr->CreateAspectRatio3D();
1484         break;
1485       case SMESH::FT_Warping:
1486         aFunctor = aFilterMgr->CreateWarping();
1487         break;
1488       case SMESH::FT_MinimumAngle:
1489         aFunctor = aFilterMgr->CreateMinimumAngle();
1490         break;
1491       case SMESH::FT_Taper:
1492         aFunctor = aFilterMgr->CreateTaper();
1493         break;
1494       case SMESH::FT_Skew:
1495         aFunctor = aFilterMgr->CreateSkew();
1496         break;
1497       case SMESH::FT_Area:
1498         aFunctor = aFilterMgr->CreateArea();
1499         break;
1500
1501       // Predicates
1502
1503       case SMESH::FT_FreeBorders:
1504         aPredicate = aFilterMgr->CreateFreeBorders();
1505         break;
1506       case SMESH::FT_FreeEdges:
1507         aPredicate = aFilterMgr->CreateFreeEdges();
1508         break;
1509       case SMESH::FT_BelongToGeom:
1510         {
1511           SMESH::BelongToGeom_ptr tmpPred = aFilterMgr->CreateBelongToGeom();
1512           tmpPred->SetElementType( aTypeOfElem );
1513           tmpPred->SetShapeName( aThresholdStr );
1514           aPredicate = tmpPred;
1515         }
1516         break;
1517       case SMESH::FT_BelongToPlane:
1518       case SMESH::FT_BelongToCylinder:
1519         {
1520           SMESH::BelongToSurface_ptr tmpPred;
1521           if ( aCriterion == SMESH::FT_BelongToPlane )
1522             tmpPred = aFilterMgr->CreateBelongToPlane();
1523           else
1524             tmpPred = aFilterMgr->CreateBelongToCylinder();
1525           tmpPred->SetShapeName( aThresholdStr, aTypeOfElem );
1526           tmpPred->SetTolerance( aTolerance );
1527           aPredicate = tmpPred;
1528         }
1529         break;
1530       case SMESH::FT_RangeOfIds:
1531         {
1532           SMESH::RangeOfIds_ptr tmpPred = aFilterMgr->CreateRangeOfIds();
1533           tmpPred->SetRangeStr( aThresholdStr );
1534           tmpPred->SetElementType( aTypeOfElem );
1535           aPredicate = tmpPred;
1536         }
1537         break;
1538               
1539       default:
1540         continue;
1541     }
1542
1543     // Comparator
1544     if ( !aFunctor->_is_nil() && aPredicate->_is_nil() )
1545     {
1546       SMESH::Comparator_ptr aComparator = SMESH::Comparator::_nil();
1547
1548       if ( aCompare == SMESH::FT_LessThan )
1549         aComparator = aFilterMgr->CreateLessThan();
1550       else if ( aCompare == SMESH::FT_MoreThan )
1551         aComparator = aFilterMgr->CreateMoreThan();
1552       else if ( aCompare == SMESH::FT_EqualTo )
1553         aComparator = aFilterMgr->CreateEqualTo();
1554       else
1555         continue;
1556
1557       aComparator->SetNumFunctor( aFunctor );
1558       aComparator->SetMargin( aThreshold );
1559
1560       if ( aCompare == FT_EqualTo )
1561       {
1562         SMESH::EqualTo_var anEqualTo = SMESH::EqualTo::_narrow( aComparator );
1563         anEqualTo->SetTolerance( aTolerance );
1564       }
1565
1566       aPredicate = aComparator;
1567
1568       aFunctor->SetPrecision( aPrecision );
1569     }
1570
1571     // Logical not
1572     if ( aUnary == FT_LogicalNOT )
1573     {
1574       SMESH::LogicalNOT_ptr aNotPred = aFilterMgr->CreateLogicalNOT();
1575       aNotPred->SetPredicate( aPredicate );
1576       aPredicate = aNotPred;
1577     }
1578
1579     // logical op
1580     aPredicates.push_back( aPredicate );
1581     aBinaries.push_back( aBinary );
1582
1583   } // end of for
1584
1585   // CREATE ONE PREDICATE FROM PREVIOUSLY CREATED MAP
1586
1587   // combine all "AND" operations
1588
1589   std::list<SMESH::Predicate_ptr> aResList;
1590
1591   std::list<SMESH::Predicate_ptr>::iterator aPredIter;
1592   std::list<int>::iterator                  aBinaryIter;
1593
1594   SMESH::Predicate_ptr aPrevPredicate = SMESH::Predicate::_nil();
1595   int aPrevBinary = SMESH::FT_Undefined;
1596
1597   for ( aPredIter = aPredicates.begin(), aBinaryIter = aBinaries.begin();
1598         aPredIter != aPredicates.end() && aBinaryIter != aBinaries.end();
1599         ++aPredIter, ++aBinaryIter )
1600   {
1601     int aCurrBinary = *aBinaryIter;
1602
1603     SMESH::Predicate_ptr aCurrPred = SMESH::Predicate::_nil();
1604
1605     if ( aPrevBinary == SMESH::FT_LogicalAND )
1606     {
1607
1608       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalAND();
1609       aBinaryPred->SetPredicate1( aPrevPredicate );
1610       aBinaryPred->SetPredicate2( *aPredIter );
1611       aCurrPred = aBinaryPred;
1612     }
1613     else
1614       aCurrPred = *aPredIter;
1615
1616     if ( aCurrBinary != SMESH::FT_LogicalAND )
1617       aResList.push_back( aCurrPred );
1618
1619     aPrevPredicate = aCurrPred;
1620     aPrevBinary = aCurrBinary;
1621   }
1622
1623   // combine all "OR" operations
1624
1625   SMESH::Predicate_ptr aResPredicate = SMESH::Predicate::_nil();
1626
1627   if ( aResList.size() == 1 )
1628     aResPredicate = *aResList.begin();
1629   else if ( aResList.size() > 1 )
1630   {
1631     std::list<SMESH::Predicate_ptr>::iterator anIter = aResList.begin();
1632     aResPredicate = *anIter;
1633     anIter++;
1634     for ( ; anIter != aResList.end(); ++anIter )
1635     {
1636       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalOR();
1637       aBinaryPred->SetPredicate1( aResPredicate );
1638       aBinaryPred->SetPredicate2( *anIter );
1639       aResPredicate = aBinaryPred;
1640     }
1641   }
1642
1643   SetPredicate( aResPredicate );
1644
1645   return !aResPredicate->_is_nil();
1646 }
1647
1648 //=======================================================================
1649 // name    : Filter_i::GetPredicate_i
1650 // Purpose : Get implementation of predicate
1651 //=======================================================================
1652 Predicate_i* Filter_i::GetPredicate_i()
1653 {
1654   return myPredicate;
1655 }
1656
1657 //=======================================================================
1658 // name    : Filter_i::GetPredicate
1659 // Purpose : Get predicate
1660 //=======================================================================
1661 Predicate_ptr Filter_i::GetPredicate()
1662 {
1663   if ( myPredicate == 0 )
1664     return SMESH::Predicate::_nil();
1665   else
1666   {
1667     SMESH::Predicate_var anObj = myPredicate->_this();
1668     return anObj._retn();
1669   }
1670 }
1671
1672 /*
1673                             FILTER LIBRARY
1674 */
1675
1676 #define ATTR_TYPE          "type"
1677 #define ATTR_COMPARE       "compare"
1678 #define ATTR_THRESHOLD     "threshold"
1679 #define ATTR_UNARY         "unary"
1680 #define ATTR_BINARY        "binary"
1681 #define ATTR_THRESHOLD_STR "threshold_str"
1682 #define ATTR_TOLERANCE     "tolerance"
1683 #define ATTR_ELEMENT_TYPE  "ElementType"
1684
1685 //=======================================================================
1686 // name    : toString
1687 // Purpose : Convert bool to LDOMString
1688 //=======================================================================
1689 static inline LDOMString toString( const bool val )
1690 {
1691   return val ? "logical not" : "";
1692 }
1693
1694 //=======================================================================
1695 // name    : toBool
1696 // Purpose : Convert LDOMString to bool
1697 //=======================================================================
1698 static inline bool toBool( const LDOMString& theStr )
1699 {
1700   return theStr.equals( "logical not" );
1701 }
1702
1703 //=======================================================================
1704 // name    : toString
1705 // Purpose : Convert double to LDOMString
1706 //=======================================================================
1707 static inline LDOMString toString( const double val )
1708 {
1709   char a[ 255 ];
1710   sprintf( a, "%e", val );
1711   return LDOMString( a );
1712 }
1713
1714 //=======================================================================
1715 // name    : toDouble
1716 // Purpose : Convert LDOMString to double
1717 //=======================================================================
1718 static inline double toDouble( const LDOMString& theStr )
1719 {
1720   return atof( theStr.GetString() );
1721 }
1722
1723 //=======================================================================
1724 // name    : toString
1725 // Purpose : Convert functor type to LDOMString
1726 //=======================================================================
1727 static inline LDOMString toString( const long theType )
1728 {
1729   switch ( theType )
1730   {
1731     case FT_AspectRatio     : return "Aspect ratio";
1732     case FT_Warping         : return "Warping";
1733     case FT_MinimumAngle    : return "Minimum angle";
1734     case FT_Taper           : return "Taper";
1735     case FT_Skew            : return "Skew";
1736     case FT_Area            : return "Area";
1737     case FT_BelongToGeom    : return "Belong to Geom";
1738     case FT_BelongToPlane   : return "Belong to Plane";
1739     case FT_BelongToCylinder: return "Belong to Cylinder";
1740     case FT_RangeOfIds      : return "Range of IDs";
1741     case FT_FreeBorders     : return "Free borders";
1742     case FT_FreeEdges       : return "Free edges";
1743     case FT_MultiConnection : return "Borders at multi-connections";
1744     case FT_Length          : return "Length";
1745     case FT_Length2D        : return "Length2D";
1746     case FT_LessThan        : return "Less than";
1747     case FT_MoreThan        : return "More than";
1748     case FT_EqualTo         : return "Equal to";
1749     case FT_LogicalNOT      : return "Not";
1750     case FT_LogicalAND      : return "And";
1751     case FT_LogicalOR       : return "Or";
1752     case FT_Undefined       : return "";
1753     default                 : return "";
1754   }
1755 }
1756
1757 //=======================================================================
1758 // name    : toFunctorType
1759 // Purpose : Convert LDOMString to functor type
1760 //=======================================================================
1761 static inline SMESH::FunctorType toFunctorType( const LDOMString& theStr )
1762 {
1763   if      ( theStr.equals( "Aspect ratio"                 ) ) return FT_AspectRatio;
1764   else if ( theStr.equals( "Warping"                      ) ) return FT_Warping;
1765   else if ( theStr.equals( "Minimum angle"                ) ) return FT_MinimumAngle;
1766   else if ( theStr.equals( "Taper"                        ) ) return FT_Taper;
1767   else if ( theStr.equals( "Skew"                         ) ) return FT_Skew;
1768   else if ( theStr.equals( "Area"                         ) ) return FT_Area;
1769   else if ( theStr.equals( "Belong to Geom"               ) ) return FT_BelongToGeom;
1770   else if ( theStr.equals( "Belong to Plane"              ) ) return FT_BelongToPlane;
1771   else if ( theStr.equals( "Belong to Cylinder"           ) ) return FT_BelongToCylinder;
1772   else if ( theStr.equals( "Free borders"                 ) ) return FT_FreeBorders;
1773   else if ( theStr.equals( "Free edges"                   ) ) return FT_FreeEdges;
1774   else if ( theStr.equals( "Borders at multi-connections" ) ) return FT_MultiConnection;
1775   else if ( theStr.equals( "Length"                       ) ) return FT_Length;
1776   else if ( theStr.equals( "Range of IDs"                 ) ) return FT_RangeOfIds;
1777   else if ( theStr.equals( "Less than"                    ) ) return FT_LessThan;
1778   else if ( theStr.equals( "More than"                    ) ) return FT_MoreThan;
1779   else if ( theStr.equals( "Equal to"                     ) ) return FT_EqualTo;
1780   else if ( theStr.equals( "Not"                          ) ) return FT_LogicalNOT;
1781   else if ( theStr.equals( "And"                          ) ) return FT_LogicalAND;
1782   else if ( theStr.equals( "Or"                           ) ) return FT_LogicalOR;
1783   else if ( theStr.equals( ""                             ) ) return FT_Undefined;
1784   else  return FT_Undefined;
1785 }
1786
1787 //=======================================================================
1788 // name    : toFunctorType
1789 // Purpose : Convert LDOMString to value of ElementType enumeration
1790 //=======================================================================
1791 static inline SMESH::ElementType toElementType( const LDOMString& theStr )
1792 {
1793   if      ( theStr.equals( "NODE"   ) ) return SMESH::NODE;
1794   else if ( theStr.equals( "EDGE"   ) ) return SMESH::EDGE;
1795   else if ( theStr.equals( "FACE"   ) ) return SMESH::FACE;
1796   else if ( theStr.equals( "VOLUME" ) ) return SMESH::VOLUME;
1797   else                                  return SMESH::ALL;
1798 }
1799
1800 //=======================================================================
1801 // name    : toString
1802 // Purpose : Convert ElementType to string
1803 //=======================================================================
1804 static inline LDOMString toString( const SMESH::ElementType theType )
1805 {
1806   switch ( theType )
1807   {
1808     case SMESH::NODE   : return "NODE";
1809     case SMESH::EDGE   : return "EDGE";
1810     case SMESH::FACE   : return "FACE";
1811     case SMESH::VOLUME : return "VOLUME";
1812     case SMESH::ALL    : return "ALL";
1813     default            : return "";
1814   }
1815 }
1816
1817 //=======================================================================
1818 // name    : findFilter
1819 // Purpose : Find filter in document
1820 //=======================================================================
1821 static LDOM_Element findFilter( const char* theFilterName,
1822                                 const LDOM_Document& theDoc,
1823                                 LDOM_Node* theParent = 0 )
1824 {
1825   LDOM_Element aRootElement = theDoc.getDocumentElement();
1826   if ( aRootElement.isNull() || !aRootElement.hasChildNodes() )
1827     return LDOM_Element();
1828
1829   for ( LDOM_Node aTypeNode = aRootElement.getFirstChild();
1830         !aTypeNode.isNull(); aTypeNode = aTypeNode.getNextSibling() )
1831   {
1832     for ( LDOM_Node aFilter = aTypeNode.getFirstChild();
1833           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
1834     {
1835       LDOM_Element* anElem = ( LDOM_Element* )&aFilter;
1836       if ( anElem->getTagName().equals( LDOMString( "filter" ) ) &&
1837            anElem->getAttribute( "name" ).equals( LDOMString( theFilterName ) ) )
1838       {
1839         if ( theParent != 0  )
1840           *theParent = aTypeNode;
1841         return (LDOM_Element&)aFilter;
1842       }
1843     }
1844   }
1845   return LDOM_Element();
1846 }
1847
1848 //=======================================================================
1849 // name    : getSectionName
1850 // Purpose : Get name of section of filters
1851 //=======================================================================
1852 static const char* getSectionName( const ElementType theType )
1853 {
1854   switch ( theType )
1855   {
1856     case SMESH::NODE   : return "Filters for nodes";
1857     case SMESH::EDGE   : return "Filters for edges";
1858     case SMESH::FACE   : return "Filters for faces";
1859     case SMESH::VOLUME : return "Filters for volumes";
1860     case SMESH::ALL    : return "Filters for elements";
1861     default            : return "";
1862   }
1863 }
1864
1865 //=======================================================================
1866 // name    : getSection
1867 // Purpose : Create section for filters corresponding to the entity type
1868 //=======================================================================
1869 static LDOM_Node getSection( const ElementType theType,
1870                              LDOM_Document&    theDoc,
1871                              const bool        toCreate = false )
1872 {
1873   LDOM_Element aRootElement = theDoc.getDocumentElement();
1874   if ( aRootElement.isNull() )
1875     return LDOM_Node();
1876
1877   // Find section
1878   bool anExist = false;
1879   const char* aSectionName = getSectionName( theType );
1880   if ( strcmp( aSectionName, "" ) == 0 )
1881     return LDOM_Node();
1882   
1883   LDOM_NodeList aSections = theDoc.getElementsByTagName( "section" );
1884   LDOM_Node aNode;
1885   for ( int i = 0, n = aSections.getLength(); i < n; i++ )
1886   {
1887     aNode = aSections.item( i );
1888     LDOM_Element& anItem = ( LDOM_Element& )aNode;
1889     if ( anItem.getAttribute( "name" ).equals( LDOMString( aSectionName ) ) )
1890     {
1891       anExist = true;
1892       break;
1893     }
1894   }
1895
1896   // Create new section if necessary
1897   if ( !anExist )
1898   {
1899     if ( toCreate )
1900     {
1901       LDOM_Element aNewItem = theDoc.createElement( "section" );
1902       aNewItem.setAttribute( "name", aSectionName );
1903       aRootElement.appendChild( aNewItem );
1904       return aNewItem;
1905     }
1906     else
1907       return LDOM_Node();
1908   }
1909   return
1910     aNode;
1911 }
1912
1913 //=======================================================================
1914 // name    : createFilterItem
1915 // Purpose : Create filter item or LDOM document
1916 //=======================================================================
1917 static LDOM_Element createFilterItem( const char*       theName,
1918                                       SMESH::Filter_ptr theFilter,
1919                                       LDOM_Document&    theDoc )
1920 {
1921   // create new filter in document
1922   LDOM_Element aFilterItem = theDoc.createElement( "filter" );
1923   aFilterItem.setAttribute( "name", theName );
1924
1925   // save filter criterions
1926   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
1927
1928   if ( !theFilter->GetCriteria( aCriteria ) )
1929     return LDOM_Element();
1930
1931   for ( CORBA::ULong i = 0, n = aCriteria->length(); i < n; i++ )
1932   {
1933     LDOM_Element aCriterionItem = theDoc.createElement( "criterion" );
1934
1935     aCriterionItem.setAttribute( ATTR_TYPE         , toString( aCriteria[ i ].Type      ) );
1936     aCriterionItem.setAttribute( ATTR_COMPARE      , toString( aCriteria[ i ].Compare   ) );
1937     aCriterionItem.setAttribute( ATTR_THRESHOLD    , toString( aCriteria[ i ].Threshold ) );
1938     aCriterionItem.setAttribute( ATTR_UNARY        , toString( aCriteria[ i ].UnaryOp   ) );
1939     aCriterionItem.setAttribute( ATTR_BINARY       , toString( aCriteria[ i ].BinaryOp  ) );
1940     
1941     aCriterionItem.setAttribute( ATTR_THRESHOLD_STR, (const char*)aCriteria[ i ].ThresholdStr );
1942     aCriterionItem.setAttribute( ATTR_TOLERANCE    , toString( aCriteria[ i ].Tolerance ) );
1943     aCriterionItem.setAttribute( ATTR_ELEMENT_TYPE ,
1944       toString( (SMESH::ElementType)aCriteria[ i ].TypeOfElement ) );
1945
1946     aFilterItem.appendChild( aCriterionItem );
1947   }
1948
1949   return aFilterItem;
1950 }
1951
1952 //=======================================================================
1953 // name    : FilterLibrary_i::FilterLibrary_i
1954 // Purpose : Constructor
1955 //=======================================================================
1956 FilterLibrary_i::FilterLibrary_i( const char* theFileName )
1957 {
1958   myFileName = strdup( theFileName );
1959   SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
1960   myFilterMgr = aFilterMgr->_this();
1961
1962   LDOMParser aParser;
1963
1964   // Try to use existing library file
1965   bool anExists = false;
1966   if ( !aParser.parse( myFileName ) )
1967   {
1968     myDoc = aParser.getDocument();
1969     anExists = true;
1970   }
1971   // Create a new XML document if it doesn't exist
1972   else
1973     myDoc = LDOM_Document::createDocument( LDOMString() );
1974
1975   LDOM_Element aRootElement = myDoc.getDocumentElement();
1976   if ( aRootElement.isNull() )
1977   {
1978     // If the existing document is empty --> try to create a new one
1979     if ( anExists )
1980       myDoc = LDOM_Document::createDocument( LDOMString() );
1981   }
1982 }
1983
1984 //=======================================================================
1985 // name    : FilterLibrary_i::FilterLibrary_i
1986 // Purpose : Constructor
1987 //=======================================================================
1988 FilterLibrary_i::FilterLibrary_i()
1989 {
1990   myFileName = 0;
1991   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
1992   myFilterMgr = aFilter->_this();
1993
1994   myDoc = LDOM_Document::createDocument( LDOMString() );
1995 }
1996
1997 FilterLibrary_i::~FilterLibrary_i()
1998 {
1999   delete myFileName;
2000 }
2001
2002 //=======================================================================
2003 // name    : FilterLibrary_i::Copy
2004 // Purpose : Create filter and initialize it with values from library
2005 //=======================================================================
2006 Filter_ptr FilterLibrary_i::Copy( const char* theFilterName )
2007 {
2008   Filter_ptr aRes;
2009   LDOM_Node aFilter = findFilter( theFilterName, myDoc );
2010
2011   if ( aFilter.isNull() )
2012     return aRes;
2013
2014   std::list<SMESH::Filter::Criterion> aCriteria;
2015     
2016   for ( LDOM_Node aCritNode = aFilter.getFirstChild();
2017         !aCritNode.isNull() ; aCritNode = aCritNode.getNextSibling() )
2018   {
2019     LDOM_Element* aCrit = (LDOM_Element*)&aCritNode;
2020
2021     const char* aTypeStr      = aCrit->getAttribute( ATTR_TYPE          ).GetString();
2022     const char* aCompareStr   = aCrit->getAttribute( ATTR_COMPARE       ).GetString();
2023     const char* aUnaryStr     = aCrit->getAttribute( ATTR_UNARY         ).GetString();
2024     const char* aBinaryStr    = aCrit->getAttribute( ATTR_BINARY        ).GetString();
2025     const char* anElemTypeStr = aCrit->getAttribute( ATTR_ELEMENT_TYPE  ).GetString();
2026     
2027     SMESH::Filter::Criterion aCriterion = createCriterion();
2028
2029     aCriterion.Type          = toFunctorType( aTypeStr );
2030     aCriterion.Compare       = toFunctorType( aCompareStr );
2031     aCriterion.UnaryOp       = toFunctorType( aUnaryStr );
2032     aCriterion.BinaryOp      = toFunctorType( aBinaryStr );
2033     
2034     aCriterion.TypeOfElement = toElementType( anElemTypeStr );
2035
2036     LDOMString str = aCrit->getAttribute( ATTR_THRESHOLD );
2037     int val = 0;
2038     aCriterion.Threshold = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
2039       ? val : atof( str.GetString() );
2040
2041     str = aCrit->getAttribute( ATTR_TOLERANCE );
2042     aCriterion.Tolerance = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
2043       ? val : atof( str.GetString() );
2044
2045     str = aCrit->getAttribute( ATTR_THRESHOLD_STR );
2046     if ( str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val ) )
2047     {
2048       char a[ 255 ];
2049       sprintf( a, "%d", val );
2050       aCriterion.ThresholdStr = strdup( a );
2051     }
2052     else
2053       aCriterion.ThresholdStr = str.GetString();
2054
2055     aCriteria.push_back( aCriterion );
2056   }
2057
2058   SMESH::Filter::Criteria_var aCriteriaVar = new SMESH::Filter::Criteria;
2059   aCriteriaVar->length( aCriteria.size() );
2060   
2061   CORBA::ULong i = 0;
2062   std::list<SMESH::Filter::Criterion>::iterator anIter = aCriteria.begin();
2063   
2064   for( ; anIter != aCriteria.end(); ++anIter )
2065     aCriteriaVar[ i++ ] = *anIter;
2066
2067   aRes = myFilterMgr->CreateFilter();
2068   aRes->SetCriteria( aCriteriaVar.inout() );
2069
2070   return aRes;
2071 }
2072
2073 //=======================================================================
2074 // name    : FilterLibrary_i::SetFileName
2075 // Purpose : Set file name for library
2076 //=======================================================================
2077 void FilterLibrary_i::SetFileName( const char* theFileName )
2078 {
2079   delete myFileName;
2080   myFileName = strdup( theFileName );
2081 }
2082
2083 //=======================================================================
2084 // name    : FilterLibrary_i::GetFileName
2085 // Purpose : Get file name of library
2086 //=======================================================================
2087 char* FilterLibrary_i::GetFileName()
2088 {
2089   return CORBA::string_dup( myFileName );
2090 }
2091
2092 //=======================================================================
2093 // name    : FilterLibrary_i::Add
2094 // Purpose : Add new filter to library
2095 //=======================================================================
2096 CORBA::Boolean FilterLibrary_i::Add( const char* theFilterName, Filter_ptr theFilter )
2097 {
2098   // if filter already in library or entry filter is null do nothing
2099   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
2100   if ( !aFilterNode.isNull() || theFilter->_is_nil() )
2101     return false;
2102
2103   // get section corresponding to the filter type
2104   ElementType anEntType = theFilter->GetElementType();
2105
2106   LDOM_Node aSection = getSection( anEntType, myDoc, true );
2107   if ( aSection.isNull() )
2108     return false;
2109
2110   // create filter item
2111   LDOM_Element aFilterItem = createFilterItem( theFilterName, theFilter, myDoc );
2112   if ( aFilterItem.isNull() )
2113     return false;
2114   else
2115   {
2116     aSection.appendChild( aFilterItem );
2117     return true;
2118   }
2119 }
2120
2121 //=======================================================================
2122 // name    : FilterLibrary_i::Add
2123 // Purpose : Add new filter to library
2124 //=======================================================================
2125 CORBA::Boolean FilterLibrary_i::AddEmpty( const char* theFilterName, ElementType theType )
2126 {
2127   // if filter already in library or entry filter is null do nothing
2128   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
2129   if ( !aFilterNode.isNull() )
2130     return false;
2131
2132   LDOM_Node aSection = getSection( theType, myDoc, true );
2133   if ( aSection.isNull() )
2134     return false;
2135
2136   // create filter item
2137   Filter_var aFilter = myFilterMgr->CreateFilter();
2138   
2139   LDOM_Element aFilterItem = createFilterItem( theFilterName, aFilter, myDoc );
2140   if ( aFilterItem.isNull() )
2141     return false;
2142   else
2143   {
2144     aSection.appendChild( aFilterItem );
2145     return true;
2146   }
2147 }
2148
2149 //=======================================================================
2150 // name    : FilterLibrary_i::Delete
2151 // Purpose : Delete filter from library
2152 //=======================================================================
2153 CORBA::Boolean FilterLibrary_i::Delete ( const char* theFilterName )
2154 {
2155   LDOM_Node aParentNode;
2156   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc, &aParentNode );
2157   if ( aFilterNode.isNull() || aParentNode.isNull() )
2158     return false;
2159
2160   aParentNode.removeChild( aFilterNode );
2161   return true;
2162 }
2163
2164 //=======================================================================
2165 // name      : FilterLibrary_i::Replace 
2166 // Purpose   : Replace existing filter with entry filter.
2167 // IMPORTANT : If filter does not exist it is not created
2168 //=======================================================================
2169 CORBA::Boolean FilterLibrary_i::Replace( const char* theFilterName,
2170                                          const char* theNewName,                 
2171                                          Filter_ptr  theFilter )
2172 {
2173   LDOM_Element aFilterItem = findFilter( theFilterName, myDoc );
2174   if ( aFilterItem.isNull() || theFilter->_is_nil() )
2175     return false;
2176
2177   LDOM_Element aNewItem = createFilterItem( theNewName, theFilter, myDoc );
2178   if ( aNewItem.isNull() )
2179     return false;
2180   else                                                                                          
2181   {
2182     aFilterItem.ReplaceElement( aNewItem );
2183     return true;
2184   }
2185 }
2186
2187 //=======================================================================
2188 // name    : FilterLibrary_i::Save
2189 // Purpose : Save library on disk
2190 //=======================================================================
2191 CORBA::Boolean FilterLibrary_i::Save()
2192 {
2193   if ( myFileName == 0 || strlen( myFileName ) == 0 )
2194     return false;
2195           
2196   FILE* aOutFile = fopen( myFileName, "wt" );
2197   if ( !aOutFile )
2198     return false;
2199
2200   LDOM_XmlWriter aWriter( aOutFile );
2201   aWriter.SetIndentation( 2 );
2202   aWriter << myDoc;
2203   fclose( aOutFile );
2204
2205   return true;
2206 }
2207
2208 //=======================================================================
2209 // name    : FilterLibrary_i::SaveAs
2210 // Purpose : Save library on disk
2211 //=======================================================================
2212 CORBA::Boolean FilterLibrary_i::SaveAs( const char* aFileName )
2213 {
2214   myFileName = strdup ( aFileName );
2215   return Save();
2216 }
2217
2218 //=======================================================================
2219 // name    : FilterLibrary_i::IsPresent
2220 // Purpose : Verify whether filter is in library
2221 //=======================================================================
2222 CORBA::Boolean FilterLibrary_i::IsPresent( const char* theFilterName )
2223 {
2224   return !findFilter( theFilterName, myDoc ).isNull();
2225 }
2226
2227 //=======================================================================
2228 // name    : FilterLibrary_i::NbFilters
2229 // Purpose : Return amount of filters in library
2230 //=======================================================================
2231 CORBA::Long FilterLibrary_i::NbFilters( ElementType theType )
2232 {
2233   string_array_var aNames = GetNames( theType );
2234   return aNames->length();
2235 }
2236
2237 //=======================================================================
2238 // name    : FilterLibrary_i::GetNames
2239 // Purpose : Get names of filters from library
2240 //=======================================================================
2241 string_array* FilterLibrary_i::GetNames( ElementType theType )
2242 {
2243   string_array_var anArray = new string_array;
2244   TColStd_SequenceOfHAsciiString aSeq;
2245
2246   LDOM_Node aSection = getSection( theType, myDoc, false );
2247
2248   if ( !aSection.isNull() )
2249   {
2250     for ( LDOM_Node aFilter = aSection.getFirstChild();
2251           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
2252     {
2253       LDOM_Element& anElem = ( LDOM_Element& )aFilter;
2254       aSeq.Append( new TCollection_HAsciiString(
2255          (Standard_CString)anElem.getAttribute( "name" ).GetString() ) );
2256     }
2257   }
2258
2259   anArray->length( aSeq.Length() );
2260   for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
2261     anArray[ i - 1 ] = CORBA::string_dup( aSeq( i )->ToCString() );
2262
2263   return anArray._retn();
2264 }
2265
2266 //=======================================================================
2267 // name    : FilterLibrary_i::GetAllNames
2268 // Purpose : Get names of filters from library
2269 //=======================================================================
2270 string_array* FilterLibrary_i::GetAllNames()
2271 {
2272   string_array_var aResArray = new string_array;
2273   for ( int type = SMESH::ALL; type <= SMESH::VOLUME; type++ )
2274   {
2275     SMESH::string_array_var aNames = GetNames( (SMESH::ElementType)type );
2276
2277     int aPrevLength = aResArray->length();
2278     aResArray->length( aPrevLength + aNames->length() );
2279     for ( int i = 0, n = aNames->length(); i < n; i++ )
2280       aResArray[ aPrevLength + i ] = aNames[ i ];
2281   }
2282
2283   return aResArray._retn();
2284 }
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304