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