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