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