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