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