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