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