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