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