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