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