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