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