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