Salome HOME
d2a7ae648d7b2510cbef06115cb41d1a3276743c
[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       : CoplanarFaces_i
1508   Description : Returns true if a mesh face is a coplanar neighbour to a given one
1509 */
1510 CoplanarFaces_i::CoplanarFaces_i()
1511 {
1512   myCoplanarFacesPtr.reset(new Controls::CoplanarFaces());
1513   myFunctorPtr = myPredicatePtr = myCoplanarFacesPtr;
1514 }
1515
1516 void CoplanarFaces_i::SetFace ( CORBA::Long theFaceID )
1517 {
1518   myCoplanarFacesPtr->SetFace(theFaceID);
1519   TPythonDump()<<this<<".SetFace("<<theFaceID<<")";
1520 }
1521
1522 void CoplanarFaces_i::SetTolerance( CORBA::Double theToler )
1523 {
1524   myCoplanarFacesPtr->SetTolerance(theToler);
1525   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1526 }
1527
1528 CORBA::Long CoplanarFaces_i::GetFace () const
1529 {
1530   return myCoplanarFacesPtr->GetFace();
1531 }
1532
1533 char* CoplanarFaces_i::GetFaceAsString () const
1534 {
1535   TCollection_AsciiString str(Standard_Integer(myCoplanarFacesPtr->GetFace()));
1536   return CORBA::string_dup( str.ToCString() );
1537 }
1538
1539 CORBA::Double CoplanarFaces_i::GetTolerance() const
1540 {
1541   return myCoplanarFacesPtr->GetTolerance();
1542 }
1543
1544 FunctorType CoplanarFaces_i::GetFunctorType()
1545 {
1546   return SMESH::FT_CoplanarFaces;
1547 }
1548
1549 /*
1550   Class       : Comparator_i
1551   Description : Base class for comparators
1552 */
1553 Comparator_i::Comparator_i():
1554   myNumericalFunctor( NULL )
1555 {}
1556
1557 Comparator_i::~Comparator_i()
1558 {
1559   if ( myNumericalFunctor )
1560     myNumericalFunctor->Destroy();
1561 }
1562
1563 void Comparator_i::SetMargin( CORBA::Double theValue )
1564 {
1565   myComparatorPtr->SetMargin( theValue );
1566   TPythonDump()<<this<<".SetMargin("<<theValue<<")";
1567 }
1568
1569 CORBA::Double Comparator_i::GetMargin()
1570 {
1571   return myComparatorPtr->GetMargin();
1572 }
1573
1574 void Comparator_i::SetNumFunctor( NumericalFunctor_ptr theFunct )
1575 {
1576   if ( myNumericalFunctor )
1577     myNumericalFunctor->Destroy();
1578
1579   myNumericalFunctor = DownCast<NumericalFunctor_i*>(theFunct);
1580
1581   if ( myNumericalFunctor )
1582   {
1583     myComparatorPtr->SetNumFunctor( myNumericalFunctor->GetNumericalFunctor() );
1584     myNumericalFunctor->Register();
1585     TPythonDump()<<this<<".SetNumFunctor("<<myNumericalFunctor<<")";
1586   }
1587 }
1588
1589 Controls::ComparatorPtr Comparator_i::GetComparator()
1590 {
1591   return myComparatorPtr;
1592 }
1593
1594 NumericalFunctor_i* Comparator_i::GetNumFunctor_i()
1595 {
1596   return myNumericalFunctor;
1597 }
1598
1599
1600 /*
1601   Class       : LessThan_i
1602   Description : Comparator "<"
1603 */
1604 LessThan_i::LessThan_i()
1605 {
1606   myComparatorPtr.reset( new Controls::LessThan() );
1607   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1608 }
1609
1610 FunctorType LessThan_i::GetFunctorType()
1611 {
1612   return SMESH::FT_LessThan;
1613 }
1614
1615
1616 /*
1617   Class       : MoreThan_i
1618   Description : Comparator ">"
1619 */
1620 MoreThan_i::MoreThan_i()
1621 {
1622   myComparatorPtr.reset( new Controls::MoreThan() );
1623   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1624 }
1625
1626 FunctorType MoreThan_i::GetFunctorType()
1627 {
1628   return SMESH::FT_MoreThan;
1629 }
1630
1631
1632 /*
1633   Class       : EqualTo_i
1634   Description : Comparator "="
1635 */
1636 EqualTo_i::EqualTo_i()
1637 : myEqualToPtr( new Controls::EqualTo() )
1638 {
1639   myFunctorPtr = myPredicatePtr = myComparatorPtr = myEqualToPtr;
1640 }
1641
1642 void EqualTo_i::SetTolerance( CORBA::Double theToler )
1643 {
1644   myEqualToPtr->SetTolerance( theToler );
1645   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1646 }
1647
1648 CORBA::Double EqualTo_i::GetTolerance()
1649 {
1650   return myEqualToPtr->GetTolerance();
1651 }
1652
1653 FunctorType EqualTo_i::GetFunctorType()
1654 {
1655   return SMESH::FT_EqualTo;
1656 }
1657
1658 /*
1659   Class       : LogicalNOT_i
1660   Description : Logical NOT predicate
1661 */
1662 LogicalNOT_i::LogicalNOT_i()
1663 : myPredicate( NULL ),
1664   myLogicalNOTPtr( new Controls::LogicalNOT() )
1665 {
1666   myFunctorPtr = myPredicatePtr = myLogicalNOTPtr;
1667 }
1668
1669 LogicalNOT_i::~LogicalNOT_i()
1670 {
1671   if ( myPredicate )
1672     myPredicate->Destroy();
1673 }
1674
1675 void LogicalNOT_i::SetPredicate( Predicate_ptr thePredicate )
1676 {
1677   if ( myPredicate )
1678     myPredicate->Destroy();
1679
1680   myPredicate = SMESH::GetPredicate(thePredicate);
1681
1682   if ( myPredicate ){
1683     myLogicalNOTPtr->SetPredicate(myPredicate->GetPredicate());
1684     myPredicate->Register();
1685     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
1686   }
1687 }
1688
1689 FunctorType LogicalNOT_i::GetFunctorType()
1690 {
1691   return SMESH::FT_LogicalNOT;
1692 }
1693
1694 Predicate_i* LogicalNOT_i::GetPredicate_i()
1695 {
1696   return myPredicate;
1697 }
1698
1699
1700 /*
1701   Class       : LogicalBinary_i
1702   Description : Base class for binary logical predicate
1703 */
1704 LogicalBinary_i::LogicalBinary_i()
1705 : myPredicate1( NULL ),
1706   myPredicate2( NULL )
1707 {}
1708
1709 LogicalBinary_i::~LogicalBinary_i()
1710 {
1711   if ( myPredicate1 )
1712     myPredicate1->Destroy();
1713
1714   if ( myPredicate2 )
1715     myPredicate2->Destroy();
1716 }
1717
1718 void LogicalBinary_i::SetMesh( SMESH_Mesh_ptr theMesh )
1719 {
1720   if ( myPredicate1 )
1721     myPredicate1->SetMesh( theMesh );
1722
1723   if ( myPredicate2 )
1724     myPredicate2->SetMesh( theMesh );
1725 }
1726
1727 void LogicalBinary_i::SetPredicate1( Predicate_ptr thePredicate )
1728 {
1729   if ( myPredicate1 )
1730     myPredicate1->Destroy();
1731
1732   myPredicate1 = SMESH::GetPredicate(thePredicate);
1733
1734   if ( myPredicate1 ){
1735     myLogicalBinaryPtr->SetPredicate1(myPredicate1->GetPredicate());
1736     myPredicate1->Register();
1737     TPythonDump()<<this<<".SetPredicate1("<<myPredicate1<<")";
1738   }
1739 }
1740
1741 void LogicalBinary_i::SetPredicate2( Predicate_ptr thePredicate )
1742 {
1743   if ( myPredicate2 )
1744     myPredicate2->Destroy();
1745
1746   myPredicate2 = SMESH::GetPredicate(thePredicate);
1747
1748   if ( myPredicate2 ){
1749     myLogicalBinaryPtr->SetPredicate2(myPredicate2->GetPredicate());
1750     myPredicate2->Register();
1751     TPythonDump()<<this<<".SetPredicate2("<<myPredicate2<<")";
1752   }
1753 }
1754
1755 Controls::LogicalBinaryPtr LogicalBinary_i::GetLogicalBinary()
1756 {
1757   return myLogicalBinaryPtr;
1758 }
1759
1760 Predicate_i* LogicalBinary_i::GetPredicate1_i()
1761 {
1762   return myPredicate1;
1763 }
1764 Predicate_i* LogicalBinary_i::GetPredicate2_i()
1765 {
1766   return myPredicate2;
1767 }
1768
1769
1770 /*
1771   Class       : LogicalAND_i
1772   Description : Logical AND
1773 */
1774 LogicalAND_i::LogicalAND_i()
1775 {
1776   myLogicalBinaryPtr.reset( new Controls::LogicalAND() );
1777   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
1778 }
1779
1780 FunctorType LogicalAND_i::GetFunctorType()
1781 {
1782   return SMESH::FT_LogicalAND;
1783 }
1784
1785
1786 /*
1787   Class       : LogicalOR_i
1788   Description : Logical OR
1789 */
1790 LogicalOR_i::LogicalOR_i()
1791 {
1792   myLogicalBinaryPtr.reset( new Controls::LogicalOR() );
1793   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
1794 }
1795
1796 FunctorType LogicalOR_i::GetFunctorType()
1797 {
1798   return SMESH::FT_LogicalOR;
1799 }
1800
1801
1802 /*
1803                             FILTER MANAGER
1804 */
1805
1806 FilterManager_i::FilterManager_i()
1807 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
1808 {
1809   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
1810   //PortableServer::ObjectId_var anObjectId =
1811   //  SMESH_Gen_i::GetPOA()->activate_object( this );
1812 }
1813
1814
1815 FilterManager_i::~FilterManager_i()
1816 {
1817   //TPythonDump()<<this<<".Destroy()";
1818 }
1819
1820
1821 MinimumAngle_ptr FilterManager_i::CreateMinimumAngle()
1822 {
1823   SMESH::MinimumAngle_i* aServant = new SMESH::MinimumAngle_i();
1824   SMESH::MinimumAngle_var anObj = aServant->_this();
1825   TPythonDump()<<aServant<<" = "<<this<<".CreateMinimumAngle()";
1826   return anObj._retn();
1827 }
1828
1829
1830 AspectRatio_ptr FilterManager_i::CreateAspectRatio()
1831 {
1832   SMESH::AspectRatio_i* aServant = new SMESH::AspectRatio_i();
1833   SMESH::AspectRatio_var anObj = aServant->_this();
1834   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio()";
1835   return anObj._retn();
1836 }
1837
1838
1839 AspectRatio3D_ptr FilterManager_i::CreateAspectRatio3D()
1840 {
1841   SMESH::AspectRatio3D_i* aServant = new SMESH::AspectRatio3D_i();
1842   SMESH::AspectRatio3D_var anObj = aServant->_this();
1843   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio3D()";
1844   return anObj._retn();
1845 }
1846
1847
1848 Warping_ptr FilterManager_i::CreateWarping()
1849 {
1850   SMESH::Warping_i* aServant = new SMESH::Warping_i();
1851   SMESH::Warping_var anObj = aServant->_this();
1852   TPythonDump()<<aServant<<" = "<<this<<".CreateWarping()";
1853   return anObj._retn();
1854 }
1855
1856
1857 Taper_ptr FilterManager_i::CreateTaper()
1858 {
1859   SMESH::Taper_i* aServant = new SMESH::Taper_i();
1860   SMESH::Taper_var anObj = aServant->_this();
1861   TPythonDump()<<aServant<<" = "<<this<<".CreateTaper()";
1862   return anObj._retn();
1863 }
1864
1865
1866 Skew_ptr FilterManager_i::CreateSkew()
1867 {
1868   SMESH::Skew_i* aServant = new SMESH::Skew_i();
1869   SMESH::Skew_var anObj = aServant->_this();
1870   TPythonDump()<<aServant<<" = "<<this<<".CreateSkew()";
1871   return anObj._retn();
1872 }
1873
1874
1875 Area_ptr FilterManager_i::CreateArea()
1876 {
1877   SMESH::Area_i* aServant = new SMESH::Area_i();
1878   SMESH::Area_var anObj = aServant->_this();
1879   TPythonDump()<<aServant<<" = "<<this<<".CreateArea()";
1880   return anObj._retn();
1881 }
1882
1883
1884 Volume3D_ptr FilterManager_i::CreateVolume3D()
1885 {
1886   SMESH::Volume3D_i* aServant = new SMESH::Volume3D_i();
1887   SMESH::Volume3D_var anObj = aServant->_this();
1888   TPythonDump()<<aServant<<" = "<<this<<".CreateVolume3D()";
1889   return anObj._retn();
1890 }
1891
1892
1893 MaxElementLength2D_ptr FilterManager_i::CreateMaxElementLength2D()
1894 {
1895   SMESH::MaxElementLength2D_i* aServant = new SMESH::MaxElementLength2D_i();
1896   SMESH::MaxElementLength2D_var anObj = aServant->_this();
1897   TPythonDump()<<aServant<<" = "<<this<<".CreateMaxElementLength2D()";
1898   return anObj._retn();
1899 }
1900
1901
1902 MaxElementLength3D_ptr FilterManager_i::CreateMaxElementLength3D()
1903 {
1904   SMESH::MaxElementLength3D_i* aServant = new SMESH::MaxElementLength3D_i();
1905   SMESH::MaxElementLength3D_var anObj = aServant->_this();
1906   TPythonDump()<<aServant<<" = "<<this<<".CreateMaxElementLength3D()";
1907   return anObj._retn();
1908 }
1909
1910
1911 Length_ptr FilterManager_i::CreateLength()
1912 {
1913   SMESH::Length_i* aServant = new SMESH::Length_i();
1914   SMESH::Length_var anObj = aServant->_this();
1915   TPythonDump()<<aServant<<" = "<<this<<".CreateLength()";
1916   return anObj._retn();
1917 }
1918
1919 Length2D_ptr FilterManager_i::CreateLength2D()
1920 {
1921   SMESH::Length2D_i* aServant = new SMESH::Length2D_i();
1922   SMESH::Length2D_var anObj = aServant->_this();
1923   TPythonDump()<<aServant<<" = "<<this<<".CreateLength2D()";
1924   return anObj._retn();
1925 }
1926
1927 MultiConnection_ptr FilterManager_i::CreateMultiConnection()
1928 {
1929   SMESH::MultiConnection_i* aServant = new SMESH::MultiConnection_i();
1930   SMESH::MultiConnection_var anObj = aServant->_this();
1931   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection()";
1932   return anObj._retn();
1933 }
1934
1935 MultiConnection2D_ptr FilterManager_i::CreateMultiConnection2D()
1936 {
1937   SMESH::MultiConnection2D_i* aServant = new SMESH::MultiConnection2D_i();
1938   SMESH::MultiConnection2D_var anObj = aServant->_this();
1939   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection2D()";
1940   return anObj._retn();
1941 }
1942
1943 BelongToGeom_ptr FilterManager_i::CreateBelongToGeom()
1944 {
1945   SMESH::BelongToGeom_i* aServant = new SMESH::BelongToGeom_i();
1946   SMESH::BelongToGeom_var anObj = aServant->_this();
1947   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGeom()";
1948   return anObj._retn();
1949 }
1950
1951 BelongToPlane_ptr FilterManager_i::CreateBelongToPlane()
1952 {
1953   SMESH::BelongToPlane_i* aServant = new SMESH::BelongToPlane_i();
1954   SMESH::BelongToPlane_var anObj = aServant->_this();
1955   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToPlane()";
1956   return anObj._retn();
1957 }
1958
1959 BelongToCylinder_ptr FilterManager_i::CreateBelongToCylinder()
1960 {
1961   SMESH::BelongToCylinder_i* aServant = new SMESH::BelongToCylinder_i();
1962   SMESH::BelongToCylinder_var anObj = aServant->_this();
1963   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToCylinder()";
1964   return anObj._retn();
1965 }
1966
1967 BelongToGenSurface_ptr FilterManager_i::CreateBelongToGenSurface()
1968 {
1969   SMESH::BelongToGenSurface_i* aServant = new SMESH::BelongToGenSurface_i();
1970   SMESH::BelongToGenSurface_var anObj = aServant->_this();
1971   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGenSurface()";
1972   return anObj._retn();
1973 }
1974
1975 LyingOnGeom_ptr FilterManager_i::CreateLyingOnGeom()
1976 {
1977   SMESH::LyingOnGeom_i* aServant = new SMESH::LyingOnGeom_i();
1978   SMESH::LyingOnGeom_var anObj = aServant->_this();
1979   TPythonDump()<<aServant<<" = "<<this<<".CreateLyingOnGeom()";
1980   return anObj._retn();
1981 }
1982
1983 CoplanarFaces_ptr FilterManager_i::CreateCoplanarFaces()
1984 {
1985   SMESH::CoplanarFaces_i* aServant = new SMESH::CoplanarFaces_i();
1986   SMESH::CoplanarFaces_var anObj = aServant->_this();
1987   TPythonDump()<<aServant<<" = "<<this<<".CreateCoplanarFaces()";
1988   return anObj._retn();
1989 }
1990
1991 FreeBorders_ptr FilterManager_i::CreateFreeBorders()
1992 {
1993   SMESH::FreeBorders_i* aServant = new SMESH::FreeBorders_i();
1994   SMESH::FreeBorders_var anObj = aServant->_this();
1995   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeBorders()";
1996   return anObj._retn();
1997 }
1998
1999 FreeEdges_ptr FilterManager_i::CreateFreeEdges()
2000 {
2001   SMESH::FreeEdges_i* aServant = new SMESH::FreeEdges_i();
2002   SMESH::FreeEdges_var anObj = aServant->_this();
2003   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeEdges()";
2004   return anObj._retn();
2005 }
2006
2007 FreeFaces_ptr FilterManager_i::CreateFreeFaces()
2008 {
2009   SMESH::FreeFaces_i* aServant = new SMESH::FreeFaces_i();
2010   SMESH::FreeFaces_var anObj = aServant->_this();
2011   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeFaces()";
2012   return anObj._retn();
2013 }
2014
2015 FreeNodes_ptr FilterManager_i::CreateFreeNodes()
2016 {
2017   SMESH::FreeNodes_i* aServant = new SMESH::FreeNodes_i();
2018   SMESH::FreeNodes_var anObj = aServant->_this();
2019   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeNodes()";
2020   return anObj._retn();
2021 }
2022
2023 RangeOfIds_ptr FilterManager_i::CreateRangeOfIds()
2024 {
2025   SMESH::RangeOfIds_i* aServant = new SMESH::RangeOfIds_i();
2026   SMESH::RangeOfIds_var anObj = aServant->_this();
2027   TPythonDump()<<aServant<<" = "<<this<<".CreateRangeOfIds()";
2028   return anObj._retn();
2029 }
2030
2031 BadOrientedVolume_ptr FilterManager_i::CreateBadOrientedVolume()
2032 {
2033   SMESH::BadOrientedVolume_i* aServant = new SMESH::BadOrientedVolume_i();
2034   SMESH::BadOrientedVolume_var anObj = aServant->_this();
2035   TPythonDump()<<aServant<<" = "<<this<<".CreateBadOrientedVolume()";
2036   return anObj._retn();
2037 }
2038
2039 LessThan_ptr FilterManager_i::CreateLessThan()
2040 {
2041   SMESH::LessThan_i* aServant = new SMESH::LessThan_i();
2042   SMESH::LessThan_var anObj = aServant->_this();
2043   TPythonDump()<<aServant<<" = "<<this<<".CreateLessThan()";
2044   return anObj._retn();
2045 }
2046
2047 MoreThan_ptr FilterManager_i::CreateMoreThan()
2048 {
2049   SMESH::MoreThan_i* aServant = new SMESH::MoreThan_i();
2050   SMESH::MoreThan_var anObj = aServant->_this();
2051   TPythonDump()<<aServant<<" = "<<this<<".CreateMoreThan()";
2052   return anObj._retn();
2053 }
2054
2055 EqualTo_ptr FilterManager_i::CreateEqualTo()
2056 {
2057   SMESH::EqualTo_i* aServant = new SMESH::EqualTo_i();
2058   SMESH::EqualTo_var anObj = aServant->_this();
2059   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualTo()";
2060   return anObj._retn();
2061 }
2062
2063 LogicalNOT_ptr FilterManager_i::CreateLogicalNOT()
2064 {
2065   SMESH::LogicalNOT_i* aServant = new SMESH::LogicalNOT_i();
2066   SMESH::LogicalNOT_var anObj = aServant->_this();
2067   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalNOT()";
2068   return anObj._retn();
2069 }
2070
2071 LogicalAND_ptr FilterManager_i::CreateLogicalAND()
2072 {
2073   SMESH::LogicalAND_i* aServant = new SMESH::LogicalAND_i();
2074   SMESH::LogicalAND_var anObj = aServant->_this();
2075   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalAND()";
2076   return anObj._retn();
2077 }
2078
2079 LogicalOR_ptr FilterManager_i::CreateLogicalOR()
2080 {
2081   SMESH::LogicalOR_i* aServant = new SMESH::LogicalOR_i();
2082   SMESH::LogicalOR_var anObj = aServant->_this();
2083   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalOR()";
2084   return anObj._retn();
2085 }
2086
2087 LinearOrQuadratic_ptr FilterManager_i::CreateLinearOrQuadratic()
2088 {
2089   SMESH::LinearOrQuadratic_i* aServant = new SMESH::LinearOrQuadratic_i();
2090   SMESH::LinearOrQuadratic_var anObj = aServant->_this();
2091   TPythonDump()<<aServant<<" = "<<this<<".CreateLinearOrQuadratic()";
2092   return anObj._retn();
2093 }
2094
2095 GroupColor_ptr FilterManager_i::CreateGroupColor()
2096 {
2097   SMESH::GroupColor_i* aServant = new SMESH::GroupColor_i();
2098   SMESH::GroupColor_var anObj = aServant->_this();
2099   TPythonDump()<<aServant<<" = "<<this<<".CreateGroupColor()";
2100   return anObj._retn();
2101 }
2102
2103 ElemGeomType_ptr FilterManager_i::CreateElemGeomType()
2104 {
2105   SMESH::ElemGeomType_i* aServant = new SMESH::ElemGeomType_i();
2106   SMESH::ElemGeomType_var anObj = aServant->_this();
2107   TPythonDump()<<aServant<<" = "<<this<<".CreateElemGeomType()";
2108   return anObj._retn();
2109 }
2110
2111 Filter_ptr FilterManager_i::CreateFilter()
2112 {
2113   SMESH::Filter_i* aServant = new SMESH::Filter_i();
2114   SMESH::Filter_var anObj = aServant->_this();
2115   TPythonDump()<<aServant<<" = "<<this<<".CreateFilter()";
2116   return anObj._retn();
2117 }
2118
2119 FilterLibrary_ptr FilterManager_i::LoadLibrary( const char* aFileName )
2120 {
2121   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i( aFileName );
2122   SMESH::FilterLibrary_var anObj = aServant->_this();
2123   TPythonDump()<<aServant<<" = "<<this<<".LoadLibrary('"<<aFileName<<"')";
2124   return anObj._retn();
2125 }
2126
2127 FilterLibrary_ptr FilterManager_i::CreateLibrary()
2128 {
2129   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i();
2130   SMESH::FilterLibrary_var anObj = aServant->_this();
2131   TPythonDump()<<aServant<<" = "<<this<<".CreateLibrary()";
2132   return anObj._retn();
2133 }
2134
2135 CORBA::Boolean FilterManager_i::DeleteLibrary( const char* aFileName )
2136 {
2137   TPythonDump()<<this<<".DeleteLibrary("<<aFileName<<")";
2138   return remove( aFileName ) ? false : true;
2139 }
2140
2141 //=============================================================================
2142 /*!
2143  *  SMESH_Gen_i::CreateFilterManager
2144  *
2145  *  Create filter manager
2146  */
2147 //=============================================================================
2148
2149 SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
2150 {
2151   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2152   SMESH::FilterManager_var anObj = aFilter->_this();
2153   return anObj._retn();
2154 }
2155
2156
2157 /*
2158                               FILTER
2159 */
2160
2161 //=======================================================================
2162 // name    : Filter_i::Filter_i
2163 // Purpose : Constructor
2164 //=======================================================================
2165 Filter_i::Filter_i()
2166 : myPredicate( NULL )
2167 {}
2168
2169 //=======================================================================
2170 // name    : Filter_i::~Filter_i
2171 // Purpose : Destructor
2172 //=======================================================================
2173 Filter_i::~Filter_i()
2174 {
2175   if ( myPredicate )
2176     myPredicate->Destroy();
2177
2178   if(!CORBA::is_nil(myMesh))
2179     myMesh->Destroy();
2180
2181   //TPythonDump()<<this<<".Destroy()";
2182 }
2183
2184 //=======================================================================
2185 // name    : Filter_i::SetPredicate
2186 // Purpose : Set predicate
2187 //=======================================================================
2188 void Filter_i::SetPredicate( Predicate_ptr thePredicate )
2189 {
2190   if ( myPredicate )
2191     myPredicate->Destroy();
2192
2193   myPredicate = SMESH::GetPredicate(thePredicate);
2194
2195   if ( myPredicate )
2196   {
2197     myFilter.SetPredicate( myPredicate->GetPredicate() );
2198     myPredicate->Register();
2199     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
2200   }
2201 }
2202
2203 //=======================================================================
2204 // name    : Filter_i::GetElementType
2205 // Purpose : Get entity type
2206 //=======================================================================
2207 SMESH::ElementType Filter_i::GetElementType()
2208 {
2209   return myPredicate != 0 ? myPredicate->GetElementType() : SMESH::ALL;
2210 }
2211
2212 //=======================================================================
2213 // name    : Filter_i::SetMesh
2214 // Purpose : Set mesh
2215 //=======================================================================
2216 void
2217 Filter_i::
2218 SetMesh( SMESH_Mesh_ptr theMesh )
2219 {
2220   if(!CORBA::is_nil(theMesh))
2221     theMesh->Register();
2222
2223   if(!CORBA::is_nil(myMesh))
2224     myMesh->Destroy();
2225
2226   myMesh = SMESH_Mesh::_duplicate( theMesh );
2227   TPythonDump()<<this<<".SetMesh("<<theMesh<<")";
2228 }
2229
2230 SMESH::long_array*
2231 Filter_i::
2232 GetIDs()
2233 {
2234   return GetElementsId(myMesh);
2235 }
2236
2237 //=======================================================================
2238 // name    : Filter_i::GetElementsId
2239 // Purpose : Get ids of entities
2240 //=======================================================================
2241 void
2242 Filter_i::
2243 GetElementsId( Predicate_i* thePredicate,
2244                const SMDS_Mesh* theMesh,
2245                Controls::Filter::TIdSequence& theSequence )
2246 {
2247   if (thePredicate)
2248     Controls::Filter::GetElementsId(theMesh,thePredicate->GetPredicate(),theSequence);
2249 }
2250
2251 void
2252 Filter_i::
2253 GetElementsId( Predicate_i* thePredicate,
2254                SMESH_Mesh_ptr theMesh,
2255                Controls::Filter::TIdSequence& theSequence )
2256 {
2257   if (thePredicate) 
2258     if(const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh))
2259       Controls::Filter::GetElementsId(aMesh,thePredicate->GetPredicate(),theSequence);
2260 }
2261
2262 SMESH::long_array*
2263 Filter_i::
2264 GetElementsId( SMESH_Mesh_ptr theMesh )
2265 {
2266   SMESH::long_array_var anArray = new SMESH::long_array;
2267   if(!CORBA::is_nil(theMesh) && myPredicate){
2268     Controls::Filter::TIdSequence aSequence;
2269     GetElementsId(myPredicate,theMesh,aSequence);
2270     long i = 0, iEnd = aSequence.size();
2271     anArray->length( iEnd );
2272     for ( ; i < iEnd; i++ )
2273       anArray[ i ] = aSequence[i];
2274   }
2275   return anArray._retn();
2276 }
2277
2278 template<class TElement, class TIterator, class TPredicate>
2279 static void collectMeshInfo(const TIterator& theItr,
2280                             TPredicate& thePred,
2281                             SMESH::long_array& theRes)
2282 {         
2283   if (!theItr)
2284     return;
2285   while (theItr->more()) {
2286     const SMDS_MeshElement* anElem = theItr->next();
2287     if ( thePred->IsSatisfy( anElem->GetID() ) )
2288       theRes[ anElem->GetEntityType() ]++;
2289   }
2290 }
2291
2292 //=============================================================================
2293 /*!
2294  * \brief Returns statistic of mesh elements
2295  */
2296 //=============================================================================
2297 SMESH::long_array* ::Filter_i::GetMeshInfo()
2298 {
2299   SMESH::long_array_var aRes = new SMESH::long_array();
2300   aRes->length(SMESH::Entity_Last);
2301   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
2302     aRes[i] = 0;
2303
2304   if(!CORBA::is_nil(myMesh) && myPredicate) {
2305     const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(myMesh);
2306     SMDS_ElemIteratorPtr it;
2307     switch( GetElementType() )
2308     {
2309   case SMDSAbs_Node:
2310     collectMeshInfo<const SMDS_MeshNode*>(aMesh->nodesIterator(),myPredicate,aRes);
2311     break;
2312   case SMDSAbs_Edge:
2313     collectMeshInfo<const SMDS_MeshElement*>(aMesh->edgesIterator(),myPredicate,aRes);
2314     break;
2315   case SMDSAbs_Face:
2316     collectMeshInfo<const SMDS_MeshElement*>(aMesh->facesIterator(),myPredicate,aRes);
2317     break;
2318   case SMDSAbs_Volume:
2319     collectMeshInfo<const SMDS_MeshElement*>(aMesh->volumesIterator(),myPredicate,aRes);
2320     break;
2321   case SMDSAbs_All:
2322   default:
2323     collectMeshInfo<const SMDS_MeshElement*>(aMesh->elementsIterator(),myPredicate,aRes);
2324     break;
2325     }
2326   }
2327
2328
2329   return aRes._retn();  
2330 }
2331
2332 //================================================================================
2333 /*!
2334  * \brief Return GetElementType() within an array
2335  * Implement SMESH_IDSource interface
2336  */
2337 //================================================================================
2338
2339 SMESH::array_of_ElementType* Filter_i::GetTypes()
2340 {
2341   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
2342   types->length( 1 );
2343   types[0] = GetElementType();
2344   return types._retn();
2345 }
2346
2347 //=======================================================================
2348 //function : GetMesh
2349 //purpose  : Returns mesh
2350 //=======================================================================
2351
2352 SMESH::SMESH_Mesh_ptr Filter_i::GetMesh()
2353 {
2354   return SMESH_Mesh::_duplicate( myMesh );
2355 }
2356
2357 //=======================================================================
2358 // name    : getCriteria
2359 // Purpose : Retrieve criterions from predicate
2360 //=======================================================================
2361 static inline bool getCriteria( Predicate_i*                thePred,
2362                                 SMESH::Filter::Criteria_out theCriteria )
2363 {
2364   int aFType = thePred->GetFunctorType();
2365
2366   switch ( aFType )
2367   {
2368   case FT_FreeBorders:
2369   case FT_FreeEdges:
2370   case FT_FreeFaces:
2371   case FT_LinearOrQuadratic:
2372   case FT_FreeNodes:
2373     {
2374       CORBA::ULong i = theCriteria->length();
2375       theCriteria->length( i + 1 );
2376
2377       theCriteria[ i ] = createCriterion();
2378
2379       theCriteria[ i ].Type = aFType;
2380       theCriteria[ i ].TypeOfElement = thePred->GetElementType();
2381       return true;
2382     }
2383   case FT_BelongToGeom:
2384     {
2385       BelongToGeom_i* aPred = dynamic_cast<BelongToGeom_i*>( thePred );
2386
2387       CORBA::ULong i = theCriteria->length();
2388       theCriteria->length( i + 1 );
2389
2390       theCriteria[ i ] = createCriterion();
2391
2392       theCriteria[ i ].Type          = FT_BelongToGeom;
2393       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2394       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2395       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2396       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2397
2398       return true;
2399     }
2400   case FT_BelongToPlane:
2401   case FT_BelongToCylinder:
2402   case FT_BelongToGenSurface:
2403     {
2404       BelongToSurface_i* aPred = dynamic_cast<BelongToSurface_i*>( thePred );
2405
2406       CORBA::ULong i = theCriteria->length();
2407       theCriteria->length( i + 1 );
2408
2409       theCriteria[ i ] = createCriterion();
2410
2411       theCriteria[ i ].Type          = aFType;
2412       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2413       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2414       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2415       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2416
2417       return true;
2418     }
2419    case FT_LyingOnGeom:
2420     {
2421       LyingOnGeom_i* aPred = dynamic_cast<LyingOnGeom_i*>( thePred );
2422
2423       CORBA::ULong i = theCriteria->length();
2424       theCriteria->length( i + 1 );
2425
2426       theCriteria[ i ] = createCriterion();
2427
2428       theCriteria[ i ].Type          = FT_LyingOnGeom;
2429       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2430       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2431       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2432       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2433
2434       return true;
2435     }
2436    case FT_CoplanarFaces:
2437     {
2438       CoplanarFaces_i* aPred = dynamic_cast<CoplanarFaces_i*>( thePred );
2439
2440       CORBA::ULong i = theCriteria->length();
2441       theCriteria->length( i + 1 );
2442
2443       theCriteria[ i ] = createCriterion();
2444       CORBA::String_var faceId = aPred->GetFaceAsString();
2445
2446       theCriteria[ i ].Type          = FT_CoplanarFaces;
2447       theCriteria[ i ].ThresholdID   = faceId;
2448       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2449
2450       return true;
2451     }
2452   case FT_RangeOfIds:
2453     {
2454       RangeOfIds_i* aPred = dynamic_cast<RangeOfIds_i*>( thePred );
2455
2456       CORBA::ULong i = theCriteria->length();
2457       theCriteria->length( i + 1 );
2458
2459       theCriteria[ i ] = createCriterion();
2460
2461       theCriteria[ i ].Type          = FT_RangeOfIds;
2462       theCriteria[ i ].ThresholdStr  = aPred->GetRangeStr();
2463       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2464
2465       return true;
2466     }
2467   case FT_BadOrientedVolume:
2468     {
2469       BadOrientedVolume_i* aPred = dynamic_cast<BadOrientedVolume_i*>( thePred );
2470
2471       CORBA::ULong i = theCriteria->length();
2472       theCriteria->length( i + 1 );
2473
2474       theCriteria[ i ] = createCriterion();
2475
2476       theCriteria[ i ].Type          = FT_BadOrientedVolume;
2477       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2478
2479       return true;
2480     }
2481   case FT_LessThan:
2482   case FT_MoreThan:
2483   case FT_EqualTo:
2484     {
2485       Comparator_i* aCompar = dynamic_cast<Comparator_i*>( thePred );
2486
2487       CORBA::ULong i = theCriteria->length();
2488       theCriteria->length( i + 1 );
2489
2490       theCriteria[ i ] = createCriterion();
2491
2492       theCriteria[ i ].Type      = aCompar->GetNumFunctor_i()->GetFunctorType();
2493       theCriteria[ i ].Compare   = aFType;
2494       theCriteria[ i ].Threshold = aCompar->GetMargin();
2495       theCriteria[ i ].TypeOfElement = aCompar->GetElementType();
2496
2497       if ( aFType == FT_EqualTo )
2498       {
2499         EqualTo_i* aCompar = dynamic_cast<EqualTo_i*>( thePred );
2500         theCriteria[ i ].Tolerance = aCompar->GetTolerance();
2501       }
2502     }
2503     return true;
2504
2505   case FT_LogicalNOT:
2506     {
2507       Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
2508       getCriteria( aPred, theCriteria );
2509       theCriteria[ theCriteria->length() - 1 ].UnaryOp = FT_LogicalNOT;
2510     }
2511     return true;
2512
2513   case FT_LogicalAND:
2514   case FT_LogicalOR:
2515     {
2516       Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
2517       Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
2518       if ( !getCriteria( aPred1, theCriteria ) )
2519         return false;
2520       theCriteria[ theCriteria->length() - 1 ].BinaryOp = aFType;
2521       return getCriteria( aPred2, theCriteria );
2522     }
2523   case FT_GroupColor:
2524     {
2525       CORBA::ULong i = theCriteria->length();
2526       theCriteria->length( i + 1 );
2527
2528       theCriteria[ i ] = createCriterion();
2529
2530       GroupColor_i* aPred = dynamic_cast<GroupColor_i*>( thePred );
2531       theCriteria[ i ].Type          = aFType;
2532       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2533       theCriteria[ i ].ThresholdStr  = aPred->GetColorStr();
2534
2535       return true;
2536     }
2537   case FT_ElemGeomType:
2538     {
2539       CORBA::ULong i = theCriteria->length();
2540       theCriteria->length( i + 1 );
2541
2542       theCriteria[ i ] = createCriterion();
2543
2544       ElemGeomType_i* aPred = dynamic_cast<ElemGeomType_i*>( thePred );
2545       theCriteria[ i ].Type          = aFType;
2546       theCriteria[ i ].TypeOfElement = aPred->GetElementType();
2547       theCriteria[ i ].Threshold     = (double)aPred->GetGeometryType();
2548       return true;
2549     }
2550
2551   case FT_Undefined:
2552     return false;
2553   default:
2554     return false;
2555   }
2556 }
2557
2558 //=======================================================================
2559 // name    : Filter_i::GetCriteria
2560 // Purpose : Retrieve criterions from predicate
2561 //=======================================================================
2562 CORBA::Boolean Filter_i::GetCriteria( SMESH::Filter::Criteria_out theCriteria )
2563 {
2564   theCriteria = new SMESH::Filter::Criteria;
2565   return myPredicate != 0 ? getCriteria( myPredicate, theCriteria ) : true;
2566 }
2567
2568 //=======================================================================
2569 // name    : Filter_i::SetCriteria
2570 // Purpose : Create new predicate and set criterions in it
2571 //=======================================================================
2572 CORBA::Boolean Filter_i::SetCriteria( const SMESH::Filter::Criteria& theCriteria )
2573 {
2574   if ( myPredicate != 0 )
2575     myPredicate->Destroy();
2576
2577   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2578   FilterManager_ptr aFilterMgr = aFilter->_this();
2579
2580   // CREATE two lists ( PREDICATES  and LOG OP )
2581
2582   // Criterion
2583   TPythonDump()<<"aCriteria = []";
2584   std::list<SMESH::Predicate_ptr> aPredicates;
2585   std::list<int>                  aBinaries;
2586   for ( int i = 0, n = theCriteria.length(); i < n; i++ )
2587   {
2588     int         aCriterion    = theCriteria[ i ].Type;
2589     int         aCompare      = theCriteria[ i ].Compare;
2590     double      aThreshold    = theCriteria[ i ].Threshold;
2591     const char* aThresholdStr = theCriteria[ i ].ThresholdStr;
2592     const char* aThresholdID  = theCriteria[ i ].ThresholdID;
2593     int         aUnary        = theCriteria[ i ].UnaryOp;
2594     int         aBinary       = theCriteria[ i ].BinaryOp;
2595     double      aTolerance    = theCriteria[ i ].Tolerance;
2596     ElementType aTypeOfElem   = theCriteria[ i ].TypeOfElement;
2597     long        aPrecision    = theCriteria[ i ].Precision;
2598
2599     {
2600       TPythonDump pd;
2601       pd << "aCriterion = SMESH.Filter.Criterion(" << aCriterion << "," << aCompare
2602          << "," << aThreshold << ",'" << aThresholdStr;
2603       if (aThresholdID && strlen(aThresholdID))
2604         //pd << "',salome.ObjectToID(" << aThresholdID
2605         pd << "','" << aThresholdID
2606            << "'," << aUnary << "," << aBinary << "," << aTolerance
2607            << "," << aTypeOfElem << "," << aPrecision << ")";
2608       else
2609         pd << "',''," << aUnary << "," << aBinary << "," << aTolerance
2610            << "," << aTypeOfElem << "," << aPrecision << ")";
2611     }
2612
2613     SMESH::Predicate_ptr aPredicate = SMESH::Predicate::_nil();
2614     SMESH::NumericalFunctor_ptr aFunctor = SMESH::NumericalFunctor::_nil();
2615
2616     switch ( aCriterion )
2617     {
2618       // Functors
2619
2620       case SMESH::FT_MultiConnection:
2621         aFunctor = aFilterMgr->CreateMultiConnection();
2622         break;
2623       case SMESH::FT_MultiConnection2D:
2624         aFunctor = aFilterMgr->CreateMultiConnection2D();
2625         break;
2626       case SMESH::FT_Length:
2627         aFunctor = aFilterMgr->CreateLength();
2628         break;
2629       case SMESH::FT_Length2D:
2630         aFunctor = aFilterMgr->CreateLength2D();
2631         break;
2632       case SMESH::FT_AspectRatio:
2633         aFunctor = aFilterMgr->CreateAspectRatio();
2634         break;
2635       case SMESH::FT_AspectRatio3D:
2636         aFunctor = aFilterMgr->CreateAspectRatio3D();
2637         break;
2638       case SMESH::FT_Warping:
2639         aFunctor = aFilterMgr->CreateWarping();
2640         break;
2641       case SMESH::FT_MinimumAngle:
2642         aFunctor = aFilterMgr->CreateMinimumAngle();
2643         break;
2644       case SMESH::FT_Taper:
2645         aFunctor = aFilterMgr->CreateTaper();
2646         break;
2647       case SMESH::FT_Skew:
2648         aFunctor = aFilterMgr->CreateSkew();
2649         break;
2650       case SMESH::FT_Area:
2651         aFunctor = aFilterMgr->CreateArea();
2652         break;
2653       case SMESH::FT_Volume3D:
2654         aFunctor = aFilterMgr->CreateVolume3D();
2655         break;
2656       case SMESH::FT_MaxElementLength2D:
2657         aFunctor = aFilterMgr->CreateMaxElementLength2D();
2658         break;
2659       case SMESH::FT_MaxElementLength3D:
2660         aFunctor = aFilterMgr->CreateMaxElementLength3D();
2661         break;
2662
2663       // Predicates
2664
2665       case SMESH::FT_FreeBorders:
2666         aPredicate = aFilterMgr->CreateFreeBorders();
2667         break;
2668       case SMESH::FT_FreeEdges:
2669         aPredicate = aFilterMgr->CreateFreeEdges();
2670         break;
2671       case SMESH::FT_FreeFaces:
2672         aPredicate = aFilterMgr->CreateFreeFaces();
2673         break;
2674       case SMESH::FT_FreeNodes:
2675         aPredicate = aFilterMgr->CreateFreeNodes();
2676         break;
2677       case SMESH::FT_BelongToGeom:
2678         {
2679           SMESH::BelongToGeom_ptr tmpPred = aFilterMgr->CreateBelongToGeom();
2680           tmpPred->SetElementType( aTypeOfElem );
2681           tmpPred->SetShape( aThresholdID, aThresholdStr );
2682           tmpPred->SetTolerance( aTolerance );
2683           aPredicate = tmpPred;
2684         }
2685         break;
2686       case SMESH::FT_BelongToPlane:
2687       case SMESH::FT_BelongToCylinder:
2688       case SMESH::FT_BelongToGenSurface:
2689         {
2690           SMESH::BelongToSurface_ptr tmpPred;
2691           switch ( aCriterion ) {
2692           case SMESH::FT_BelongToPlane:
2693             tmpPred = aFilterMgr->CreateBelongToPlane(); break;
2694           case SMESH::FT_BelongToCylinder:
2695             tmpPred = aFilterMgr->CreateBelongToCylinder(); break;
2696           default:
2697             tmpPred = aFilterMgr->CreateBelongToGenSurface();
2698           }
2699           tmpPred->SetShape( aThresholdID, aThresholdStr, aTypeOfElem );
2700           tmpPred->SetTolerance( aTolerance );
2701           aPredicate = tmpPred;
2702         }
2703         break;
2704       case SMESH::FT_LyingOnGeom:
2705         {
2706           SMESH::LyingOnGeom_ptr tmpPred = aFilterMgr->CreateLyingOnGeom();
2707           tmpPred->SetElementType( aTypeOfElem );
2708           tmpPred->SetShape( aThresholdID, aThresholdStr );
2709           tmpPred->SetTolerance( aTolerance );
2710           aPredicate = tmpPred;
2711         }
2712         break;
2713       case SMESH::FT_RangeOfIds:
2714         {
2715           SMESH::RangeOfIds_ptr tmpPred = aFilterMgr->CreateRangeOfIds();
2716           tmpPred->SetRangeStr( aThresholdStr );
2717           tmpPred->SetElementType( aTypeOfElem );
2718           aPredicate = tmpPred;
2719         }
2720         break;
2721       case SMESH::FT_BadOrientedVolume:
2722         {
2723           aPredicate = aFilterMgr->CreateBadOrientedVolume();
2724         }
2725         break;
2726       case SMESH::FT_LinearOrQuadratic:
2727         {
2728           SMESH::LinearOrQuadratic_ptr tmpPred = aFilterMgr->CreateLinearOrQuadratic();
2729           tmpPred->SetElementType( aTypeOfElem );
2730           aPredicate = tmpPred;
2731           break;
2732         }
2733       case SMESH::FT_GroupColor:
2734         {
2735           SMESH::GroupColor_ptr tmpPred = aFilterMgr->CreateGroupColor();
2736           tmpPred->SetElementType( aTypeOfElem );
2737           tmpPred->SetColorStr( aThresholdStr );
2738           aPredicate = tmpPred;
2739           break;
2740         }
2741       case SMESH::FT_ElemGeomType:
2742         {
2743           SMESH::ElemGeomType_ptr tmpPred = aFilterMgr->CreateElemGeomType();
2744           tmpPred->SetElementType( aTypeOfElem );
2745           tmpPred->SetGeometryType( (GeometryType)(int)(aThreshold + 0.5) );
2746           aPredicate = tmpPred;
2747           break;
2748         }
2749       case SMESH::FT_CoplanarFaces:
2750         {
2751           SMESH::CoplanarFaces_ptr tmpPred = aFilterMgr->CreateCoplanarFaces();
2752           tmpPred->SetFace( atol (aThresholdID ));
2753           tmpPred->SetTolerance( aTolerance );
2754           aPredicate = tmpPred;
2755           break;
2756         }
2757
2758       default:
2759         continue;
2760     }
2761
2762     // Comparator
2763     if ( !aFunctor->_is_nil() && aPredicate->_is_nil() )
2764     {
2765       SMESH::Comparator_ptr aComparator = SMESH::Comparator::_nil();
2766
2767       if ( aCompare == SMESH::FT_LessThan )
2768         aComparator = aFilterMgr->CreateLessThan();
2769       else if ( aCompare == SMESH::FT_MoreThan )
2770         aComparator = aFilterMgr->CreateMoreThan();
2771       else if ( aCompare == SMESH::FT_EqualTo )
2772         aComparator = aFilterMgr->CreateEqualTo();
2773       else
2774         continue;
2775
2776       aComparator->SetNumFunctor( aFunctor );
2777       aComparator->SetMargin( aThreshold );
2778
2779       if ( aCompare == FT_EqualTo )
2780       {
2781         SMESH::EqualTo_var anEqualTo = SMESH::EqualTo::_narrow( aComparator );
2782         anEqualTo->SetTolerance( aTolerance );
2783       }
2784
2785       aPredicate = aComparator;
2786
2787       aFunctor->SetPrecision( aPrecision );
2788     }
2789
2790     // Logical not
2791     if ( aUnary == FT_LogicalNOT )
2792     {
2793       SMESH::LogicalNOT_ptr aNotPred = aFilterMgr->CreateLogicalNOT();
2794       aNotPred->SetPredicate( aPredicate );
2795       aPredicate = aNotPred;
2796     }
2797
2798     // logical op
2799     aPredicates.push_back( aPredicate );
2800     aBinaries.push_back( aBinary );
2801     TPythonDump()<<"aCriteria.append(aCriterion)";
2802
2803   } // end of for
2804   TPythonDump()<<this<<".SetCriteria(aCriteria)";
2805
2806   // CREATE ONE PREDICATE FROM PREVIOUSLY CREATED MAP
2807
2808   // combine all "AND" operations
2809
2810   std::list<SMESH::Predicate_ptr> aResList;
2811
2812   std::list<SMESH::Predicate_ptr>::iterator aPredIter;
2813   std::list<int>::iterator                  aBinaryIter;
2814
2815   SMESH::Predicate_ptr aPrevPredicate = SMESH::Predicate::_nil();
2816   int aPrevBinary = SMESH::FT_Undefined;
2817
2818   for ( aPredIter = aPredicates.begin(), aBinaryIter = aBinaries.begin();
2819         aPredIter != aPredicates.end() && aBinaryIter != aBinaries.end();
2820         ++aPredIter, ++aBinaryIter )
2821   {
2822     int aCurrBinary = *aBinaryIter;
2823
2824     SMESH::Predicate_ptr aCurrPred = SMESH::Predicate::_nil();
2825
2826     if ( aPrevBinary == SMESH::FT_LogicalAND )
2827     {
2828
2829       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalAND();
2830       aBinaryPred->SetPredicate1( aPrevPredicate );
2831       aBinaryPred->SetPredicate2( *aPredIter );
2832       aCurrPred = aBinaryPred;
2833     }
2834     else
2835       aCurrPred = *aPredIter;
2836
2837     if ( aCurrBinary != SMESH::FT_LogicalAND )
2838       aResList.push_back( aCurrPred );
2839
2840     aPrevPredicate = aCurrPred;
2841     aPrevBinary = aCurrBinary;
2842   }
2843
2844   // combine all "OR" operations
2845
2846   SMESH::Predicate_ptr aResPredicate = SMESH::Predicate::_nil();
2847
2848   if ( aResList.size() == 1 )
2849     aResPredicate = *aResList.begin();
2850   else if ( aResList.size() > 1 )
2851   {
2852     std::list<SMESH::Predicate_ptr>::iterator anIter = aResList.begin();
2853     aResPredicate = *anIter;
2854     anIter++;
2855     for ( ; anIter != aResList.end(); ++anIter )
2856     {
2857       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalOR();
2858       aBinaryPred->SetPredicate1( aResPredicate );
2859       aBinaryPred->SetPredicate2( *anIter );
2860       aResPredicate = aBinaryPred;
2861     }
2862   }
2863
2864   SetPredicate( aResPredicate );
2865
2866   return !aResPredicate->_is_nil();
2867 }
2868
2869 //=======================================================================
2870 // name    : Filter_i::GetPredicate_i
2871 // Purpose : Get implementation of predicate
2872 //=======================================================================
2873 Predicate_i* Filter_i::GetPredicate_i()
2874 {
2875   return myPredicate;
2876 }
2877
2878 //=======================================================================
2879 // name    : Filter_i::GetPredicate
2880 // Purpose : Get predicate
2881 //=======================================================================
2882 Predicate_ptr Filter_i::GetPredicate()
2883 {
2884   if ( myPredicate == 0 )
2885     return SMESH::Predicate::_nil();
2886   else
2887   {
2888     SMESH::Predicate_var anObj = myPredicate->_this();
2889     return anObj._retn();
2890   }
2891 }
2892
2893 /*
2894                             FILTER LIBRARY
2895 */
2896
2897 #define ATTR_TYPE          "type"
2898 #define ATTR_COMPARE       "compare"
2899 #define ATTR_THRESHOLD     "threshold"
2900 #define ATTR_UNARY         "unary"
2901 #define ATTR_BINARY        "binary"
2902 #define ATTR_THRESHOLD_STR "threshold_str"
2903 #define ATTR_TOLERANCE     "tolerance"
2904 #define ATTR_ELEMENT_TYPE  "ElementType"
2905
2906 //=======================================================================
2907 // name    : toString
2908 // Purpose : Convert bool to LDOMString
2909 //=======================================================================
2910 static inline LDOMString toString( CORBA::Boolean val )
2911 {
2912   return val ? "logical not" : "";
2913 }
2914
2915 //=======================================================================
2916 // name    : toBool
2917 // Purpose : Convert LDOMString to bool
2918 //=======================================================================
2919 static inline bool toBool( const LDOMString& theStr )
2920 {
2921   return theStr.equals( "logical not" );
2922 }
2923
2924 //=======================================================================
2925 // name    : toString
2926 // Purpose : Convert double to LDOMString
2927 //=======================================================================
2928 static inline LDOMString toString( CORBA::Double val )
2929 {
2930   char a[ 255 ];
2931   sprintf( a, "%e", val );
2932   return LDOMString( a );
2933 }
2934
2935 //=======================================================================
2936 // name    : toDouble
2937 // Purpose : Convert LDOMString to double
2938 //=======================================================================
2939 static inline double toDouble( const LDOMString& theStr )
2940 {
2941   return atof( theStr.GetString() );
2942 }
2943
2944 //=======================================================================
2945 // name    : toString
2946 // Purpose : Convert functor type to LDOMString
2947 //=======================================================================
2948 static inline LDOMString toString( CORBA::Long theType )
2949 {
2950   switch ( theType )
2951   {
2952     case FT_AspectRatio     : return "Aspect ratio";
2953     case FT_Warping         : return "Warping";
2954     case FT_MinimumAngle    : return "Minimum angle";
2955     case FT_Taper           : return "Taper";
2956     case FT_Skew            : return "Skew";
2957     case FT_Area            : return "Area";
2958     case FT_Volume3D        : return "Volume3D";
2959     case FT_MaxElementLength2D: return "Max element length 2D";
2960     case FT_MaxElementLength3D: return "Max element length 3D";
2961     case FT_BelongToGeom    : return "Belong to Geom";
2962     case FT_BelongToPlane   : return "Belong to Plane";
2963     case FT_BelongToCylinder: return "Belong to Cylinder";
2964     case FT_BelongToGenSurface: return "Belong to Generic Surface";
2965     case FT_LyingOnGeom     : return "Lying on Geom";
2966     case FT_BadOrientedVolume: return "Bad Oriented Volume";
2967     case FT_RangeOfIds      : return "Range of IDs";
2968     case FT_FreeBorders     : return "Free borders";
2969     case FT_FreeEdges       : return "Free edges";
2970     case FT_FreeFaces       : return "Free faces";
2971     case FT_FreeNodes       : return "Free nodes";
2972     case FT_MultiConnection : return "Borders at multi-connections";
2973     case FT_MultiConnection2D: return "Borders at multi-connections 2D";
2974     case FT_Length          : return "Length";
2975     case FT_Length2D        : return "Length 2D";
2976     case FT_LessThan        : return "Less than";
2977     case FT_MoreThan        : return "More than";
2978     case FT_EqualTo         : return "Equal to";
2979     case FT_LogicalNOT      : return "Not";
2980     case FT_LogicalAND      : return "And";
2981     case FT_LogicalOR       : return "Or";
2982     case FT_GroupColor      : return "Color of Group";
2983     case FT_LinearOrQuadratic : return "Linear or Quadratic";
2984     case FT_ElemGeomType    : return "Element geomtry type";
2985     case FT_Undefined       : return "";
2986     default                 : return "";
2987   }
2988 }
2989
2990 //=======================================================================
2991 // name    : toFunctorType
2992 // Purpose : Convert LDOMString to functor type
2993 //=======================================================================
2994 static inline SMESH::FunctorType toFunctorType( const LDOMString& theStr )
2995 {
2996   if      ( theStr.equals( "Aspect ratio"                 ) ) return FT_AspectRatio;
2997   else if ( theStr.equals( "Warping"                      ) ) return FT_Warping;
2998   else if ( theStr.equals( "Minimum angle"                ) ) return FT_MinimumAngle;
2999   else if ( theStr.equals( "Taper"                        ) ) return FT_Taper;
3000   else if ( theStr.equals( "Skew"                         ) ) return FT_Skew;
3001   else if ( theStr.equals( "Area"                         ) ) return FT_Area;
3002   else if ( theStr.equals( "Volume3D"                     ) ) return FT_Volume3D;
3003   else if ( theStr.equals( "Max element length 2D"        ) ) return FT_MaxElementLength2D;
3004   else if ( theStr.equals( "Max element length 3D"        ) ) return FT_MaxElementLength3D;
3005   else if ( theStr.equals( "Belong to Geom"               ) ) return FT_BelongToGeom;
3006   else if ( theStr.equals( "Belong to Plane"              ) ) return FT_BelongToPlane;
3007   else if ( theStr.equals( "Belong to Cylinder"           ) ) return FT_BelongToCylinder;
3008   else if ( theStr.equals( "Belong to Generic Surface"    ) ) return FT_BelongToGenSurface;
3009   else if ( theStr.equals( "Lying on Geom"                ) ) return FT_LyingOnGeom;
3010   else if ( theStr.equals( "Free borders"                 ) ) return FT_FreeBorders;
3011   else if ( theStr.equals( "Free edges"                   ) ) return FT_FreeEdges;
3012   else if ( theStr.equals( "Free faces"                   ) ) return FT_FreeFaces;
3013   else if ( theStr.equals( "Free nodes"                   ) ) return FT_FreeNodes;
3014   else if ( theStr.equals( "Borders at multi-connections" ) ) return FT_MultiConnection;
3015   //  else if ( theStr.equals( "Borders at multi-connections 2D" ) ) return FT_MultiConnection2D;
3016   else if ( theStr.equals( "Length"                       ) ) return FT_Length;
3017   //  else if ( theStr.equals( "Length2D"                     ) ) return FT_Length2D;
3018   else if ( theStr.equals( "Range of IDs"                 ) ) return FT_RangeOfIds;
3019   else if ( theStr.equals( "Bad Oriented Volume"          ) ) return FT_BadOrientedVolume;
3020   else if ( theStr.equals( "Less than"                    ) ) return FT_LessThan;
3021   else if ( theStr.equals( "More than"                    ) ) return FT_MoreThan;
3022   else if ( theStr.equals( "Equal to"                     ) ) return FT_EqualTo;
3023   else if ( theStr.equals( "Not"                          ) ) return FT_LogicalNOT;
3024   else if ( theStr.equals( "And"                          ) ) return FT_LogicalAND;
3025   else if ( theStr.equals( "Or"                           ) ) return FT_LogicalOR;
3026   else if ( theStr.equals( "Color of Group"               ) ) return FT_GroupColor;
3027   else if ( theStr.equals( "Linear or Quadratic"          ) ) return FT_LinearOrQuadratic;
3028   else if ( theStr.equals( "Element geomtry type"         ) ) return FT_ElemGeomType;
3029   else if ( theStr.equals( ""                             ) ) return FT_Undefined;
3030   else  return FT_Undefined;
3031 }
3032
3033 //=======================================================================
3034 // name    : toFunctorType
3035 // Purpose : Convert LDOMString to value of ElementType enumeration
3036 //=======================================================================
3037 static inline SMESH::ElementType toElementType( const LDOMString& theStr )
3038 {
3039   if      ( theStr.equals( "NODE"   ) ) return SMESH::NODE;
3040   else if ( theStr.equals( "EDGE"   ) ) return SMESH::EDGE;
3041   else if ( theStr.equals( "FACE"   ) ) return SMESH::FACE;
3042   else if ( theStr.equals( "VOLUME" ) ) return SMESH::VOLUME;
3043   else                                  return SMESH::ALL;
3044 }
3045
3046 //=======================================================================
3047 // name    : toString
3048 // Purpose : Convert ElementType to string
3049 //=======================================================================
3050 static inline LDOMString toString( const SMESH::ElementType theType )
3051 {
3052   switch ( theType )
3053   {
3054     case SMESH::NODE   : return "NODE";
3055     case SMESH::EDGE   : return "EDGE";
3056     case SMESH::FACE   : return "FACE";
3057     case SMESH::VOLUME : return "VOLUME";
3058     case SMESH::ALL    : return "ALL";
3059     default            : return "";
3060   }
3061 }
3062
3063 //=======================================================================
3064 // name    : findFilter
3065 // Purpose : Find filter in document
3066 //=======================================================================
3067 static LDOM_Element findFilter( const char* theFilterName,
3068                                 const LDOM_Document& theDoc,
3069                                 LDOM_Node* theParent = 0 )
3070 {
3071   LDOM_Element aRootElement = theDoc.getDocumentElement();
3072   if ( aRootElement.isNull() || !aRootElement.hasChildNodes() )
3073     return LDOM_Element();
3074
3075   for ( LDOM_Node aTypeNode = aRootElement.getFirstChild();
3076         !aTypeNode.isNull(); aTypeNode = aTypeNode.getNextSibling() )
3077   {
3078     for ( LDOM_Node aFilter = aTypeNode.getFirstChild();
3079           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
3080     {
3081       LDOM_Element* anElem = ( LDOM_Element* )&aFilter;
3082       if ( anElem->getTagName().equals( LDOMString( "filter" ) ) &&
3083            anElem->getAttribute( "name" ).equals( LDOMString( theFilterName ) ) )
3084       {
3085         if ( theParent != 0  )
3086           *theParent = aTypeNode;
3087         return (LDOM_Element&)aFilter;
3088       }
3089     }
3090   }
3091   return LDOM_Element();
3092 }
3093
3094 //=======================================================================
3095 // name    : getSectionName
3096 // Purpose : Get name of section of filters
3097 //=======================================================================
3098 static const char* getSectionName( const ElementType theType )
3099 {
3100   switch ( theType )
3101   {
3102     case SMESH::NODE   : return "Filters for nodes";
3103     case SMESH::EDGE   : return "Filters for edges";
3104     case SMESH::FACE   : return "Filters for faces";
3105     case SMESH::VOLUME : return "Filters for volumes";
3106     case SMESH::ALL    : return "Filters for elements";
3107     default            : return "";
3108   }
3109 }
3110
3111 //=======================================================================
3112 // name    : getSection
3113 // Purpose : Create section for filters corresponding to the entity type
3114 //=======================================================================
3115 static LDOM_Node getSection( const ElementType theType,
3116                              LDOM_Document&    theDoc,
3117                              const bool        toCreate = false )
3118 {
3119   LDOM_Element aRootElement = theDoc.getDocumentElement();
3120   if ( aRootElement.isNull() )
3121     return LDOM_Node();
3122
3123   // Find section
3124   bool anExist = false;
3125   const char* aSectionName = getSectionName( theType );
3126   if ( strcmp( aSectionName, "" ) == 0 )
3127     return LDOM_Node();
3128
3129   LDOM_NodeList aSections = theDoc.getElementsByTagName( "section" );
3130   LDOM_Node aNode;
3131   for ( int i = 0, n = aSections.getLength(); i < n; i++ )
3132   {
3133     aNode = aSections.item( i );
3134     LDOM_Element& anItem = ( LDOM_Element& )aNode;
3135     if ( anItem.getAttribute( "name" ).equals( LDOMString( aSectionName ) ) )
3136     {
3137       anExist = true;
3138       break;
3139     }
3140   }
3141
3142   // Create new section if necessary
3143   if ( !anExist )
3144   {
3145     if ( toCreate )
3146     {
3147       LDOM_Element aNewItem = theDoc.createElement( "section" );
3148       aNewItem.setAttribute( "name", aSectionName );
3149       aRootElement.appendChild( aNewItem );
3150       return aNewItem;
3151     }
3152     else
3153       return LDOM_Node();
3154   }
3155   return
3156     aNode;
3157 }
3158
3159 //=======================================================================
3160 // name    : createFilterItem
3161 // Purpose : Create filter item or LDOM document
3162 //=======================================================================
3163 static LDOM_Element createFilterItem( const char*       theName,
3164                                       SMESH::Filter_ptr theFilter,
3165                                       LDOM_Document&    theDoc )
3166 {
3167   // create new filter in document
3168   LDOM_Element aFilterItem = theDoc.createElement( "filter" );
3169   aFilterItem.setAttribute( "name", theName );
3170
3171   // save filter criterions
3172   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
3173
3174   if ( !theFilter->GetCriteria( aCriteria ) )
3175     return LDOM_Element();
3176
3177   for ( CORBA::ULong i = 0, n = aCriteria->length(); i < n; i++ )
3178   {
3179     LDOM_Element aCriterionItem = theDoc.createElement( "criterion" );
3180     
3181     aCriterionItem.setAttribute( ATTR_TYPE         , toString(  aCriteria[ i ].Type) );
3182     aCriterionItem.setAttribute( ATTR_COMPARE      , toString(  aCriteria[ i ].Compare ) );
3183     aCriterionItem.setAttribute( ATTR_THRESHOLD    , toString(  aCriteria[ i ].Threshold ) );
3184     aCriterionItem.setAttribute( ATTR_UNARY        , toString(  aCriteria[ i ].UnaryOp ) );
3185     aCriterionItem.setAttribute( ATTR_BINARY       , toString(  aCriteria[ i ].BinaryOp ) );
3186
3187     aCriterionItem.setAttribute( ATTR_THRESHOLD_STR, (const char*)aCriteria[ i ].ThresholdStr );
3188     aCriterionItem.setAttribute( ATTR_TOLERANCE    , toString( aCriteria[ i ].Tolerance ) );
3189     aCriterionItem.setAttribute( ATTR_ELEMENT_TYPE ,
3190       toString( (SMESH::ElementType)aCriteria[ i ].TypeOfElement ) );
3191
3192     aFilterItem.appendChild( aCriterionItem );
3193   }
3194
3195   return aFilterItem;
3196 }
3197
3198 //=======================================================================
3199 // name    : FilterLibrary_i::FilterLibrary_i
3200 // Purpose : Constructor
3201 //=======================================================================
3202 FilterLibrary_i::FilterLibrary_i( const char* theFileName )
3203 {
3204   myFileName = strdup( theFileName );
3205   SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
3206   myFilterMgr = aFilterMgr->_this();
3207
3208   LDOMParser aParser;
3209
3210   // Try to use existing library file
3211   bool anExists = false;
3212   if ( !aParser.parse( myFileName ) )
3213   {
3214     myDoc = aParser.getDocument();
3215     anExists = true;
3216   }
3217   // Create a new XML document if it doesn't exist
3218   else
3219     myDoc = LDOM_Document::createDocument( LDOMString() );
3220
3221   LDOM_Element aRootElement = myDoc.getDocumentElement();
3222   if ( aRootElement.isNull() )
3223   {
3224     // If the existing document is empty --> try to create a new one
3225     if ( anExists )
3226       myDoc = LDOM_Document::createDocument( LDOMString() );
3227   }
3228 }
3229
3230 //=======================================================================
3231 // name    : FilterLibrary_i::FilterLibrary_i
3232 // Purpose : Constructor
3233 //=======================================================================
3234 FilterLibrary_i::FilterLibrary_i()
3235 {
3236   myFileName = 0;
3237   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
3238   myFilterMgr = aFilter->_this();
3239
3240   myDoc = LDOM_Document::createDocument( LDOMString() );
3241 }
3242
3243 FilterLibrary_i::~FilterLibrary_i()
3244 {
3245   delete myFileName;
3246   //TPythonDump()<<this<<".Destroy()";
3247 }
3248
3249 //=======================================================================
3250 // name    : FilterLibrary_i::Copy
3251 // Purpose : Create filter and initialize it with values from library
3252 //=======================================================================
3253 Filter_ptr FilterLibrary_i::Copy( const char* theFilterName )
3254 {
3255   Filter_ptr aRes = Filter::_nil();
3256   LDOM_Node aFilter = findFilter( theFilterName, myDoc );
3257
3258   if ( aFilter.isNull() )
3259     return aRes;
3260
3261   std::list<SMESH::Filter::Criterion> aCriteria;
3262
3263   for ( LDOM_Node aCritNode = aFilter.getFirstChild();
3264         !aCritNode.isNull() ; aCritNode = aCritNode.getNextSibling() )
3265   {
3266     LDOM_Element* aCrit = (LDOM_Element*)&aCritNode;
3267
3268     const char* aTypeStr      = aCrit->getAttribute( ATTR_TYPE          ).GetString();
3269     const char* aCompareStr   = aCrit->getAttribute( ATTR_COMPARE       ).GetString();
3270     const char* aUnaryStr     = aCrit->getAttribute( ATTR_UNARY         ).GetString();
3271     const char* aBinaryStr    = aCrit->getAttribute( ATTR_BINARY        ).GetString();
3272     const char* anElemTypeStr = aCrit->getAttribute( ATTR_ELEMENT_TYPE  ).GetString();
3273
3274     SMESH::Filter::Criterion aCriterion = createCriterion();
3275
3276     aCriterion.Type          = toFunctorType( aTypeStr );
3277     aCriterion.Compare       = toFunctorType( aCompareStr );
3278     aCriterion.UnaryOp       = toFunctorType( aUnaryStr );
3279     aCriterion.BinaryOp      = toFunctorType( aBinaryStr );
3280
3281     aCriterion.TypeOfElement = toElementType( anElemTypeStr );
3282
3283     LDOMString str = aCrit->getAttribute( ATTR_THRESHOLD );
3284     int val = 0;
3285     aCriterion.Threshold = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3286       ? val : atof( str.GetString() );
3287
3288     str = aCrit->getAttribute( ATTR_TOLERANCE );
3289     aCriterion.Tolerance = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3290       ? val : atof( str.GetString() );
3291
3292     str = aCrit->getAttribute( ATTR_THRESHOLD_STR );
3293     if ( str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val ) )
3294     {
3295       char a[ 255 ];
3296       sprintf( a, "%d", val );
3297       aCriterion.ThresholdStr = strdup( a );
3298     }
3299     else
3300       aCriterion.ThresholdStr = str.GetString();
3301
3302     aCriteria.push_back( aCriterion );
3303   }
3304
3305   SMESH::Filter::Criteria_var aCriteriaVar = new SMESH::Filter::Criteria;
3306   aCriteriaVar->length( aCriteria.size() );
3307
3308   CORBA::ULong i = 0;
3309   std::list<SMESH::Filter::Criterion>::iterator anIter = aCriteria.begin();
3310
3311   for( ; anIter != aCriteria.end(); ++anIter )
3312     aCriteriaVar[ i++ ] = *anIter;
3313
3314   aRes = myFilterMgr->CreateFilter();
3315   aRes->SetCriteria( aCriteriaVar.inout() );
3316
3317   TPythonDump()<<this<<".Copy('"<<theFilterName<<"')";
3318
3319   return aRes;
3320 }
3321
3322 //=======================================================================
3323 // name    : FilterLibrary_i::SetFileName
3324 // Purpose : Set file name for library
3325 //=======================================================================
3326 void FilterLibrary_i::SetFileName( const char* theFileName )
3327 {
3328   delete myFileName;
3329   myFileName = strdup( theFileName );
3330   TPythonDump()<<this<<".SetFileName('"<<theFileName<<"')";
3331 }
3332
3333 //=======================================================================
3334 // name    : FilterLibrary_i::GetFileName
3335 // Purpose : Get file name of library
3336 //=======================================================================
3337 char* FilterLibrary_i::GetFileName()
3338 {
3339   return CORBA::string_dup( myFileName );
3340 }
3341
3342 //=======================================================================
3343 // name    : FilterLibrary_i::Add
3344 // Purpose : Add new filter to library
3345 //=======================================================================
3346 CORBA::Boolean FilterLibrary_i::Add( const char* theFilterName, Filter_ptr theFilter )
3347 {
3348   // if filter already in library or entry filter is null do nothing
3349   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3350   if ( !aFilterNode.isNull() || theFilter->_is_nil() )
3351     return false;
3352
3353   // get section corresponding to the filter type
3354   ElementType anEntType = theFilter->GetElementType();
3355
3356   LDOM_Node aSection = getSection( anEntType, myDoc, true );
3357   if ( aSection.isNull() )
3358     return false;
3359
3360   // create filter item
3361   LDOM_Element aFilterItem = createFilterItem( theFilterName, theFilter, myDoc );
3362   if ( aFilterItem.isNull() )
3363     return false;
3364   else
3365   {
3366     aSection.appendChild( aFilterItem );
3367     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
3368       TPythonDump()<<this<<".Add('"<<theFilterName<<"',"<<aFilter<<")";
3369     return true;
3370   }
3371 }
3372
3373 //=======================================================================
3374 // name    : FilterLibrary_i::Add
3375 // Purpose : Add new filter to library
3376 //=======================================================================
3377 CORBA::Boolean FilterLibrary_i::AddEmpty( const char* theFilterName, ElementType theType )
3378 {
3379   // if filter already in library or entry filter is null do nothing
3380   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3381   if ( !aFilterNode.isNull() )
3382     return false;
3383
3384   LDOM_Node aSection = getSection( theType, myDoc, true );
3385   if ( aSection.isNull() )
3386     return false;
3387
3388   // create filter item
3389   Filter_var aFilter = myFilterMgr->CreateFilter();
3390
3391   LDOM_Element aFilterItem = createFilterItem( theFilterName, aFilter, myDoc );
3392   if ( aFilterItem.isNull() )
3393     return false;
3394   else
3395   {
3396     aSection.appendChild( aFilterItem );
3397     TPythonDump()<<this<<".AddEmpty('"<<theFilterName<<"',"<<theType<<")";
3398     return true;
3399   }
3400 }
3401
3402 //=======================================================================
3403 // name    : FilterLibrary_i::Delete
3404 // Purpose : Delete filter from library
3405 //=======================================================================
3406 CORBA::Boolean FilterLibrary_i::Delete ( const char* theFilterName )
3407 {
3408   LDOM_Node aParentNode;
3409   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc, &aParentNode );
3410   if ( aFilterNode.isNull() || aParentNode.isNull() )
3411     return false;
3412
3413   aParentNode.removeChild( aFilterNode );
3414   TPythonDump()<<this<<".Delete('"<<theFilterName<<"')";
3415   return true;
3416 }
3417
3418 //=======================================================================
3419 // name      : FilterLibrary_i::Replace
3420 // Purpose   : Replace existing filter with entry filter.
3421 // IMPORTANT : If filter does not exist it is not created
3422 //=======================================================================
3423 CORBA::Boolean FilterLibrary_i::Replace( const char* theFilterName,
3424                                          const char* theNewName,
3425                                          Filter_ptr  theFilter )
3426 {
3427   LDOM_Element aFilterItem = findFilter( theFilterName, myDoc );
3428   if ( aFilterItem.isNull() || theFilter->_is_nil() )
3429     return false;
3430
3431   LDOM_Element aNewItem = createFilterItem( theNewName, theFilter, myDoc );
3432   if ( aNewItem.isNull() )
3433     return false;
3434   else
3435   {
3436     aFilterItem.ReplaceElement( aNewItem );
3437     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
3438       TPythonDump()<<this<<".Replace('"<<theFilterName<<"',"<<theNewName<<"',"<<aFilter<<")";
3439     return true;
3440   }
3441 }
3442
3443 //=======================================================================
3444 // name    : FilterLibrary_i::Save
3445 // Purpose : Save library on disk
3446 //=======================================================================
3447 CORBA::Boolean FilterLibrary_i::Save()
3448 {
3449   if ( myFileName == 0 || strlen( myFileName ) == 0 )
3450     return false;
3451
3452   FILE* aOutFile = fopen( myFileName, "wt" );
3453   if ( !aOutFile )
3454     return false;
3455
3456   LDOM_XmlWriter aWriter( aOutFile );
3457   aWriter.SetIndentation( 2 );
3458   aWriter << myDoc;
3459   fclose( aOutFile );
3460
3461   TPythonDump()<<this<<".Save()";
3462   return true;
3463 }
3464
3465 //=======================================================================
3466 // name    : FilterLibrary_i::SaveAs
3467 // Purpose : Save library on disk
3468 //=======================================================================
3469 CORBA::Boolean FilterLibrary_i::SaveAs( const char* aFileName )
3470 {
3471   myFileName = strdup ( aFileName );
3472   TPythonDump()<<this<<".SaveAs('"<<aFileName<<"')";
3473   return Save();
3474 }
3475
3476 //=======================================================================
3477 // name    : FilterLibrary_i::IsPresent
3478 // Purpose : Verify whether filter is in library
3479 //=======================================================================
3480 CORBA::Boolean FilterLibrary_i::IsPresent( const char* theFilterName )
3481 {
3482   return !findFilter( theFilterName, myDoc ).isNull();
3483 }
3484
3485 //=======================================================================
3486 // name    : FilterLibrary_i::NbFilters
3487 // Purpose : Return amount of filters in library
3488 //=======================================================================
3489 CORBA::Long FilterLibrary_i::NbFilters( ElementType theType )
3490 {
3491   string_array_var aNames = GetNames( theType );
3492   return aNames->length();
3493 }
3494
3495 //=======================================================================
3496 // name    : FilterLibrary_i::GetNames
3497 // Purpose : Get names of filters from library
3498 //=======================================================================
3499 string_array* FilterLibrary_i::GetNames( ElementType theType )
3500 {
3501   string_array_var anArray = new string_array;
3502   TColStd_SequenceOfHAsciiString aSeq;
3503
3504   LDOM_Node aSection = getSection( theType, myDoc, false );
3505
3506   if ( !aSection.isNull() )
3507   {
3508     for ( LDOM_Node aFilter = aSection.getFirstChild();
3509           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
3510     {
3511       LDOM_Element& anElem = ( LDOM_Element& )aFilter;
3512       aSeq.Append( new TCollection_HAsciiString(
3513          (Standard_CString)anElem.getAttribute( "name" ).GetString() ) );
3514     }
3515   }
3516
3517   anArray->length( aSeq.Length() );
3518   for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
3519     anArray[ i - 1 ] = CORBA::string_dup( aSeq( i )->ToCString() );
3520
3521   return anArray._retn();
3522 }
3523
3524 //=======================================================================
3525 // name    : FilterLibrary_i::GetAllNames
3526 // Purpose : Get names of filters from library
3527 //=======================================================================
3528 string_array* FilterLibrary_i::GetAllNames()
3529 {
3530   string_array_var aResArray = new string_array;
3531   for ( int type = SMESH::ALL; type <= SMESH::VOLUME; type++ )
3532   {
3533     SMESH::string_array_var aNames = GetNames( (SMESH::ElementType)type );
3534
3535     int aPrevLength = aResArray->length();
3536     aResArray->length( aPrevLength + aNames->length() );
3537     for ( int i = 0, n = aNames->length(); i < n; i++ )
3538       aResArray[ aPrevLength + i ] = aNames[ i ];
3539   }
3540
3541   return aResArray._retn();
3542 }