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