Salome HOME
Update copyright info (2010->2011)
[modules/smesh.git] / src / SMESH_I / SMESH_Filter_i.cxx
1 //  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 //  File   : SMESH_Filter_i.cxx
25 //  Author : Alexey Petrov, OCC
26 //  Module : SMESH
27 //
28 #include "SMESH_Filter_i.hxx"
29
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_PythonDump.hxx"
32
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshNode.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_ElemIterator.hxx"
37
38 #include "SMESHDS_Mesh.hxx"
39
40 #include <BRep_Tool.hxx>
41 #include <Geom_CylindricalSurface.hxx>
42 #include <Geom_Plane.hxx>
43 #include <LDOMParser.hxx>
44 #include <LDOMString.hxx>
45 #include <LDOM_Document.hxx>
46 #include <LDOM_Element.hxx>
47 #include <LDOM_Node.hxx>
48 #include <LDOM_XmlWriter.hxx>
49 #include <Precision.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <TColStd_ListIteratorOfListOfReal.hxx>
52 #include <TColStd_ListOfInteger.hxx>
53 #include <TColStd_ListOfReal.hxx>
54 #include <TColStd_SequenceOfHAsciiString.hxx>
55 #include <TCollection_HAsciiString.hxx>
56 #include <TopExp.hxx>
57 #include <TopExp_Explorer.hxx>
58 #include <TopoDS.hxx>
59 #include <TopoDS_Face.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopTools_IndexedMapOfShape.hxx>
62
63 using namespace SMESH;
64 using namespace SMESH::Controls;
65
66
67 namespace SMESH
68 {
69   Predicate_i*
70   GetPredicate( Predicate_ptr thePredicate )
71   {
72     return DownCast<Predicate_i*>(thePredicate);
73   }
74 }
75
76
77 /*
78   Class       : BelongToGeom
79   Description : Predicate for verifying whether entity belongs to
80                 specified geometrical support
81 */
82
83 Controls::BelongToGeom::BelongToGeom()
84   : myMeshDS(NULL),
85     myType(SMDSAbs_All),
86     myIsSubshape(false),
87     myTolerance(Precision::Confusion())
88 {}
89
90 void Controls::BelongToGeom::SetMesh( const SMDS_Mesh* theMesh )
91 {
92   myMeshDS = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
93   init();
94 }
95
96 void Controls::BelongToGeom::SetGeom( const TopoDS_Shape& theShape )
97 {
98   myShape = theShape;
99   init();
100 }
101
102 static bool IsSubShape (const TopTools_IndexedMapOfShape& theMap,
103                         const TopoDS_Shape& theShape)
104 {
105   if (theMap.Contains(theShape)) return true;
106
107   if (theShape.ShapeType() == TopAbs_COMPOUND ||
108       theShape.ShapeType() == TopAbs_COMPSOLID)
109   {
110     TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
111     for (; anIt.More(); anIt.Next())
112     {
113       if (!IsSubShape(theMap, anIt.Value())) {
114         return false;
115       }
116     }
117     return true;
118   }
119
120   return false;
121 }
122
123 void Controls::BelongToGeom::init()
124 {
125   if (!myMeshDS || myShape.IsNull()) return;
126
127   // is subshape of main shape?
128   TopoDS_Shape aMainShape = myMeshDS->ShapeToMesh();
129   if (aMainShape.IsNull()) {
130     myIsSubshape = false;
131   }
132   else {
133     TopTools_IndexedMapOfShape aMap;
134     TopExp::MapShapes(aMainShape, aMap);
135     myIsSubshape = IsSubShape(aMap, myShape);
136   }
137
138   if (!myIsSubshape)
139   {
140     myElementsOnShapePtr.reset(new Controls::ElementsOnShape());
141     myElementsOnShapePtr->SetTolerance(myTolerance);
142     myElementsOnShapePtr->SetAllNodes(true); // belong, while false means "lays on"
143     myElementsOnShapePtr->SetMesh(myMeshDS);
144     myElementsOnShapePtr->SetShape(myShape, myType);
145   }
146 }
147
148 static bool IsContains( const SMESHDS_Mesh*     theMeshDS,
149                         const TopoDS_Shape&     theShape,
150                         const SMDS_MeshElement* theElem,
151                         TopAbs_ShapeEnum        theFindShapeEnum,
152                         TopAbs_ShapeEnum        theAvoidShapeEnum = TopAbs_SHAPE )
153 {
154   TopExp_Explorer anExp( theShape,theFindShapeEnum,theAvoidShapeEnum );
155
156   while( anExp.More() )
157   {
158     const TopoDS_Shape& aShape = anExp.Current();
159     if( SMESHDS_SubMesh* aSubMesh = theMeshDS->MeshElements( aShape ) ){
160       if( aSubMesh->Contains( theElem ) )
161         return true;
162     }
163     anExp.Next();
164   }
165   return false;
166 }
167
168 bool Controls::BelongToGeom::IsSatisfy (long theId)
169 {
170   if (myMeshDS == 0 || myShape.IsNull())
171     return false;
172
173   if (!myIsSubshape)
174   {
175     return myElementsOnShapePtr->IsSatisfy(theId);
176   }
177
178   // Case of submesh
179   if (myType == SMDSAbs_Node)
180   {
181     if( const SMDS_MeshNode* aNode = myMeshDS->FindNode( theId ) )
182     {
183       const SMDS_PositionPtr& aPosition = aNode->GetPosition();
184       SMDS_TypeOfPosition aTypeOfPosition = aPosition->GetTypeOfPosition();
185       switch( aTypeOfPosition )
186       {
187       case SMDS_TOP_VERTEX : return IsContains( myMeshDS,myShape,aNode,TopAbs_VERTEX );
188       case SMDS_TOP_EDGE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_EDGE );
189       case SMDS_TOP_FACE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_FACE );
190       case SMDS_TOP_3DSPACE: return IsContains( myMeshDS,myShape,aNode,TopAbs_SHELL );
191       }
192     }
193   }
194   else
195   {
196     if( const SMDS_MeshElement* anElem = myMeshDS->FindElement( theId ) )
197     {
198       if( myType == SMDSAbs_All )
199       {
200         return IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE ) ||
201                IsContains( myMeshDS,myShape,anElem,TopAbs_FACE ) ||
202                IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
203                IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID );
204       }
205       else if( myType == anElem->GetType() )
206       {
207         switch( myType )
208         {
209         case SMDSAbs_Edge  : return IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE );
210         case SMDSAbs_Face  : return IsContains( myMeshDS,myShape,anElem,TopAbs_FACE );
211         case SMDSAbs_Volume: return IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
212                                     IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID );
213         }
214       }
215     }
216   }
217
218   return false;
219 }
220
221 void Controls::BelongToGeom::SetType (SMDSAbs_ElementType theType)
222 {
223   myType = theType;
224   init();
225 }
226
227 SMDSAbs_ElementType Controls::BelongToGeom::GetType() const
228 {
229   return myType;
230 }
231
232 TopoDS_Shape Controls::BelongToGeom::GetShape()
233 {
234   return myShape;
235 }
236
237 const SMESHDS_Mesh* Controls::BelongToGeom::GetMeshDS() const
238 {
239   return myMeshDS;
240 }
241
242 void Controls::BelongToGeom::SetTolerance (double theTolerance)
243 {
244   myTolerance = theTolerance;
245   if (!myIsSubshape)
246     init();
247 }
248
249 double Controls::BelongToGeom::GetTolerance()
250 {
251   return myTolerance;
252 }
253
254 /*
255   Class       : LyingOnGeom
256   Description : Predicate for verifying whether entiy lying or partially lying on
257                 specified geometrical support
258 */
259
260 Controls::LyingOnGeom::LyingOnGeom()
261   : myMeshDS(NULL),
262     myType(SMDSAbs_All),
263     myIsSubshape(false),
264     myTolerance(Precision::Confusion())
265 {}
266
267 void Controls::LyingOnGeom::SetMesh( const SMDS_Mesh* theMesh )
268 {
269   myMeshDS = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
270   init();
271 }
272
273 void Controls::LyingOnGeom::SetGeom( const TopoDS_Shape& theShape )
274 {
275   myShape = theShape;
276   init();
277 }
278
279 void Controls::LyingOnGeom::init()
280 {
281   if (!myMeshDS || myShape.IsNull()) return;
282
283   // is subshape of main shape?
284   TopoDS_Shape aMainShape = myMeshDS->ShapeToMesh();
285   if (aMainShape.IsNull()) {
286     myIsSubshape = false;
287   }
288   else {
289     TopTools_IndexedMapOfShape aMap;
290     TopExp::MapShapes(aMainShape, aMap);
291     myIsSubshape = IsSubShape(aMap, myShape);
292   }
293
294   if (!myIsSubshape)
295   {
296     myElementsOnShapePtr.reset(new Controls::ElementsOnShape());
297     myElementsOnShapePtr->SetTolerance(myTolerance);
298     myElementsOnShapePtr->SetAllNodes(false); // lays on, while true means "belong"
299     myElementsOnShapePtr->SetMesh(myMeshDS);
300     myElementsOnShapePtr->SetShape(myShape, myType);
301   }
302 }
303
304 bool Controls::LyingOnGeom::IsSatisfy( long theId )
305 {
306   if ( myMeshDS == 0 || myShape.IsNull() )
307     return false;
308
309   if (!myIsSubshape)
310   {
311     return myElementsOnShapePtr->IsSatisfy(theId);
312   }
313
314   // Case of submesh
315   if( myType == SMDSAbs_Node )
316   {
317     if( const SMDS_MeshNode* aNode = myMeshDS->FindNode( theId ) )
318     {
319       const SMDS_PositionPtr& aPosition = aNode->GetPosition();
320       SMDS_TypeOfPosition aTypeOfPosition = aPosition->GetTypeOfPosition();
321       switch( aTypeOfPosition )
322       {
323       case SMDS_TOP_VERTEX : return IsContains( myMeshDS,myShape,aNode,TopAbs_VERTEX );
324       case SMDS_TOP_EDGE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_EDGE );
325       case SMDS_TOP_FACE   : return IsContains( myMeshDS,myShape,aNode,TopAbs_FACE );
326       case SMDS_TOP_3DSPACE: return IsContains( myMeshDS,myShape,aNode,TopAbs_SHELL );
327       }
328     }
329   }
330   else
331   {
332     if( const SMDS_MeshElement* anElem = myMeshDS->FindElement( theId ) )
333     {
334       if( myType == SMDSAbs_All )
335       {
336         return Contains( myMeshDS,myShape,anElem,TopAbs_EDGE ) ||
337                Contains( myMeshDS,myShape,anElem,TopAbs_FACE ) ||
338                Contains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
339                Contains( myMeshDS,myShape,anElem,TopAbs_SOLID );
340       }
341       else if( myType == anElem->GetType() )
342       {
343         switch( myType )
344         {
345         case SMDSAbs_Edge  : return Contains( myMeshDS,myShape,anElem,TopAbs_EDGE );
346         case SMDSAbs_Face  : return Contains( myMeshDS,myShape,anElem,TopAbs_FACE );
347         case SMDSAbs_Volume: return Contains( myMeshDS,myShape,anElem,TopAbs_SHELL )||
348                                     Contains( myMeshDS,myShape,anElem,TopAbs_SOLID );
349         }
350       }
351     }
352   }
353
354   return false;
355 }
356
357 void Controls::LyingOnGeom::SetType( SMDSAbs_ElementType theType )
358 {
359   myType = theType;
360   init();
361 }
362
363 SMDSAbs_ElementType Controls::LyingOnGeom::GetType() const
364 {
365   return myType;
366 }
367
368 TopoDS_Shape Controls::LyingOnGeom::GetShape()
369 {
370   return myShape;
371 }
372
373 const SMESHDS_Mesh* Controls::LyingOnGeom::GetMeshDS() const
374 {
375   return myMeshDS;
376 }
377
378 void Controls::LyingOnGeom::SetTolerance (double theTolerance)
379 {
380   myTolerance = theTolerance;
381   if (!myIsSubshape)
382     init();
383 }
384
385 double Controls::LyingOnGeom::GetTolerance()
386 {
387   return myTolerance;
388 }
389
390 bool Controls::LyingOnGeom::Contains( const SMESHDS_Mesh*     theMeshDS,
391                                       const TopoDS_Shape&     theShape,
392                                       const SMDS_MeshElement* theElem,
393                                       TopAbs_ShapeEnum        theFindShapeEnum,
394                                       TopAbs_ShapeEnum        theAvoidShapeEnum )
395 {
396   if (IsContains(theMeshDS, theShape, theElem, theFindShapeEnum, theAvoidShapeEnum))
397     return true;
398
399   TopTools_IndexedMapOfShape aSubShapes;
400   TopExp::MapShapes( theShape, aSubShapes );
401
402   for (int i = 1; i <= aSubShapes.Extent(); i++)
403   {
404     const TopoDS_Shape& aShape = aSubShapes.FindKey(i);
405
406     if( SMESHDS_SubMesh* aSubMesh = theMeshDS->MeshElements( aShape ) ){
407       if( aSubMesh->Contains( theElem ) )
408         return true;
409
410       SMDS_NodeIteratorPtr aNodeIt = aSubMesh->GetNodes();
411       while ( aNodeIt->more() )
412       {
413         const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(aNodeIt->next());
414         SMDS_ElemIteratorPtr anElemIt = aNode->GetInverseElementIterator();
415         while ( anElemIt->more() )
416         {
417           const SMDS_MeshElement* anElement = static_cast<const SMDS_MeshElement*>(anElemIt->next());
418           if (anElement == theElem)
419             return true;
420         }
421       }
422     }
423   }
424   return false;
425 }
426
427
428 /*
429                             AUXILIARY METHODS
430 */
431
432 inline
433 const SMDS_Mesh*
434 MeshPtr2SMDSMesh( SMESH_Mesh_ptr theMesh )
435 {
436   SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theMesh);
437   return anImplPtr ? anImplPtr->GetImpl().GetMeshDS() : 0;
438 }
439
440 inline
441 SMESH::long_array*
442 toArray( const TColStd_ListOfInteger& aList )
443 {
444   SMESH::long_array_var anArray = new SMESH::long_array;
445   anArray->length( aList.Extent() );
446   TColStd_ListIteratorOfListOfInteger anIter( aList );
447   int i = 0;
448   for( ; anIter.More(); anIter.Next() )
449     anArray[ i++ ] = anIter.Value();
450
451   return anArray._retn();
452 }
453
454 inline
455 SMESH::double_array*
456 toArray( const TColStd_ListOfReal& aList )
457 {
458   SMESH::double_array_var anArray = new SMESH::double_array;
459   anArray->length( aList.Extent() );
460   TColStd_ListIteratorOfListOfReal anIter( aList );
461   int i = 0;
462   for( ; anIter.More(); anIter.Next() )
463     anArray[ i++ ] = anIter.Value();
464
465   return anArray._retn();
466 }
467
468 static SMESH::Filter::Criterion createCriterion()
469 {
470   SMESH::Filter::Criterion aCriterion;
471
472   aCriterion.Type          = FT_Undefined;
473   aCriterion.Compare       = FT_Undefined;
474   aCriterion.Threshold     = 0;
475   aCriterion.UnaryOp       = FT_Undefined;
476   aCriterion.BinaryOp      = FT_Undefined;
477   aCriterion.ThresholdStr  = "";
478   aCriterion.ThresholdID   = "";
479   aCriterion.Tolerance     = Precision::Confusion();
480   aCriterion.TypeOfElement = SMESH::ALL;
481   aCriterion.Precision     = -1;
482
483   return aCriterion;
484 }
485
486 static TopoDS_Shape getShapeByName( const char* theName )
487 {
488   if ( theName != 0 )
489   {
490     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
491     SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
492     if (!CORBA::is_nil(aStudy))
493     {
494       SALOMEDS::Study::ListOfSObject_var aList =
495         aStudy->FindObjectByName( theName, "GEOM" );
496       if ( aList->length() > 0 )
497       {
498         GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aList[ 0 ]->GetObject() );
499         if ( !aGeomObj->_is_nil() )
500         {
501           GEOM::GEOM_Gen_ptr aGEOMGen = SMESH_Gen_i::GetGeomEngine();
502           TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, aGeomObj );
503           return aLocShape;
504         }
505       }
506     }
507   }
508   return TopoDS_Shape();
509 }
510
511 static TopoDS_Shape getShapeByID (const char* theID)
512 {
513   if (theID != 0 && theID != "") {
514     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
515     SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
516     if (aStudy != 0) {
517       SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theID);
518       SALOMEDS::GenericAttribute_var anAttr;
519       if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeIOR")) {
520         SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
521         CORBA::String_var aVal = anIOR->Value();
522         CORBA::Object_var obj = aStudy->ConvertIORToObject(aVal);
523         GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(obj);
524       
525         if (!aGeomObj->_is_nil()) {
526           GEOM::GEOM_Gen_ptr aGEOMGen = SMESH_Gen_i::GetGeomEngine();
527           TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, aGeomObj );
528           return aLocShape;
529         }
530       }
531     }
532   }
533   return TopoDS_Shape();
534 }
535
536 static char* getShapeNameByID (const char* theID)
537 {
538   char* aName = (char*)"";
539
540   if (theID != 0 && theID != "") {
541     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
542     SALOMEDS::Study_ptr aStudy = aSMESHGen->GetCurrentStudy();
543     if (aStudy != 0) {
544       //SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( theID );
545       SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theID);
546       SALOMEDS::GenericAttribute_var anAttr;
547       if (!aSObj->_is_nil() && aSObj->FindAttribute(anAttr, "AttributeName")) {
548         SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow(anAttr);
549         aName = aNameAttr->Value();
550       }
551     }
552   }
553
554   return aName;
555 }
556
557 /*
558                                 FUNCTORS
559 */
560
561 /*
562   Class       : Functor_i
563   Description : An abstact class for all functors
564 */
565 Functor_i::Functor_i():
566   SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
567 {
568   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method  
569   //PortableServer::ObjectId_var anObjectId =
570   //  SMESH_Gen_i::GetPOA()->activate_object( this );
571 }
572
573 Functor_i::~Functor_i()
574 {
575   //TPythonDump()<<this<<".Destroy()";
576 }
577
578 void Functor_i::SetMesh( SMESH_Mesh_ptr theMesh )
579 {
580   myFunctorPtr->SetMesh( MeshPtr2SMDSMesh( theMesh ) );
581   TPythonDump()<<this<<".SetMesh("<<theMesh<<")";
582 }
583
584 ElementType Functor_i::GetElementType()
585 {
586   return ( ElementType )myFunctorPtr->GetType();
587 }
588
589
590 /*
591   Class       : NumericalFunctor_i
592   Description : Base class for numerical functors
593 */
594 CORBA::Double NumericalFunctor_i::GetValue( CORBA::Long theId )
595 {
596   return myNumericalFunctorPtr->GetValue( theId );
597 }
598
599 void NumericalFunctor_i::SetPrecision( CORBA::Long thePrecision )
600 {
601   myNumericalFunctorPtr->SetPrecision( thePrecision );
602   TPythonDump()<<this<<".SetPrecision("<<thePrecision<<")";
603 }
604
605 CORBA::Long NumericalFunctor_i::GetPrecision()
606 {
607  return myNumericalFunctorPtr->GetPrecision();
608 }
609
610 Controls::NumericalFunctorPtr NumericalFunctor_i::GetNumericalFunctor()
611 {
612   return myNumericalFunctorPtr;
613 }
614
615
616 /*
617   Class       : SMESH_MinimumAngle
618   Description : Functor for calculation of minimum angle
619 */
620 MinimumAngle_i::MinimumAngle_i()
621 {
622   myNumericalFunctorPtr.reset( new Controls::MinimumAngle() );
623   myFunctorPtr = myNumericalFunctorPtr;
624 }
625
626 FunctorType MinimumAngle_i::GetFunctorType()
627 {
628   return SMESH::FT_MinimumAngle;
629 }
630
631
632 /*
633   Class       : AspectRatio
634   Description : Functor for calculating aspect ratio
635 */
636 AspectRatio_i::AspectRatio_i()
637 {
638   myNumericalFunctorPtr.reset( new Controls::AspectRatio() );
639   myFunctorPtr = myNumericalFunctorPtr;
640 }
641
642 FunctorType AspectRatio_i::GetFunctorType()
643 {
644   return SMESH::FT_AspectRatio;
645 }
646
647
648 /*
649   Class       : AspectRatio3D
650   Description : Functor for calculating aspect ratio 3D
651 */
652 AspectRatio3D_i::AspectRatio3D_i()
653 {
654   myNumericalFunctorPtr.reset( new Controls::AspectRatio3D() );
655   myFunctorPtr = myNumericalFunctorPtr;
656 }
657
658 FunctorType AspectRatio3D_i::GetFunctorType()
659 {
660   return SMESH::FT_AspectRatio3D;
661 }
662
663
664 /*
665   Class       : Warping_i
666   Description : Functor for calculating warping
667 */
668 Warping_i::Warping_i()
669 {
670   myNumericalFunctorPtr.reset( new Controls::Warping() );
671   myFunctorPtr = myNumericalFunctorPtr;
672 }
673
674 FunctorType Warping_i::GetFunctorType()
675 {
676   return SMESH::FT_Warping;
677 }
678
679
680 /*
681   Class       : Taper_i
682   Description : Functor for calculating taper
683 */
684 Taper_i::Taper_i()
685 {
686   myNumericalFunctorPtr.reset( new Controls::Taper() );
687   myFunctorPtr = myNumericalFunctorPtr;
688 }
689
690 FunctorType Taper_i::GetFunctorType()
691 {
692   return SMESH::FT_Taper;
693 }
694
695
696 /*
697   Class       : Skew_i
698   Description : Functor for calculating skew in degrees
699 */
700 Skew_i::Skew_i()
701 {
702   myNumericalFunctorPtr.reset( new Controls::Skew() );
703   myFunctorPtr = myNumericalFunctorPtr;
704 }
705
706 FunctorType Skew_i::GetFunctorType()
707 {
708   return SMESH::FT_Skew;
709 }
710
711 /*
712   Class       : Area_i
713   Description : Functor for calculating area
714 */
715 Area_i::Area_i()
716 {
717   myNumericalFunctorPtr.reset( new Controls::Area() );
718   myFunctorPtr = myNumericalFunctorPtr;
719 }
720
721 FunctorType Area_i::GetFunctorType()
722 {
723   return SMESH::FT_Area;
724 }
725
726 /*
727   Class       : Volume3D_i
728   Description : Functor for calculating volume of 3D element
729 */
730 Volume3D_i::Volume3D_i()
731 {
732   myNumericalFunctorPtr.reset( new Controls::Volume() );
733   myFunctorPtr = myNumericalFunctorPtr;
734 }
735
736 FunctorType Volume3D_i::GetFunctorType()
737 {
738   return SMESH::FT_Volume3D;
739 }
740
741 /*
742   Class       : Length_i
743   Description : Functor for calculating length off edge
744 */
745 Length_i::Length_i()
746 {
747   myNumericalFunctorPtr.reset( new Controls::Length() );
748   myFunctorPtr = myNumericalFunctorPtr;
749 }
750
751 FunctorType Length_i::GetFunctorType()
752 {
753   return SMESH::FT_Length;
754 }
755
756 /*
757   Class       : Length2D_i
758   Description : Functor for calculating length of edge
759 */
760 Length2D_i::Length2D_i()
761 {
762   myNumericalFunctorPtr.reset( new Controls::Length2D() );
763   myFunctorPtr = myNumericalFunctorPtr;
764 }
765
766 FunctorType Length2D_i::GetFunctorType()
767 {
768   return SMESH::FT_Length2D;
769 }
770
771 SMESH::Length2D::Values* Length2D_i::GetValues()
772 {
773   INFOS("Length2D_i::GetValues");
774   SMESH::Controls::Length2D::TValues aValues;
775   (dynamic_cast<SMESH::Controls::Length2D*>(myFunctorPtr.get()))->GetValues( aValues );
776
777   long i = 0, iEnd = aValues.size();
778
779   SMESH::Length2D::Values_var aResult = new SMESH::Length2D::Values(iEnd);
780   aResult->length(iEnd);
781
782   SMESH::Controls::Length2D::TValues::const_iterator anIter;
783   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
784   {
785     const SMESH::Controls::Length2D::Value&  aVal = *anIter;
786     SMESH::Length2D::Value &aValue = aResult[ i ];
787
788     aValue.myLength = aVal.myLength;
789     aValue.myPnt1 = aVal.myPntId[ 0 ];
790     aValue.myPnt2 = aVal.myPntId[ 1 ];
791   }
792
793   INFOS("Length2D_i::GetValuess~");
794   return aResult._retn();
795 }
796
797 /*
798   Class       : MultiConnection_i
799   Description : Functor for calculating number of faces conneted to the edge
800 */
801 MultiConnection_i::MultiConnection_i()
802 {
803   myNumericalFunctorPtr.reset( new Controls::MultiConnection() );
804   myFunctorPtr = myNumericalFunctorPtr;
805 }
806
807 FunctorType MultiConnection_i::GetFunctorType()
808 {
809   return SMESH::FT_MultiConnection;
810 }
811
812 /*
813   Class       : MultiConnection2D_i
814   Description : Functor for calculating number of faces conneted to the edge
815 */
816 MultiConnection2D_i::MultiConnection2D_i()
817 {
818   myNumericalFunctorPtr.reset( new Controls::MultiConnection2D() );
819   myFunctorPtr = myNumericalFunctorPtr;
820 }
821
822 FunctorType MultiConnection2D_i::GetFunctorType()
823 {
824   return SMESH::FT_MultiConnection2D;
825 }
826
827 SMESH::MultiConnection2D::Values* MultiConnection2D_i::GetValues()
828 {
829   INFOS("MultiConnection2D_i::GetValues");
830   SMESH::Controls::MultiConnection2D::MValues aValues;
831   (dynamic_cast<SMESH::Controls::MultiConnection2D*>(myFunctorPtr.get()))->GetValues( aValues );
832   
833   long i = 0, iEnd = aValues.size();
834
835   SMESH::MultiConnection2D::Values_var aResult = new SMESH::MultiConnection2D::Values(iEnd);
836   aResult->length(iEnd);
837
838   SMESH::Controls::MultiConnection2D::MValues::const_iterator anIter;
839   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
840   {
841     const SMESH::Controls::MultiConnection2D::Value&  aVal = (*anIter).first;
842     SMESH::MultiConnection2D::Value &aValue = aResult[ i ];
843
844     aValue.myPnt1 = aVal.myPntId[ 0 ];
845     aValue.myPnt2 = aVal.myPntId[ 1 ];
846     aValue.myNbConnects = (*anIter).second;
847   }
848
849   INFOS("Multiconnection2D_i::GetValuess~");
850   return aResult._retn();
851 }
852
853 /*
854                             PREDICATES
855 */
856
857
858 /*
859   Class       : Predicate_i
860   Description : Base class for all predicates
861 */
862 CORBA::Boolean Predicate_i::IsSatisfy( CORBA::Long theId )
863 {
864   return myPredicatePtr->IsSatisfy( theId );
865 }
866
867 Controls::PredicatePtr Predicate_i::GetPredicate()
868 {
869   return myPredicatePtr;
870 }
871
872 /*
873   Class       : BadOrientedVolume_i
874   Description : Verify whether a mesh volume is incorrectly oriented from
875                 the point of view of MED convention
876 */
877 BadOrientedVolume_i::BadOrientedVolume_i()
878 {
879   Controls::PredicatePtr control( new Controls::BadOrientedVolume() );
880   myFunctorPtr = myPredicatePtr = control;
881 };
882
883 FunctorType BadOrientedVolume_i::GetFunctorType()
884 {
885   return SMESH::FT_BadOrientedVolume;
886 }
887
888 /*
889   Class       : BelongToGeom_i
890   Description : Predicate for selection on geometrical support
891 */
892 BelongToGeom_i::BelongToGeom_i()
893 {
894   myBelongToGeomPtr.reset( new Controls::BelongToGeom() );
895   myFunctorPtr = myPredicatePtr = myBelongToGeomPtr;
896   myShapeName = 0;
897   myShapeID   = 0;
898 }
899
900 BelongToGeom_i::~BelongToGeom_i()
901 {
902   delete myShapeName;
903   delete myShapeID;
904 }
905
906 void BelongToGeom_i::SetGeom( GEOM::GEOM_Object_ptr theGeom )
907 {
908   if ( theGeom->_is_nil() )
909     return;
910   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
911   GEOM::GEOM_Gen_ptr aGEOMGen = SMESH_Gen_i::GetGeomEngine();
912   TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, theGeom );
913   myBelongToGeomPtr->SetGeom( aLocShape );
914   TPythonDump()<<this<<".SetGeom("<<theGeom<<")";
915 }
916
917 void BelongToGeom_i::SetGeom( const TopoDS_Shape& theShape )
918 {
919   myBelongToGeomPtr->SetGeom( theShape );
920 }
921
922 void BelongToGeom_i::SetElementType(ElementType theType){
923   myBelongToGeomPtr->SetType(SMDSAbs_ElementType(theType));
924   TPythonDump()<<this<<".SetElementType("<<theType<<")";
925 }
926
927 FunctorType BelongToGeom_i::GetFunctorType()
928 {
929   return SMESH::FT_BelongToGeom;
930 }
931
932 void BelongToGeom_i::SetShapeName( const char* theName )
933 {
934   delete myShapeName;
935   myShapeName = strdup( theName );
936   myBelongToGeomPtr->SetGeom( getShapeByName( myShapeName ) );
937   TPythonDump()<<this<<".SetShapeName('"<<theName<<"')";
938 }
939
940 void BelongToGeom_i::SetShape( const char* theID, const char* theName )
941 {
942   delete myShapeName;
943   myShapeName = strdup( theName );
944   delete myShapeID;
945   if ( theID )
946     myShapeID = strdup( theID );
947   else
948     myShapeID = 0;
949
950   if ( myShapeID && strcmp(myShapeName, getShapeNameByID(myShapeID)) == 0 )
951     myBelongToGeomPtr->SetGeom( getShapeByID(myShapeID) );
952   else
953     myBelongToGeomPtr->SetGeom( getShapeByName( myShapeName ) );
954 }
955
956 char* BelongToGeom_i::GetShapeName()
957 {
958   return CORBA::string_dup( myShapeName );
959 }
960
961 char* BelongToGeom_i::GetShapeID()
962 {
963   return CORBA::string_dup( myShapeID );
964 }
965
966 void BelongToGeom_i::SetTolerance( CORBA::Double theToler )
967 {
968   myBelongToGeomPtr->SetTolerance( theToler );
969   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
970 }
971
972 CORBA::Double BelongToGeom_i::GetTolerance()
973 {
974   return myBelongToGeomPtr->GetTolerance();
975 }
976
977 /*
978   Class       : BelongToSurface_i
979   Description : Predicate for selection on geometrical support
980 */
981 BelongToSurface_i::BelongToSurface_i( const Handle(Standard_Type)& theSurfaceType )
982 {
983   myElementsOnSurfacePtr.reset( new Controls::ElementsOnSurface() );
984   myFunctorPtr = myPredicatePtr = myElementsOnSurfacePtr;
985   myShapeName = 0;
986   myShapeID   = 0;
987   mySurfaceType = theSurfaceType;
988 }
989
990 BelongToSurface_i::~BelongToSurface_i()
991 {
992   delete myShapeName;
993   delete myShapeID;
994 }
995
996 void BelongToSurface_i::SetSurface( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
997 {
998   if ( theGeom->_is_nil() )
999     return;
1000   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
1001   GEOM::GEOM_Gen_ptr aGEOMGen = SMESH_Gen_i::GetGeomEngine();
1002   TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, theGeom );
1003
1004   if ( aLocShape.ShapeType() == TopAbs_FACE )
1005   {
1006     Handle(Geom_Surface) aSurf = BRep_Tool::Surface( TopoDS::Face( aLocShape ) );
1007     if ( !aSurf.IsNull() && aSurf->DynamicType() == mySurfaceType )
1008     {
1009       myElementsOnSurfacePtr->SetSurface( aLocShape, (SMDSAbs_ElementType)theType );
1010       return;
1011     }
1012   }
1013
1014   myElementsOnSurfacePtr->SetSurface( TopoDS_Shape(), (SMDSAbs_ElementType)theType );
1015 }
1016
1017 void BelongToSurface_i::SetShapeName( const char* theName, ElementType theType )
1018 {
1019   delete myShapeName;
1020   myShapeName = strdup( theName );
1021   myElementsOnSurfacePtr->SetSurface( getShapeByName( myShapeName ), (SMDSAbs_ElementType)theType );
1022   TPythonDump()<<this<<".SetShapeName('"<<theName<<"',"<<theType<<")";
1023 }
1024
1025 void BelongToSurface_i::SetShape( const char* theID,  const char* theName, ElementType theType )
1026 {
1027   delete myShapeName;
1028   myShapeName = strdup( theName );
1029   delete myShapeID;
1030   if ( theID )
1031     myShapeID = strdup( theID );
1032   else
1033     myShapeID = 0;
1034   
1035   if ( myShapeID && strcmp(myShapeName, getShapeNameByID(myShapeID)) == 0 )
1036     myElementsOnSurfacePtr->SetSurface( getShapeByID(myShapeID), (SMDSAbs_ElementType)theType );
1037   else
1038     myElementsOnSurfacePtr->SetSurface( getShapeByName( myShapeName ), (SMDSAbs_ElementType)theType );
1039 }
1040
1041 char* BelongToSurface_i::GetShapeName()
1042 {
1043   return CORBA::string_dup( myShapeName );
1044 }
1045
1046 char* BelongToSurface_i::GetShapeID()
1047 {
1048   return CORBA::string_dup( myShapeID );
1049 }
1050
1051 void BelongToSurface_i::SetTolerance( CORBA::Double theToler )
1052 {
1053   myElementsOnSurfacePtr->SetTolerance( theToler );
1054   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1055 }
1056
1057 CORBA::Double BelongToSurface_i::GetTolerance()
1058 {
1059   return myElementsOnSurfacePtr->GetTolerance();
1060 }
1061
1062 void BelongToSurface_i::SetUseBoundaries( CORBA::Boolean theUseBndRestrictions )
1063 {
1064   myElementsOnSurfacePtr->SetUseBoundaries( theUseBndRestrictions );
1065   TPythonDump()<<this<<".SetUseBoundaries( " << theUseBndRestrictions << " )";
1066 }
1067
1068 CORBA::Boolean BelongToSurface_i::GetUseBoundaries()
1069 {
1070   return myElementsOnSurfacePtr->GetUseBoundaries();
1071 }
1072
1073
1074 /*
1075   Class       : BelongToPlane_i
1076   Description : Verify whether mesh element lie in pointed Geom planar object
1077 */
1078
1079 BelongToPlane_i::BelongToPlane_i()
1080 : BelongToSurface_i( STANDARD_TYPE( Geom_Plane ) )
1081 {
1082 }
1083
1084 void BelongToPlane_i::SetPlane( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1085 {
1086   BelongToSurface_i::SetSurface( theGeom, theType );
1087   TPythonDump()<<this<<".SetPlane("<<theGeom<<","<<theType<<")";
1088 }
1089
1090 FunctorType BelongToPlane_i::GetFunctorType()
1091 {
1092   return FT_BelongToPlane;
1093 }
1094
1095 /*
1096   Class       : BelongToCylinder_i
1097   Description : Verify whether mesh element lie in pointed Geom planar object
1098 */
1099
1100 BelongToCylinder_i::BelongToCylinder_i()
1101 : BelongToSurface_i( STANDARD_TYPE( Geom_CylindricalSurface ) )
1102 {
1103 }
1104
1105 void BelongToCylinder_i::SetCylinder( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1106 {
1107   BelongToSurface_i::SetSurface( theGeom, theType );
1108   TPythonDump()<<this<<".SetCylinder("<<theGeom<<","<<theType<<")";
1109 }
1110
1111 FunctorType BelongToCylinder_i::GetFunctorType()
1112 {
1113   return FT_BelongToCylinder;
1114 }
1115
1116 /*
1117   Class       : BelongToGenSurface_i
1118   Description : Verify whether mesh element lie in pointed Geom planar object
1119 */
1120
1121 BelongToGenSurface_i::BelongToGenSurface_i()
1122 : BelongToSurface_i( STANDARD_TYPE( Geom_CylindricalSurface ) )
1123 {
1124 }
1125
1126 void BelongToGenSurface_i::SetSurface( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1127 {
1128   if ( theGeom->_is_nil() )
1129     return;
1130   TopoDS_Shape aLocShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theGeom );
1131   if ( !aLocShape.IsNull() && aLocShape.ShapeType() != TopAbs_FACE )
1132     aLocShape.Nullify();
1133   
1134   BelongToSurface_i::myElementsOnSurfacePtr->SetSurface( aLocShape, (SMDSAbs_ElementType)theType );
1135   TPythonDump()<<this<<".SetGenSurface("<<theGeom<<","<<theType<<")";
1136 }
1137
1138 FunctorType BelongToGenSurface_i::GetFunctorType()
1139 {
1140   return FT_BelongToGenSurface;
1141 }
1142
1143 /*
1144   Class       : LyingOnGeom_i
1145   Description : Predicate for selection on geometrical support
1146 */
1147 LyingOnGeom_i::LyingOnGeom_i()
1148 {
1149   myLyingOnGeomPtr.reset( new Controls::LyingOnGeom() );
1150   myFunctorPtr = myPredicatePtr = myLyingOnGeomPtr;
1151   myShapeName = 0;
1152   myShapeID = 0;
1153 }
1154
1155 LyingOnGeom_i::~LyingOnGeom_i()
1156 {
1157   delete myShapeName;
1158   delete myShapeID;
1159 }
1160
1161 void LyingOnGeom_i::SetGeom( GEOM::GEOM_Object_ptr theGeom )
1162 {
1163   if ( theGeom->_is_nil() )
1164     return;
1165   SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
1166   GEOM::GEOM_Gen_ptr aGEOMGen = SMESH_Gen_i::GetGeomEngine();
1167   TopoDS_Shape aLocShape = aSMESHGen->GetShapeReader()->GetShape( aGEOMGen, theGeom );
1168   myLyingOnGeomPtr->SetGeom( aLocShape );
1169   TPythonDump()<<this<<".SetGeom("<<theGeom<<")";
1170 }
1171
1172 void LyingOnGeom_i::SetGeom( const TopoDS_Shape& theShape )
1173 {
1174   myLyingOnGeomPtr->SetGeom( theShape );
1175 }
1176
1177 void LyingOnGeom_i::SetElementType(ElementType theType){
1178   myLyingOnGeomPtr->SetType(SMDSAbs_ElementType(theType));
1179   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1180 }
1181
1182 FunctorType LyingOnGeom_i::GetFunctorType()
1183 {
1184   return SMESH::FT_LyingOnGeom;
1185 }
1186
1187 void LyingOnGeom_i::SetShapeName( const char* theName )
1188 {
1189   delete myShapeName;
1190   myShapeName = strdup( theName );
1191   myLyingOnGeomPtr->SetGeom( getShapeByName( myShapeName ) );
1192   TPythonDump()<<this<<".SetShapeName('"<<theName<<"')";
1193 }
1194
1195 void LyingOnGeom_i::SetShape( const char* theID, const char* theName )
1196 {
1197   delete myShapeName;
1198   myShapeName = strdup( theName );
1199   delete myShapeID;
1200   if ( theID )
1201     myShapeID = strdup( theID );
1202   else
1203     myShapeID = 0;
1204   
1205   if ( myShapeID && strcmp(myShapeName, getShapeNameByID(myShapeID)) == 0 )
1206     myLyingOnGeomPtr->SetGeom( getShapeByID(myShapeID) );
1207   else
1208     myLyingOnGeomPtr->SetGeom( getShapeByName( myShapeName ) );
1209 }
1210
1211 char* LyingOnGeom_i::GetShapeName()
1212 {
1213   return CORBA::string_dup( myShapeName );
1214 }
1215
1216 char* LyingOnGeom_i::GetShapeID()
1217 {
1218   return CORBA::string_dup( myShapeID );
1219 }
1220
1221 void LyingOnGeom_i::SetTolerance( CORBA::Double theToler )
1222 {
1223   myLyingOnGeomPtr->SetTolerance( theToler );
1224   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1225 }
1226
1227 CORBA::Double LyingOnGeom_i::GetTolerance()
1228 {
1229   return myLyingOnGeomPtr->GetTolerance();
1230 }
1231
1232 /*
1233   Class       : FreeBorders_i
1234   Description : Predicate for free borders
1235 */
1236 FreeBorders_i::FreeBorders_i()
1237 {
1238   myPredicatePtr.reset(new Controls::FreeBorders());
1239   myFunctorPtr = myPredicatePtr;
1240 }
1241
1242 FunctorType FreeBorders_i::GetFunctorType()
1243 {
1244   return SMESH::FT_FreeBorders;
1245 }
1246
1247 /*
1248   Class       : FreeEdges_i
1249   Description : Predicate for free borders
1250 */
1251 FreeEdges_i::FreeEdges_i()
1252 : myFreeEdgesPtr( new Controls::FreeEdges() )
1253 {
1254   myFunctorPtr = myPredicatePtr = myFreeEdgesPtr;
1255 }
1256
1257 SMESH::FreeEdges::Borders* FreeEdges_i::GetBorders()
1258 {
1259   INFOS("FreeEdges_i::GetBorders");
1260   SMESH::Controls::FreeEdges::TBorders aBorders;
1261   myFreeEdgesPtr->GetBoreders( aBorders );
1262
1263   long i = 0, iEnd = aBorders.size();
1264
1265   SMESH::FreeEdges::Borders_var aResult = new SMESH::FreeEdges::Borders;
1266   aResult->length(iEnd);
1267
1268   SMESH::Controls::FreeEdges::TBorders::const_iterator anIter;
1269   for ( anIter = aBorders.begin() ; anIter != aBorders.end(); anIter++, i++ )
1270   {
1271     const SMESH::Controls::FreeEdges::Border&  aBord = *anIter;
1272     SMESH::FreeEdges::Border &aBorder = aResult[ i ];
1273
1274     aBorder.myElemId = aBord.myElemId;
1275     aBorder.myPnt1 = aBord.myPntId[ 0 ];
1276     aBorder.myPnt2 = aBord.myPntId[ 1 ];
1277   }
1278
1279   INFOS("FreeEdges_i::GetBorders~");
1280   return aResult._retn();
1281 }
1282
1283 FunctorType FreeEdges_i::GetFunctorType()
1284 {
1285   return SMESH::FT_FreeEdges;
1286 }
1287
1288 /*
1289   Class       : FreeFaces_i
1290   Description : Predicate for free faces
1291 */
1292 FreeFaces_i::FreeFaces_i()
1293 {
1294   myPredicatePtr.reset(new Controls::FreeFaces());
1295   myFunctorPtr = myPredicatePtr;
1296 }
1297
1298 FunctorType FreeFaces_i::GetFunctorType()
1299 {
1300   return SMESH::FT_FreeFaces;
1301 }
1302
1303 /*
1304   Class       : FreeNodes_i
1305   Description : Predicate for free nodes
1306 */
1307 FreeNodes_i::FreeNodes_i()
1308 {
1309   myPredicatePtr.reset(new Controls::FreeNodes());
1310   myFunctorPtr = myPredicatePtr;
1311 }
1312
1313 FunctorType FreeNodes_i::GetFunctorType()
1314 {
1315   return SMESH::FT_FreeNodes;
1316 }
1317
1318 /*
1319   Class       : RangeOfIds_i
1320   Description : Predicate for Range of Ids.
1321                 Range may be specified with two ways.
1322                 1. Using AddToRange method
1323                 2. With SetRangeStr method. Parameter of this method is a string
1324                    like as "1,2,3,50-60,63,67,70-"
1325 */
1326
1327 RangeOfIds_i::RangeOfIds_i()
1328 {
1329   myRangeOfIdsPtr.reset( new Controls::RangeOfIds() );
1330   myFunctorPtr = myPredicatePtr = myRangeOfIdsPtr;
1331 }
1332
1333 void RangeOfIds_i::SetRange( const SMESH::long_array& theIds )
1334 {
1335   CORBA::Long iEnd = theIds.length();
1336   for ( CORBA::Long i = 0; i < iEnd; i++ )
1337     myRangeOfIdsPtr->AddToRange( theIds[ i ] );
1338   TPythonDump()<<this<<".SetRange("<<theIds<<")";
1339 }
1340
1341 CORBA::Boolean RangeOfIds_i::SetRangeStr( const char* theRange )
1342 {
1343   TPythonDump()<<this<<".SetRangeStr('"<<theRange<<"')";
1344   return myRangeOfIdsPtr->SetRangeStr(
1345     TCollection_AsciiString( (Standard_CString)theRange ) );
1346 }
1347
1348 char* RangeOfIds_i::GetRangeStr()
1349 {
1350   TCollection_AsciiString aStr;
1351   myRangeOfIdsPtr->GetRangeStr( aStr );
1352   return CORBA::string_dup( aStr.ToCString() );
1353 }
1354
1355 void RangeOfIds_i::SetElementType( ElementType theType )
1356 {
1357   myRangeOfIdsPtr->SetType( SMDSAbs_ElementType( theType ) );
1358   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1359 }
1360
1361 FunctorType RangeOfIds_i::GetFunctorType()
1362 {
1363   return SMESH::FT_RangeOfIds;
1364 }
1365
1366 /*
1367   Class       : LinearOrQuadratic_i
1368   Description : Predicate to verify whether a mesh element is linear
1369 */
1370 LinearOrQuadratic_i::LinearOrQuadratic_i()
1371 {
1372   myLinearOrQuadraticPtr.reset(new Controls::LinearOrQuadratic());
1373   myFunctorPtr = myPredicatePtr = myLinearOrQuadraticPtr;
1374 }
1375
1376 void LinearOrQuadratic_i::SetElementType(ElementType theType)
1377 {
1378   myLinearOrQuadraticPtr->SetType(SMDSAbs_ElementType(theType));
1379   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1380 }
1381
1382 FunctorType LinearOrQuadratic_i::GetFunctorType()
1383 {
1384   return SMESH::FT_LinearOrQuadratic;
1385 }
1386
1387 /*
1388   Class       : GroupColor_i
1389   Description : Functor for check color of group to whic mesh element belongs to
1390 */
1391 GroupColor_i::GroupColor_i()
1392 {
1393   myGroupColorPtr.reset(new Controls::GroupColor());
1394   myFunctorPtr = myPredicatePtr = myGroupColorPtr;
1395 }
1396
1397 FunctorType GroupColor_i::GetFunctorType()
1398 {
1399   return SMESH::FT_GroupColor;
1400 }
1401
1402 void GroupColor_i::SetColorStr( const char* theColor )
1403 {
1404   myGroupColorPtr->SetColorStr(
1405     TCollection_AsciiString( (Standard_CString)theColor ) );
1406   TPythonDump()<<this<<".SetColorStr('"<<theColor<<"')";
1407 }
1408
1409 char* GroupColor_i::GetColorStr()
1410 {
1411   TCollection_AsciiString aStr;
1412   myGroupColorPtr->GetColorStr( aStr );
1413   return CORBA::string_dup( aStr.ToCString() );
1414 }
1415
1416 void GroupColor_i::SetElementType(ElementType theType)
1417 {
1418   myGroupColorPtr->SetType(SMDSAbs_ElementType(theType));
1419   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1420 }
1421
1422 /*
1423   Class       : ElemGeomType_i
1424   Description : Predicate check is element has indicated geometry type
1425 */
1426 ElemGeomType_i::ElemGeomType_i()
1427 {
1428   myElemGeomTypePtr.reset(new Controls::ElemGeomType());
1429   myFunctorPtr = myPredicatePtr = myElemGeomTypePtr;
1430 }
1431
1432 void ElemGeomType_i::SetElementType(ElementType theType)
1433 {
1434   myElemGeomTypePtr->SetType(SMDSAbs_ElementType(theType));
1435   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1436 }
1437
1438 void ElemGeomType_i::SetGeometryType(GeometryType theType)
1439 {
1440   myElemGeomTypePtr->SetGeomType(SMDSAbs_GeometryType(theType));
1441   TPythonDump()<<this<<".SetGeometryType("<<theType<<")";
1442 }
1443
1444 GeometryType ElemGeomType_i::GetGeometryType() const
1445 {
1446   return (GeometryType)myElemGeomTypePtr->GetGeomType();
1447 }
1448
1449 FunctorType ElemGeomType_i::GetFunctorType()
1450 {
1451   return SMESH::FT_ElemGeomType;
1452 }
1453
1454 /*
1455   Class       : CoplanarFaces_i
1456   Description : Returns true if a mesh face is a coplanar neighbour to a given one
1457 */
1458 CoplanarFaces_i::CoplanarFaces_i()
1459 {
1460   myCoplanarFacesPtr.reset(new Controls::CoplanarFaces());
1461   myFunctorPtr = myPredicatePtr = myCoplanarFacesPtr;
1462 }
1463
1464 void CoplanarFaces_i::SetFace ( CORBA::Long theFaceID )
1465 {
1466   myCoplanarFacesPtr->SetFace(theFaceID);
1467   TPythonDump()<<this<<".SetFace("<<theFaceID<<")";
1468 }
1469
1470 void CoplanarFaces_i::SetTolerance( CORBA::Double theToler )
1471 {
1472   myCoplanarFacesPtr->SetTolerance(theToler);
1473   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1474 }
1475
1476 CORBA::Long CoplanarFaces_i::GetFace () const
1477 {
1478   return myCoplanarFacesPtr->GetFace();
1479 }
1480
1481 char* CoplanarFaces_i::GetFaceAsString () const
1482 {
1483   TCollection_AsciiString str(Standard_Integer(myCoplanarFacesPtr->GetFace()));
1484   return CORBA::string_dup( str.ToCString() );
1485 }
1486
1487 CORBA::Double CoplanarFaces_i::GetTolerance() const
1488 {
1489   return myCoplanarFacesPtr->GetTolerance();
1490 }
1491
1492 FunctorType CoplanarFaces_i::GetFunctorType()
1493 {
1494   return SMESH::FT_CoplanarFaces;
1495 }
1496
1497 /*
1498   Class       : Comparator_i
1499   Description : Base class for comparators
1500 */
1501 Comparator_i::Comparator_i():
1502   myNumericalFunctor( NULL )
1503 {}
1504
1505 Comparator_i::~Comparator_i()
1506 {
1507   if ( myNumericalFunctor )
1508     myNumericalFunctor->Destroy();
1509 }
1510
1511 void Comparator_i::SetMargin( CORBA::Double theValue )
1512 {
1513   myComparatorPtr->SetMargin( theValue );
1514   TPythonDump()<<this<<".SetMargin("<<theValue<<")";
1515 }
1516
1517 CORBA::Double Comparator_i::GetMargin()
1518 {
1519   return myComparatorPtr->GetMargin();
1520 }
1521
1522 void Comparator_i::SetNumFunctor( NumericalFunctor_ptr theFunct )
1523 {
1524   if ( myNumericalFunctor )
1525     myNumericalFunctor->Destroy();
1526
1527   myNumericalFunctor = DownCast<NumericalFunctor_i*>(theFunct);
1528
1529   if ( myNumericalFunctor )
1530   {
1531     myComparatorPtr->SetNumFunctor( myNumericalFunctor->GetNumericalFunctor() );
1532     myNumericalFunctor->Register();
1533     TPythonDump()<<this<<".SetNumFunctor("<<myNumericalFunctor<<")";
1534   }
1535 }
1536
1537 Controls::ComparatorPtr Comparator_i::GetComparator()
1538 {
1539   return myComparatorPtr;
1540 }
1541
1542 NumericalFunctor_i* Comparator_i::GetNumFunctor_i()
1543 {
1544   return myNumericalFunctor;
1545 }
1546
1547
1548 /*
1549   Class       : LessThan_i
1550   Description : Comparator "<"
1551 */
1552 LessThan_i::LessThan_i()
1553 {
1554   myComparatorPtr.reset( new Controls::LessThan() );
1555   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1556 }
1557
1558 FunctorType LessThan_i::GetFunctorType()
1559 {
1560   return SMESH::FT_LessThan;
1561 }
1562
1563
1564 /*
1565   Class       : MoreThan_i
1566   Description : Comparator ">"
1567 */
1568 MoreThan_i::MoreThan_i()
1569 {
1570   myComparatorPtr.reset( new Controls::MoreThan() );
1571   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1572 }
1573
1574 FunctorType MoreThan_i::GetFunctorType()
1575 {
1576   return SMESH::FT_MoreThan;
1577 }
1578
1579
1580 /*
1581   Class       : EqualTo_i
1582   Description : Comparator "="
1583 */
1584 EqualTo_i::EqualTo_i()
1585 : myEqualToPtr( new Controls::EqualTo() )
1586 {
1587   myFunctorPtr = myPredicatePtr = myComparatorPtr = myEqualToPtr;
1588 }
1589
1590 void EqualTo_i::SetTolerance( CORBA::Double theToler )
1591 {
1592   myEqualToPtr->SetTolerance( theToler );
1593   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1594 }
1595
1596 CORBA::Double EqualTo_i::GetTolerance()
1597 {
1598   return myEqualToPtr->GetTolerance();
1599 }
1600
1601 FunctorType EqualTo_i::GetFunctorType()
1602 {
1603   return SMESH::FT_EqualTo;
1604 }
1605
1606 /*
1607   Class       : LogicalNOT_i
1608   Description : Logical NOT predicate
1609 */
1610 LogicalNOT_i::LogicalNOT_i()
1611 : myPredicate( NULL ),
1612   myLogicalNOTPtr( new Controls::LogicalNOT() )
1613 {
1614   myFunctorPtr = myPredicatePtr = myLogicalNOTPtr;
1615 }
1616
1617 LogicalNOT_i::~LogicalNOT_i()
1618 {
1619   if ( myPredicate )
1620     myPredicate->Destroy();
1621 }
1622
1623 void LogicalNOT_i::SetPredicate( Predicate_ptr thePredicate )
1624 {
1625   if ( myPredicate )
1626     myPredicate->Destroy();
1627
1628   myPredicate = SMESH::GetPredicate(thePredicate);
1629
1630   if ( myPredicate ){
1631     myLogicalNOTPtr->SetPredicate(myPredicate->GetPredicate());
1632     myPredicate->Register();
1633     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
1634   }
1635 }
1636
1637 FunctorType LogicalNOT_i::GetFunctorType()
1638 {
1639   return SMESH::FT_LogicalNOT;
1640 }
1641
1642 Predicate_i* LogicalNOT_i::GetPredicate_i()
1643 {
1644   return myPredicate;
1645 }
1646
1647
1648 /*
1649   Class       : LogicalBinary_i
1650   Description : Base class for binary logical predicate
1651 */
1652 LogicalBinary_i::LogicalBinary_i()
1653 : myPredicate1( NULL ),
1654   myPredicate2( NULL )
1655 {}
1656
1657 LogicalBinary_i::~LogicalBinary_i()
1658 {
1659   if ( myPredicate1 )
1660     myPredicate1->Destroy();
1661
1662   if ( myPredicate2 )
1663     myPredicate2->Destroy();
1664 }
1665
1666 void LogicalBinary_i::SetMesh( SMESH_Mesh_ptr theMesh )
1667 {
1668   if ( myPredicate1 )
1669     myPredicate1->SetMesh( theMesh );
1670
1671   if ( myPredicate2 )
1672     myPredicate2->SetMesh( theMesh );
1673 }
1674
1675 void LogicalBinary_i::SetPredicate1( Predicate_ptr thePredicate )
1676 {
1677   if ( myPredicate1 )
1678     myPredicate1->Destroy();
1679
1680   myPredicate1 = SMESH::GetPredicate(thePredicate);
1681
1682   if ( myPredicate1 ){
1683     myLogicalBinaryPtr->SetPredicate1(myPredicate1->GetPredicate());
1684     myPredicate1->Register();
1685     TPythonDump()<<this<<".SetPredicate1("<<myPredicate1<<")";
1686   }
1687 }
1688
1689 void LogicalBinary_i::SetPredicate2( Predicate_ptr thePredicate )
1690 {
1691   if ( myPredicate2 )
1692     myPredicate2->Destroy();
1693
1694   myPredicate2 = SMESH::GetPredicate(thePredicate);
1695
1696   if ( myPredicate2 ){
1697     myLogicalBinaryPtr->SetPredicate2(myPredicate2->GetPredicate());
1698     myPredicate2->Register();
1699     TPythonDump()<<this<<".SetPredicate2("<<myPredicate2<<")";
1700   }
1701 }
1702
1703 Controls::LogicalBinaryPtr LogicalBinary_i::GetLogicalBinary()
1704 {
1705   return myLogicalBinaryPtr;
1706 }
1707
1708 Predicate_i* LogicalBinary_i::GetPredicate1_i()
1709 {
1710   return myPredicate1;
1711 }
1712 Predicate_i* LogicalBinary_i::GetPredicate2_i()
1713 {
1714   return myPredicate2;
1715 }
1716
1717
1718 /*
1719   Class       : LogicalAND_i
1720   Description : Logical AND
1721 */
1722 LogicalAND_i::LogicalAND_i()
1723 {
1724   myLogicalBinaryPtr.reset( new Controls::LogicalAND() );
1725   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
1726 }
1727
1728 FunctorType LogicalAND_i::GetFunctorType()
1729 {
1730   return SMESH::FT_LogicalAND;
1731 }
1732
1733
1734 /*
1735   Class       : LogicalOR_i
1736   Description : Logical OR
1737 */
1738 LogicalOR_i::LogicalOR_i()
1739 {
1740   myLogicalBinaryPtr.reset( new Controls::LogicalOR() );
1741   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
1742 }
1743
1744 FunctorType LogicalOR_i::GetFunctorType()
1745 {
1746   return SMESH::FT_LogicalOR;
1747 }
1748
1749
1750 /*
1751                             FILTER MANAGER
1752 */
1753
1754 FilterManager_i::FilterManager_i()
1755 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
1756 {
1757   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
1758   //PortableServer::ObjectId_var anObjectId =
1759   //  SMESH_Gen_i::GetPOA()->activate_object( this );
1760 }
1761
1762
1763 FilterManager_i::~FilterManager_i()
1764 {
1765   //TPythonDump()<<this<<".Destroy()";
1766 }
1767
1768
1769 MinimumAngle_ptr FilterManager_i::CreateMinimumAngle()
1770 {
1771   SMESH::MinimumAngle_i* aServant = new SMESH::MinimumAngle_i();
1772   SMESH::MinimumAngle_var anObj = aServant->_this();
1773   TPythonDump()<<aServant<<" = "<<this<<".CreateMinimumAngle()";
1774   return anObj._retn();
1775 }
1776
1777
1778 AspectRatio_ptr FilterManager_i::CreateAspectRatio()
1779 {
1780   SMESH::AspectRatio_i* aServant = new SMESH::AspectRatio_i();
1781   SMESH::AspectRatio_var anObj = aServant->_this();
1782   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio()";
1783   return anObj._retn();
1784 }
1785
1786
1787 AspectRatio3D_ptr FilterManager_i::CreateAspectRatio3D()
1788 {
1789   SMESH::AspectRatio3D_i* aServant = new SMESH::AspectRatio3D_i();
1790   SMESH::AspectRatio3D_var anObj = aServant->_this();
1791   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio3D()";
1792   return anObj._retn();
1793 }
1794
1795
1796 Warping_ptr FilterManager_i::CreateWarping()
1797 {
1798   SMESH::Warping_i* aServant = new SMESH::Warping_i();
1799   SMESH::Warping_var anObj = aServant->_this();
1800   TPythonDump()<<aServant<<" = "<<this<<".CreateWarping()";
1801   return anObj._retn();
1802 }
1803
1804
1805 Taper_ptr FilterManager_i::CreateTaper()
1806 {
1807   SMESH::Taper_i* aServant = new SMESH::Taper_i();
1808   SMESH::Taper_var anObj = aServant->_this();
1809   TPythonDump()<<aServant<<" = "<<this<<".CreateTaper()";
1810   return anObj._retn();
1811 }
1812
1813
1814 Skew_ptr FilterManager_i::CreateSkew()
1815 {
1816   SMESH::Skew_i* aServant = new SMESH::Skew_i();
1817   SMESH::Skew_var anObj = aServant->_this();
1818   TPythonDump()<<aServant<<" = "<<this<<".CreateSkew()";
1819   return anObj._retn();
1820 }
1821
1822
1823 Area_ptr FilterManager_i::CreateArea()
1824 {
1825   SMESH::Area_i* aServant = new SMESH::Area_i();
1826   SMESH::Area_var anObj = aServant->_this();
1827   TPythonDump()<<aServant<<" = "<<this<<".CreateArea()";
1828   return anObj._retn();
1829 }
1830
1831
1832 Volume3D_ptr FilterManager_i::CreateVolume3D()
1833 {
1834   SMESH::Volume3D_i* aServant = new SMESH::Volume3D_i();
1835   SMESH::Volume3D_var anObj = aServant->_this();
1836   TPythonDump()<<aServant<<" = "<<this<<".CreateVolume3D()";
1837   return anObj._retn();
1838 }
1839
1840
1841 Length_ptr FilterManager_i::CreateLength()
1842 {
1843   SMESH::Length_i* aServant = new SMESH::Length_i();
1844   SMESH::Length_var anObj = aServant->_this();
1845   TPythonDump()<<aServant<<" = "<<this<<".CreateLength()";
1846   return anObj._retn();
1847 }
1848
1849 Length2D_ptr FilterManager_i::CreateLength2D()
1850 {
1851   SMESH::Length2D_i* aServant = new SMESH::Length2D_i();
1852   SMESH::Length2D_var anObj = aServant->_this();
1853   TPythonDump()<<aServant<<" = "<<this<<".CreateLength2D()";
1854   return anObj._retn();
1855 }
1856
1857 MultiConnection_ptr FilterManager_i::CreateMultiConnection()
1858 {
1859   SMESH::MultiConnection_i* aServant = new SMESH::MultiConnection_i();
1860   SMESH::MultiConnection_var anObj = aServant->_this();
1861   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection()";
1862   return anObj._retn();
1863 }
1864
1865 MultiConnection2D_ptr FilterManager_i::CreateMultiConnection2D()
1866 {
1867   SMESH::MultiConnection2D_i* aServant = new SMESH::MultiConnection2D_i();
1868   SMESH::MultiConnection2D_var anObj = aServant->_this();
1869   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection2D()";
1870   return anObj._retn();
1871 }
1872
1873 BelongToGeom_ptr FilterManager_i::CreateBelongToGeom()
1874 {
1875   SMESH::BelongToGeom_i* aServant = new SMESH::BelongToGeom_i();
1876   SMESH::BelongToGeom_var anObj = aServant->_this();
1877   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGeom()";
1878   return anObj._retn();
1879 }
1880
1881 BelongToPlane_ptr FilterManager_i::CreateBelongToPlane()
1882 {
1883   SMESH::BelongToPlane_i* aServant = new SMESH::BelongToPlane_i();
1884   SMESH::BelongToPlane_var anObj = aServant->_this();
1885   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToPlane()";
1886   return anObj._retn();
1887 }
1888
1889 BelongToCylinder_ptr FilterManager_i::CreateBelongToCylinder()
1890 {
1891   SMESH::BelongToCylinder_i* aServant = new SMESH::BelongToCylinder_i();
1892   SMESH::BelongToCylinder_var anObj = aServant->_this();
1893   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToCylinder()";
1894   return anObj._retn();
1895 }
1896
1897 BelongToGenSurface_ptr FilterManager_i::CreateBelongToGenSurface()
1898 {
1899   SMESH::BelongToGenSurface_i* aServant = new SMESH::BelongToGenSurface_i();
1900   SMESH::BelongToGenSurface_var anObj = aServant->_this();
1901   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGenSurface()";
1902   return anObj._retn();
1903 }
1904
1905 LyingOnGeom_ptr FilterManager_i::CreateLyingOnGeom()
1906 {
1907   SMESH::LyingOnGeom_i* aServant = new SMESH::LyingOnGeom_i();
1908   SMESH::LyingOnGeom_var anObj = aServant->_this();
1909   TPythonDump()<<aServant<<" = "<<this<<".CreateLyingOnGeom()";
1910   return anObj._retn();
1911 }
1912
1913 CoplanarFaces_ptr FilterManager_i::CreateCoplanarFaces()
1914 {
1915   SMESH::CoplanarFaces_i* aServant = new SMESH::CoplanarFaces_i();
1916   SMESH::CoplanarFaces_var anObj = aServant->_this();
1917   TPythonDump()<<aServant<<" = "<<this<<".CreateCoplanarFaces()";
1918   return anObj._retn();
1919 }
1920
1921 FreeBorders_ptr FilterManager_i::CreateFreeBorders()
1922 {
1923   SMESH::FreeBorders_i* aServant = new SMESH::FreeBorders_i();
1924   SMESH::FreeBorders_var anObj = aServant->_this();
1925   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeBorders()";
1926   return anObj._retn();
1927 }
1928
1929 FreeEdges_ptr FilterManager_i::CreateFreeEdges()
1930 {
1931   SMESH::FreeEdges_i* aServant = new SMESH::FreeEdges_i();
1932   SMESH::FreeEdges_var anObj = aServant->_this();
1933   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeEdges()";
1934   return anObj._retn();
1935 }
1936
1937 FreeFaces_ptr FilterManager_i::CreateFreeFaces()
1938 {
1939   SMESH::FreeFaces_i* aServant = new SMESH::FreeFaces_i();
1940   SMESH::FreeFaces_var anObj = aServant->_this();
1941   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeFaces()";
1942   return anObj._retn();
1943 }
1944
1945 FreeNodes_ptr FilterManager_i::CreateFreeNodes()
1946 {
1947   SMESH::FreeNodes_i* aServant = new SMESH::FreeNodes_i();
1948   SMESH::FreeNodes_var anObj = aServant->_this();
1949   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeNodes()";
1950   return anObj._retn();
1951 }
1952
1953 RangeOfIds_ptr FilterManager_i::CreateRangeOfIds()
1954 {
1955   SMESH::RangeOfIds_i* aServant = new SMESH::RangeOfIds_i();
1956   SMESH::RangeOfIds_var anObj = aServant->_this();
1957   TPythonDump()<<aServant<<" = "<<this<<".CreateRangeOfIds()";
1958   return anObj._retn();
1959 }
1960
1961 BadOrientedVolume_ptr FilterManager_i::CreateBadOrientedVolume()
1962 {
1963   SMESH::BadOrientedVolume_i* aServant = new SMESH::BadOrientedVolume_i();
1964   SMESH::BadOrientedVolume_var anObj = aServant->_this();
1965   TPythonDump()<<aServant<<" = "<<this<<".CreateBadOrientedVolume()";
1966   return anObj._retn();
1967 }
1968
1969 LessThan_ptr FilterManager_i::CreateLessThan()
1970 {
1971   SMESH::LessThan_i* aServant = new SMESH::LessThan_i();
1972   SMESH::LessThan_var anObj = aServant->_this();
1973   TPythonDump()<<aServant<<" = "<<this<<".CreateLessThan()";
1974   return anObj._retn();
1975 }
1976
1977 MoreThan_ptr FilterManager_i::CreateMoreThan()
1978 {
1979   SMESH::MoreThan_i* aServant = new SMESH::MoreThan_i();
1980   SMESH::MoreThan_var anObj = aServant->_this();
1981   TPythonDump()<<aServant<<" = "<<this<<".CreateMoreThan()";
1982   return anObj._retn();
1983 }
1984
1985 EqualTo_ptr FilterManager_i::CreateEqualTo()
1986 {
1987   SMESH::EqualTo_i* aServant = new SMESH::EqualTo_i();
1988   SMESH::EqualTo_var anObj = aServant->_this();
1989   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualTo()";
1990   return anObj._retn();
1991 }
1992
1993 LogicalNOT_ptr FilterManager_i::CreateLogicalNOT()
1994 {
1995   SMESH::LogicalNOT_i* aServant = new SMESH::LogicalNOT_i();
1996   SMESH::LogicalNOT_var anObj = aServant->_this();
1997   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalNOT()";
1998   return anObj._retn();
1999 }
2000
2001 LogicalAND_ptr FilterManager_i::CreateLogicalAND()
2002 {
2003   SMESH::LogicalAND_i* aServant = new SMESH::LogicalAND_i();
2004   SMESH::LogicalAND_var anObj = aServant->_this();
2005   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalAND()";
2006   return anObj._retn();
2007 }
2008
2009 LogicalOR_ptr FilterManager_i::CreateLogicalOR()
2010 {
2011   SMESH::LogicalOR_i* aServant = new SMESH::LogicalOR_i();
2012   SMESH::LogicalOR_var anObj = aServant->_this();
2013   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalOR()";
2014   return anObj._retn();
2015 }
2016
2017 LinearOrQuadratic_ptr FilterManager_i::CreateLinearOrQuadratic()
2018 {
2019   SMESH::LinearOrQuadratic_i* aServant = new SMESH::LinearOrQuadratic_i();
2020   SMESH::LinearOrQuadratic_var anObj = aServant->_this();
2021   TPythonDump()<<aServant<<" = "<<this<<".CreateLinearOrQuadratic()";
2022   return anObj._retn();
2023 }
2024
2025 GroupColor_ptr FilterManager_i::CreateGroupColor()
2026 {
2027   SMESH::GroupColor_i* aServant = new SMESH::GroupColor_i();
2028   SMESH::GroupColor_var anObj = aServant->_this();
2029   TPythonDump()<<aServant<<" = "<<this<<".CreateGroupColor()";
2030   return anObj._retn();
2031 }
2032
2033 ElemGeomType_ptr FilterManager_i::CreateElemGeomType()
2034 {
2035   SMESH::ElemGeomType_i* aServant = new SMESH::ElemGeomType_i();
2036   SMESH::ElemGeomType_var anObj = aServant->_this();
2037   TPythonDump()<<aServant<<" = "<<this<<".CreateElemGeomType()";
2038   return anObj._retn();
2039 }
2040
2041 Filter_ptr FilterManager_i::CreateFilter()
2042 {
2043   SMESH::Filter_i* aServant = new SMESH::Filter_i();
2044   SMESH::Filter_var anObj = aServant->_this();
2045   TPythonDump()<<aServant<<" = "<<this<<".CreateFilter()";
2046   return anObj._retn();
2047 }
2048
2049 FilterLibrary_ptr FilterManager_i::LoadLibrary( const char* aFileName )
2050 {
2051   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i( aFileName );
2052   SMESH::FilterLibrary_var anObj = aServant->_this();
2053   TPythonDump()<<aServant<<" = "<<this<<".LoadLibrary('"<<aFileName<<"')";
2054   return anObj._retn();
2055 }
2056
2057 FilterLibrary_ptr FilterManager_i::CreateLibrary()
2058 {
2059   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i();
2060   SMESH::FilterLibrary_var anObj = aServant->_this();
2061   TPythonDump()<<aServant<<" = "<<this<<".CreateLibrary()";
2062   return anObj._retn();
2063 }
2064
2065 CORBA::Boolean FilterManager_i::DeleteLibrary( const char* aFileName )
2066 {
2067   TPythonDump()<<this<<".DeleteLibrary("<<aFileName<<")";
2068   return remove( aFileName ) ? false : true;
2069 }
2070
2071 //=============================================================================
2072 /*!
2073  *  SMESH_Gen_i::CreateFilterManager
2074  *
2075  *  Create filter manager
2076  */
2077 //=============================================================================
2078
2079 SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
2080 {
2081   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2082   SMESH::FilterManager_var anObj = aFilter->_this();
2083   return anObj._retn();
2084 }
2085
2086
2087 /*
2088                               FILTER
2089 */
2090
2091 //=======================================================================
2092 // name    : Filter_i::Filter_i
2093 // Purpose : Constructor
2094 //=======================================================================
2095 Filter_i::Filter_i()
2096 : myPredicate( NULL )
2097 {}
2098
2099 //=======================================================================
2100 // name    : Filter_i::~Filter_i
2101 // Purpose : Destructor
2102 //=======================================================================
2103 Filter_i::~Filter_i()
2104 {
2105   if ( myPredicate )
2106     myPredicate->Destroy();
2107
2108   if(!CORBA::is_nil(myMesh))
2109     myMesh->Destroy();
2110
2111   //TPythonDump()<<this<<".Destroy()";
2112 }
2113
2114 //=======================================================================
2115 // name    : Filter_i::SetPredicate
2116 // Purpose : Set predicate
2117 //=======================================================================
2118 void Filter_i::SetPredicate( Predicate_ptr thePredicate )
2119 {
2120   if ( myPredicate )
2121     myPredicate->Destroy();
2122
2123   myPredicate = SMESH::GetPredicate(thePredicate);
2124
2125   if ( myPredicate )
2126   {
2127     myFilter.SetPredicate( myPredicate->GetPredicate() );
2128     myPredicate->Register();
2129     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
2130   }
2131 }
2132
2133 //=======================================================================
2134 // name    : Filter_i::GetElementType
2135 // Purpose : Get entity type
2136 //=======================================================================
2137 SMESH::ElementType Filter_i::GetElementType()
2138 {
2139   return myPredicate != 0 ? myPredicate->GetElementType() : SMESH::ALL;
2140 }
2141
2142 //=======================================================================
2143 // name    : Filter_i::SetMesh
2144 // Purpose : Set mesh
2145 //=======================================================================
2146 void
2147 Filter_i::
2148 SetMesh( SMESH_Mesh_ptr theMesh )
2149 {
2150   if(!CORBA::is_nil(theMesh))
2151     theMesh->Register();
2152
2153   if(!CORBA::is_nil(myMesh))
2154     myMesh->Destroy();
2155
2156   myMesh = SMESH_Mesh::_duplicate( theMesh );
2157   TPythonDump()<<this<<".SetMesh("<<theMesh<<")";
2158 }
2159
2160 SMESH::long_array*
2161 Filter_i::
2162 GetIDs()
2163 {
2164   return GetElementsId(myMesh);
2165 }
2166
2167 //=======================================================================
2168 // name    : Filter_i::GetElementsId
2169 // Purpose : Get ids of entities
2170 //=======================================================================
2171 void
2172 Filter_i::
2173 GetElementsId( Predicate_i* thePredicate,
2174                const SMDS_Mesh* theMesh,
2175                Controls::Filter::TIdSequence& theSequence )
2176 {
2177   if (thePredicate)
2178     Controls::Filter::GetElementsId(theMesh,thePredicate->GetPredicate(),theSequence);
2179 }
2180
2181 void
2182 Filter_i::
2183 GetElementsId( Predicate_i* thePredicate,
2184                SMESH_Mesh_ptr theMesh,
2185                Controls::Filter::TIdSequence& theSequence )
2186 {
2187   if (thePredicate) 
2188     if(const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh))
2189       Controls::Filter::GetElementsId(aMesh,thePredicate->GetPredicate(),theSequence);
2190 }
2191
2192 SMESH::long_array*
2193 Filter_i::
2194 GetElementsId( SMESH_Mesh_ptr theMesh )
2195 {
2196   SMESH::long_array_var anArray = new SMESH::long_array;
2197   if(!CORBA::is_nil(theMesh) && myPredicate){
2198     Controls::Filter::TIdSequence aSequence;
2199     GetElementsId(myPredicate,theMesh,aSequence);
2200     long i = 0, iEnd = aSequence.size();
2201     anArray->length( iEnd );
2202     for ( ; i < iEnd; i++ )
2203       anArray[ i ] = aSequence[i];
2204   }
2205   return anArray._retn();
2206 }
2207
2208 template<class TElement, class TIterator, class TPredicate>
2209 static void collectMeshInfo(const TIterator& theItr,
2210                             TPredicate& thePred,
2211                             SMESH::long_array& theRes)
2212 {         
2213   if (!theItr)
2214     return;
2215   while (theItr->more()) {
2216     const SMDS_MeshElement* anElem = theItr->next();
2217     if ( thePred->IsSatisfy( anElem->GetID() ) )
2218       theRes[ anElem->GetEntityType() ]++;
2219   }
2220 }
2221
2222 //=============================================================================
2223 /*!
2224  * \brief Returns statistic of mesh elements
2225  */
2226 //=============================================================================
2227 SMESH::long_array* ::Filter_i::GetMeshInfo()
2228 {
2229   SMESH::long_array_var aRes = new SMESH::long_array();
2230   aRes->length(SMESH::Entity_Last);
2231   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
2232     aRes[i] = 0;
2233
2234   if(!CORBA::is_nil(myMesh) && myPredicate) {
2235     const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(myMesh);
2236     SMDS_ElemIteratorPtr it;
2237     switch( GetElementType() )
2238     {
2239   case SMDSAbs_Node:
2240     collectMeshInfo<const SMDS_MeshNode*>(aMesh->nodesIterator(),myPredicate,aRes);
2241     break;
2242   case SMDSAbs_Edge:
2243     collectMeshInfo<const SMDS_MeshElement*>(aMesh->edgesIterator(),myPredicate,aRes);
2244     break;
2245   case SMDSAbs_Face:
2246     collectMeshInfo<const SMDS_MeshElement*>(aMesh->facesIterator(),myPredicate,aRes);
2247     break;
2248   case SMDSAbs_Volume:
2249     collectMeshInfo<const SMDS_MeshElement*>(aMesh->volumesIterator(),myPredicate,aRes);
2250     break;
2251   case SMDSAbs_All:
2252   default:
2253     collectMeshInfo<const SMDS_MeshElement*>(aMesh->elementsIterator(),myPredicate,aRes);
2254     break;
2255     }
2256   }
2257
2258
2259   return aRes._retn();  
2260 }
2261
2262 //================================================================================
2263 /*!
2264  * \brief Return GetElementType() within an array
2265  * Implement SMESH_IDSource interface
2266  */
2267 //================================================================================
2268
2269 SMESH::array_of_ElementType* Filter_i::GetTypes()
2270 {
2271   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
2272   types->length( 1 );
2273   types[0] = GetElementType();
2274   return types._retn();
2275 }
2276
2277 //=======================================================================
2278 // name    : getCriteria
2279 // Purpose : Retrieve criterions from predicate
2280 //=======================================================================
2281 static inline bool getCriteria( Predicate_i*                thePred,
2282                                 SMESH::Filter::Criteria_out theCriteria )
2283 {
2284   int aFType = thePred->GetFunctorType();
2285
2286   switch ( aFType )
2287   {
2288   case FT_FreeBorders:
2289   case FT_FreeEdges:
2290   case FT_FreeFaces:
2291   case FT_LinearOrQuadratic:
2292   case FT_FreeNodes:
2293     {
2294       CORBA::ULong i = theCriteria->length();
2295       theCriteria->length( i + 1 );
2296
2297       theCriteria[ i ] = createCriterion();
2298
2299       theCriteria[ i ].Type = aFType;
2300       theCriteria[ i ].TypeOfElement = thePred->GetElementType();
2301       return true;
2302     }
2303   case FT_BelongToGeom:
2304     {
2305       BelongToGeom_i* aPred = dynamic_cast<BelongToGeom_i*>( thePred );
2306
2307       CORBA::ULong i = theCriteria->length();
2308       theCriteria->length( i + 1 );
2309
2310       theCriteria[ i ] = createCriterion();
2311
2312       theCriteria[ i ].Type          = FT_BelongToGeom;
2313       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2314       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2315       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2316       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2317
2318       return true;
2319     }
2320   case FT_BelongToPlane:
2321   case FT_BelongToCylinder:
2322   case FT_BelongToGenSurface:
2323     {
2324       BelongToSurface_i* aPred = dynamic_cast<BelongToSurface_i*>( thePred );
2325
2326       CORBA::ULong i = theCriteria->length();
2327       theCriteria->length( i + 1 );
2328
2329       theCriteria[ i ] = createCriterion();
2330
2331       theCriteria[ i ].Type          = aFType;
2332       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2333       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2334       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2335       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2336
2337       return true;
2338     }
2339    case FT_LyingOnGeom:
2340     {
2341       LyingOnGeom_i* aPred = dynamic_cast<LyingOnGeom_i*>( thePred );
2342
2343       CORBA::ULong i = theCriteria->length();
2344       theCriteria->length( i + 1 );
2345
2346       theCriteria[ i ] = createCriterion();
2347
2348       theCriteria[ i ].Type          = FT_LyingOnGeom;
2349       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2350       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2351       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2352       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2353
2354       return true;
2355     }
2356    case FT_CoplanarFaces:
2357     {
2358       CoplanarFaces_i* aPred = dynamic_cast<CoplanarFaces_i*>( thePred );
2359
2360       CORBA::ULong i = theCriteria->length();
2361       theCriteria->length( i + 1 );
2362
2363       theCriteria[ i ] = createCriterion();
2364       CORBA::String_var faceId = aPred->GetFaceAsString();
2365
2366       theCriteria[ i ].Type          = FT_CoplanarFaces;
2367       theCriteria[ i ].ThresholdID   = faceId;
2368       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2369
2370       return true;
2371     }
2372   case FT_RangeOfIds:
2373     {
2374       RangeOfIds_i* aPred = dynamic_cast<RangeOfIds_i*>( thePred );
2375
2376       CORBA::ULong i = theCriteria->length();
2377       theCriteria->length( i + 1 );
2378
2379       theCriteria[ i ] = createCriterion();
2380
2381       theCriteria[ i ].Type          = FT_RangeOfIds;
2382       theCriteria[ i ].ThresholdStr  = aPred->GetRangeStr();
2383       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2384
2385       return true;
2386     }
2387   case FT_BadOrientedVolume:
2388     {
2389       BadOrientedVolume_i* aPred = dynamic_cast<BadOrientedVolume_i*>( thePred );
2390
2391       CORBA::ULong i = theCriteria->length();
2392       theCriteria->length( i + 1 );
2393
2394       theCriteria[ i ] = createCriterion();
2395
2396       theCriteria[ i ].Type          = FT_BadOrientedVolume;
2397       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2398
2399       return true;
2400     }
2401   case FT_LessThan:
2402   case FT_MoreThan:
2403   case FT_EqualTo:
2404     {
2405       Comparator_i* aCompar = dynamic_cast<Comparator_i*>( thePred );
2406
2407       CORBA::ULong i = theCriteria->length();
2408       theCriteria->length( i + 1 );
2409
2410       theCriteria[ i ] = createCriterion();
2411
2412       theCriteria[ i ].Type      = aCompar->GetNumFunctor_i()->GetFunctorType();
2413       theCriteria[ i ].Compare   = aFType;
2414       theCriteria[ i ].Threshold = aCompar->GetMargin();
2415       theCriteria[ i ].TypeOfElement = aCompar->GetElementType();
2416
2417       if ( aFType == FT_EqualTo )
2418       {
2419         EqualTo_i* aCompar = dynamic_cast<EqualTo_i*>( thePred );
2420         theCriteria[ i ].Tolerance = aCompar->GetTolerance();
2421       }
2422     }
2423     return true;
2424
2425   case FT_LogicalNOT:
2426     {
2427       Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
2428       getCriteria( aPred, theCriteria );
2429       theCriteria[ theCriteria->length() - 1 ].UnaryOp = FT_LogicalNOT;
2430     }
2431     return true;
2432
2433   case FT_LogicalAND:
2434   case FT_LogicalOR:
2435     {
2436       Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
2437       Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
2438       if ( !getCriteria( aPred1, theCriteria ) )
2439         return false;
2440       theCriteria[ theCriteria->length() - 1 ].BinaryOp = aFType;
2441       return getCriteria( aPred2, theCriteria );
2442     }
2443   case FT_GroupColor:
2444     {
2445       CORBA::ULong i = theCriteria->length();
2446       theCriteria->length( i + 1 );
2447
2448       theCriteria[ i ] = createCriterion();
2449
2450       GroupColor_i* aPred = dynamic_cast<GroupColor_i*>( thePred );
2451       theCriteria[ i ].Type          = aFType;
2452       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2453       theCriteria[ i ].ThresholdStr  = aPred->GetColorStr();
2454
2455       return true;
2456     }
2457   case FT_ElemGeomType:
2458     {
2459       CORBA::ULong i = theCriteria->length();
2460       theCriteria->length( i + 1 );
2461
2462       theCriteria[ i ] = createCriterion();
2463
2464       ElemGeomType_i* aPred = dynamic_cast<ElemGeomType_i*>( thePred );
2465       theCriteria[ i ].Type          = aFType;
2466       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2467       theCriteria[ i ].Threshold     = (double)aPred->GetGeometryType();
2468       return true;
2469     }
2470
2471   case FT_Undefined:
2472     return false;
2473   default:
2474     return false;
2475   }
2476 }
2477
2478 //=======================================================================
2479 // name    : Filter_i::GetCriteria
2480 // Purpose : Retrieve criterions from predicate
2481 //=======================================================================
2482 CORBA::Boolean Filter_i::GetCriteria( SMESH::Filter::Criteria_out theCriteria )
2483 {
2484   theCriteria = new SMESH::Filter::Criteria;
2485   return myPredicate != 0 ? getCriteria( myPredicate, theCriteria ) : true;
2486 }
2487
2488 //=======================================================================
2489 // name    : Filter_i::SetCriteria
2490 // Purpose : Create new predicate and set criterions in it
2491 //=======================================================================
2492 CORBA::Boolean Filter_i::SetCriteria( const SMESH::Filter::Criteria& theCriteria )
2493 {
2494   if ( myPredicate != 0 )
2495     myPredicate->Destroy();
2496
2497   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2498   FilterManager_ptr aFilterMgr = aFilter->_this();
2499
2500   // CREATE two lists ( PREDICATES  and LOG OP )
2501
2502   // Criterion
2503   TPythonDump()<<"aCriteria = []";
2504   std::list<SMESH::Predicate_ptr> aPredicates;
2505   std::list<int>                  aBinaries;
2506   for ( int i = 0, n = theCriteria.length(); i < n; i++ )
2507   {
2508     int         aCriterion    = theCriteria[ i ].Type;
2509     int         aCompare      = theCriteria[ i ].Compare;
2510     double      aThreshold    = theCriteria[ i ].Threshold;
2511     const char* aThresholdStr = theCriteria[ i ].ThresholdStr;
2512     const char* aThresholdID  = theCriteria[ i ].ThresholdID;
2513     int         aUnary        = theCriteria[ i ].UnaryOp;
2514     int         aBinary       = theCriteria[ i ].BinaryOp;
2515     double      aTolerance    = theCriteria[ i ].Tolerance;
2516     ElementType aTypeOfElem   = theCriteria[ i ].TypeOfElement;
2517     long        aPrecision    = theCriteria[ i ].Precision;
2518
2519     {
2520       TPythonDump pd;
2521       pd << "aCriterion = SMESH.Filter.Criterion(" << aCriterion << "," << aCompare
2522          << "," << aThreshold << ",'" << aThresholdStr;
2523       if (aThresholdID && strlen(aThresholdID))
2524         //pd << "',salome.ObjectToID(" << aThresholdID
2525         pd << "','" << aThresholdID
2526            << "'," << aUnary << "," << aBinary << "," << aTolerance
2527            << "," << aTypeOfElem << "," << aPrecision << ")";
2528       else
2529         pd << "',''," << aUnary << "," << aBinary << "," << aTolerance
2530            << "," << aTypeOfElem << "," << aPrecision << ")";
2531     }
2532
2533     SMESH::Predicate_ptr aPredicate = SMESH::Predicate::_nil();
2534     SMESH::NumericalFunctor_ptr aFunctor = SMESH::NumericalFunctor::_nil();
2535
2536     switch ( aCriterion )
2537     {
2538       // Functors
2539
2540       case SMESH::FT_MultiConnection:
2541         aFunctor = aFilterMgr->CreateMultiConnection();
2542         break;
2543       case SMESH::FT_MultiConnection2D:
2544         aFunctor = aFilterMgr->CreateMultiConnection2D();
2545         break;
2546       case SMESH::FT_Length:
2547         aFunctor = aFilterMgr->CreateLength();
2548         break;
2549       case SMESH::FT_Length2D:
2550         aFunctor = aFilterMgr->CreateLength2D();
2551         break;
2552       case SMESH::FT_AspectRatio:
2553         aFunctor = aFilterMgr->CreateAspectRatio();
2554         break;
2555       case SMESH::FT_AspectRatio3D:
2556         aFunctor = aFilterMgr->CreateAspectRatio3D();
2557         break;
2558       case SMESH::FT_Warping:
2559         aFunctor = aFilterMgr->CreateWarping();
2560         break;
2561       case SMESH::FT_MinimumAngle:
2562         aFunctor = aFilterMgr->CreateMinimumAngle();
2563         break;
2564       case SMESH::FT_Taper:
2565         aFunctor = aFilterMgr->CreateTaper();
2566         break;
2567       case SMESH::FT_Skew:
2568         aFunctor = aFilterMgr->CreateSkew();
2569         break;
2570       case SMESH::FT_Area:
2571         aFunctor = aFilterMgr->CreateArea();
2572         break;
2573       case SMESH::FT_Volume3D:
2574         aFunctor = aFilterMgr->CreateVolume3D();
2575         break;
2576
2577       // Predicates
2578
2579       case SMESH::FT_FreeBorders:
2580         aPredicate = aFilterMgr->CreateFreeBorders();
2581         break;
2582       case SMESH::FT_FreeEdges:
2583         aPredicate = aFilterMgr->CreateFreeEdges();
2584         break;
2585       case SMESH::FT_FreeFaces:
2586         aPredicate = aFilterMgr->CreateFreeFaces();
2587         break;
2588       case SMESH::FT_FreeNodes:
2589         aPredicate = aFilterMgr->CreateFreeNodes();
2590         break;
2591       case SMESH::FT_BelongToGeom:
2592         {
2593           SMESH::BelongToGeom_ptr tmpPred = aFilterMgr->CreateBelongToGeom();
2594           tmpPred->SetElementType( aTypeOfElem );
2595           tmpPred->SetShape( aThresholdID, aThresholdStr );
2596           tmpPred->SetTolerance( aTolerance );
2597           aPredicate = tmpPred;
2598         }
2599         break;
2600       case SMESH::FT_BelongToPlane:
2601       case SMESH::FT_BelongToCylinder:
2602       case SMESH::FT_BelongToGenSurface:
2603         {
2604           SMESH::BelongToSurface_ptr tmpPred;
2605           switch ( aCriterion ) {
2606           case SMESH::FT_BelongToPlane:
2607             tmpPred = aFilterMgr->CreateBelongToPlane(); break;
2608           case SMESH::FT_BelongToCylinder:
2609             tmpPred = aFilterMgr->CreateBelongToCylinder(); break;
2610           default:
2611             tmpPred = aFilterMgr->CreateBelongToGenSurface();
2612           }
2613           tmpPred->SetShape( aThresholdID, aThresholdStr, aTypeOfElem );
2614           tmpPred->SetTolerance( aTolerance );
2615           aPredicate = tmpPred;
2616         }
2617         break;
2618       case SMESH::FT_LyingOnGeom:
2619         {
2620           SMESH::LyingOnGeom_ptr tmpPred = aFilterMgr->CreateLyingOnGeom();
2621           tmpPred->SetElementType( aTypeOfElem );
2622           tmpPred->SetShape( aThresholdID, aThresholdStr );
2623           tmpPred->SetTolerance( aTolerance );
2624           aPredicate = tmpPred;
2625         }
2626         break;
2627       case SMESH::FT_RangeOfIds:
2628         {
2629           SMESH::RangeOfIds_ptr tmpPred = aFilterMgr->CreateRangeOfIds();
2630           tmpPred->SetRangeStr( aThresholdStr );
2631           tmpPred->SetElementType( aTypeOfElem );
2632           aPredicate = tmpPred;
2633         }
2634         break;
2635       case SMESH::FT_BadOrientedVolume:
2636         {
2637           aPredicate = aFilterMgr->CreateBadOrientedVolume();
2638         }
2639         break;
2640       case SMESH::FT_LinearOrQuadratic:
2641         {
2642           SMESH::LinearOrQuadratic_ptr tmpPred = aFilterMgr->CreateLinearOrQuadratic();
2643           tmpPred->SetElementType( aTypeOfElem );
2644           aPredicate = tmpPred;
2645           break;
2646         }
2647       case SMESH::FT_GroupColor:
2648         {
2649           SMESH::GroupColor_ptr tmpPred = aFilterMgr->CreateGroupColor();
2650           tmpPred->SetElementType( aTypeOfElem );
2651           tmpPred->SetColorStr( aThresholdStr );
2652           aPredicate = tmpPred;
2653           break;
2654         }
2655       case SMESH::FT_ElemGeomType:
2656         {
2657           SMESH::ElemGeomType_ptr tmpPred = aFilterMgr->CreateElemGeomType();
2658           tmpPred->SetElementType( aTypeOfElem );
2659           tmpPred->SetGeometryType( (GeometryType)(int)(aThreshold + 0.5) );
2660           aPredicate = tmpPred;
2661           break;
2662         }
2663       case SMESH::FT_CoplanarFaces:
2664         {
2665           SMESH::CoplanarFaces_ptr tmpPred = aFilterMgr->CreateCoplanarFaces();
2666           tmpPred->SetFace( atol (aThresholdID ));
2667           tmpPred->SetTolerance( aTolerance );
2668           aPredicate = tmpPred;
2669           break;
2670         }
2671
2672       default:
2673         continue;
2674     }
2675
2676     // Comparator
2677     if ( !aFunctor->_is_nil() && aPredicate->_is_nil() )
2678     {
2679       SMESH::Comparator_ptr aComparator = SMESH::Comparator::_nil();
2680
2681       if ( aCompare == SMESH::FT_LessThan )
2682         aComparator = aFilterMgr->CreateLessThan();
2683       else if ( aCompare == SMESH::FT_MoreThan )
2684         aComparator = aFilterMgr->CreateMoreThan();
2685       else if ( aCompare == SMESH::FT_EqualTo )
2686         aComparator = aFilterMgr->CreateEqualTo();
2687       else
2688         continue;
2689
2690       aComparator->SetNumFunctor( aFunctor );
2691       aComparator->SetMargin( aThreshold );
2692
2693       if ( aCompare == FT_EqualTo )
2694       {
2695         SMESH::EqualTo_var anEqualTo = SMESH::EqualTo::_narrow( aComparator );
2696         anEqualTo->SetTolerance( aTolerance );
2697       }
2698
2699       aPredicate = aComparator;
2700
2701       aFunctor->SetPrecision( aPrecision );
2702     }
2703
2704     // Logical not
2705     if ( aUnary == FT_LogicalNOT )
2706     {
2707       SMESH::LogicalNOT_ptr aNotPred = aFilterMgr->CreateLogicalNOT();
2708       aNotPred->SetPredicate( aPredicate );
2709       aPredicate = aNotPred;
2710     }
2711
2712     // logical op
2713     aPredicates.push_back( aPredicate );
2714     aBinaries.push_back( aBinary );
2715     TPythonDump()<<"aCriteria.append(aCriterion)";
2716
2717   } // end of for
2718   TPythonDump()<<this<<".SetCriteria(aCriteria)";
2719
2720   // CREATE ONE PREDICATE FROM PREVIOUSLY CREATED MAP
2721
2722   // combine all "AND" operations
2723
2724   std::list<SMESH::Predicate_ptr> aResList;
2725
2726   std::list<SMESH::Predicate_ptr>::iterator aPredIter;
2727   std::list<int>::iterator                  aBinaryIter;
2728
2729   SMESH::Predicate_ptr aPrevPredicate = SMESH::Predicate::_nil();
2730   int aPrevBinary = SMESH::FT_Undefined;
2731
2732   for ( aPredIter = aPredicates.begin(), aBinaryIter = aBinaries.begin();
2733         aPredIter != aPredicates.end() && aBinaryIter != aBinaries.end();
2734         ++aPredIter, ++aBinaryIter )
2735   {
2736     int aCurrBinary = *aBinaryIter;
2737
2738     SMESH::Predicate_ptr aCurrPred = SMESH::Predicate::_nil();
2739
2740     if ( aPrevBinary == SMESH::FT_LogicalAND )
2741     {
2742
2743       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalAND();
2744       aBinaryPred->SetPredicate1( aPrevPredicate );
2745       aBinaryPred->SetPredicate2( *aPredIter );
2746       aCurrPred = aBinaryPred;
2747     }
2748     else
2749       aCurrPred = *aPredIter;
2750
2751     if ( aCurrBinary != SMESH::FT_LogicalAND )
2752       aResList.push_back( aCurrPred );
2753
2754     aPrevPredicate = aCurrPred;
2755     aPrevBinary = aCurrBinary;
2756   }
2757
2758   // combine all "OR" operations
2759
2760   SMESH::Predicate_ptr aResPredicate = SMESH::Predicate::_nil();
2761
2762   if ( aResList.size() == 1 )
2763     aResPredicate = *aResList.begin();
2764   else if ( aResList.size() > 1 )
2765   {
2766     std::list<SMESH::Predicate_ptr>::iterator anIter = aResList.begin();
2767     aResPredicate = *anIter;
2768     anIter++;
2769     for ( ; anIter != aResList.end(); ++anIter )
2770     {
2771       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalOR();
2772       aBinaryPred->SetPredicate1( aResPredicate );
2773       aBinaryPred->SetPredicate2( *anIter );
2774       aResPredicate = aBinaryPred;
2775     }
2776   }
2777
2778   SetPredicate( aResPredicate );
2779
2780   return !aResPredicate->_is_nil();
2781 }
2782
2783 //=======================================================================
2784 // name    : Filter_i::GetPredicate_i
2785 // Purpose : Get implementation of predicate
2786 //=======================================================================
2787 Predicate_i* Filter_i::GetPredicate_i()
2788 {
2789   return myPredicate;
2790 }
2791
2792 //=======================================================================
2793 // name    : Filter_i::GetPredicate
2794 // Purpose : Get predicate
2795 //=======================================================================
2796 Predicate_ptr Filter_i::GetPredicate()
2797 {
2798   if ( myPredicate == 0 )
2799     return SMESH::Predicate::_nil();
2800   else
2801   {
2802     SMESH::Predicate_var anObj = myPredicate->_this();
2803     return anObj._retn();
2804   }
2805 }
2806
2807 /*
2808                             FILTER LIBRARY
2809 */
2810
2811 #define ATTR_TYPE          "type"
2812 #define ATTR_COMPARE       "compare"
2813 #define ATTR_THRESHOLD     "threshold"
2814 #define ATTR_UNARY         "unary"
2815 #define ATTR_BINARY        "binary"
2816 #define ATTR_THRESHOLD_STR "threshold_str"
2817 #define ATTR_TOLERANCE     "tolerance"
2818 #define ATTR_ELEMENT_TYPE  "ElementType"
2819
2820 //=======================================================================
2821 // name    : toString
2822 // Purpose : Convert bool to LDOMString
2823 //=======================================================================
2824 static inline LDOMString toString( CORBA::Boolean val )
2825 {
2826   return val ? "logical not" : "";
2827 }
2828
2829 //=======================================================================
2830 // name    : toBool
2831 // Purpose : Convert LDOMString to bool
2832 //=======================================================================
2833 static inline bool toBool( const LDOMString& theStr )
2834 {
2835   return theStr.equals( "logical not" );
2836 }
2837
2838 //=======================================================================
2839 // name    : toString
2840 // Purpose : Convert double to LDOMString
2841 //=======================================================================
2842 static inline LDOMString toString( CORBA::Double val )
2843 {
2844   char a[ 255 ];
2845   sprintf( a, "%e", val );
2846   return LDOMString( a );
2847 }
2848
2849 //=======================================================================
2850 // name    : toDouble
2851 // Purpose : Convert LDOMString to double
2852 //=======================================================================
2853 static inline double toDouble( const LDOMString& theStr )
2854 {
2855   return atof( theStr.GetString() );
2856 }
2857
2858 //=======================================================================
2859 // name    : toString
2860 // Purpose : Convert functor type to LDOMString
2861 //=======================================================================
2862 static inline LDOMString toString( CORBA::Long theType )
2863 {
2864   switch ( theType )
2865   {
2866     case FT_AspectRatio     : return "Aspect ratio";
2867     case FT_Warping         : return "Warping";
2868     case FT_MinimumAngle    : return "Minimum angle";
2869     case FT_Taper           : return "Taper";
2870     case FT_Skew            : return "Skew";
2871     case FT_Area            : return "Area";
2872     case FT_Volume3D        : return "Volume3D";
2873     case FT_BelongToGeom    : return "Belong to Geom";
2874     case FT_BelongToPlane   : return "Belong to Plane";
2875     case FT_BelongToCylinder: return "Belong to Cylinder";
2876     case FT_BelongToGenSurface: return "Belong to Generic Surface";
2877     case FT_LyingOnGeom     : return "Lying on Geom";
2878     case FT_BadOrientedVolume: return "Bad Oriented Volume";
2879     case FT_RangeOfIds      : return "Range of IDs";
2880     case FT_FreeBorders     : return "Free borders";
2881     case FT_FreeEdges       : return "Free edges";
2882     case FT_FreeFaces       : return "Free faces";
2883     case FT_FreeNodes       : return "Free nodes";
2884     case FT_MultiConnection : return "Borders at multi-connections";
2885     case FT_MultiConnection2D: return "Borders at multi-connections 2D";
2886     case FT_Length          : return "Length";
2887     case FT_Length2D        : return "Length 2D";
2888     case FT_LessThan        : return "Less than";
2889     case FT_MoreThan        : return "More than";
2890     case FT_EqualTo         : return "Equal to";
2891     case FT_LogicalNOT      : return "Not";
2892     case FT_LogicalAND      : return "And";
2893     case FT_LogicalOR       : return "Or";
2894     case FT_GroupColor      : return "Color of Group";
2895     case FT_LinearOrQuadratic : return "Linear or Quadratic";
2896     case FT_ElemGeomType    : return "Element geomtry type";
2897     case FT_Undefined       : return "";
2898     default                 : return "";
2899   }
2900 }
2901
2902 //=======================================================================
2903 // name    : toFunctorType
2904 // Purpose : Convert LDOMString to functor type
2905 //=======================================================================
2906 static inline SMESH::FunctorType toFunctorType( const LDOMString& theStr )
2907 {
2908   if      ( theStr.equals( "Aspect ratio"                 ) ) return FT_AspectRatio;
2909   else if ( theStr.equals( "Warping"                      ) ) return FT_Warping;
2910   else if ( theStr.equals( "Minimum angle"                ) ) return FT_MinimumAngle;
2911   else if ( theStr.equals( "Taper"                        ) ) return FT_Taper;
2912   else if ( theStr.equals( "Skew"                         ) ) return FT_Skew;
2913   else if ( theStr.equals( "Area"                         ) ) return FT_Area;
2914   else if ( theStr.equals( "Volume3D"                     ) ) return FT_Volume3D;
2915   else if ( theStr.equals( "Belong to Geom"               ) ) return FT_BelongToGeom;
2916   else if ( theStr.equals( "Belong to Plane"              ) ) return FT_BelongToPlane;
2917   else if ( theStr.equals( "Belong to Cylinder"           ) ) return FT_BelongToCylinder;
2918   else if ( theStr.equals( "Belong to Generic Surface"    ) ) return FT_BelongToGenSurface;
2919   else if ( theStr.equals( "Lying on Geom"                ) ) return FT_LyingOnGeom;
2920   else if ( theStr.equals( "Free borders"                 ) ) return FT_FreeBorders;
2921   else if ( theStr.equals( "Free edges"                   ) ) return FT_FreeEdges;
2922   else if ( theStr.equals( "Free faces"                   ) ) return FT_FreeFaces;
2923   else if ( theStr.equals( "Free nodes"                   ) ) return FT_FreeNodes;
2924   else if ( theStr.equals( "Borders at multi-connections" ) ) return FT_MultiConnection;
2925   //  else if ( theStr.equals( "Borders at multi-connections 2D" ) ) return FT_MultiConnection2D;
2926   else if ( theStr.equals( "Length"                       ) ) return FT_Length;
2927   //  else if ( theStr.equals( "Length2D"                     ) ) return FT_Length2D;
2928   else if ( theStr.equals( "Range of IDs"                 ) ) return FT_RangeOfIds;
2929   else if ( theStr.equals( "Bad Oriented Volume"          ) ) return FT_BadOrientedVolume;
2930   else if ( theStr.equals( "Less than"                    ) ) return FT_LessThan;
2931   else if ( theStr.equals( "More than"                    ) ) return FT_MoreThan;
2932   else if ( theStr.equals( "Equal to"                     ) ) return FT_EqualTo;
2933   else if ( theStr.equals( "Not"                          ) ) return FT_LogicalNOT;
2934   else if ( theStr.equals( "And"                          ) ) return FT_LogicalAND;
2935   else if ( theStr.equals( "Or"                           ) ) return FT_LogicalOR;
2936   else if ( theStr.equals( "Color of Group"               ) ) return FT_GroupColor;
2937   else if ( theStr.equals( "Linear or Quadratic"          ) ) return FT_LinearOrQuadratic;
2938   else if ( theStr.equals( "Element geomtry type"         ) ) return FT_ElemGeomType;
2939   else if ( theStr.equals( ""                             ) ) return FT_Undefined;
2940   else  return FT_Undefined;
2941 }
2942
2943 //=======================================================================
2944 // name    : toFunctorType
2945 // Purpose : Convert LDOMString to value of ElementType enumeration
2946 //=======================================================================
2947 static inline SMESH::ElementType toElementType( const LDOMString& theStr )
2948 {
2949   if      ( theStr.equals( "NODE"   ) ) return SMESH::NODE;
2950   else if ( theStr.equals( "EDGE"   ) ) return SMESH::EDGE;
2951   else if ( theStr.equals( "FACE"   ) ) return SMESH::FACE;
2952   else if ( theStr.equals( "VOLUME" ) ) return SMESH::VOLUME;
2953   else                                  return SMESH::ALL;
2954 }
2955
2956 //=======================================================================
2957 // name    : toString
2958 // Purpose : Convert ElementType to string
2959 //=======================================================================
2960 static inline LDOMString toString( const SMESH::ElementType theType )
2961 {
2962   switch ( theType )
2963   {
2964     case SMESH::NODE   : return "NODE";
2965     case SMESH::EDGE   : return "EDGE";
2966     case SMESH::FACE   : return "FACE";
2967     case SMESH::VOLUME : return "VOLUME";
2968     case SMESH::ALL    : return "ALL";
2969     default            : return "";
2970   }
2971 }
2972
2973 //=======================================================================
2974 // name    : findFilter
2975 // Purpose : Find filter in document
2976 //=======================================================================
2977 static LDOM_Element findFilter( const char* theFilterName,
2978                                 const LDOM_Document& theDoc,
2979                                 LDOM_Node* theParent = 0 )
2980 {
2981   LDOM_Element aRootElement = theDoc.getDocumentElement();
2982   if ( aRootElement.isNull() || !aRootElement.hasChildNodes() )
2983     return LDOM_Element();
2984
2985   for ( LDOM_Node aTypeNode = aRootElement.getFirstChild();
2986         !aTypeNode.isNull(); aTypeNode = aTypeNode.getNextSibling() )
2987   {
2988     for ( LDOM_Node aFilter = aTypeNode.getFirstChild();
2989           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
2990     {
2991       LDOM_Element* anElem = ( LDOM_Element* )&aFilter;
2992       if ( anElem->getTagName().equals( LDOMString( "filter" ) ) &&
2993            anElem->getAttribute( "name" ).equals( LDOMString( theFilterName ) ) )
2994       {
2995         if ( theParent != 0  )
2996           *theParent = aTypeNode;
2997         return (LDOM_Element&)aFilter;
2998       }
2999     }
3000   }
3001   return LDOM_Element();
3002 }
3003
3004 //=======================================================================
3005 // name    : getSectionName
3006 // Purpose : Get name of section of filters
3007 //=======================================================================
3008 static const char* getSectionName( const ElementType theType )
3009 {
3010   switch ( theType )
3011   {
3012     case SMESH::NODE   : return "Filters for nodes";
3013     case SMESH::EDGE   : return "Filters for edges";
3014     case SMESH::FACE   : return "Filters for faces";
3015     case SMESH::VOLUME : return "Filters for volumes";
3016     case SMESH::ALL    : return "Filters for elements";
3017     default            : return "";
3018   }
3019 }
3020
3021 //=======================================================================
3022 // name    : getSection
3023 // Purpose : Create section for filters corresponding to the entity type
3024 //=======================================================================
3025 static LDOM_Node getSection( const ElementType theType,
3026                              LDOM_Document&    theDoc,
3027                              const bool        toCreate = false )
3028 {
3029   LDOM_Element aRootElement = theDoc.getDocumentElement();
3030   if ( aRootElement.isNull() )
3031     return LDOM_Node();
3032
3033   // Find section
3034   bool anExist = false;
3035   const char* aSectionName = getSectionName( theType );
3036   if ( strcmp( aSectionName, "" ) == 0 )
3037     return LDOM_Node();
3038
3039   LDOM_NodeList aSections = theDoc.getElementsByTagName( "section" );
3040   LDOM_Node aNode;
3041   for ( int i = 0, n = aSections.getLength(); i < n; i++ )
3042   {
3043     aNode = aSections.item( i );
3044     LDOM_Element& anItem = ( LDOM_Element& )aNode;
3045     if ( anItem.getAttribute( "name" ).equals( LDOMString( aSectionName ) ) )
3046     {
3047       anExist = true;
3048       break;
3049     }
3050   }
3051
3052   // Create new section if necessary
3053   if ( !anExist )
3054   {
3055     if ( toCreate )
3056     {
3057       LDOM_Element aNewItem = theDoc.createElement( "section" );
3058       aNewItem.setAttribute( "name", aSectionName );
3059       aRootElement.appendChild( aNewItem );
3060       return aNewItem;
3061     }
3062     else
3063       return LDOM_Node();
3064   }
3065   return
3066     aNode;
3067 }
3068
3069 //=======================================================================
3070 // name    : createFilterItem
3071 // Purpose : Create filter item or LDOM document
3072 //=======================================================================
3073 static LDOM_Element createFilterItem( const char*       theName,
3074                                       SMESH::Filter_ptr theFilter,
3075                                       LDOM_Document&    theDoc )
3076 {
3077   // create new filter in document
3078   LDOM_Element aFilterItem = theDoc.createElement( "filter" );
3079   aFilterItem.setAttribute( "name", theName );
3080
3081   // save filter criterions
3082   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
3083
3084   if ( !theFilter->GetCriteria( aCriteria ) )
3085     return LDOM_Element();
3086
3087   for ( CORBA::ULong i = 0, n = aCriteria->length(); i < n; i++ )
3088   {
3089     LDOM_Element aCriterionItem = theDoc.createElement( "criterion" );
3090     
3091     aCriterionItem.setAttribute( ATTR_TYPE         , toString(  aCriteria[ i ].Type) );
3092     aCriterionItem.setAttribute( ATTR_COMPARE      , toString(  aCriteria[ i ].Compare ) );
3093     aCriterionItem.setAttribute( ATTR_THRESHOLD    , toString(  aCriteria[ i ].Threshold ) );
3094     aCriterionItem.setAttribute( ATTR_UNARY        , toString(  aCriteria[ i ].UnaryOp ) );
3095     aCriterionItem.setAttribute( ATTR_BINARY       , toString(  aCriteria[ i ].BinaryOp ) );
3096
3097     aCriterionItem.setAttribute( ATTR_THRESHOLD_STR, (const char*)aCriteria[ i ].ThresholdStr );
3098     aCriterionItem.setAttribute( ATTR_TOLERANCE    , toString( aCriteria[ i ].Tolerance ) );
3099     aCriterionItem.setAttribute( ATTR_ELEMENT_TYPE ,
3100       toString( (SMESH::ElementType)aCriteria[ i ].TypeOfElement ) );
3101
3102     aFilterItem.appendChild( aCriterionItem );
3103   }
3104
3105   return aFilterItem;
3106 }
3107
3108 //=======================================================================
3109 // name    : FilterLibrary_i::FilterLibrary_i
3110 // Purpose : Constructor
3111 //=======================================================================
3112 FilterLibrary_i::FilterLibrary_i( const char* theFileName )
3113 {
3114   myFileName = strdup( theFileName );
3115   SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
3116   myFilterMgr = aFilterMgr->_this();
3117
3118   LDOMParser aParser;
3119
3120   // Try to use existing library file
3121   bool anExists = false;
3122   if ( !aParser.parse( myFileName ) )
3123   {
3124     myDoc = aParser.getDocument();
3125     anExists = true;
3126   }
3127   // Create a new XML document if it doesn't exist
3128   else
3129     myDoc = LDOM_Document::createDocument( LDOMString() );
3130
3131   LDOM_Element aRootElement = myDoc.getDocumentElement();
3132   if ( aRootElement.isNull() )
3133   {
3134     // If the existing document is empty --> try to create a new one
3135     if ( anExists )
3136       myDoc = LDOM_Document::createDocument( LDOMString() );
3137   }
3138 }
3139
3140 //=======================================================================
3141 // name    : FilterLibrary_i::FilterLibrary_i
3142 // Purpose : Constructor
3143 //=======================================================================
3144 FilterLibrary_i::FilterLibrary_i()
3145 {
3146   myFileName = 0;
3147   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
3148   myFilterMgr = aFilter->_this();
3149
3150   myDoc = LDOM_Document::createDocument( LDOMString() );
3151 }
3152
3153 FilterLibrary_i::~FilterLibrary_i()
3154 {
3155   delete myFileName;
3156   //TPythonDump()<<this<<".Destroy()";
3157 }
3158
3159 //=======================================================================
3160 // name    : FilterLibrary_i::Copy
3161 // Purpose : Create filter and initialize it with values from library
3162 //=======================================================================
3163 Filter_ptr FilterLibrary_i::Copy( const char* theFilterName )
3164 {
3165   Filter_ptr aRes = Filter::_nil();
3166   LDOM_Node aFilter = findFilter( theFilterName, myDoc );
3167
3168   if ( aFilter.isNull() )
3169     return aRes;
3170
3171   std::list<SMESH::Filter::Criterion> aCriteria;
3172
3173   for ( LDOM_Node aCritNode = aFilter.getFirstChild();
3174         !aCritNode.isNull() ; aCritNode = aCritNode.getNextSibling() )
3175   {
3176     LDOM_Element* aCrit = (LDOM_Element*)&aCritNode;
3177
3178     const char* aTypeStr      = aCrit->getAttribute( ATTR_TYPE          ).GetString();
3179     const char* aCompareStr   = aCrit->getAttribute( ATTR_COMPARE       ).GetString();
3180     const char* aUnaryStr     = aCrit->getAttribute( ATTR_UNARY         ).GetString();
3181     const char* aBinaryStr    = aCrit->getAttribute( ATTR_BINARY        ).GetString();
3182     const char* anElemTypeStr = aCrit->getAttribute( ATTR_ELEMENT_TYPE  ).GetString();
3183
3184     SMESH::Filter::Criterion aCriterion = createCriterion();
3185
3186     aCriterion.Type          = toFunctorType( aTypeStr );
3187     aCriterion.Compare       = toFunctorType( aCompareStr );
3188     aCriterion.UnaryOp       = toFunctorType( aUnaryStr );
3189     aCriterion.BinaryOp      = toFunctorType( aBinaryStr );
3190
3191     aCriterion.TypeOfElement = toElementType( anElemTypeStr );
3192
3193     LDOMString str = aCrit->getAttribute( ATTR_THRESHOLD );
3194     int val = 0;
3195     aCriterion.Threshold = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3196       ? val : atof( str.GetString() );
3197
3198     str = aCrit->getAttribute( ATTR_TOLERANCE );
3199     aCriterion.Tolerance = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3200       ? val : atof( str.GetString() );
3201
3202     str = aCrit->getAttribute( ATTR_THRESHOLD_STR );
3203     if ( str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val ) )
3204     {
3205       char a[ 255 ];
3206       sprintf( a, "%d", val );
3207       aCriterion.ThresholdStr = strdup( a );
3208     }
3209     else
3210       aCriterion.ThresholdStr = str.GetString();
3211
3212     aCriteria.push_back( aCriterion );
3213   }
3214
3215   SMESH::Filter::Criteria_var aCriteriaVar = new SMESH::Filter::Criteria;
3216   aCriteriaVar->length( aCriteria.size() );
3217
3218   CORBA::ULong i = 0;
3219   std::list<SMESH::Filter::Criterion>::iterator anIter = aCriteria.begin();
3220
3221   for( ; anIter != aCriteria.end(); ++anIter )
3222     aCriteriaVar[ i++ ] = *anIter;
3223
3224   aRes = myFilterMgr->CreateFilter();
3225   aRes->SetCriteria( aCriteriaVar.inout() );
3226
3227   TPythonDump()<<this<<".Copy('"<<theFilterName<<"')";
3228
3229   return aRes;
3230 }
3231
3232 //=======================================================================
3233 // name    : FilterLibrary_i::SetFileName
3234 // Purpose : Set file name for library
3235 //=======================================================================
3236 void FilterLibrary_i::SetFileName( const char* theFileName )
3237 {
3238   delete myFileName;
3239   myFileName = strdup( theFileName );
3240   TPythonDump()<<this<<".SetFileName('"<<theFileName<<"')";
3241 }
3242
3243 //=======================================================================
3244 // name    : FilterLibrary_i::GetFileName
3245 // Purpose : Get file name of library
3246 //=======================================================================
3247 char* FilterLibrary_i::GetFileName()
3248 {
3249   return CORBA::string_dup( myFileName );
3250 }
3251
3252 //=======================================================================
3253 // name    : FilterLibrary_i::Add
3254 // Purpose : Add new filter to library
3255 //=======================================================================
3256 CORBA::Boolean FilterLibrary_i::Add( const char* theFilterName, Filter_ptr theFilter )
3257 {
3258   // if filter already in library or entry filter is null do nothing
3259   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3260   if ( !aFilterNode.isNull() || theFilter->_is_nil() )
3261     return false;
3262
3263   // get section corresponding to the filter type
3264   ElementType anEntType = theFilter->GetElementType();
3265
3266   LDOM_Node aSection = getSection( anEntType, myDoc, true );
3267   if ( aSection.isNull() )
3268     return false;
3269
3270   // create filter item
3271   LDOM_Element aFilterItem = createFilterItem( theFilterName, theFilter, myDoc );
3272   if ( aFilterItem.isNull() )
3273     return false;
3274   else
3275   {
3276     aSection.appendChild( aFilterItem );
3277     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
3278       TPythonDump()<<this<<".Add('"<<theFilterName<<"',"<<aFilter<<")";
3279     return true;
3280   }
3281 }
3282
3283 //=======================================================================
3284 // name    : FilterLibrary_i::Add
3285 // Purpose : Add new filter to library
3286 //=======================================================================
3287 CORBA::Boolean FilterLibrary_i::AddEmpty( const char* theFilterName, ElementType theType )
3288 {
3289   // if filter already in library or entry filter is null do nothing
3290   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3291   if ( !aFilterNode.isNull() )
3292     return false;
3293
3294   LDOM_Node aSection = getSection( theType, myDoc, true );
3295   if ( aSection.isNull() )
3296     return false;
3297
3298   // create filter item
3299   Filter_var aFilter = myFilterMgr->CreateFilter();
3300
3301   LDOM_Element aFilterItem = createFilterItem( theFilterName, aFilter, myDoc );
3302   if ( aFilterItem.isNull() )
3303     return false;
3304   else
3305   {
3306     aSection.appendChild( aFilterItem );
3307     TPythonDump()<<this<<".AddEmpty('"<<theFilterName<<"',"<<theType<<")";
3308     return true;
3309   }
3310 }
3311
3312 //=======================================================================
3313 // name    : FilterLibrary_i::Delete
3314 // Purpose : Delete filter from library
3315 //=======================================================================
3316 CORBA::Boolean FilterLibrary_i::Delete ( const char* theFilterName )
3317 {
3318   LDOM_Node aParentNode;
3319   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc, &aParentNode );
3320   if ( aFilterNode.isNull() || aParentNode.isNull() )
3321     return false;
3322
3323   aParentNode.removeChild( aFilterNode );
3324   TPythonDump()<<this<<".Delete('"<<theFilterName<<"')";
3325   return true;
3326 }
3327
3328 //=======================================================================
3329 // name      : FilterLibrary_i::Replace
3330 // Purpose   : Replace existing filter with entry filter.
3331 // IMPORTANT : If filter does not exist it is not created
3332 //=======================================================================
3333 CORBA::Boolean FilterLibrary_i::Replace( const char* theFilterName,
3334                                          const char* theNewName,
3335                                          Filter_ptr  theFilter )
3336 {
3337   LDOM_Element aFilterItem = findFilter( theFilterName, myDoc );
3338   if ( aFilterItem.isNull() || theFilter->_is_nil() )
3339     return false;
3340
3341   LDOM_Element aNewItem = createFilterItem( theNewName, theFilter, myDoc );
3342   if ( aNewItem.isNull() )
3343     return false;
3344   else
3345   {
3346     aFilterItem.ReplaceElement( aNewItem );
3347     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
3348       TPythonDump()<<this<<".Replace('"<<theFilterName<<"',"<<theNewName<<"',"<<aFilter<<")";
3349     return true;
3350   }
3351 }
3352
3353 //=======================================================================
3354 // name    : FilterLibrary_i::Save
3355 // Purpose : Save library on disk
3356 //=======================================================================
3357 CORBA::Boolean FilterLibrary_i::Save()
3358 {
3359   if ( myFileName == 0 || strlen( myFileName ) == 0 )
3360     return false;
3361
3362   FILE* aOutFile = fopen( myFileName, "wt" );
3363   if ( !aOutFile )
3364     return false;
3365
3366   LDOM_XmlWriter aWriter( aOutFile );
3367   aWriter.SetIndentation( 2 );
3368   aWriter << myDoc;
3369   fclose( aOutFile );
3370
3371   TPythonDump()<<this<<".Save()";
3372   return true;
3373 }
3374
3375 //=======================================================================
3376 // name    : FilterLibrary_i::SaveAs
3377 // Purpose : Save library on disk
3378 //=======================================================================
3379 CORBA::Boolean FilterLibrary_i::SaveAs( const char* aFileName )
3380 {
3381   myFileName = strdup ( aFileName );
3382   TPythonDump()<<this<<".SaveAs('"<<aFileName<<"')";
3383   return Save();
3384 }
3385
3386 //=======================================================================
3387 // name    : FilterLibrary_i::IsPresent
3388 // Purpose : Verify whether filter is in library
3389 //=======================================================================
3390 CORBA::Boolean FilterLibrary_i::IsPresent( const char* theFilterName )
3391 {
3392   return !findFilter( theFilterName, myDoc ).isNull();
3393 }
3394
3395 //=======================================================================
3396 // name    : FilterLibrary_i::NbFilters
3397 // Purpose : Return amount of filters in library
3398 //=======================================================================
3399 CORBA::Long FilterLibrary_i::NbFilters( ElementType theType )
3400 {
3401   string_array_var aNames = GetNames( theType );
3402   return aNames->length();
3403 }
3404
3405 //=======================================================================
3406 // name    : FilterLibrary_i::GetNames
3407 // Purpose : Get names of filters from library
3408 //=======================================================================
3409 string_array* FilterLibrary_i::GetNames( ElementType theType )
3410 {
3411   string_array_var anArray = new string_array;
3412   TColStd_SequenceOfHAsciiString aSeq;
3413
3414   LDOM_Node aSection = getSection( theType, myDoc, false );
3415
3416   if ( !aSection.isNull() )
3417   {
3418     for ( LDOM_Node aFilter = aSection.getFirstChild();
3419           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
3420     {
3421       LDOM_Element& anElem = ( LDOM_Element& )aFilter;
3422       aSeq.Append( new TCollection_HAsciiString(
3423          (Standard_CString)anElem.getAttribute( "name" ).GetString() ) );
3424     }
3425   }
3426
3427   anArray->length( aSeq.Length() );
3428   for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
3429     anArray[ i - 1 ] = CORBA::string_dup( aSeq( i )->ToCString() );
3430
3431   return anArray._retn();
3432 }
3433
3434 //=======================================================================
3435 // name    : FilterLibrary_i::GetAllNames
3436 // Purpose : Get names of filters from library
3437 //=======================================================================
3438 string_array* FilterLibrary_i::GetAllNames()
3439 {
3440   string_array_var aResArray = new string_array;
3441   for ( int type = SMESH::ALL; type <= SMESH::VOLUME; type++ )
3442   {
3443     SMESH::string_array_var aNames = GetNames( (SMESH::ElementType)type );
3444
3445     int aPrevLength = aResArray->length();
3446     aResArray->length( aPrevLength + aNames->length() );
3447     for ( int i = 0, n = aNames->length(); i < n; i++ )
3448       aResArray[ aPrevLength + i ] = aNames[ i ];
3449   }
3450
3451   return aResArray._retn();
3452 }