Salome HOME
#19765 EDF 21730 - long time to load med file file with huge amount of groups
[modules/smesh.git] / src / SMESH_I / SMESH_Filter_i.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 //  File   : SMESH_Filter_i.cxx
25 //  Author : Alexey Petrov, OCC
26 //  Module : SMESH
27
28 #include "SMESH_Filter_i.hxx"
29
30 #include "SMDS_ElemIterator.hxx"
31 #include "SMDS_Mesh.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMESHDS_GroupBase.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Gen_i.hxx"
37 #include "SMESH_Group_i.hxx"
38 #include "SMESH_PythonDump.hxx"
39
40 #include <SALOMEDS_wrap.hxx>
41 #include <GEOM_wrap.hxx>
42
43 #include <BRep_Tool.hxx>
44 #include <Geom_CylindricalSurface.hxx>
45 #include <Geom_Plane.hxx>
46 #include <LDOMParser.hxx>
47 #include <LDOMString.hxx>
48 #include <LDOM_Document.hxx>
49 #include <LDOM_Element.hxx>
50 #include <LDOM_Node.hxx>
51 #include <LDOM_XmlWriter.hxx>
52 #include <Precision.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TColStd_ListIteratorOfListOfReal.hxx>
55 #include <TColStd_ListOfInteger.hxx>
56 #include <TColStd_ListOfReal.hxx>
57 #include <TColStd_SequenceOfHAsciiString.hxx>
58 #include <TCollection_HAsciiString.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopoDS.hxx>
61 #include <TopoDS_Shape.hxx>
62
63 using namespace SMESH;
64 using namespace SMESH::Controls;
65
66
67 namespace SMESH
68 {
69   Predicate_i*
70   GetPredicate( Predicate_ptr thePredicate )
71   {
72     return DownCast<Predicate_i*>(thePredicate);
73   }
74 }
75
76 /*
77                             AUXILIARY METHODS
78 */
79
80 inline
81 const SMDS_Mesh*
82 MeshPtr2SMDSMesh( SMESH_Mesh_ptr theMesh )
83 {
84   SMESH_Mesh_i* anImplPtr = DownCast<SMESH_Mesh_i*>(theMesh);
85   return anImplPtr ? anImplPtr->GetImpl().GetMeshDS() : 0;
86 }
87
88 inline
89 SMESH::long_array*
90 toArray( const TColStd_ListOfInteger& aList )
91 {
92   SMESH::long_array_var anArray = new SMESH::long_array;
93   anArray->length( aList.Extent() );
94   TColStd_ListIteratorOfListOfInteger anIter( aList );
95   int i = 0;
96   for( ; anIter.More(); anIter.Next() )
97     anArray[ i++ ] = anIter.Value();
98
99   return anArray._retn();
100 }
101
102 inline
103 SMESH::double_array*
104 toArray( const TColStd_ListOfReal& aList )
105 {
106   SMESH::double_array_var anArray = new SMESH::double_array;
107   anArray->length( aList.Extent() );
108   TColStd_ListIteratorOfListOfReal anIter( aList );
109   int i = 0;
110   for( ; anIter.More(); anIter.Next() )
111     anArray[ i++ ] = anIter.Value();
112
113   return anArray._retn();
114 }
115
116 static SMESH::Filter::Criterion createCriterion()
117 {
118   SMESH::Filter::Criterion aCriterion;
119
120   aCriterion.Type          = FT_Undefined;
121   aCriterion.Compare       = FT_Undefined;
122   aCriterion.Threshold     = 0;
123   aCriterion.UnaryOp       = FT_Undefined;
124   aCriterion.BinaryOp      = FT_Undefined;
125   aCriterion.ThresholdStr  = "";
126   aCriterion.ThresholdID   = "";
127   aCriterion.Tolerance     = Precision::Confusion();
128   aCriterion.TypeOfElement = SMESH::ALL;
129   aCriterion.Precision     = -1;
130
131   return aCriterion;
132 }
133
134 static TopoDS_Shape getShapeByName( const char* theName )
135 {
136   if ( theName != 0 )
137   {
138     SMESH_Gen_i* aSMESHGen     = SMESH_Gen_i::GetSMESHGen();
139     SALOMEDS::Study::ListOfSObject_var aList = SMESH_Gen_i::getStudyServant()->FindObjectByName( theName, "GEOM" );
140     if ( aList->length() == 0 )
141       aList = SMESH_Gen_i::getStudyServant()->FindObjectByName( theName, "SHAPERSTUDY" );
142     if ( aList->length() > 0 )
143     {
144       CORBA::Object_var        anObj = aList[ 0 ]->GetObject();
145       GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( anObj );
146       TopoDS_Shape             shape = aSMESHGen->GeomObjectToShape( aGeomObj );
147       SALOME::UnRegister( aList ); // UnRegister() objects in aList
148       return shape;
149     }
150   }
151   return TopoDS_Shape();
152 }
153
154 static TopoDS_Shape getShapeByID (const char* theID)
155 {
156   if ( theID && strlen( theID ) > 0 ) {
157     SMESH_Gen_i*     aSMESHGen = SMESH_Gen_i::GetSMESHGen();
158     SALOMEDS::SObject_wrap aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID(theID);
159     if ( !aSObj->_is_nil() ) {
160       CORBA::Object_var          obj = aSObj->GetObject();
161       GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(obj);
162       return aSMESHGen->GeomObjectToShape( aGeomObj );
163     }
164   }
165   return TopoDS_Shape();
166 }
167
168 // static std::string getShapeNameByID (const char* theID)
169 // {
170 //   if ( theID && strlen( theID ) > 0 ) {
171 //     SALOMEDS::SObject_wrap aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID(theID);
172 //     if ( !aSObj->_is_nil() ) {
173 //       CORBA::String_var name = aSObj->GetName();
174 //       return name.in();
175 //     }
176 //   }
177 //   return "";
178 // }
179
180 /*
181                                 FUNCTORS
182 */
183
184 /*
185   Class       : Functor_i
186   Description : An abstract class for all functors
187 */
188 Functor_i::Functor_i():
189   SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
190 {
191   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method  
192   //PortableServer::ObjectId_var anObjectId =
193   //  SMESH_Gen_i::GetPOA()->activate_object( this );
194 }
195
196 Functor_i::~Functor_i()
197 {
198   //TPythonDump()<<this<<".UnRegister()";
199 }
200
201 void Functor_i::SetMesh( SMESH_Mesh_ptr theMesh )
202 {
203   myFunctorPtr->SetMesh( MeshPtr2SMDSMesh( theMesh ) );
204   TPythonDump()<<this<<".SetMesh("<<theMesh<<")";
205 }
206
207 ElementType Functor_i::GetElementType()
208 {
209   return ( ElementType )myFunctorPtr->GetType();
210 }
211
212
213 /*
214   Class       : NumericalFunctor_i
215   Description : Base class for numerical functors
216 */
217 CORBA::Double NumericalFunctor_i::GetValue( CORBA::Long theId )
218 {
219   return myNumericalFunctorPtr->GetValue( theId );
220 }
221
222 CORBA::Boolean NumericalFunctor_i::IsApplicable( CORBA::Long theElementId )
223 {
224   return myNumericalFunctorPtr->IsApplicable( theElementId );
225 }
226
227 SMESH::Histogram* NumericalFunctor_i::GetHistogram(CORBA::Short nbIntervals, CORBA::Boolean isLogarithmic)
228 {
229   std::vector<int> nbEvents;
230   std::vector<double> funValues;
231   std::vector<int> elements;
232   myNumericalFunctorPtr->GetHistogram(nbIntervals,nbEvents,funValues,elements,0,isLogarithmic);
233
234   SMESH::Histogram_var histogram = new SMESH::Histogram;
235
236   nbIntervals = CORBA::Short( Min( int( nbEvents.size()),
237                                    int( funValues.size() - 1 )));
238   if ( nbIntervals > 0 )
239   {
240     histogram->length( nbIntervals );
241     for ( int i = 0; i < nbIntervals; ++i )
242     {
243       HistogramRectangle& rect = histogram[i];
244       rect.nbEvents = nbEvents[i];
245       rect.min = funValues[i];
246       rect.max = funValues[i+1];
247     }
248   }
249   return histogram._retn();
250 }
251
252 SMESH::Histogram* NumericalFunctor_i::GetLocalHistogram(CORBA::Short              nbIntervals,
253                                                         CORBA::Boolean            isLogarithmic,
254                                                         SMESH::SMESH_IDSource_ptr object)
255 {
256   SMESH::Histogram_var histogram = new SMESH::Histogram;
257
258   std::vector<int>    nbEvents;
259   std::vector<double> funValues;
260   std::vector<int>    elements;
261
262   SMDS_ElemIteratorPtr elemIt;
263   if ( SMESH::DownCast< SMESH_GroupOnFilter_i* >( object ) ||
264        SMESH::DownCast< SMESH::Filter_i* >( object ))
265   {
266     elemIt = SMESH_Mesh_i::GetElements( object, GetElementType() );
267     if ( !elemIt ) return histogram._retn();
268   }
269   else
270   {
271     SMESH::SMESH_Mesh_var        mesh = object->GetMesh();
272     SMESH::long_array_var  objNbElems = object->GetNbElementsByType();
273     SMESH::long_array_var meshNbElems = mesh->  GetNbElementsByType();
274     if ( meshNbElems[ GetElementType() ] !=
275          objNbElems [ GetElementType() ] )
276     {
277       elements.reserve( objNbElems[ GetElementType() ]);
278       elemIt = SMESH_Mesh_i::GetElements( object, GetElementType() );
279     }
280   }
281   if ( elemIt )
282   {
283     while ( elemIt->more() )
284       elements.push_back( elemIt->next()->GetID() );
285     if ( elements.empty() ) return histogram._retn();
286   }
287
288   myNumericalFunctorPtr->GetHistogram(nbIntervals,nbEvents,funValues,elements,0,isLogarithmic);
289
290   nbIntervals = CORBA::Short( Min( int( nbEvents.size()),
291                                    int( funValues.size() - 1 )));
292   if ( nbIntervals > 0 )
293   {
294     histogram->length( nbIntervals );
295     for ( int i = 0; i < nbIntervals; ++i )
296     {
297       HistogramRectangle& rect = histogram[i];
298       rect.nbEvents = nbEvents[i];
299       rect.min = funValues[i];
300       rect.max = funValues[i+1];
301     }
302   }
303   return histogram._retn();
304 }
305
306 void NumericalFunctor_i::SetPrecision( CORBA::Long thePrecision )
307 {
308   myNumericalFunctorPtr->SetPrecision( thePrecision );
309   TPythonDump()<<this<<".SetPrecision("<<thePrecision<<")";
310 }
311
312 CORBA::Long NumericalFunctor_i::GetPrecision()
313 {
314  return myNumericalFunctorPtr->GetPrecision();
315 }
316
317 Controls::NumericalFunctorPtr NumericalFunctor_i::GetNumericalFunctor()
318 {
319   return myNumericalFunctorPtr;
320 }
321
322
323 /*
324   Class       : SMESH_MinimumAngle
325   Description : Functor for calculation of minimum angle
326 */
327 MinimumAngle_i::MinimumAngle_i()
328 {
329   myNumericalFunctorPtr.reset( new Controls::MinimumAngle() );
330   myFunctorPtr = myNumericalFunctorPtr;
331 }
332
333 FunctorType MinimumAngle_i::GetFunctorType()
334 {
335   return SMESH::FT_MinimumAngle;
336 }
337
338
339 /*
340   Class       : AspectRatio
341   Description : Functor for calculating aspect ratio
342 */
343 AspectRatio_i::AspectRatio_i()
344 {
345   myNumericalFunctorPtr.reset( new Controls::AspectRatio() );
346   myFunctorPtr = myNumericalFunctorPtr;
347 }
348
349 FunctorType AspectRatio_i::GetFunctorType()
350 {
351   return SMESH::FT_AspectRatio;
352 }
353
354
355 /*
356   Class       : AspectRatio3D
357   Description : Functor for calculating aspect ratio 3D
358 */
359 AspectRatio3D_i::AspectRatio3D_i()
360 {
361   myNumericalFunctorPtr.reset( new Controls::AspectRatio3D() );
362   myFunctorPtr = myNumericalFunctorPtr;
363 }
364
365 FunctorType AspectRatio3D_i::GetFunctorType()
366 {
367   return SMESH::FT_AspectRatio3D;
368 }
369
370
371 /*
372   Class       : Warping_i
373   Description : Functor for calculating warping
374 */
375 Warping_i::Warping_i()
376 {
377   myNumericalFunctorPtr.reset( new Controls::Warping() );
378   myFunctorPtr = myNumericalFunctorPtr;
379 }
380
381 FunctorType Warping_i::GetFunctorType()
382 {
383   return SMESH::FT_Warping;
384 }
385
386
387 /*
388   Class       : Taper_i
389   Description : Functor for calculating taper
390 */
391 Taper_i::Taper_i()
392 {
393   myNumericalFunctorPtr.reset( new Controls::Taper() );
394   myFunctorPtr = myNumericalFunctorPtr;
395 }
396
397 FunctorType Taper_i::GetFunctorType()
398 {
399   return SMESH::FT_Taper;
400 }
401
402 /*
403   Class       : Skew_i
404   Description : Functor for calculating skew in degrees
405 */
406 Skew_i::Skew_i()
407 {
408   myNumericalFunctorPtr.reset( new Controls::Skew() );
409   myFunctorPtr = myNumericalFunctorPtr;
410 }
411
412 FunctorType Skew_i::GetFunctorType()
413 {
414   return SMESH::FT_Skew;
415 }
416
417 /*
418   Class       : Area_i
419   Description : Functor for calculating area
420 */
421 Area_i::Area_i()
422 {
423   myNumericalFunctorPtr.reset( new Controls::Area() );
424   myFunctorPtr = myNumericalFunctorPtr;
425 }
426
427 FunctorType Area_i::GetFunctorType()
428 {
429   return SMESH::FT_Area;
430 }
431
432 /*
433   Class       : Volume3D_i
434   Description : Functor for calculating volume of 3D element
435 */
436 Volume3D_i::Volume3D_i()
437 {
438   myNumericalFunctorPtr.reset( new Controls::Volume() );
439   myFunctorPtr = myNumericalFunctorPtr;
440 }
441
442 FunctorType Volume3D_i::GetFunctorType()
443 {
444   return SMESH::FT_Volume3D;
445 }
446
447 /*
448   Class       : MaxElementLength2D_i
449   Description : Functor for calculating maximum length of 2D element
450 */
451 MaxElementLength2D_i::MaxElementLength2D_i()
452 {
453   myNumericalFunctorPtr.reset( new Controls::MaxElementLength2D() );
454   myFunctorPtr = myNumericalFunctorPtr;
455 }
456
457 FunctorType MaxElementLength2D_i::GetFunctorType()
458 {
459   return SMESH::FT_MaxElementLength2D;
460 }
461
462 /*
463   Class       : MaxElementLength3D_i
464   Description : Functor for calculating maximum length of 3D element
465 */
466 MaxElementLength3D_i::MaxElementLength3D_i()
467 {
468   myNumericalFunctorPtr.reset( new Controls::MaxElementLength3D() );
469   myFunctorPtr = myNumericalFunctorPtr;
470 }
471
472 FunctorType MaxElementLength3D_i::GetFunctorType()
473 {
474   return SMESH::FT_MaxElementLength3D;
475 }
476
477 /*
478   Class       : Length_i
479   Description : Functor for calculating length off edge
480 */
481 Length_i::Length_i()
482 {
483   myNumericalFunctorPtr.reset( new Controls::Length() );
484   myFunctorPtr = myNumericalFunctorPtr;
485 }
486
487 FunctorType Length_i::GetFunctorType()
488 {
489   return SMESH::FT_Length;
490 }
491
492 /*
493   Class       : Length2D_i
494   Description : Functor for calculating length of edge
495 */
496 Length2D_i::Length2D_i()
497 {
498   myNumericalFunctorPtr.reset( new Controls::Length2D() );
499   myFunctorPtr = myNumericalFunctorPtr;
500 }
501
502 FunctorType Length2D_i::GetFunctorType()
503 {
504   return SMESH::FT_Length2D;
505 }
506
507 SMESH::Length2D::Values* Length2D_i::GetValues()
508 {
509   SMESH::Controls::Length2D::TValues aValues;
510   (dynamic_cast<SMESH::Controls::Length2D*>(myFunctorPtr.get()))->GetValues( aValues );
511
512   long i = 0, iEnd = aValues.size();
513
514   SMESH::Length2D::Values_var aResult = new SMESH::Length2D::Values(iEnd);
515   aResult->length(iEnd);
516
517   SMESH::Controls::Length2D::TValues::const_iterator anIter;
518   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
519   {
520     const SMESH::Controls::Length2D::Value&  aVal = *anIter;
521     SMESH::Length2D::Value &aValue = aResult[ i ];
522
523     aValue.myLength = aVal.myLength;
524     aValue.myPnt1 = aVal.myPntId[ 0 ];
525     aValue.myPnt2 = aVal.myPntId[ 1 ];
526   }
527
528   return aResult._retn();
529 }
530
531
532 /*
533   Class       : Length3D_i
534   Description : Functor for calculating length of edge
535 */
536 Length3D_i::Length3D_i()
537 {
538   myNumericalFunctorPtr.reset( new Controls::Length3D() );
539   myFunctorPtr = myNumericalFunctorPtr;
540 }
541
542 FunctorType Length3D_i::GetFunctorType()
543 {
544   return SMESH::FT_Length3D;
545 }
546
547 // SMESH::Length3D::Values* Length3D_i::GetValues()
548 // {
549 //   SMESH::Controls::Length3D::TValues aValues;
550 //   (dynamic_cast<SMESH::Controls::Length3D*>(myFunctorPtr.get()))->GetValues( aValues );
551
552 //   long i = 0, iEnd = aValues.size();
553
554 //   SMESH::Length3D::Values_var aResult = new SMESH::Length3D::Values(iEnd);
555 //   aResult->length(iEnd);
556
557 //   SMESH::Controls::Length3D::TValues::const_iterator anIter;
558 //   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
559 //   {
560 //     const SMESH::Controls::Length3D::Value&  aVal = *anIter;
561 //     SMESH::Length3D::Value &aValue = aResult[ i ];
562
563 //     aValue.myLength = aVal.myLength;
564 //     aValue.myPnt1 = aVal.myPntId[ 0 ];
565 //     aValue.myPnt2 = aVal.myPntId[ 1 ];
566 //   }
567
568 //   return aResult._retn();
569 // }
570
571 /*
572   Class       : Deflection2D_i
573   Description : Functor for calculating distance between a face and geometry
574 */
575 Deflection2D_i::Deflection2D_i()
576 {
577   myNumericalFunctorPtr.reset( new Controls::Deflection2D() );
578   myFunctorPtr = myNumericalFunctorPtr;
579 }
580
581 FunctorType Deflection2D_i::GetFunctorType()
582 {
583   return SMESH::FT_Deflection2D;
584 }
585
586 /*
587   Class       : MultiConnection_i
588   Description : Functor for calculating number of faces conneted to the edge
589 */
590 MultiConnection_i::MultiConnection_i()
591 {
592   myNumericalFunctorPtr.reset( new Controls::MultiConnection() );
593   myFunctorPtr = myNumericalFunctorPtr;
594 }
595
596 FunctorType MultiConnection_i::GetFunctorType()
597 {
598   return SMESH::FT_MultiConnection;
599 }
600
601 /*
602   Class       : BallDiameter_i
603   Description : Functor returning diameter of a ball element
604 */
605 BallDiameter_i::BallDiameter_i()
606 {
607   myNumericalFunctorPtr.reset( new Controls::BallDiameter() );
608   myFunctorPtr = myNumericalFunctorPtr;
609 }
610
611 FunctorType BallDiameter_i::GetFunctorType()
612 {
613   return SMESH::FT_BallDiameter;
614 }
615
616 /*
617   Class       : NodeConnectivityNumber_i
618   Description : Functor returning diameter of a ball element
619 */
620 NodeConnectivityNumber_i::NodeConnectivityNumber_i()
621 {
622   myNumericalFunctorPtr.reset( new Controls::NodeConnectivityNumber() );
623   myFunctorPtr = myNumericalFunctorPtr;
624 }
625
626 FunctorType NodeConnectivityNumber_i::GetFunctorType()
627 {
628   return SMESH::FT_NodeConnectivityNumber;
629 }
630
631 /*
632   Class       : MultiConnection2D_i
633   Description : Functor for calculating number of faces conneted to the edge
634 */
635 MultiConnection2D_i::MultiConnection2D_i()
636 {
637   myNumericalFunctorPtr.reset( new Controls::MultiConnection2D() );
638   myFunctorPtr = myNumericalFunctorPtr;
639 }
640
641 FunctorType MultiConnection2D_i::GetFunctorType()
642 {
643   return SMESH::FT_MultiConnection2D;
644 }
645
646 SMESH::MultiConnection2D::Values* MultiConnection2D_i::GetValues()
647 {
648   SMESH::Controls::MultiConnection2D::MValues aValues;
649   (dynamic_cast<SMESH::Controls::MultiConnection2D*>(myFunctorPtr.get()))->GetValues( aValues );
650   
651   long i = 0, iEnd = aValues.size();
652
653   SMESH::MultiConnection2D::Values_var aResult = new SMESH::MultiConnection2D::Values(iEnd);
654   aResult->length(iEnd);
655
656   SMESH::Controls::MultiConnection2D::MValues::const_iterator anIter;
657   for ( anIter = aValues.begin() ; anIter != aValues.end(); anIter++, i++ )
658   {
659     const SMESH::Controls::MultiConnection2D::Value&  aVal = (*anIter).first;
660     SMESH::MultiConnection2D::Value &aValue = aResult[ i ];
661
662     aValue.myPnt1 = aVal.myPntId[ 0 ];
663     aValue.myPnt2 = aVal.myPntId[ 1 ];
664     aValue.myNbConnects = (*anIter).second;
665   }
666
667   return aResult._retn();
668 }
669
670 /*
671                             PREDICATES
672 */
673
674
675 /*
676   Class       : Predicate_i
677   Description : Base class for all predicates
678 */
679 CORBA::Boolean Predicate_i::IsSatisfy( CORBA::Long theId )
680 {
681   return myPredicatePtr->IsSatisfy( theId );
682 }
683
684 CORBA::Long Predicate_i::NbSatisfying( SMESH::SMESH_IDSource_ptr obj )
685 {
686   SMESH::SMESH_Mesh_var meshVar = obj->GetMesh();
687   const SMDS_Mesh*       meshDS = MeshPtr2SMDSMesh( meshVar );
688   if ( !meshDS )
689     return 0;
690   myPredicatePtr->SetMesh( meshDS );
691
692   SMDSAbs_ElementType elemType = SMDSAbs_ElementType( GetElementType() );
693
694   int nb = 0;
695   SMDS_ElemIteratorPtr elemIt =
696     SMESH::DownCast<SMESH_Mesh_i*>( meshVar )->GetElements( obj, GetElementType() );
697   if ( elemIt )
698     while ( elemIt->more() )
699     {
700       const SMDS_MeshElement* e = elemIt->next();
701       if ( e && e->GetType() == elemType )
702         nb += myPredicatePtr->IsSatisfy( e->GetID() );
703     }
704   return nb;
705 }
706
707 Controls::PredicatePtr Predicate_i::GetPredicate()
708 {
709   return myPredicatePtr;
710 }
711
712 /*
713   Class       : BadOrientedVolume_i
714   Description : Verify whether a mesh volume is incorrectly oriented from
715                 the point of view of MED convention
716 */
717 BadOrientedVolume_i::BadOrientedVolume_i()
718 {
719   Controls::PredicatePtr control( new Controls::BadOrientedVolume() );
720   myFunctorPtr = myPredicatePtr = control;
721 };
722
723 FunctorType BadOrientedVolume_i::GetFunctorType()
724 {
725   return SMESH::FT_BadOrientedVolume;
726 }
727
728 /*
729   Class       : BareBorderVolume_i
730   Description : Verify whether a mesh volume has a free facet without a face on it
731 */
732 BareBorderVolume_i::BareBorderVolume_i()
733 {
734   Controls::PredicatePtr control( new Controls::BareBorderVolume() );
735   myFunctorPtr = myPredicatePtr = control;
736 };
737
738 FunctorType BareBorderVolume_i::GetFunctorType()
739 {
740   return SMESH::FT_BareBorderVolume;
741 }
742
743 /*
744   Class       : BareBorderFace_i
745   Description : Verify whether a mesh face has a free border without an edge on it
746 */
747 BareBorderFace_i::BareBorderFace_i()
748 {
749   Controls::PredicatePtr control( new Controls::BareBorderFace() );
750   myFunctorPtr = myPredicatePtr = control;
751 };
752
753 FunctorType BareBorderFace_i::GetFunctorType()
754 {
755   return SMESH::FT_BareBorderFace;
756 }
757
758 /*
759   Class       : OverConstrainedVolume_i
760   Description : Verify whether a mesh volume has only one facet shared with other volumes
761 */
762 OverConstrainedVolume_i::OverConstrainedVolume_i()
763 {
764   Controls::PredicatePtr control( new Controls::OverConstrainedVolume() );
765   myFunctorPtr = myPredicatePtr = control;
766 };
767
768 FunctorType OverConstrainedVolume_i::GetFunctorType()
769 {
770   return SMESH::FT_OverConstrainedVolume;
771 }
772
773 /*
774   Class       : OverConstrainedFace_i
775   Description : Verify whether a mesh face has only one border shared with other faces
776 */
777 OverConstrainedFace_i::OverConstrainedFace_i()
778 {
779   Controls::PredicatePtr control( new Controls::OverConstrainedFace() );
780   myFunctorPtr = myPredicatePtr = control;
781 };
782
783 FunctorType OverConstrainedFace_i::GetFunctorType()
784 {
785   return SMESH::FT_OverConstrainedFace;
786 }
787
788 /*
789   Class       : BelongToMeshGroup_i
790   Description : Verify whether a mesh element is included into a mesh group
791 */
792 BelongToMeshGroup_i::BelongToMeshGroup_i()
793 {
794   myBelongToMeshGroup = Controls::BelongToMeshGroupPtr( new Controls::BelongToMeshGroup() );
795   myFunctorPtr = myPredicatePtr = myBelongToMeshGroup;
796 }
797
798 BelongToMeshGroup_i::~BelongToMeshGroup_i()
799 {
800   SetGroup( SMESH::SMESH_GroupBase::_nil() );
801 }
802
803 void BelongToMeshGroup_i::SetGroup( SMESH::SMESH_GroupBase_ptr theGroup )
804 {
805   if ( myGroup->_is_equivalent( theGroup ))
806     return;
807
808   if ( ! myGroup->_is_nil() )
809     myGroup->UnRegister();
810
811   myGroup = SMESH_GroupBase::_duplicate( theGroup );
812
813   myBelongToMeshGroup->SetGroup( 0 );
814   if ( SMESH_GroupBase_i* gr_i = SMESH::DownCast< SMESH_GroupBase_i* >( myGroup ))
815   {
816     myBelongToMeshGroup->SetGroup( gr_i->GetGroupDS() );
817     myGroup->Register();
818   }
819 }
820
821 void BelongToMeshGroup_i::SetGroupID( const char* theID ) // IOR or StoreName
822 {
823   myID = theID;
824   if ( strncmp( "IOR:", myID.c_str(), 4 ) == 0 ) // transient mode, no GUI
825   {
826     CORBA::Object_var obj = SMESH_Gen_i::GetORB()->string_to_object( myID.c_str() );
827     SetGroup( SMESH::SMESH_GroupBase::_narrow( obj ));
828   }
829   else if ( strncmp( "0:", myID.c_str(), 2 ) == 0 ) // transient mode + GUI
830   {
831     SALOMEDS::SObject_wrap aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( myID.c_str() );
832     if ( !aSObj->_is_nil() ) {
833       CORBA::Object_var obj = aSObj->GetObject();
834       SetGroup( SMESH::SMESH_GroupBase::_narrow( obj ));
835     }
836   }
837   else if ( !myID.empty() ) // persistent mode
838   {
839     myBelongToMeshGroup->SetStoreName( myID );
840   }
841 }
842
843 std::string BelongToMeshGroup_i::GetGroupID()
844 {
845   if ( myGroup->_is_nil() )
846     SMESH::SMESH_GroupBase_var( GetGroup() );
847
848   if ( !myGroup->_is_nil() )
849     myID = SMESH_Gen_i::GetORB()->object_to_string( myGroup );
850
851   return myID;
852 }
853
854 SMESH::SMESH_GroupBase_ptr BelongToMeshGroup_i::GetGroup()
855 {
856   if ( myGroup->_is_nil() && myBelongToMeshGroup->GetGroup() )
857   {
858     // search for a group in a current study
859     SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen();
860     if ( StudyContext*  sc = aSMESHGen->GetStudyContext() )
861     {
862       int id = 1;
863       std::string ior;
864       while (true)
865       {
866         ior = sc->getIORbyId( id++ );
867         if ( ior.empty() ) break;
868         CORBA::Object_var obj = aSMESHGen->GetORB()->string_to_object( ior.c_str() );
869         if ( SMESH_GroupBase_i* g_i = SMESH::DownCast<SMESH_GroupBase_i*>( obj ))
870           if ( g_i->GetGroupDS() == myBelongToMeshGroup->GetGroup() )
871           {
872             SetGroup( g_i->_this() );
873             break;
874           }
875       }
876     }
877   }
878   return SMESH::SMESH_GroupBase::_duplicate( myGroup );
879 }
880
881 FunctorType BelongToMeshGroup_i::GetFunctorType()
882 {
883   return SMESH::FT_BelongToMeshGroup;
884 }
885
886 /*
887   Class       : BelongToGeom_i
888   Description : Predicate for selection on geometrical support
889 */
890 BelongToGeom_i::BelongToGeom_i()
891 {
892   myBelongToGeomPtr.reset( new Controls::BelongToGeom() );
893   myFunctorPtr = myPredicatePtr = myBelongToGeomPtr;
894   myShapeName = 0;
895   myShapeID   = 0;
896 }
897
898 BelongToGeom_i::~BelongToGeom_i()
899 {
900   CORBA::string_free( myShapeName );
901   CORBA::string_free( myShapeID );
902 }
903
904 void BelongToGeom_i::SetGeom( GEOM::GEOM_Object_ptr theGeom )
905 {
906   if ( theGeom->_is_nil() )
907     return;
908   TopoDS_Shape aLocShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theGeom );
909   myBelongToGeomPtr->SetGeom( aLocShape );
910   TPythonDump()<<this<<".SetGeom("<<theGeom<<")";
911 }
912
913 void BelongToGeom_i::SetGeom( const TopoDS_Shape& theShape )
914 {
915   myBelongToGeomPtr->SetGeom( theShape );
916 }
917
918 void BelongToGeom_i::SetElementType(ElementType theType)
919 {
920   myBelongToGeomPtr->SetType(SMDSAbs_ElementType(theType));
921   TPythonDump()<<this<<".SetElementType("<<theType<<")";
922 }
923
924 FunctorType BelongToGeom_i::GetFunctorType()
925 {
926   return SMESH::FT_BelongToGeom;
927 }
928
929 void BelongToGeom_i::SetShapeName( const char* theName )
930 {
931   CORBA::string_free( myShapeName );
932   myShapeName = CORBA::string_dup( theName );
933   myBelongToGeomPtr->SetGeom( getShapeByName( myShapeName ) );
934   TPythonDump()<<this<<".SetShapeName('"<<theName<<"')";
935 }
936
937 void BelongToGeom_i::SetShape( const char* theID, const char* theName )
938 {
939   CORBA::string_free( myShapeName );
940   CORBA::string_free( myShapeID );
941   myShapeName = CORBA::string_dup( theName );
942   myShapeID   = CORBA::string_dup( theID );
943   bool hasName = ( theName && theName[0] );
944   bool hasID   = ( theID   && theID[0] );
945
946   TopoDS_Shape S;
947   if ( hasName && hasID )
948   {
949     S = getShapeByID( myShapeID );
950     if ( S.IsNull() )
951       S = getShapeByName( myShapeName );
952   }
953   else
954   {
955     S = hasID ? getShapeByID( myShapeID ) : getShapeByName( myShapeName );
956   }
957   myBelongToGeomPtr->SetGeom( S );
958 }
959
960 char* BelongToGeom_i::GetShapeName()
961 {
962   return CORBA::string_dup( myShapeName );
963 }
964
965 char* BelongToGeom_i::GetShapeID()
966 {
967   return CORBA::string_dup( myShapeID );
968 }
969
970 void BelongToGeom_i::SetTolerance( CORBA::Double theToler )
971 {
972   myBelongToGeomPtr->SetTolerance( theToler );
973   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
974 }
975
976 CORBA::Double BelongToGeom_i::GetTolerance()
977 {
978   return myBelongToGeomPtr->GetTolerance();
979 }
980
981 /*
982   Class       : BelongToSurface_i
983   Description : Predicate for selection on geometrical support
984 */
985 BelongToSurface_i::BelongToSurface_i( const Handle(Standard_Type)& theSurfaceType )
986 {
987   myElementsOnSurfacePtr.reset( new Controls::ElementsOnSurface() );
988   myFunctorPtr = myPredicatePtr = myElementsOnSurfacePtr;
989   myShapeName = 0;
990   myShapeID   = 0;
991   mySurfaceType = theSurfaceType;
992 }
993
994 BelongToSurface_i::~BelongToSurface_i()
995 {
996   CORBA::string_free( myShapeName );
997   CORBA::string_free( myShapeID );
998 }
999
1000 void BelongToSurface_i::SetSurface( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1001 {
1002   if ( theGeom->_is_nil() )
1003     return;
1004   TopoDS_Shape aLocShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theGeom );
1005
1006   if ( aLocShape.ShapeType() == TopAbs_FACE )
1007   {
1008     Handle(Geom_Surface) aSurf = BRep_Tool::Surface( TopoDS::Face( aLocShape ) );
1009     if ( !aSurf.IsNull() && aSurf->DynamicType() == mySurfaceType )
1010     {
1011       myElementsOnSurfacePtr->SetSurface( aLocShape, (SMDSAbs_ElementType)theType );
1012       return;
1013     }
1014   }
1015
1016   myElementsOnSurfacePtr->SetSurface( TopoDS_Shape(), (SMDSAbs_ElementType)theType );
1017 }
1018
1019 void BelongToSurface_i::SetShapeName( const char* theName, ElementType theType )
1020 {
1021   CORBA::string_free( myShapeName );
1022   myShapeName = CORBA::string_dup( theName );
1023   myElementsOnSurfacePtr->SetSurface( getShapeByName( myShapeName ), (SMDSAbs_ElementType)theType );
1024   TPythonDump()<<this<<".SetShapeName('"<<theName<<"',"<<theType<<")";
1025 }
1026
1027 void BelongToSurface_i::SetShape( const char* theID,  const char* theName, ElementType theType )
1028 {
1029   CORBA::string_free( myShapeName );
1030   CORBA::string_free( myShapeID );
1031   myShapeName = CORBA::string_dup( theName );
1032   myShapeID   = CORBA::string_dup( theID );
1033   bool hasName = ( theName && theName[0] );
1034   bool hasID   = ( theID   && theID[0] );
1035
1036   TopoDS_Shape S;
1037   if ( hasName && hasID )
1038   {
1039     S = getShapeByID( myShapeID );
1040     if ( S.IsNull() )
1041       S = getShapeByName( myShapeName );
1042   }
1043   else
1044   {
1045     S = hasID ? getShapeByID( myShapeID ) : getShapeByName( myShapeName );
1046   }
1047   myElementsOnSurfacePtr->SetSurface( S, (SMDSAbs_ElementType)theType );
1048 }
1049
1050 char* BelongToSurface_i::GetShapeName()
1051 {
1052   return CORBA::string_dup( myShapeName );
1053 }
1054
1055 char* BelongToSurface_i::GetShapeID()
1056 {
1057   return CORBA::string_dup( myShapeID );
1058 }
1059
1060 void BelongToSurface_i::SetTolerance( CORBA::Double theToler )
1061 {
1062   myElementsOnSurfacePtr->SetTolerance( theToler );
1063   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1064 }
1065
1066 CORBA::Double BelongToSurface_i::GetTolerance()
1067 {
1068   return myElementsOnSurfacePtr->GetTolerance();
1069 }
1070
1071 void BelongToSurface_i::SetUseBoundaries( CORBA::Boolean theUseBndRestrictions )
1072 {
1073   myElementsOnSurfacePtr->SetUseBoundaries( theUseBndRestrictions );
1074   TPythonDump()<<this<<".SetUseBoundaries( " << theUseBndRestrictions << " )";
1075 }
1076
1077 CORBA::Boolean BelongToSurface_i::GetUseBoundaries()
1078 {
1079   return myElementsOnSurfacePtr->GetUseBoundaries();
1080 }
1081
1082
1083 /*
1084   Class       : BelongToPlane_i
1085   Description : Verify whether mesh element lie in pointed Geom planar object
1086 */
1087
1088 BelongToPlane_i::BelongToPlane_i()
1089 : BelongToSurface_i( STANDARD_TYPE( Geom_Plane ) )
1090 {
1091 }
1092
1093 void BelongToPlane_i::SetPlane( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1094 {
1095   BelongToSurface_i::SetSurface( theGeom, theType );
1096   TPythonDump()<<this<<".SetPlane("<<theGeom<<","<<theType<<")";
1097 }
1098
1099 FunctorType BelongToPlane_i::GetFunctorType()
1100 {
1101   return FT_BelongToPlane;
1102 }
1103
1104 /*
1105   Class       : BelongToCylinder_i
1106   Description : Verify whether mesh element lie in pointed Geom planar object
1107 */
1108
1109 BelongToCylinder_i::BelongToCylinder_i()
1110 : BelongToSurface_i( STANDARD_TYPE( Geom_CylindricalSurface ) )
1111 {
1112 }
1113
1114 void BelongToCylinder_i::SetCylinder( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1115 {
1116   BelongToSurface_i::SetSurface( theGeom, theType );
1117   TPythonDump()<<this<<".SetCylinder("<<theGeom<<","<<theType<<")";
1118 }
1119
1120 FunctorType BelongToCylinder_i::GetFunctorType()
1121 {
1122   return FT_BelongToCylinder;
1123 }
1124
1125 /*
1126   Class       : BelongToGenSurface_i
1127   Description : Verify whether mesh element lie in pointed Geom planar object
1128 */
1129
1130 BelongToGenSurface_i::BelongToGenSurface_i()
1131 : BelongToSurface_i( STANDARD_TYPE( Geom_CylindricalSurface ) )
1132 {
1133 }
1134
1135 void BelongToGenSurface_i::SetSurface( GEOM::GEOM_Object_ptr theGeom, ElementType theType )
1136 {
1137   if ( theGeom->_is_nil() )
1138     return;
1139   TopoDS_Shape aLocShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theGeom );
1140   if ( !aLocShape.IsNull() && aLocShape.ShapeType() != TopAbs_FACE )
1141     aLocShape.Nullify();
1142   
1143   BelongToSurface_i::myElementsOnSurfacePtr->SetSurface( aLocShape, (SMDSAbs_ElementType)theType );
1144   TPythonDump()<<this<<".SetGenSurface("<<theGeom<<","<<theType<<")";
1145 }
1146
1147 FunctorType BelongToGenSurface_i::GetFunctorType()
1148 {
1149   return FT_BelongToGenSurface;
1150 }
1151
1152 /*
1153   Class       : LyingOnGeom_i
1154   Description : Predicate for selection on geometrical support
1155 */
1156 LyingOnGeom_i::LyingOnGeom_i()
1157 {
1158   myLyingOnGeomPtr.reset( new Controls::LyingOnGeom() );
1159   myFunctorPtr = myPredicatePtr = myLyingOnGeomPtr;
1160   myShapeName = 0;
1161   myShapeID = 0;
1162 }
1163
1164 LyingOnGeom_i::~LyingOnGeom_i()
1165 {
1166   CORBA::string_free( myShapeName );
1167   CORBA::string_free( myShapeID );
1168 }
1169
1170 void LyingOnGeom_i::SetGeom( GEOM::GEOM_Object_ptr theGeom )
1171 {
1172   if ( theGeom->_is_nil() )
1173     return;
1174   TopoDS_Shape aLocShape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( theGeom );
1175   myLyingOnGeomPtr->SetGeom( aLocShape );
1176   TPythonDump()<<this<<".SetGeom("<<theGeom<<")";
1177 }
1178
1179 void LyingOnGeom_i::SetGeom( const TopoDS_Shape& theShape )
1180 {
1181   myLyingOnGeomPtr->SetGeom( theShape );
1182 }
1183
1184 void LyingOnGeom_i::SetElementType(ElementType theType){
1185   myLyingOnGeomPtr->SetType(SMDSAbs_ElementType(theType));
1186   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1187 }
1188
1189 FunctorType LyingOnGeom_i::GetFunctorType()
1190 {
1191   return SMESH::FT_LyingOnGeom;
1192 }
1193
1194 void LyingOnGeom_i::SetShapeName( const char* theName )
1195 {
1196   CORBA::string_free( myShapeName );
1197   myShapeName = CORBA::string_dup( theName );
1198   myLyingOnGeomPtr->SetGeom( getShapeByName( myShapeName ) );
1199   TPythonDump()<<this<<".SetShapeName('"<<theName<<"')";
1200 }
1201
1202 void LyingOnGeom_i::SetShape( const char* theID, const char* theName )
1203 {
1204   CORBA::string_free( myShapeName );
1205   CORBA::string_free( myShapeID   );
1206   myShapeName = CORBA::string_dup( theName );
1207   myShapeID   = CORBA::string_dup( theID   );
1208   bool hasName = ( theName && theName[0] );
1209   bool hasID   = ( theID   && theID[0]   );
1210
1211   TopoDS_Shape S;
1212   if ( hasName && hasID )
1213   {
1214     S = getShapeByID( myShapeID );
1215     if ( S.IsNull() )
1216       S = getShapeByName( myShapeName );
1217   }
1218   else
1219   {
1220     S = hasID ? getShapeByID( myShapeID ) : getShapeByName( myShapeName );
1221   }
1222   myLyingOnGeomPtr->SetGeom( S );
1223 }
1224
1225 char* LyingOnGeom_i::GetShapeName()
1226 {
1227   return CORBA::string_dup( myShapeName );
1228 }
1229
1230 char* LyingOnGeom_i::GetShapeID()
1231 {
1232   return CORBA::string_dup( myShapeID );
1233 }
1234
1235 void LyingOnGeom_i::SetTolerance( CORBA::Double theToler )
1236 {
1237   myLyingOnGeomPtr->SetTolerance( theToler );
1238   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1239 }
1240
1241 CORBA::Double LyingOnGeom_i::GetTolerance()
1242 {
1243   return myLyingOnGeomPtr->GetTolerance();
1244 }
1245
1246 /*
1247   Class       : FreeBorders_i
1248   Description : Predicate for free borders
1249 */
1250 FreeBorders_i::FreeBorders_i()
1251 {
1252   myPredicatePtr.reset(new Controls::FreeBorders());
1253   myFunctorPtr = myPredicatePtr;
1254 }
1255
1256 FunctorType FreeBorders_i::GetFunctorType()
1257 {
1258   return SMESH::FT_FreeBorders;
1259 }
1260
1261 /*
1262   Class       : FreeEdges_i
1263   Description : Predicate for free borders
1264 */
1265 FreeEdges_i::FreeEdges_i()
1266 : myFreeEdgesPtr( new Controls::FreeEdges() )
1267 {
1268   myFunctorPtr = myPredicatePtr = myFreeEdgesPtr;
1269 }
1270
1271 SMESH::FreeEdges::Borders* FreeEdges_i::GetBorders()
1272 {
1273   SMESH::Controls::FreeEdges::TBorders aBorders;
1274   myFreeEdgesPtr->GetBoreders( aBorders );
1275
1276   long i = 0, iEnd = aBorders.size();
1277
1278   SMESH::FreeEdges::Borders_var aResult = new SMESH::FreeEdges::Borders;
1279   aResult->length(iEnd);
1280
1281   SMESH::Controls::FreeEdges::TBorders::const_iterator anIter;
1282   for ( anIter = aBorders.begin() ; anIter != aBorders.end(); anIter++, i++ )
1283   {
1284     const SMESH::Controls::FreeEdges::Border&  aBord = *anIter;
1285     SMESH::FreeEdges::Border &aBorder = aResult[ i ];
1286
1287     aBorder.myElemId = aBord.myElemId;
1288     aBorder.myPnt1 = aBord.myPntId[ 0 ];
1289     aBorder.myPnt2 = aBord.myPntId[ 1 ];
1290   }
1291   return aResult._retn();
1292 }
1293
1294 FunctorType FreeEdges_i::GetFunctorType()
1295 {
1296   return SMESH::FT_FreeEdges;
1297 }
1298
1299 /*
1300   Class       : FreeFaces_i
1301   Description : Predicate for free faces
1302 */
1303 FreeFaces_i::FreeFaces_i()
1304 {
1305   myPredicatePtr.reset(new Controls::FreeFaces());
1306   myFunctorPtr = myPredicatePtr;
1307 }
1308
1309 FunctorType FreeFaces_i::GetFunctorType()
1310 {
1311   return SMESH::FT_FreeFaces;
1312 }
1313
1314 /*
1315   Class       : FreeNodes_i
1316   Description : Predicate for free nodes
1317 */
1318 FreeNodes_i::FreeNodes_i()
1319 {
1320   myPredicatePtr.reset(new Controls::FreeNodes());
1321   myFunctorPtr = myPredicatePtr;
1322 }
1323
1324 FunctorType FreeNodes_i::GetFunctorType()
1325 {
1326   return SMESH::FT_FreeNodes;
1327 }
1328
1329 /*
1330   Class       : EqualNodes_i
1331   Description : Predicate for Equal nodes
1332 */
1333 EqualNodes_i::EqualNodes_i()
1334 {
1335   myCoincidentNodesPtr.reset(new Controls::CoincidentNodes());
1336   myFunctorPtr = myPredicatePtr = myCoincidentNodesPtr;
1337 }
1338
1339 FunctorType EqualNodes_i::GetFunctorType()
1340 {
1341   return SMESH::FT_EqualNodes;
1342 }
1343
1344 void EqualNodes_i::SetTolerance( double tol )
1345 {
1346   myCoincidentNodesPtr->SetTolerance( tol );
1347 }
1348
1349 double EqualNodes_i::GetTolerance()
1350 {
1351   return myCoincidentNodesPtr->GetTolerance();
1352 }
1353
1354 /*
1355   Class       : EqualEdges_i
1356   Description : Predicate for Equal Edges
1357 */
1358 EqualEdges_i::EqualEdges_i()
1359 {
1360   myPredicatePtr.reset(new Controls::CoincidentElements1D());
1361   myFunctorPtr = myPredicatePtr;
1362 }
1363
1364 FunctorType EqualEdges_i::GetFunctorType()
1365 {
1366   return SMESH::FT_EqualEdges;
1367 }
1368
1369 /*
1370   Class       : EqualFaces_i
1371   Description : Predicate for Equal Faces
1372 */
1373 EqualFaces_i::EqualFaces_i()
1374 {
1375   myPredicatePtr.reset(new Controls::CoincidentElements2D());
1376   myFunctorPtr = myPredicatePtr;
1377 }
1378
1379 FunctorType EqualFaces_i::GetFunctorType()
1380 {
1381   return SMESH::FT_EqualFaces;
1382 }
1383
1384 /*
1385   Class       : EqualVolumes_i
1386   Description : Predicate for Equal Volumes
1387 */
1388 EqualVolumes_i::EqualVolumes_i()
1389 {
1390   myPredicatePtr.reset(new Controls::CoincidentElements3D());
1391   myFunctorPtr = myPredicatePtr;
1392 }
1393
1394 FunctorType EqualVolumes_i::GetFunctorType()
1395 {
1396   return SMESH::FT_EqualVolumes;
1397 }
1398
1399
1400 /*
1401   Class       : RangeOfIds_i
1402   Description : Predicate for Range of Ids.
1403                 Range may be specified with two ways.
1404                 1. Using AddToRange method
1405                 2. With SetRangeStr method. Parameter of this method is a string
1406                    like as "1,2,3,50-60,63,67,70-"
1407 */
1408
1409 RangeOfIds_i::RangeOfIds_i()
1410 {
1411   myRangeOfIdsPtr.reset( new Controls::RangeOfIds() );
1412   myFunctorPtr = myPredicatePtr = myRangeOfIdsPtr;
1413 }
1414
1415 void RangeOfIds_i::SetRange( const SMESH::long_array& theIds )
1416 {
1417   CORBA::Long iEnd = theIds.length();
1418   for ( CORBA::Long i = 0; i < iEnd; i++ )
1419     myRangeOfIdsPtr->AddToRange( theIds[ i ] );
1420   TPythonDump()<<this<<".SetRange("<<theIds<<")";
1421 }
1422
1423 CORBA::Boolean RangeOfIds_i::SetRangeStr( const char* theRange )
1424 {
1425   TPythonDump()<<this<<".SetRangeStr('"<<theRange<<"')";
1426   return myRangeOfIdsPtr->SetRangeStr(
1427     TCollection_AsciiString( (Standard_CString)theRange ) );
1428 }
1429
1430 char* RangeOfIds_i::GetRangeStr()
1431 {
1432   TCollection_AsciiString aStr;
1433   myRangeOfIdsPtr->GetRangeStr( aStr );
1434   return CORBA::string_dup( aStr.ToCString() );
1435 }
1436
1437 void RangeOfIds_i::SetElementType( ElementType theType )
1438 {
1439   myRangeOfIdsPtr->SetType( SMDSAbs_ElementType( theType ) );
1440   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1441 }
1442
1443 FunctorType RangeOfIds_i::GetFunctorType()
1444 {
1445   return SMESH::FT_RangeOfIds;
1446 }
1447
1448 /*
1449   Class       : LinearOrQuadratic_i
1450   Description : Predicate to verify whether a mesh element is linear
1451 */
1452 LinearOrQuadratic_i::LinearOrQuadratic_i()
1453 {
1454   myLinearOrQuadraticPtr.reset(new Controls::LinearOrQuadratic());
1455   myFunctorPtr = myPredicatePtr = myLinearOrQuadraticPtr;
1456 }
1457
1458 void LinearOrQuadratic_i::SetElementType(ElementType theType)
1459 {
1460   myLinearOrQuadraticPtr->SetType(SMDSAbs_ElementType(theType));
1461   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1462 }
1463
1464 FunctorType LinearOrQuadratic_i::GetFunctorType()
1465 {
1466   return SMESH::FT_LinearOrQuadratic;
1467 }
1468
1469 /*
1470   Class       : GroupColor_i
1471   Description : Functor for check color of group to which mesh element belongs to
1472 */
1473 GroupColor_i::GroupColor_i()
1474 {
1475   myGroupColorPtr.reset(new Controls::GroupColor());
1476   myFunctorPtr = myPredicatePtr = myGroupColorPtr;
1477 }
1478
1479 FunctorType GroupColor_i::GetFunctorType()
1480 {
1481   return SMESH::FT_GroupColor;
1482 }
1483
1484 void GroupColor_i::SetColorStr( const char* theColor )
1485 {
1486   myGroupColorPtr->SetColorStr(
1487     TCollection_AsciiString( (Standard_CString)theColor ) );
1488   TPythonDump()<<this<<".SetColorStr('"<<theColor<<"')";
1489 }
1490
1491 char* GroupColor_i::GetColorStr()
1492 {
1493   TCollection_AsciiString aStr;
1494   myGroupColorPtr->GetColorStr( aStr );
1495   return CORBA::string_dup( aStr.ToCString() );
1496 }
1497
1498 void GroupColor_i::SetElementType(ElementType theType)
1499 {
1500   myGroupColorPtr->SetType(SMDSAbs_ElementType(theType));
1501   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1502 }
1503
1504 /*
1505   Class       : ElemGeomType_i
1506   Description : Predicate check is element has indicated geometry type
1507 */
1508 ElemGeomType_i::ElemGeomType_i()
1509 {
1510   myElemGeomTypePtr.reset(new Controls::ElemGeomType());
1511   myFunctorPtr = myPredicatePtr = myElemGeomTypePtr;
1512 }
1513
1514 void ElemGeomType_i::SetElementType(ElementType theType)
1515 {
1516   myElemGeomTypePtr->SetType(SMDSAbs_ElementType(theType));
1517   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1518 }
1519
1520 void ElemGeomType_i::SetGeometryType(GeometryType theType)
1521 {
1522   myElemGeomTypePtr->SetGeomType(SMDSAbs_GeometryType(theType));
1523   TPythonDump()<<this<<".SetGeometryType("<<theType<<")";
1524 }
1525
1526 GeometryType ElemGeomType_i::GetGeometryType() const
1527 {
1528   return (GeometryType)myElemGeomTypePtr->GetGeomType();
1529 }
1530
1531 FunctorType ElemGeomType_i::GetFunctorType()
1532 {
1533   return SMESH::FT_ElemGeomType;
1534 }
1535
1536 /*
1537   Class       : ElemEntityType_i
1538   Description : Predicate check is element has indicated entity type
1539 */
1540 ElemEntityType_i::ElemEntityType_i()
1541 {
1542   myElemEntityTypePtr.reset(new Controls::ElemEntityType());
1543   myFunctorPtr = myPredicatePtr = myElemEntityTypePtr;
1544 }
1545
1546 void ElemEntityType_i::SetElementType(ElementType theType)
1547 {
1548   myElemEntityTypePtr->SetType(SMDSAbs_ElementType(theType));
1549   TPythonDump()<<this<<".SetElementType("<<theType<<")";
1550 }
1551
1552 void ElemEntityType_i::SetEntityType(EntityType theEntityType)
1553 {
1554   myElemEntityTypePtr->SetElemEntityType(SMDSAbs_EntityType (theEntityType));
1555   TPythonDump()<<this<<".SetEntityType("<<theEntityType<<")";
1556 }
1557 EntityType ElemEntityType_i::GetEntityType() const
1558 {
1559  return (EntityType) myElemEntityTypePtr->GetElemEntityType();
1560 }
1561
1562 FunctorType ElemEntityType_i::GetFunctorType()
1563 {
1564   return SMESH::FT_EntityType;
1565 }
1566
1567 /*
1568   Class       : CoplanarFaces_i
1569   Description : Returns true if a mesh face is a coplanar neighbour to a given one
1570 */
1571 CoplanarFaces_i::CoplanarFaces_i()
1572 {
1573   myCoplanarFacesPtr.reset(new Controls::CoplanarFaces());
1574   myFunctorPtr = myPredicatePtr = myCoplanarFacesPtr;
1575 }
1576
1577 void CoplanarFaces_i::SetFace ( CORBA::Long theFaceID )
1578 {
1579   myCoplanarFacesPtr->SetFace(theFaceID);
1580   TPythonDump()<<this<<".SetFace("<<theFaceID<<")";
1581 }
1582
1583 void CoplanarFaces_i::SetTolerance( CORBA::Double theToler )
1584 {
1585   myCoplanarFacesPtr->SetTolerance(theToler);
1586   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1587 }
1588
1589 CORBA::Long CoplanarFaces_i::GetFace () const
1590 {
1591   return myCoplanarFacesPtr->GetFace();
1592 }
1593
1594 char* CoplanarFaces_i::GetFaceAsString () const
1595 {
1596   TCollection_AsciiString str(Standard_Integer(myCoplanarFacesPtr->GetFace()));
1597   return CORBA::string_dup( str.ToCString() );
1598 }
1599
1600 CORBA::Double CoplanarFaces_i::GetTolerance() const
1601 {
1602   return myCoplanarFacesPtr->GetTolerance();
1603 }
1604
1605 FunctorType CoplanarFaces_i::GetFunctorType()
1606 {
1607   return SMESH::FT_CoplanarFaces;
1608 }
1609
1610 /*
1611  * Class       : ConnectedElements_i
1612  * Description : Returns true if an element is connected via other elements to the element
1613  *               located at a given point.
1614  */
1615 ConnectedElements_i::ConnectedElements_i()
1616 {
1617   myConnectedElementsPtr.reset(new Controls::ConnectedElements());
1618   myFunctorPtr = myPredicatePtr = myConnectedElementsPtr;
1619 }
1620
1621 FunctorType ConnectedElements_i::GetFunctorType()
1622 {
1623   return FT_ConnectedElements;
1624 }
1625
1626 void ConnectedElements_i::SetElementType( ElementType theType )
1627 {
1628   myConnectedElementsPtr->SetType( SMDSAbs_ElementType( theType ));
1629   TPythonDump() << this << ".SetElementType( " << theType << " )";
1630 }
1631
1632 void ConnectedElements_i::SetPoint( CORBA::Double x, CORBA::Double y, CORBA::Double z )
1633 {
1634   myConnectedElementsPtr->SetPoint( x,y,z );
1635   myVertexID.clear();
1636   TPythonDump() << this << ".SetPoint( " << x << ", " << y << ", " << z << " )";
1637 }
1638
1639 void ConnectedElements_i::SetVertex( GEOM::GEOM_Object_ptr vertex )
1640   throw (SALOME::SALOME_Exception)
1641 {
1642   TopoDS_Shape shape = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( vertex );
1643   if ( shape.IsNull() )
1644     THROW_SALOME_CORBA_EXCEPTION( "ConnectedElements_i::SetVertex(): NULL Vertex",
1645                                   SALOME::BAD_PARAM );
1646
1647   TopExp_Explorer v( shape, TopAbs_VERTEX );
1648   if ( !v.More() )
1649     THROW_SALOME_CORBA_EXCEPTION( "ConnectedElements_i::SetVertex(): empty vertex",
1650                                   SALOME::BAD_PARAM );
1651
1652   gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( v.Current() ));
1653   myConnectedElementsPtr->SetPoint( p.X(), p.Y(), p.Z() );
1654   //
1655   CORBA::String_var id = vertex->GetStudyEntry();
1656   myVertexID = id.in();
1657
1658   TPythonDump() << this << ".SetVertex( " << vertex << " )";
1659 }
1660
1661 void ConnectedElements_i::SetNode ( CORBA::Long nodeID )
1662   throw (SALOME::SALOME_Exception)
1663 {
1664   if ( nodeID < 1 )
1665     THROW_SALOME_CORBA_EXCEPTION( "ConnectedElements_i::SetNode(): nodeID must be > 0",
1666                                   SALOME::BAD_PARAM );
1667
1668   myConnectedElementsPtr->SetNode( nodeID );
1669   myVertexID.clear();
1670   TPythonDump() << this << ".SetNode( " << nodeID << " )";
1671 }
1672
1673 /*!
1674  * \brief This is a comfort method for Filter dialog
1675  */
1676 void ConnectedElements_i::SetThreshold ( const char*                             threshold,
1677                                          SMESH::ConnectedElements::ThresholdType type )
1678   throw (SALOME::SALOME_Exception)
1679 {
1680   if ( !threshold )
1681     THROW_SALOME_CORBA_EXCEPTION( "ConnectedElements_i::SetThreshold(): NULL threshold",
1682                                   SALOME::BAD_PARAM );
1683   switch ( type )
1684   {
1685   case SMESH::ConnectedElements::POINT: // read 3 node coordinates ///////////////////
1686     {
1687       std::vector< double > xyz;
1688       char* endptr;
1689       do
1690       {
1691         // skip a separator
1692         while ( *threshold &&
1693                 *threshold != '+' &&
1694                 *threshold != '-' &&
1695                 !isdigit( *threshold ))
1696           ++threshold;
1697         if ( !*threshold )
1698           break;
1699         // read a coordinate
1700         xyz.push_back( strtod( threshold, &endptr ));
1701         if ( threshold == endptr )
1702         {
1703           xyz.resize( xyz.size() - 1 );
1704           break;
1705         }
1706         threshold = endptr;
1707       }
1708       while ( xyz.size() < 3 );
1709
1710       if ( xyz.size() < 3 )
1711         THROW_SALOME_CORBA_EXCEPTION
1712           ( "ConnectedElements_i::SetThreshold(): invalid point coordinates", SALOME::BAD_PARAM );
1713
1714       SetPoint( xyz[0], xyz[1], xyz[2] );
1715       break;
1716     }
1717   case SMESH::ConnectedElements::VERTEX: // get a VERTEX by its entry /////////////////
1718     {
1719       SALOMEDS::SObject_wrap sobj = SMESH_Gen_i::getStudyServant()->FindObjectID( threshold );
1720       if ( sobj->_is_nil() )
1721         THROW_SALOME_CORBA_EXCEPTION
1722           ( "ConnectedElements_i::SetThreshold(): invalid vertex study entry", SALOME::BAD_PARAM );
1723       CORBA::Object_var        obj = sobj->GetObject();
1724       GEOM::GEOM_Object_var vertex = GEOM::GEOM_Object::_narrow( obj );
1725       if ( vertex->_is_nil() )
1726         THROW_SALOME_CORBA_EXCEPTION
1727           ( "ConnectedElements_i::SetThreshold(): no GEOM_Object in SObject", SALOME::BAD_PARAM );
1728       SetVertex( vertex );
1729       break;
1730     }
1731   case SMESH::ConnectedElements::NODE: // read a node ID ////////////////////////////
1732     {
1733       char* endptr;
1734       int id = strtol( threshold, &endptr, 10 );
1735       if ( threshold == endptr )
1736         THROW_SALOME_CORBA_EXCEPTION
1737           ( "ConnectedElements_i::SetThreshold(): invalid node ID", SALOME::BAD_PARAM );
1738       SetNode( id );
1739       break;
1740     }
1741   default:
1742     THROW_SALOME_CORBA_EXCEPTION
1743       ( "ConnectedElements_i::SetThreshold(): invalid ThresholdType", SALOME::BAD_PARAM );
1744   }
1745 }
1746
1747 char* ConnectedElements_i::GetThreshold ( SMESH::ConnectedElements::ThresholdType& type )
1748 {
1749   std::string threshold;
1750   if ( !myVertexID.empty() )
1751   {
1752     threshold = myVertexID;
1753     type      = SMESH::ConnectedElements::VERTEX;
1754   }
1755   else
1756   {
1757     std::vector<double> xyz = myConnectedElementsPtr->GetPoint();
1758     if ( xyz.size() == 3 )
1759     {
1760       threshold = SMESH_Comment( xyz[0] ) << "; " << xyz[1] << "; " << xyz[2];
1761       type      = SMESH::ConnectedElements::POINT;
1762     }
1763     else
1764     {
1765       threshold = SMESH_Comment( myConnectedElementsPtr->GetNode() );
1766       type      = SMESH::ConnectedElements::NODE;
1767     }
1768   }
1769   return CORBA::string_dup( threshold.c_str() );
1770 }
1771
1772 /*
1773   Class       : Comparator_i
1774   Description : Base class for comparators
1775 */
1776 Comparator_i::Comparator_i():
1777   myNumericalFunctor( NULL )
1778 {}
1779
1780 Comparator_i::~Comparator_i()
1781 {
1782   if ( myNumericalFunctor )
1783     myNumericalFunctor->UnRegister();
1784 }
1785
1786 void Comparator_i::SetMargin( CORBA::Double theValue )
1787 {
1788   myComparatorPtr->SetMargin( theValue );
1789   TPythonDump()<<this<<".SetMargin("<<theValue<<")";
1790 }
1791
1792 CORBA::Double Comparator_i::GetMargin()
1793 {
1794   return myComparatorPtr->GetMargin();
1795 }
1796
1797 void Comparator_i::SetNumFunctor( NumericalFunctor_ptr theFunct )
1798 {
1799   if ( myNumericalFunctor )
1800     myNumericalFunctor->UnRegister();
1801
1802   myNumericalFunctor = DownCast<NumericalFunctor_i*>(theFunct);
1803
1804   if ( myNumericalFunctor )
1805   {
1806     myComparatorPtr->SetNumFunctor( myNumericalFunctor->GetNumericalFunctor() );
1807     myNumericalFunctor->Register();
1808     TPythonDump()<<this<<".SetNumFunctor("<<myNumericalFunctor<<")";
1809   }
1810 }
1811
1812 Controls::ComparatorPtr Comparator_i::GetComparator()
1813 {
1814   return myComparatorPtr;
1815 }
1816
1817 NumericalFunctor_i* Comparator_i::GetNumFunctor_i()
1818 {
1819   return myNumericalFunctor;
1820 }
1821
1822
1823 /*
1824   Class       : LessThan_i
1825   Description : Comparator "<"
1826 */
1827 LessThan_i::LessThan_i()
1828 {
1829   myComparatorPtr.reset( new Controls::LessThan() );
1830   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1831 }
1832
1833 FunctorType LessThan_i::GetFunctorType()
1834 {
1835   return SMESH::FT_LessThan;
1836 }
1837
1838
1839 /*
1840   Class       : MoreThan_i
1841   Description : Comparator ">"
1842 */
1843 MoreThan_i::MoreThan_i()
1844 {
1845   myComparatorPtr.reset( new Controls::MoreThan() );
1846   myFunctorPtr = myPredicatePtr = myComparatorPtr;
1847 }
1848
1849 FunctorType MoreThan_i::GetFunctorType()
1850 {
1851   return SMESH::FT_MoreThan;
1852 }
1853
1854
1855 /*
1856   Class       : EqualTo_i
1857   Description : Comparator "="
1858 */
1859 EqualTo_i::EqualTo_i()
1860 : myEqualToPtr( new Controls::EqualTo() )
1861 {
1862   myFunctorPtr = myPredicatePtr = myComparatorPtr = myEqualToPtr;
1863 }
1864
1865 void EqualTo_i::SetTolerance( CORBA::Double theToler )
1866 {
1867   myEqualToPtr->SetTolerance( theToler );
1868   TPythonDump()<<this<<".SetTolerance("<<theToler<<")";
1869 }
1870
1871 CORBA::Double EqualTo_i::GetTolerance()
1872 {
1873   return myEqualToPtr->GetTolerance();
1874 }
1875
1876 FunctorType EqualTo_i::GetFunctorType()
1877 {
1878   return SMESH::FT_EqualTo;
1879 }
1880
1881 /*
1882   Class       : LogicalNOT_i
1883   Description : Logical NOT predicate
1884 */
1885 LogicalNOT_i::LogicalNOT_i():
1886   myLogicalNOTPtr( new Controls::LogicalNOT() ),
1887   myPredicate( NULL )
1888 {
1889   myFunctorPtr = myPredicatePtr = myLogicalNOTPtr;
1890 }
1891
1892 LogicalNOT_i::~LogicalNOT_i()
1893 {
1894   if ( myPredicate )
1895     myPredicate->UnRegister();
1896 }
1897
1898 void LogicalNOT_i::SetPredicate( Predicate_ptr thePredicate )
1899 {
1900   if ( myPredicate )
1901     myPredicate->UnRegister();
1902
1903   myPredicate = SMESH::GetPredicate(thePredicate);
1904
1905   if ( myPredicate ){
1906     myLogicalNOTPtr->SetPredicate(myPredicate->GetPredicate());
1907     myPredicate->Register();
1908     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
1909   }
1910 }
1911
1912 FunctorType LogicalNOT_i::GetFunctorType()
1913 {
1914   return SMESH::FT_LogicalNOT;
1915 }
1916
1917 Predicate_i* LogicalNOT_i::GetPredicate_i()
1918 {
1919   return myPredicate;
1920 }
1921
1922
1923
1924 /*
1925   Class       : LogicalBinary_i
1926   Description : Base class for binary logical predicate
1927 */
1928 LogicalBinary_i::LogicalBinary_i()
1929 : myPredicate1( NULL ),
1930   myPredicate2( NULL )
1931 {}
1932
1933 LogicalBinary_i::~LogicalBinary_i()
1934 {
1935   if ( myPredicate1 )
1936     myPredicate1->UnRegister();
1937
1938   if ( myPredicate2 )
1939     myPredicate2->UnRegister();
1940 }
1941
1942 void LogicalBinary_i::SetMesh( SMESH_Mesh_ptr theMesh )
1943 {
1944   if ( myPredicate1 )
1945     myPredicate1->SetMesh( theMesh );
1946
1947   if ( myPredicate2 )
1948     myPredicate2->SetMesh( theMesh );
1949 }
1950
1951 void LogicalBinary_i::SetPredicate1( Predicate_ptr thePredicate )
1952 {
1953   if ( myPredicate1 )
1954     myPredicate1->UnRegister();
1955
1956   myPredicate1 = SMESH::GetPredicate(thePredicate);
1957
1958   if ( myPredicate1 ){
1959     myLogicalBinaryPtr->SetPredicate1(myPredicate1->GetPredicate());
1960     myPredicate1->Register();
1961     TPythonDump()<<this<<".SetPredicate1("<<myPredicate1<<")";
1962   }
1963 }
1964
1965 void LogicalBinary_i::SetPredicate2( Predicate_ptr thePredicate )
1966 {
1967   if ( myPredicate2 )
1968     myPredicate2->UnRegister();
1969
1970   myPredicate2 = SMESH::GetPredicate(thePredicate);
1971
1972   if ( myPredicate2 ){
1973     myLogicalBinaryPtr->SetPredicate2(myPredicate2->GetPredicate());
1974     myPredicate2->Register();
1975     TPythonDump()<<this<<".SetPredicate2("<<myPredicate2<<")";
1976   }
1977 }
1978
1979 Controls::LogicalBinaryPtr LogicalBinary_i::GetLogicalBinary()
1980 {
1981   return myLogicalBinaryPtr;
1982 }
1983
1984 Predicate_i* LogicalBinary_i::GetPredicate1_i()
1985 {
1986   return myPredicate1;
1987 }
1988 Predicate_i* LogicalBinary_i::GetPredicate2_i()
1989 {
1990   return myPredicate2;
1991 }
1992
1993
1994 /*
1995   Class       : LogicalAND_i
1996   Description : Logical AND
1997 */
1998 LogicalAND_i::LogicalAND_i()
1999 {
2000   myLogicalBinaryPtr.reset( new Controls::LogicalAND() );
2001   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
2002 }
2003
2004 FunctorType LogicalAND_i::GetFunctorType()
2005 {
2006   return SMESH::FT_LogicalAND;
2007 }
2008
2009
2010 /*
2011   Class       : LogicalOR_i
2012   Description : Logical OR
2013 */
2014 LogicalOR_i::LogicalOR_i()
2015 {
2016   myLogicalBinaryPtr.reset( new Controls::LogicalOR() );
2017   myFunctorPtr = myPredicatePtr = myLogicalBinaryPtr;
2018 }
2019
2020 FunctorType LogicalOR_i::GetFunctorType()
2021 {
2022   return SMESH::FT_LogicalOR;
2023 }
2024
2025
2026 /*
2027                             FILTER MANAGER
2028 */
2029
2030 FilterManager_i::FilterManager_i()
2031 : SALOME::GenericObj_i( SMESH_Gen_i::GetPOA() )
2032 {
2033   //Base class Salome_GenericObject do it inmplicitly by overriding PortableServer::POA_ptr _default_POA() method
2034   //PortableServer::ObjectId_var anObjectId =
2035   //  SMESH_Gen_i::GetPOA()->activate_object( this );
2036 }
2037
2038
2039 FilterManager_i::~FilterManager_i()
2040 {
2041   //TPythonDump()<<this<<".UnRegister()";
2042 }
2043
2044
2045 MinimumAngle_ptr FilterManager_i::CreateMinimumAngle()
2046 {
2047   SMESH::MinimumAngle_i* aServant = new SMESH::MinimumAngle_i();
2048   SMESH::MinimumAngle_var anObj = aServant->_this();
2049   TPythonDump()<<aServant<<" = "<<this<<".CreateMinimumAngle()";
2050   return anObj._retn();
2051 }
2052
2053
2054 AspectRatio_ptr FilterManager_i::CreateAspectRatio()
2055 {
2056   SMESH::AspectRatio_i* aServant = new SMESH::AspectRatio_i();
2057   SMESH::AspectRatio_var anObj = aServant->_this();
2058   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio()";
2059   return anObj._retn();
2060 }
2061
2062
2063 AspectRatio3D_ptr FilterManager_i::CreateAspectRatio3D()
2064 {
2065   SMESH::AspectRatio3D_i* aServant = new SMESH::AspectRatio3D_i();
2066   SMESH::AspectRatio3D_var anObj = aServant->_this();
2067   TPythonDump()<<aServant<<" = "<<this<<".CreateAspectRatio3D()";
2068   return anObj._retn();
2069 }
2070
2071
2072 Warping_ptr FilterManager_i::CreateWarping()
2073 {
2074   SMESH::Warping_i* aServant = new SMESH::Warping_i();
2075   SMESH::Warping_var anObj = aServant->_this();
2076   TPythonDump()<<aServant<<" = "<<this<<".CreateWarping()";
2077   return anObj._retn();
2078 }
2079
2080
2081 Taper_ptr FilterManager_i::CreateTaper()
2082 {
2083   SMESH::Taper_i* aServant = new SMESH::Taper_i();
2084   SMESH::Taper_var anObj = aServant->_this();
2085   TPythonDump()<<aServant<<" = "<<this<<".CreateTaper()";
2086   return anObj._retn();
2087 }
2088
2089
2090 Skew_ptr FilterManager_i::CreateSkew()
2091 {
2092   SMESH::Skew_i* aServant = new SMESH::Skew_i();
2093   SMESH::Skew_var anObj = aServant->_this();
2094   TPythonDump()<<aServant<<" = "<<this<<".CreateSkew()";
2095   return anObj._retn();
2096 }
2097
2098
2099 Area_ptr FilterManager_i::CreateArea()
2100 {
2101   SMESH::Area_i* aServant = new SMESH::Area_i();
2102   SMESH::Area_var anObj = aServant->_this();
2103   TPythonDump()<<aServant<<" = "<<this<<".CreateArea()";
2104   return anObj._retn();
2105 }
2106
2107
2108 Volume3D_ptr FilterManager_i::CreateVolume3D()
2109 {
2110   SMESH::Volume3D_i* aServant = new SMESH::Volume3D_i();
2111   SMESH::Volume3D_var anObj = aServant->_this();
2112   TPythonDump()<<aServant<<" = "<<this<<".CreateVolume3D()";
2113   return anObj._retn();
2114 }
2115
2116
2117 MaxElementLength2D_ptr FilterManager_i::CreateMaxElementLength2D()
2118 {
2119   SMESH::MaxElementLength2D_i* aServant = new SMESH::MaxElementLength2D_i();
2120   SMESH::MaxElementLength2D_var anObj = aServant->_this();
2121   TPythonDump()<<aServant<<" = "<<this<<".CreateMaxElementLength2D()";
2122   return anObj._retn();
2123 }
2124
2125
2126 MaxElementLength3D_ptr FilterManager_i::CreateMaxElementLength3D()
2127 {
2128   SMESH::MaxElementLength3D_i* aServant = new SMESH::MaxElementLength3D_i();
2129   SMESH::MaxElementLength3D_var anObj = aServant->_this();
2130   TPythonDump()<<aServant<<" = "<<this<<".CreateMaxElementLength3D()";
2131   return anObj._retn();
2132 }
2133
2134
2135 Length_ptr FilterManager_i::CreateLength()
2136 {
2137   SMESH::Length_i* aServant = new SMESH::Length_i();
2138   SMESH::Length_var anObj = aServant->_this();
2139   TPythonDump()<<aServant<<" = "<<this<<".CreateLength()";
2140   return anObj._retn();
2141 }
2142
2143 Length2D_ptr FilterManager_i::CreateLength2D()
2144 {
2145   SMESH::Length2D_i* aServant = new SMESH::Length2D_i();
2146   SMESH::Length2D_var anObj = aServant->_this();
2147   TPythonDump()<<aServant<<" = "<<this<<".CreateLength2D()";
2148   return anObj._retn();
2149 }
2150
2151 Length3D_ptr FilterManager_i::CreateLength3D()
2152 {
2153   SMESH::Length3D_i* aServant = new SMESH::Length3D_i();
2154   SMESH::Length3D_var anObj = aServant->_this();
2155   TPythonDump()<<aServant<<" = "<<this<<".CreateLength3D()";
2156   return anObj._retn();
2157 }
2158
2159 Deflection2D_ptr FilterManager_i::CreateDeflection2D()
2160 {
2161   SMESH::Deflection2D_i* aServant = new SMESH::Deflection2D_i();
2162   SMESH::Deflection2D_var   anObj = aServant->_this();
2163   TPythonDump()<<aServant<<" = "<<this<<".CreateDeflection2D()";
2164   return anObj._retn();
2165 }
2166
2167 MultiConnection_ptr FilterManager_i::CreateMultiConnection()
2168 {
2169   SMESH::MultiConnection_i* aServant = new SMESH::MultiConnection_i();
2170   SMESH::MultiConnection_var anObj = aServant->_this();
2171   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection()";
2172   return anObj._retn();
2173 }
2174
2175 MultiConnection2D_ptr FilterManager_i::CreateMultiConnection2D()
2176 {
2177   SMESH::MultiConnection2D_i* aServant = new SMESH::MultiConnection2D_i();
2178   SMESH::MultiConnection2D_var anObj = aServant->_this();
2179   TPythonDump()<<aServant<<" = "<<this<<".CreateMultiConnection2D()";
2180   return anObj._retn();
2181 }
2182
2183 BallDiameter_ptr FilterManager_i::CreateBallDiameter()
2184 {
2185   SMESH::BallDiameter_i* aServant = new SMESH::BallDiameter_i();
2186   SMESH::BallDiameter_var anObj = aServant->_this();
2187   TPythonDump()<<aServant<<" = "<<this<<".CreateBallDiameter()";
2188   return anObj._retn();
2189 }
2190
2191 NodeConnectivityNumber_ptr FilterManager_i::CreateNodeConnectivityNumber()
2192 {
2193   SMESH::NodeConnectivityNumber_i* aServant = new SMESH::NodeConnectivityNumber_i();
2194   SMESH::NodeConnectivityNumber_var anObj = aServant->_this();
2195   TPythonDump()<<aServant<<" = "<<this<<".CreateNodeConnectivityNumber()";
2196   return anObj._retn();
2197 }
2198
2199 BelongToMeshGroup_ptr FilterManager_i::CreateBelongToMeshGroup()
2200 {
2201   SMESH::BelongToMeshGroup_i* aServant = new SMESH::BelongToMeshGroup_i();
2202   SMESH::BelongToMeshGroup_var anObj = aServant->_this();
2203   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToMeshGroup()";
2204   return anObj._retn();
2205 }
2206
2207 BelongToGeom_ptr FilterManager_i::CreateBelongToGeom()
2208 {
2209   SMESH::BelongToGeom_i* aServant = new SMESH::BelongToGeom_i();
2210   SMESH::BelongToGeom_var anObj = aServant->_this();
2211   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGeom()";
2212   return anObj._retn();
2213 }
2214
2215 BelongToPlane_ptr FilterManager_i::CreateBelongToPlane()
2216 {
2217   SMESH::BelongToPlane_i* aServant = new SMESH::BelongToPlane_i();
2218   SMESH::BelongToPlane_var anObj = aServant->_this();
2219   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToPlane()";
2220   return anObj._retn();
2221 }
2222
2223 BelongToCylinder_ptr FilterManager_i::CreateBelongToCylinder()
2224 {
2225   SMESH::BelongToCylinder_i* aServant = new SMESH::BelongToCylinder_i();
2226   SMESH::BelongToCylinder_var anObj = aServant->_this();
2227   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToCylinder()";
2228   return anObj._retn();
2229 }
2230
2231 BelongToGenSurface_ptr FilterManager_i::CreateBelongToGenSurface()
2232 {
2233   SMESH::BelongToGenSurface_i* aServant = new SMESH::BelongToGenSurface_i();
2234   SMESH::BelongToGenSurface_var anObj = aServant->_this();
2235   TPythonDump()<<aServant<<" = "<<this<<".CreateBelongToGenSurface()";
2236   return anObj._retn();
2237 }
2238
2239 LyingOnGeom_ptr FilterManager_i::CreateLyingOnGeom()
2240 {
2241   SMESH::LyingOnGeom_i* aServant = new SMESH::LyingOnGeom_i();
2242   SMESH::LyingOnGeom_var anObj = aServant->_this();
2243   TPythonDump()<<aServant<<" = "<<this<<".CreateLyingOnGeom()";
2244   return anObj._retn();
2245 }
2246
2247 CoplanarFaces_ptr FilterManager_i::CreateCoplanarFaces()
2248 {
2249   SMESH::CoplanarFaces_i* aServant = new SMESH::CoplanarFaces_i();
2250   SMESH::CoplanarFaces_var anObj = aServant->_this();
2251   TPythonDump()<<aServant<<" = "<<this<<".CreateCoplanarFaces()";
2252   return anObj._retn();
2253 }
2254
2255 ConnectedElements_ptr FilterManager_i::CreateConnectedElements()
2256 {
2257   SMESH::ConnectedElements_i* aServant = new SMESH::ConnectedElements_i();
2258   SMESH::ConnectedElements_var anObj = aServant->_this();
2259   TPythonDump()<<aServant<<" = "<<this<<".CreateConnectedElements()";
2260   return anObj._retn();
2261 }
2262
2263 FreeBorders_ptr FilterManager_i::CreateFreeBorders()
2264 {
2265   SMESH::FreeBorders_i* aServant = new SMESH::FreeBorders_i();
2266   SMESH::FreeBorders_var anObj = aServant->_this();
2267   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeBorders()";
2268   return anObj._retn();
2269 }
2270
2271 FreeEdges_ptr FilterManager_i::CreateFreeEdges()
2272 {
2273   SMESH::FreeEdges_i* aServant = new SMESH::FreeEdges_i();
2274   SMESH::FreeEdges_var anObj = aServant->_this();
2275   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeEdges()";
2276   return anObj._retn();
2277 }
2278
2279 FreeFaces_ptr FilterManager_i::CreateFreeFaces()
2280 {
2281   SMESH::FreeFaces_i* aServant = new SMESH::FreeFaces_i();
2282   SMESH::FreeFaces_var anObj = aServant->_this();
2283   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeFaces()";
2284   return anObj._retn();
2285 }
2286
2287 FreeNodes_ptr FilterManager_i::CreateFreeNodes()
2288 {
2289   SMESH::FreeNodes_i* aServant = new SMESH::FreeNodes_i();
2290   SMESH::FreeNodes_var anObj = aServant->_this();
2291   TPythonDump()<<aServant<<" = "<<this<<".CreateFreeNodes()";
2292   return anObj._retn();
2293 }
2294
2295 EqualNodes_ptr FilterManager_i::CreateEqualNodes()
2296 {
2297   SMESH::EqualNodes_i* aServant = new SMESH::EqualNodes_i();
2298   SMESH::EqualNodes_var anObj = aServant->_this();
2299   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualNodes()";
2300   return anObj._retn();
2301 }
2302
2303 EqualEdges_ptr FilterManager_i::CreateEqualEdges()
2304 {
2305   SMESH::EqualEdges_i* aServant = new SMESH::EqualEdges_i();
2306   SMESH::EqualEdges_var anObj = aServant->_this();
2307   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualEdges()";
2308   return anObj._retn();
2309 }
2310 EqualFaces_ptr FilterManager_i::CreateEqualFaces()
2311 {
2312   SMESH::EqualFaces_i* aServant = new SMESH::EqualFaces_i();
2313   SMESH::EqualFaces_var anObj = aServant->_this();
2314   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualFaces()";
2315   return anObj._retn();
2316 }
2317 EqualVolumes_ptr FilterManager_i::CreateEqualVolumes()
2318 {
2319   SMESH::EqualVolumes_i* aServant = new SMESH::EqualVolumes_i();
2320   SMESH::EqualVolumes_var anObj = aServant->_this();
2321   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualVolumes()";
2322   return anObj._retn();
2323 }
2324
2325 RangeOfIds_ptr FilterManager_i::CreateRangeOfIds()
2326 {
2327   SMESH::RangeOfIds_i* aServant = new SMESH::RangeOfIds_i();
2328   SMESH::RangeOfIds_var anObj = aServant->_this();
2329   TPythonDump()<<aServant<<" = "<<this<<".CreateRangeOfIds()";
2330   return anObj._retn();
2331 }
2332
2333 BadOrientedVolume_ptr FilterManager_i::CreateBadOrientedVolume()
2334 {
2335   SMESH::BadOrientedVolume_i* aServant = new SMESH::BadOrientedVolume_i();
2336   SMESH::BadOrientedVolume_var anObj = aServant->_this();
2337   TPythonDump()<<aServant<<" = "<<this<<".CreateBadOrientedVolume()";
2338   return anObj._retn();
2339 }
2340
2341 BareBorderVolume_ptr FilterManager_i::CreateBareBorderVolume()
2342 {
2343   SMESH::BareBorderVolume_i* aServant = new SMESH::BareBorderVolume_i();
2344   SMESH::BareBorderVolume_var anObj = aServant->_this();
2345   TPythonDump()<<aServant<<" = "<<this<<".CreateBareBorderVolume()";
2346   return anObj._retn();
2347 }
2348
2349 BareBorderFace_ptr FilterManager_i::CreateBareBorderFace()
2350 {
2351   SMESH::BareBorderFace_i* aServant = new SMESH::BareBorderFace_i();
2352   SMESH::BareBorderFace_var anObj = aServant->_this();
2353   TPythonDump()<<aServant<<" = "<<this<<".CreateBareBorderFace()";
2354   return anObj._retn();
2355 }
2356
2357 OverConstrainedVolume_ptr FilterManager_i::CreateOverConstrainedVolume()
2358 {
2359   SMESH::OverConstrainedVolume_i* aServant = new SMESH::OverConstrainedVolume_i();
2360   SMESH::OverConstrainedVolume_var anObj = aServant->_this();
2361   TPythonDump()<<aServant<<" = "<<this<<".CreateOverConstrainedVolume()";
2362   return anObj._retn();
2363 }
2364
2365 OverConstrainedFace_ptr FilterManager_i::CreateOverConstrainedFace()
2366 {
2367   SMESH::OverConstrainedFace_i* aServant = new SMESH::OverConstrainedFace_i();
2368   SMESH::OverConstrainedFace_var anObj = aServant->_this();
2369   TPythonDump()<<aServant<<" = "<<this<<".CreateOverConstrainedFace()";
2370   return anObj._retn();
2371 }
2372
2373 LessThan_ptr FilterManager_i::CreateLessThan()
2374 {
2375   SMESH::LessThan_i* aServant = new SMESH::LessThan_i();
2376   SMESH::LessThan_var anObj = aServant->_this();
2377   TPythonDump()<<aServant<<" = "<<this<<".CreateLessThan()";
2378   return anObj._retn();
2379 }
2380
2381 MoreThan_ptr FilterManager_i::CreateMoreThan()
2382 {
2383   SMESH::MoreThan_i* aServant = new SMESH::MoreThan_i();
2384   SMESH::MoreThan_var anObj = aServant->_this();
2385   TPythonDump()<<aServant<<" = "<<this<<".CreateMoreThan()";
2386   return anObj._retn();
2387 }
2388
2389 EqualTo_ptr FilterManager_i::CreateEqualTo()
2390 {
2391   SMESH::EqualTo_i* aServant = new SMESH::EqualTo_i();
2392   SMESH::EqualTo_var anObj = aServant->_this();
2393   TPythonDump()<<aServant<<" = "<<this<<".CreateEqualTo()";
2394   return anObj._retn();
2395 }
2396
2397 LogicalNOT_ptr FilterManager_i::CreateLogicalNOT()
2398 {
2399   SMESH::LogicalNOT_i* aServant = new SMESH::LogicalNOT_i();
2400   SMESH::LogicalNOT_var anObj = aServant->_this();
2401   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalNOT()";
2402   return anObj._retn();
2403 }
2404
2405 LogicalAND_ptr FilterManager_i::CreateLogicalAND()
2406 {
2407   SMESH::LogicalAND_i* aServant = new SMESH::LogicalAND_i();
2408   SMESH::LogicalAND_var anObj = aServant->_this();
2409   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalAND()";
2410   return anObj._retn();
2411 }
2412
2413 LogicalOR_ptr FilterManager_i::CreateLogicalOR()
2414 {
2415   SMESH::LogicalOR_i* aServant = new SMESH::LogicalOR_i();
2416   SMESH::LogicalOR_var anObj = aServant->_this();
2417   TPythonDump()<<aServant<<" = "<<this<<".CreateLogicalOR()";
2418   return anObj._retn();
2419 }
2420
2421 LinearOrQuadratic_ptr FilterManager_i::CreateLinearOrQuadratic()
2422 {
2423   SMESH::LinearOrQuadratic_i* aServant = new SMESH::LinearOrQuadratic_i();
2424   SMESH::LinearOrQuadratic_var anObj = aServant->_this();
2425   TPythonDump()<<aServant<<" = "<<this<<".CreateLinearOrQuadratic()";
2426   return anObj._retn();
2427 }
2428
2429 GroupColor_ptr FilterManager_i::CreateGroupColor()
2430 {
2431   SMESH::GroupColor_i* aServant = new SMESH::GroupColor_i();
2432   SMESH::GroupColor_var anObj = aServant->_this();
2433   TPythonDump()<<aServant<<" = "<<this<<".CreateGroupColor()";
2434   return anObj._retn();
2435 }
2436
2437 ElemGeomType_ptr FilterManager_i::CreateElemGeomType()
2438 {
2439   SMESH::ElemGeomType_i* aServant = new SMESH::ElemGeomType_i();
2440   SMESH::ElemGeomType_var anObj = aServant->_this();
2441   TPythonDump()<<aServant<<" = "<<this<<".CreateElemGeomType()";
2442   return anObj._retn();
2443 }
2444
2445 ElemEntityType_ptr FilterManager_i::CreateElemEntityType()
2446 {
2447   SMESH::ElemEntityType_i* aServant = new SMESH::ElemEntityType_i();
2448   SMESH::ElemEntityType_var anObj = aServant->_this();
2449   TPythonDump()<<aServant<<" = "<<this<<".CreateElemEntityType()";
2450   return anObj._retn();
2451 }
2452
2453 Filter_ptr FilterManager_i::CreateFilter()
2454 {
2455   SMESH::Filter_i* aServant = new SMESH::Filter_i();
2456   SMESH::Filter_var anObj = aServant->_this();
2457   TPythonDump()<<aServant<<" = "<<this<<".CreateFilter()";
2458   return anObj._retn();
2459 }
2460
2461 FilterLibrary_ptr FilterManager_i::LoadLibrary( const char* aFileName )
2462 {
2463   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i( aFileName );
2464   SMESH::FilterLibrary_var anObj = aServant->_this();
2465   TPythonDump()<<aServant<<" = "<<this<<".LoadLibrary('"<<aFileName<<"')";
2466   return anObj._retn();
2467 }
2468
2469 FilterLibrary_ptr FilterManager_i::CreateLibrary()
2470 {
2471   SMESH::FilterLibrary_i* aServant = new SMESH::FilterLibrary_i();
2472   SMESH::FilterLibrary_var anObj = aServant->_this();
2473   TPythonDump()<<aServant<<" = "<<this<<".CreateLibrary()";
2474   return anObj._retn();
2475 }
2476
2477 CORBA::Boolean FilterManager_i::DeleteLibrary( const char* aFileName )
2478 {
2479   TPythonDump()<<this<<".DeleteLibrary("<<aFileName<<")";
2480   return remove( aFileName ) ? false : true;
2481 }
2482
2483 //=============================================================================
2484 /*!
2485  *  SMESH_Gen_i::CreateFilterManager
2486  *
2487  *  Create filter manager
2488  */
2489 //=============================================================================
2490
2491 SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
2492 {
2493   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2494   SMESH::FilterManager_var anObj = aFilter->_this();
2495   return anObj._retn();
2496 }
2497
2498
2499 /*
2500                               FILTER
2501 */
2502
2503 //=======================================================================
2504 // name    : Filter_i::Filter_i
2505 // Purpose : Constructor
2506 //=======================================================================
2507 Filter_i::Filter_i()
2508 : myPredicate( NULL )
2509 {}
2510
2511 //=======================================================================
2512 // name    : Filter_i::~Filter_i
2513 // Purpose : Destructor
2514 //=======================================================================
2515 Filter_i::~Filter_i()
2516 {
2517   if ( myPredicate )
2518     myPredicate->UnRegister();
2519
2520   if(!CORBA::is_nil(myMesh))
2521     myMesh->UnRegister();
2522
2523   myPredicate = 0;
2524   FindBaseObjects();
2525 }
2526
2527 //=======================================================================
2528 // name    : Filter_i::SetPredicate
2529 // Purpose : Set predicate
2530 //=======================================================================
2531 void Filter_i::SetPredicate( Predicate_ptr thePredicate )
2532 {
2533   if ( myPredicate )
2534     myPredicate->UnRegister();
2535
2536   myPredicate = SMESH::GetPredicate(thePredicate);
2537
2538   if ( myPredicate )
2539   {
2540     myFilter.SetPredicate( myPredicate->GetPredicate() );
2541     myPredicate->Register();
2542     if ( const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(myMesh))
2543       myPredicate->GetPredicate()->SetMesh( aMesh );
2544     TPythonDump()<<this<<".SetPredicate("<<myPredicate<<")";
2545   }
2546   NotifyerAndWaiter::Modified();
2547 }
2548
2549 //=======================================================================
2550 // name    : Filter_i::GetElementType
2551 // Purpose : Get entity type
2552 //=======================================================================
2553 SMESH::ElementType Filter_i::GetElementType()
2554 {
2555   return myPredicate != 0 ? myPredicate->GetElementType() : SMESH::ALL;
2556 }
2557
2558 //=======================================================================
2559 // name    : Filter_i::SetMesh
2560 // Purpose : Set mesh
2561 //=======================================================================
2562 void
2563 Filter_i::
2564 SetMesh( SMESH_Mesh_ptr theMesh )
2565 {
2566   if(!CORBA::is_nil(theMesh))
2567     theMesh->Register();
2568
2569   if(!CORBA::is_nil(myMesh))
2570     myMesh->UnRegister();
2571
2572   myMesh = SMESH_Mesh::_duplicate( theMesh );
2573   TPythonDump()<<this<<".SetMesh("<<theMesh<<")";
2574
2575   if ( myPredicate )
2576     if ( const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh))
2577       myPredicate->GetPredicate()->SetMesh( aMesh );
2578 }
2579
2580 SMESH::long_array*
2581 Filter_i::
2582 GetIDs()
2583 {
2584   return GetElementsId(myMesh);
2585 }
2586
2587 //=======================================================================
2588 // name    : Filter_i::GetElementsId
2589 // Purpose : Get ids of entities
2590 //=======================================================================
2591 void
2592 Filter_i::
2593 GetElementsId( Predicate_i* thePredicate,
2594                const SMDS_Mesh* theMesh,
2595                Controls::Filter::TIdSequence& theSequence )
2596 {
2597   if (thePredicate)
2598     Controls::Filter::GetElementsId(theMesh,thePredicate->GetPredicate(),theSequence);
2599 }
2600
2601 void
2602 Filter_i::
2603 GetElementsId( Predicate_i* thePredicate,
2604                SMESH_Mesh_ptr theMesh,
2605                Controls::Filter::TIdSequence& theSequence )
2606 {
2607   if (thePredicate) 
2608     if(const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(theMesh))
2609       Controls::Filter::GetElementsId(aMesh,thePredicate->GetPredicate(),theSequence);
2610 }
2611
2612 SMESH::long_array*
2613 Filter_i::
2614 GetElementsId( SMESH_Mesh_ptr theMesh )
2615 {
2616   SMESH::long_array_var anArray = new SMESH::long_array;
2617   if(!CORBA::is_nil(theMesh) && myPredicate){
2618     theMesh->Load();
2619     Controls::Filter::TIdSequence aSequence;
2620     GetElementsId(myPredicate,theMesh,aSequence);
2621     long i = 0, iEnd = aSequence.size();
2622     anArray->length( iEnd );
2623     for ( ; i < iEnd; i++ )
2624       anArray[ i ] = aSequence[i];
2625   }
2626   return anArray._retn();
2627 }
2628
2629 SMESH::long_array*
2630 Filter_i::
2631 GetElementsIdFromParts( const ListOfIDSources& theParts )
2632 {
2633   SMESH::long_array_var array = new SMESH::long_array;
2634   if ( theParts.length() > 0 && myPredicate )
2635   {
2636     SMESH_Mesh_ptr mesh = theParts[0]->GetMesh();
2637     mesh->Load();
2638     const SMDS_Mesh* meshDS = MeshPtr2SMDSMesh( mesh );
2639     Controls::Filter::TIdSequence totalSequence;
2640     for ( CORBA::ULong i = 0; i < theParts.length(); ++i )
2641     {
2642       if ( SMESH::Filter_i* filter = SMESH::DownCast<SMESH::Filter_i*>( theParts[i] ))
2643         filter->SetMesh( mesh );
2644       SMDS_ElemIteratorPtr iter = SMESH_Mesh_i::GetElements( theParts[i], GetElementType() );
2645       if ( iter && meshDS )
2646       {
2647         Controls::Filter::TIdSequence sequence;
2648         Controls::Filter::GetElementsId( meshDS, myPredicate->GetPredicate(), sequence, iter );
2649         totalSequence.insert( totalSequence.end(), sequence.begin(), sequence.end() );
2650       }
2651     }
2652     array->length( totalSequence.size() );
2653     for ( size_t i = 0; i < totalSequence.size(); ++i )
2654       array[ i ] = totalSequence[ i ];
2655   }
2656   return array._retn();
2657 }
2658
2659 //=============================================================================
2660 /*!
2661  * \brief Returns number of mesh elements per each \a EntityType
2662  */
2663 //=============================================================================
2664
2665 SMESH::long_array* ::Filter_i::GetMeshInfo()
2666 {
2667   SMESH::long_array_var aRes = new SMESH::long_array();
2668   aRes->length(SMESH::Entity_Last);
2669   for (int i = 0; i < SMESH::Entity_Last; i++)
2670     aRes[i] = 0;
2671
2672   if ( !CORBA::is_nil(myMesh) && myPredicate )
2673   {
2674     const SMDS_Mesh*  aMesh = MeshPtr2SMDSMesh(myMesh);
2675     SMDS_ElemIteratorPtr it = aMesh->elementsIterator( SMDSAbs_ElementType( GetElementType() ));
2676     while ( it->more() )
2677     {
2678       const SMDS_MeshElement* anElem = it->next();
2679       if ( myPredicate->IsSatisfy( anElem->GetID() ) )
2680         aRes[ anElem->GetEntityType() ]++;
2681     }
2682   }
2683
2684   return aRes._retn();  
2685 }
2686
2687 //=============================================================================
2688 /*!
2689  * \brief Returns number of mesh elements of each \a ElementType
2690  */
2691 //=============================================================================
2692
2693 SMESH::long_array* ::Filter_i::GetNbElementsByType()
2694 {
2695   SMESH::long_array_var aRes = new SMESH::long_array();
2696   aRes->length(SMESH::NB_ELEMENT_TYPES);
2697   for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
2698     aRes[i] = 0;
2699
2700   if ( !CORBA::is_nil(myMesh) && myPredicate ) {
2701     const SMDS_Mesh*  aMesh = MeshPtr2SMDSMesh(myMesh);
2702     SMDS_ElemIteratorPtr it = aMesh->elementsIterator( SMDSAbs_ElementType( GetElementType() ));
2703     CORBA::Long& nbElems = aRes[ GetElementType() ];
2704     while ( it->more() )
2705     {
2706       const SMDS_MeshElement* anElem = it->next();
2707       if ( myPredicate->IsSatisfy( anElem->GetID() ) )
2708         nbElems++;
2709     }
2710   }
2711
2712   return aRes._retn();  
2713 }
2714
2715
2716 //================================================================================
2717 /*!
2718  * \brief Return GetElementType() within an array
2719  * Implement SMESH_IDSource interface
2720  */
2721 //================================================================================
2722
2723 SMESH::array_of_ElementType* Filter_i::GetTypes()
2724 {
2725   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
2726
2727   // check if any element passes through the filter
2728   if ( !CORBA::is_nil(myMesh) && myPredicate )
2729   {
2730     const SMDS_Mesh* aMesh = MeshPtr2SMDSMesh(myMesh);
2731     SMDS_ElemIteratorPtr it = aMesh->elementsIterator( SMDSAbs_ElementType( GetElementType() ));
2732     bool satisfies = false;
2733     while ( !satisfies && it->more() )
2734       satisfies = myPredicate->IsSatisfy( it->next()->GetID() );
2735     if ( satisfies ) {
2736       types->length( 1 );
2737       types[0] = GetElementType();
2738     }
2739   }
2740   return types._retn();
2741 }
2742
2743 //=======================================================================
2744 //function : GetMesh
2745 //purpose  : Returns mesh
2746 //=======================================================================
2747
2748 SMESH::SMESH_Mesh_ptr Filter_i::GetMesh()
2749 {
2750   return SMESH_Mesh::_duplicate( myMesh );
2751 }
2752
2753 //=======================================================================
2754 //function : GetVtkUgStream
2755 //purpose  : Return data vtk unstructured grid (not implemented)
2756 //=======================================================================
2757
2758 SALOMEDS::TMPFile* Filter_i::GetVtkUgStream()
2759 {
2760   SALOMEDS::TMPFile_var SeqFile;
2761   return SeqFile._retn();
2762 }
2763 //=======================================================================
2764 // name    : getCriteria
2765 // Purpose : Retrieve criterions from predicate
2766 //=======================================================================
2767 static inline void getPrediacates( Predicate_i*                thePred,
2768                                    std::vector<Predicate_i*> & thePredVec )
2769 {
2770   const int aFType = thePred->GetFunctorType();
2771
2772   switch ( aFType )
2773   {
2774   case FT_LogicalNOT:
2775   {
2776     Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
2777     getPrediacates( aPred, thePredVec );
2778     break;
2779   }
2780   case FT_LogicalAND:
2781   case FT_LogicalOR:
2782   {
2783     Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
2784     Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
2785     getPrediacates( aPred1, thePredVec );
2786     getPrediacates( aPred2, thePredVec );
2787     break;
2788    }
2789   default:;
2790   }
2791   thePredVec.push_back( thePred );
2792 }
2793
2794 //=======================================================================
2795 // name    : getCriteria
2796 // Purpose : Retrieve criterions from predicate
2797 //=======================================================================
2798 static inline bool getCriteria( Predicate_i*                thePred,
2799                                 SMESH::Filter::Criteria_out theCriteria )
2800 {
2801   const int aFType = thePred->GetFunctorType();
2802
2803   switch ( aFType )
2804   {
2805   case FT_LogicalNOT:
2806     {
2807       Predicate_i* aPred = ( dynamic_cast<LogicalNOT_i*>( thePred ) )->GetPredicate_i();
2808       getCriteria( aPred, theCriteria );
2809       theCriteria[ theCriteria->length() - 1 ].UnaryOp = FT_LogicalNOT;
2810     }
2811     return true;
2812
2813   case FT_LogicalAND:
2814   case FT_LogicalOR:
2815     {
2816       Predicate_i* aPred1 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate1_i();
2817       Predicate_i* aPred2 = ( dynamic_cast<LogicalBinary_i*>( thePred ) )->GetPredicate2_i();
2818       if ( !getCriteria( aPred1, theCriteria ) )
2819         return false;
2820       theCriteria[ theCriteria->length() - 1 ].BinaryOp = aFType;
2821       return getCriteria( aPred2, theCriteria );
2822     }
2823   case FT_Undefined:
2824     return false;
2825   }
2826
2827   // resize theCriteria
2828   CORBA::ULong i = theCriteria->length();
2829   theCriteria->length( i + 1 );
2830   theCriteria[ i ] = createCriterion();
2831
2832   // set members of the added Criterion
2833
2834   theCriteria[ i ].Type = aFType;
2835   theCriteria[ i ].TypeOfElement = thePred->GetElementType();
2836
2837   switch ( aFType )
2838   {
2839   case FT_FreeBorders:
2840   case FT_FreeEdges:
2841   case FT_FreeFaces:
2842   case FT_LinearOrQuadratic:
2843   case FT_FreeNodes:
2844   case FT_EqualEdges:
2845   case FT_EqualFaces:
2846   case FT_EqualVolumes:
2847   case FT_BadOrientedVolume:
2848   case FT_BareBorderVolume:
2849   case FT_BareBorderFace:
2850   case FT_OverConstrainedVolume:
2851   case FT_OverConstrainedFace:
2852     {
2853       return true;
2854     }
2855   case FT_BelongToMeshGroup:
2856     {
2857       BelongToMeshGroup_i* aPred = dynamic_cast<BelongToMeshGroup_i*>( thePred );
2858       SMESH::SMESH_GroupBase_var grp = aPred->GetGroup();
2859       if ( !grp->_is_nil() )
2860       {
2861         theCriteria[ i ].ThresholdStr = grp->GetName();
2862         theCriteria[ i ].ThresholdID  = aPred->GetGroupID().c_str();
2863       }
2864       return true;
2865     }
2866   case FT_BelongToGeom:
2867     {
2868       BelongToGeom_i* aPred = dynamic_cast<BelongToGeom_i*>( thePred );
2869       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2870       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2871       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2872       return true;
2873     }
2874   case FT_BelongToPlane:
2875   case FT_BelongToCylinder:
2876   case FT_BelongToGenSurface:
2877     {
2878       BelongToSurface_i* aPred = dynamic_cast<BelongToSurface_i*>( thePred );
2879       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2880       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2881       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2882       return true;
2883     }
2884   case FT_LyingOnGeom:
2885     {
2886       LyingOnGeom_i* aPred = dynamic_cast<LyingOnGeom_i*>( thePred );
2887       theCriteria[ i ].ThresholdStr  = aPred->GetShapeName();
2888       theCriteria[ i ].ThresholdID   = aPred->GetShapeID();
2889       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2890       return true;
2891     }
2892   case FT_CoplanarFaces:
2893     {
2894       CoplanarFaces_i* aPred = dynamic_cast<CoplanarFaces_i*>( thePred );
2895       theCriteria[ i ].ThresholdID   = aPred->GetFaceAsString();
2896       theCriteria[ i ].Tolerance     = aPred->GetTolerance();
2897       return true;
2898     }
2899   case FT_ConnectedElements:
2900     {
2901       ConnectedElements_i* aPred = dynamic_cast<ConnectedElements_i*>( thePred );
2902       SMESH::ConnectedElements::ThresholdType type;
2903       CORBA::String_var threshold = aPred->GetThreshold( type );
2904       switch ( type ) {
2905       case SMESH::ConnectedElements::POINT:
2906         theCriteria[ i ].ThresholdStr = threshold; break;
2907       case SMESH::ConnectedElements::VERTEX:
2908         theCriteria[ i ].ThresholdID = threshold; break;
2909       case SMESH::ConnectedElements::NODE:
2910         theCriteria[ i ].Threshold = atof( threshold.in() ); break;
2911       default:;
2912       }
2913       return true;
2914     }
2915   case FT_EqualNodes:
2916     {
2917       EqualNodes_i* aPred = dynamic_cast<EqualNodes_i*>( thePred );
2918       theCriteria[ i ].Tolerance  = aPred->GetTolerance();
2919       return true;
2920     }
2921   case FT_RangeOfIds:
2922     {
2923       RangeOfIds_i* aPred = dynamic_cast<RangeOfIds_i*>( thePred );
2924       theCriteria[ i ].ThresholdStr  = aPred->GetRangeStr();
2925       return true;
2926     }
2927   case FT_LessThan:
2928   case FT_MoreThan:
2929   case FT_EqualTo:
2930     {
2931       Comparator_i* aCompar = dynamic_cast<Comparator_i*>( thePred );
2932       theCriteria[ i ].Type      = aCompar->GetNumFunctor_i()->GetFunctorType();
2933       theCriteria[ i ].Compare   = aFType;
2934       theCriteria[ i ].Threshold = aCompar->GetMargin();
2935       if ( aFType == FT_EqualTo )
2936       {
2937         EqualTo_i* aCompar = dynamic_cast<EqualTo_i*>( thePred );
2938         theCriteria[ i ].Tolerance = aCompar->GetTolerance();
2939       }
2940       return true;
2941     }
2942   case FT_GroupColor:
2943     {
2944       GroupColor_i* aPred = dynamic_cast<GroupColor_i*>( thePred );
2945       theCriteria[ i ].ThresholdStr = aPred->GetColorStr();
2946       return true;
2947     }
2948   case FT_ElemGeomType:
2949     {
2950       ElemGeomType_i* aPred = dynamic_cast<ElemGeomType_i*>( thePred );
2951       theCriteria[ i ].Threshold = (double)aPred->GetGeometryType();
2952       return true;
2953     }
2954   case FT_EntityType:
2955     {
2956       ElemEntityType_i* aPred = dynamic_cast<ElemEntityType_i*>( thePred );
2957       theCriteria[ i ].Threshold = (double)aPred->GetEntityType();
2958       return true;
2959     }
2960   default:
2961     return false;
2962   }
2963 }
2964
2965 //=======================================================================
2966 // name    : Filter_i::GetCriteria
2967 // Purpose : Retrieve criterions from predicate
2968 //=======================================================================
2969 CORBA::Boolean Filter_i::GetCriteria( SMESH::Filter::Criteria_out theCriteria )
2970 {
2971   theCriteria = new SMESH::Filter::Criteria;
2972   return myPredicate != 0 ? getCriteria( myPredicate, theCriteria ) : true;
2973 }
2974
2975 //=======================================================================
2976 // name    : Filter_i::SetCriteria
2977 // Purpose : Create new predicate and set criterions in it
2978 //=======================================================================
2979 CORBA::Boolean Filter_i::SetCriteria( const SMESH::Filter::Criteria& theCriteria )
2980 {
2981   SetPredicate( SMESH::Predicate::_nil() );
2982
2983   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
2984   FilterManager_ptr aFilterMgr = aFilter->_this();
2985
2986   // CREATE two lists ( PREDICATES  and LOG OP )
2987
2988   // Criterion
2989   TPythonDump()<<"aCriteria = []";
2990   std::list<SMESH::Predicate_ptr> aPredicates;
2991   std::list<int>                  aBinaries;
2992   for ( int i = 0, n = theCriteria.length(); i < n; i++ )
2993   {
2994     int         aCriterion    = theCriteria[ i ].Type;
2995     int         aCompare      = theCriteria[ i ].Compare;
2996     double      aThreshold    = theCriteria[ i ].Threshold;
2997     const char* aThresholdStr = theCriteria[ i ].ThresholdStr;
2998     const char* aThresholdID  = theCriteria[ i ].ThresholdID;
2999     int         aUnary        = theCriteria[ i ].UnaryOp;
3000     int         aBinary       = theCriteria[ i ].BinaryOp;
3001     double      aTolerance    = theCriteria[ i ].Tolerance;
3002     ElementType aTypeOfElem   = theCriteria[ i ].TypeOfElement;
3003     long        aPrecision    = theCriteria[ i ].Precision;
3004
3005     {
3006       TPythonDump pd;
3007       pd << "aCriterion = SMESH.Filter.Criterion("
3008          << aCriterion    << ", "
3009          << aCompare      << ", "
3010          << aThreshold    << ", '"
3011          << aThresholdStr << "', '"
3012          << aThresholdID  << "', "
3013          << aUnary        << ", "
3014          << aBinary       << ", "
3015          << aTolerance    << ", "
3016          << aTypeOfElem   << ", "
3017          << aPrecision    << ")";
3018     }
3019     TPythonDump pd;
3020
3021     SMESH::Predicate_ptr aPredicate = SMESH::Predicate::_nil();
3022     SMESH::NumericalFunctor_ptr aFunctor = SMESH::NumericalFunctor::_nil();
3023
3024     switch ( aCriterion )
3025     {
3026       // Functors
3027
3028       case SMESH::FT_MultiConnection:
3029         aFunctor = aFilterMgr->CreateMultiConnection();
3030         break;
3031       case SMESH::FT_MultiConnection2D:
3032         aFunctor = aFilterMgr->CreateMultiConnection2D();
3033         break;
3034       case SMESH::FT_Length:
3035         aFunctor = aFilterMgr->CreateLength();
3036         break;
3037       case SMESH::FT_Length2D:
3038         aFunctor = aFilterMgr->CreateLength2D();
3039         break;
3040       case SMESH::FT_Length3D:
3041         aFunctor = aFilterMgr->CreateLength3D();
3042         break;
3043       case SMESH::FT_Deflection2D:
3044         aFunctor = aFilterMgr->CreateDeflection2D();
3045         break;
3046       case SMESH::FT_AspectRatio:
3047         aFunctor = aFilterMgr->CreateAspectRatio();
3048         break;
3049       case SMESH::FT_AspectRatio3D:
3050         aFunctor = aFilterMgr->CreateAspectRatio3D();
3051         break;
3052       case SMESH::FT_Warping:
3053         aFunctor = aFilterMgr->CreateWarping();
3054         break;
3055       case SMESH::FT_MinimumAngle:
3056         aFunctor = aFilterMgr->CreateMinimumAngle();
3057         break;
3058       case SMESH::FT_Taper:
3059         aFunctor = aFilterMgr->CreateTaper();
3060         break;
3061       case SMESH::FT_Skew:
3062         aFunctor = aFilterMgr->CreateSkew();
3063         break;
3064       case SMESH::FT_Area:
3065         aFunctor = aFilterMgr->CreateArea();
3066         break;
3067       case SMESH::FT_Volume3D:
3068         aFunctor = aFilterMgr->CreateVolume3D();
3069         break;
3070       case SMESH::FT_MaxElementLength2D:
3071         aFunctor = aFilterMgr->CreateMaxElementLength2D();
3072         break;
3073       case SMESH::FT_MaxElementLength3D:
3074         aFunctor = aFilterMgr->CreateMaxElementLength3D();
3075         break;
3076       case SMESH::FT_BallDiameter:
3077         aFunctor = aFilterMgr->CreateBallDiameter();
3078         break;
3079       case SMESH::FT_NodeConnectivityNumber:
3080         aFunctor = aFilterMgr->CreateNodeConnectivityNumber();
3081         break;
3082
3083       // Predicates
3084
3085       case SMESH::FT_FreeBorders:
3086         aPredicate = aFilterMgr->CreateFreeBorders();
3087         break;
3088       case SMESH::FT_FreeEdges:
3089         aPredicate = aFilterMgr->CreateFreeEdges();
3090         break;
3091       case SMESH::FT_FreeFaces:
3092         aPredicate = aFilterMgr->CreateFreeFaces();
3093         break;
3094       case SMESH::FT_FreeNodes:
3095         aPredicate = aFilterMgr->CreateFreeNodes();
3096         break;
3097       case SMESH::FT_EqualNodes:
3098         {
3099           SMESH::EqualNodes_ptr pred = aFilterMgr->CreateEqualNodes();
3100           pred->SetTolerance( aTolerance );
3101           aPredicate = pred;
3102           break;
3103         }
3104       case SMESH::FT_EqualEdges:
3105         aPredicate = aFilterMgr->CreateEqualEdges();
3106         break;
3107       case SMESH::FT_EqualFaces:
3108         aPredicate = aFilterMgr->CreateEqualFaces();
3109         break;
3110       case SMESH::FT_EqualVolumes:
3111         aPredicate = aFilterMgr->CreateEqualVolumes();
3112         break;
3113       case SMESH::FT_BelongToMeshGroup:
3114         {
3115           SMESH::BelongToMeshGroup_ptr tmpPred = aFilterMgr->CreateBelongToMeshGroup();
3116           tmpPred->SetGroupID( aThresholdID );
3117           aPredicate = tmpPred;
3118         }
3119         break;
3120       case SMESH::FT_BelongToGeom:
3121         {
3122           SMESH::BelongToGeom_ptr tmpPred = aFilterMgr->CreateBelongToGeom();
3123           tmpPred->SetElementType( aTypeOfElem );
3124           tmpPred->SetShape( aThresholdID, aThresholdStr );
3125           tmpPred->SetTolerance( aTolerance );
3126           aPredicate = tmpPred;
3127         }
3128         break;
3129       case SMESH::FT_BelongToPlane:
3130       case SMESH::FT_BelongToCylinder:
3131       case SMESH::FT_BelongToGenSurface:
3132         {
3133           SMESH::BelongToSurface_ptr tmpPred;
3134           switch ( aCriterion ) {
3135           case SMESH::FT_BelongToPlane:
3136             tmpPred = aFilterMgr->CreateBelongToPlane(); break;
3137           case SMESH::FT_BelongToCylinder:
3138             tmpPred = aFilterMgr->CreateBelongToCylinder(); break;
3139           default:
3140             tmpPred = aFilterMgr->CreateBelongToGenSurface();
3141           }
3142           tmpPred->SetShape( aThresholdID, aThresholdStr, aTypeOfElem );
3143           tmpPred->SetTolerance( aTolerance );
3144           aPredicate = tmpPred;
3145         }
3146         break;
3147       case SMESH::FT_LyingOnGeom:
3148         {
3149           SMESH::LyingOnGeom_ptr tmpPred = aFilterMgr->CreateLyingOnGeom();
3150           tmpPred->SetElementType( aTypeOfElem );
3151           tmpPred->SetShape( aThresholdID, aThresholdStr );
3152           tmpPred->SetTolerance( aTolerance );
3153           aPredicate = tmpPred;
3154         }
3155         break;
3156       case SMESH::FT_RangeOfIds:
3157         {
3158           SMESH::RangeOfIds_ptr tmpPred = aFilterMgr->CreateRangeOfIds();
3159           tmpPred->SetRangeStr( aThresholdStr );
3160           tmpPred->SetElementType( aTypeOfElem );
3161           aPredicate = tmpPred;
3162         }
3163         break;
3164       case SMESH::FT_BadOrientedVolume:
3165         {
3166           aPredicate = aFilterMgr->CreateBadOrientedVolume();
3167         }
3168         break;
3169       case SMESH::FT_BareBorderVolume:
3170         {
3171           aPredicate = aFilterMgr->CreateBareBorderVolume();
3172         }
3173         break;
3174       case SMESH::FT_BareBorderFace:
3175         {
3176           aPredicate = aFilterMgr->CreateBareBorderFace();
3177         }
3178         break;
3179       case SMESH::FT_OverConstrainedVolume:
3180         {
3181           aPredicate = aFilterMgr->CreateOverConstrainedVolume();
3182         }
3183         break;
3184       case SMESH::FT_OverConstrainedFace:
3185         {
3186           aPredicate = aFilterMgr->CreateOverConstrainedFace();
3187         }
3188         break;
3189       case SMESH::FT_LinearOrQuadratic:
3190         {
3191           SMESH::LinearOrQuadratic_ptr tmpPred = aFilterMgr->CreateLinearOrQuadratic();
3192           tmpPred->SetElementType( aTypeOfElem );
3193           aPredicate = tmpPred;
3194           break;
3195         }
3196       case SMESH::FT_GroupColor:
3197         {
3198           SMESH::GroupColor_ptr tmpPred = aFilterMgr->CreateGroupColor();
3199           tmpPred->SetElementType( aTypeOfElem );
3200           tmpPred->SetColorStr( aThresholdStr );
3201           aPredicate = tmpPred;
3202           break;
3203         }
3204       case SMESH::FT_ElemGeomType:
3205         {
3206           SMESH::ElemGeomType_ptr tmpPred = aFilterMgr->CreateElemGeomType();
3207           tmpPred->SetElementType( aTypeOfElem );
3208           tmpPred->SetGeometryType( (GeometryType)(int)(aThreshold + 0.5) );
3209           aPredicate = tmpPred;
3210           break;
3211         }
3212       case SMESH::FT_EntityType:
3213         {
3214           SMESH::ElemEntityType_ptr tmpPred = aFilterMgr->CreateElemEntityType();
3215           tmpPred->SetElementType( aTypeOfElem );
3216           tmpPred->SetEntityType( EntityType( (int (aThreshold + 0.5))));
3217           aPredicate = tmpPred;
3218           break;
3219         }
3220       case SMESH::FT_CoplanarFaces:
3221         {
3222           SMESH::CoplanarFaces_ptr tmpPred = aFilterMgr->CreateCoplanarFaces();
3223           tmpPred->SetFace( atol (aThresholdID ));
3224           tmpPred->SetTolerance( aTolerance );
3225           aPredicate = tmpPred;
3226           break;
3227         }
3228       case SMESH::FT_ConnectedElements:
3229         {
3230           SMESH::ConnectedElements_ptr tmpPred = aFilterMgr->CreateConnectedElements();
3231           if ( strlen( aThresholdID ) > 0 ) // shape ID
3232             tmpPred->SetThreshold( aThresholdID, SMESH::ConnectedElements::VERTEX );
3233           else if ( strlen( aThresholdStr ) > 0 ) // point coords
3234             tmpPred->SetThreshold( aThresholdStr, SMESH::ConnectedElements::POINT );
3235           else if ( aThreshold >= 1 )
3236             tmpPred->SetNode( (CORBA::Long) aThreshold ); // node ID
3237           tmpPred->SetElementType( aTypeOfElem );
3238           aPredicate = tmpPred;
3239           break;
3240         }
3241
3242       default:
3243         continue;
3244     }
3245
3246     // Comparator
3247     if ( !aFunctor->_is_nil() && aPredicate->_is_nil() )
3248     {
3249       SMESH::Comparator_ptr aComparator = SMESH::Comparator::_nil();
3250
3251       if ( aCompare == SMESH::FT_LessThan )
3252         aComparator = aFilterMgr->CreateLessThan();
3253       else if ( aCompare == SMESH::FT_MoreThan )
3254         aComparator = aFilterMgr->CreateMoreThan();
3255       else if ( aCompare == SMESH::FT_EqualTo )
3256         aComparator = aFilterMgr->CreateEqualTo();
3257       else
3258         continue;
3259
3260       aComparator->SetNumFunctor( aFunctor );
3261       aComparator->SetMargin( aThreshold );
3262
3263       if ( aCompare == FT_EqualTo )
3264       {
3265         SMESH::EqualTo_var anEqualTo = SMESH::EqualTo::_narrow( aComparator );
3266         anEqualTo->SetTolerance( aTolerance );
3267       }
3268
3269       aPredicate = aComparator;
3270
3271       aFunctor->SetPrecision( aPrecision );
3272     }
3273
3274     // Logical not
3275     if ( aUnary == FT_LogicalNOT )
3276     {
3277       SMESH::LogicalNOT_ptr aNotPred = aFilterMgr->CreateLogicalNOT();
3278       aNotPred->SetPredicate( aPredicate );
3279       aPredicate = aNotPred;
3280     }
3281
3282     // logical op
3283     aPredicates.push_back( aPredicate );
3284     aBinaries.push_back( aBinary );
3285     pd <<"aCriteria.append(aCriterion)";
3286
3287   } // end of for
3288   TPythonDump pd; pd<<this<<".SetCriteria(aCriteria)";
3289
3290   // CREATE ONE PREDICATE FROM PREVIOUSLY CREATED MAP
3291
3292   // combine all "AND" operations
3293
3294   std::list<SMESH::Predicate_ptr> aResList;
3295
3296   std::list<SMESH::Predicate_ptr>::iterator aPredIter;
3297   std::list<int>::iterator                  aBinaryIter;
3298
3299   SMESH::Predicate_ptr aPrevPredicate = SMESH::Predicate::_nil();
3300   int aPrevBinary = SMESH::FT_Undefined;
3301   if ( !aBinaries.empty() )
3302     aBinaries.back() = SMESH::FT_Undefined;
3303
3304   for ( aPredIter = aPredicates.begin(), aBinaryIter = aBinaries.begin();
3305         aPredIter != aPredicates.end() && aBinaryIter != aBinaries.end();
3306         ++aPredIter, ++aBinaryIter )
3307   {
3308     int aCurrBinary = *aBinaryIter;
3309
3310     SMESH::Predicate_ptr aCurrPred = SMESH::Predicate::_nil();
3311
3312     if ( aPrevBinary == SMESH::FT_LogicalAND )
3313     {
3314
3315       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalAND();
3316       aBinaryPred->SetPredicate1( aPrevPredicate );
3317       aBinaryPred->SetPredicate2( *aPredIter );
3318       aCurrPred = aBinaryPred;
3319     }
3320     else
3321       aCurrPred = *aPredIter;
3322
3323     if ( aCurrBinary != SMESH::FT_LogicalAND )
3324       aResList.push_back( aCurrPred );
3325
3326     aPrevPredicate = aCurrPred;
3327     aPrevBinary = aCurrBinary;
3328   }
3329
3330   // combine all "OR" operations
3331
3332   SMESH::Predicate_ptr aResPredicate = SMESH::Predicate::_nil();
3333
3334   if ( aResList.size() == 1 )
3335     aResPredicate = *aResList.begin();
3336   else if ( aResList.size() > 1 )
3337   {
3338     std::list<SMESH::Predicate_ptr>::iterator anIter = aResList.begin();
3339     aResPredicate = *anIter;
3340     anIter++;
3341     for ( ; anIter != aResList.end(); ++anIter )
3342     {
3343       SMESH::LogicalBinary_ptr aBinaryPred = aFilterMgr->CreateLogicalOR();
3344       aBinaryPred->SetPredicate1( aResPredicate );
3345       aBinaryPred->SetPredicate2( *anIter );
3346       aResPredicate = aBinaryPred;
3347     }
3348   }
3349
3350   SetPredicate( aResPredicate );
3351   if ( !aResPredicate->_is_nil() )
3352     aResPredicate->UnRegister();
3353
3354   return !aResPredicate->_is_nil();
3355 }
3356
3357 //=======================================================================
3358 // name    : Filter_i::GetPredicate_i
3359 // Purpose : Get implementation of predicate
3360 //=======================================================================
3361 Predicate_i* Filter_i::GetPredicate_i()
3362 {
3363   return myPredicate;
3364 }
3365
3366 //=======================================================================
3367 // name    : Filter_i::GetPredicate
3368 // Purpose : Get predicate
3369 //=======================================================================
3370 Predicate_ptr Filter_i::GetPredicate()
3371 {
3372   if ( myPredicate == 0 )
3373     return SMESH::Predicate::_nil();
3374   else
3375   {
3376     SMESH::Predicate_var anObj = myPredicate->_this();
3377     // if ( SMESH::Functor_i* fun = SMESH::DownCast<SMESH::Functor_i*>( anObj ))
3378     //   TPythonDump() << fun << " = " << this << ".GetPredicate()";
3379     return anObj._retn();
3380   }
3381 }
3382
3383 //================================================================================
3384 /*!
3385  * \brief Find groups it depends on
3386  */
3387 //================================================================================
3388
3389 void Filter_i::FindBaseObjects()
3390 {
3391   // release current groups
3392   for ( size_t i = 0; i < myBaseGroups.size(); ++i )
3393     if ( myBaseGroups[i] )
3394     {
3395       myBaseGroups[i]->RemoveModifWaiter( this );
3396       myBaseGroups[i]->UnRegister();
3397     }
3398
3399   // remember new groups
3400   myBaseGroups.clear();
3401   if ( myPredicate )
3402   {
3403     std::vector<Predicate_i*> predicates;
3404     getPrediacates( myPredicate, predicates );
3405     for ( size_t i = 0; i < predicates.size(); ++i )
3406       if ( BelongToMeshGroup_i* bmg = dynamic_cast< BelongToMeshGroup_i* >( predicates[i] ))
3407       {
3408         SMESH::SMESH_GroupBase_var g = bmg->GetGroup();
3409         SMESH_GroupBase_i* g_i = SMESH::DownCast< SMESH_GroupBase_i*>( g );
3410         if ( g_i )
3411         {
3412           g_i->AddModifWaiter( this );
3413           g_i->Register();
3414           myBaseGroups.push_back( g_i );
3415         }
3416       }
3417   }
3418 }
3419
3420 //================================================================================
3421 /*!
3422  * \brief When notified on removal of myBaseGroups[i], remove a reference to a
3423  *        group from a predicate
3424  */
3425 //================================================================================
3426
3427 void Filter_i::OnBaseObjModified(NotifyerAndWaiter* group, bool removed)
3428 {
3429   if ( !removed )
3430     return; // a GroupOnFilter holding this filter is notified automatically
3431
3432   if ( myPredicate )
3433   {
3434     std::vector<Predicate_i*> predicates;
3435     getPrediacates( myPredicate, predicates );
3436     for ( size_t i = 0; i < predicates.size(); ++i )
3437       if ( BelongToMeshGroup_i* bmg = dynamic_cast< BelongToMeshGroup_i* >( predicates[i] ))
3438       {
3439         SMESH::SMESH_GroupBase_var g = bmg->GetGroup();
3440         SMESH_GroupBase_i* g_i = SMESH::DownCast< SMESH_GroupBase_i*>( g );
3441         if ( g_i == group )
3442         {
3443           bmg->SetGroup( SMESH::SMESH_GroupBase::_nil() );
3444           bmg->SetGroupID( "" );
3445         }
3446       }
3447   }
3448
3449   FindBaseObjects(); // release and update myBaseGroups;
3450 }
3451
3452 /*
3453                             FILTER LIBRARY
3454 */
3455
3456 #define ATTR_TYPE          "type"
3457 #define ATTR_COMPARE       "compare"
3458 #define ATTR_THRESHOLD     "threshold"
3459 #define ATTR_UNARY         "unary"
3460 #define ATTR_BINARY        "binary"
3461 #define ATTR_THRESHOLD_STR "threshold_str"
3462 #define ATTR_TOLERANCE     "tolerance"
3463 #define ATTR_ELEMENT_TYPE  "ElementType"
3464
3465 //=======================================================================
3466 // name    : toString
3467 // Purpose : Convert bool to LDOMString
3468 //=======================================================================
3469 static inline LDOMString toString( CORBA::Boolean val )
3470 {
3471   return val ? "logical not" : "";
3472 }
3473
3474 //=======================================================================
3475 // name    : toBool
3476 // Purpose : Convert LDOMString to bool
3477 //=======================================================================
3478 static inline bool toBool( const LDOMString& theStr )
3479 {
3480   return theStr.equals( "logical not" );
3481 }
3482
3483 //=======================================================================
3484 // name    : toString
3485 // Purpose : Convert double to LDOMString
3486 //=======================================================================
3487 static inline LDOMString toString( CORBA::Double val )
3488 {
3489   char a[ 255 ];
3490   sprintf( a, "%e", val );
3491   return LDOMString( a );
3492 }
3493
3494 //=======================================================================
3495 // name    : toDouble
3496 // Purpose : Convert LDOMString to double
3497 //=======================================================================
3498 static inline double toDouble( const LDOMString& theStr )
3499 {
3500   return atof( theStr.GetString() );
3501 }
3502
3503 //=======================================================================
3504 // name    : toString
3505 // Purpose : Convert functor type to LDOMString
3506 //=======================================================================
3507 static inline LDOMString toString( CORBA::Long theType )
3508 {
3509   switch ( theType )
3510   {
3511     case FT_AspectRatio           : return "Aspect ratio";
3512     case FT_Warping               : return "Warping";
3513     case FT_MinimumAngle          : return "Minimum angle";
3514     case FT_Taper                 : return "Taper";
3515     case FT_Skew                  : return "Skew";
3516     case FT_Area                  : return "Area";
3517     case FT_Volume3D              : return "Volume3D";
3518     case FT_MaxElementLength2D    : return "Max element length 2D";
3519     case FT_MaxElementLength3D    : return "Max element length 3D";
3520     case FT_BelongToMeshGroup     : return "Belong to Mesh Group";
3521     case FT_BelongToGeom          : return "Belong to Geom";
3522     case FT_BelongToPlane         : return "Belong to Plane";
3523     case FT_BelongToCylinder      : return "Belong to Cylinder";
3524     case FT_BelongToGenSurface    : return "Belong to Generic Surface";
3525     case FT_LyingOnGeom           : return "Lying on Geom";
3526     case FT_BadOrientedVolume     : return "Bad Oriented Volume";
3527     case FT_BareBorderVolume      : return "Volumes with bare border";
3528     case FT_BareBorderFace        : return "Faces with bare border";
3529     case FT_OverConstrainedVolume : return "Over-constrained Volumes";
3530     case FT_OverConstrainedFace   : return "Over-constrained Faces";
3531     case FT_RangeOfIds            : return "Range of IDs";
3532     case FT_FreeBorders           : return "Free borders";
3533     case FT_FreeEdges             : return "Free edges";
3534     case FT_FreeFaces             : return "Free faces";
3535     case FT_FreeNodes             : return "Free nodes";
3536     case FT_EqualNodes            : return "Equal nodes";
3537     case FT_EqualEdges            : return "Equal edges";
3538     case FT_EqualFaces            : return "Equal faces";
3539     case FT_EqualVolumes          : return "Equal volumes";
3540     case FT_MultiConnection       : return "Borders at multi-connections";
3541     case FT_MultiConnection2D     : return "Borders at multi-connections 2D";
3542     case FT_Length                : return "Length";
3543     case FT_Length2D              : return "Length 2D";
3544     case FT_Length3D              : return "Length 3D";
3545     case FT_Deflection2D          : return "Deflection 2D";
3546     case FT_LessThan              : return "Less than";
3547     case FT_MoreThan              : return "More than";
3548     case FT_EqualTo               : return "Equal to";
3549     case FT_LogicalNOT            : return "Not";
3550     case FT_LogicalAND            : return "And";
3551     case FT_LogicalOR             : return "Or";
3552     case FT_GroupColor            : return "Color of Group";
3553     case FT_LinearOrQuadratic     : return "Linear or Quadratic";
3554     case FT_ElemGeomType          : return "Element geometry type";
3555     case FT_EntityType            : return "Entity type";
3556     case FT_Undefined             : return "";
3557     default                       : return "";
3558   }
3559 }
3560
3561 //=======================================================================
3562 // name    : toFunctorType
3563 // Purpose : Convert LDOMString to functor type
3564 //=======================================================================
3565 static inline SMESH::FunctorType toFunctorType( const LDOMString& theStr )
3566 {
3567   if      ( theStr.equals( "Aspect ratio"                 ) ) return FT_AspectRatio;
3568   else if ( theStr.equals( "Warping"                      ) ) return FT_Warping;
3569   else if ( theStr.equals( "Minimum angle"                ) ) return FT_MinimumAngle;
3570   else if ( theStr.equals( "Taper"                        ) ) return FT_Taper;
3571   else if ( theStr.equals( "Skew"                         ) ) return FT_Skew;
3572   else if ( theStr.equals( "Area"                         ) ) return FT_Area;
3573   else if ( theStr.equals( "Volume3D"                     ) ) return FT_Volume3D;
3574   else if ( theStr.equals( "Max element length 2D"        ) ) return FT_MaxElementLength2D;
3575   else if ( theStr.equals( "Max element length 3D"        ) ) return FT_MaxElementLength3D;
3576   else if ( theStr.equals( "Belong to Mesh Group"         ) ) return FT_BelongToMeshGroup;
3577   else if ( theStr.equals( "Belong to Geom"               ) ) return FT_BelongToGeom;
3578   else if ( theStr.equals( "Belong to Plane"              ) ) return FT_BelongToPlane;
3579   else if ( theStr.equals( "Belong to Cylinder"           ) ) return FT_BelongToCylinder;
3580   else if ( theStr.equals( "Belong to Generic Surface"    ) ) return FT_BelongToGenSurface;
3581   else if ( theStr.equals( "Lying on Geom"                ) ) return FT_LyingOnGeom;
3582   else if ( theStr.equals( "Free borders"                 ) ) return FT_FreeBorders;
3583   else if ( theStr.equals( "Free edges"                   ) ) return FT_FreeEdges;
3584   else if ( theStr.equals( "Free faces"                   ) ) return FT_FreeFaces;
3585   else if ( theStr.equals( "Free nodes"                   ) ) return FT_FreeNodes;
3586   else if ( theStr.equals( "Equal nodes"                  ) ) return FT_EqualNodes;
3587   else if ( theStr.equals( "Equal edges"                  ) ) return FT_EqualEdges;
3588   else if ( theStr.equals( "Equal faces"                  ) ) return FT_EqualFaces;
3589   else if ( theStr.equals( "Equal volumes"                ) ) return FT_EqualVolumes;
3590   else if ( theStr.equals( "Borders at multi-connections" ) ) return FT_MultiConnection;
3591   //  else if ( theStr.equals( "Borders at multi-connections 2D" ) ) return FT_MultiConnection2D;
3592   else if ( theStr.equals( "Length"                       ) ) return FT_Length;
3593   //  else if ( theStr.equals( "Length2D"                     ) ) return FT_Length2D;
3594   //  else if ( theStr.equals( "Length3D"                     ) ) return FT_Length3D;
3595   else if ( theStr.equals( "Deflection"                   ) ) return FT_Deflection2D;
3596   else if ( theStr.equals( "Range of IDs"                 ) ) return FT_RangeOfIds;
3597   else if ( theStr.equals( "Bad Oriented Volume"          ) ) return FT_BadOrientedVolume;
3598   else if ( theStr.equals( "Volumes with bare border"     ) ) return FT_BareBorderVolume;
3599   else if ( theStr.equals( "Faces with bare border"       ) ) return FT_BareBorderFace;
3600   else if ( theStr.equals( "Over-constrained Volumes"     ) ) return FT_OverConstrainedVolume;
3601   else if ( theStr.equals( "Over-constrained Faces"       ) ) return FT_OverConstrainedFace;
3602   else if ( theStr.equals( "Less than"                    ) ) return FT_LessThan;
3603   else if ( theStr.equals( "More than"                    ) ) return FT_MoreThan;
3604   else if ( theStr.equals( "Equal to"                     ) ) return FT_EqualTo;
3605   else if ( theStr.equals( "Not"                          ) ) return FT_LogicalNOT;
3606   else if ( theStr.equals( "And"                          ) ) return FT_LogicalAND;
3607   else if ( theStr.equals( "Or"                           ) ) return FT_LogicalOR;
3608   else if ( theStr.equals( "Color of Group"               ) ) return FT_GroupColor;
3609   else if ( theStr.equals( "Linear or Quadratic"          ) ) return FT_LinearOrQuadratic;
3610   else if ( theStr.equals( "Element geometry type"        ) ) return FT_ElemGeomType;
3611   else if ( theStr.equals( "Entity type"                  ) ) return FT_EntityType;
3612   else if ( theStr.equals( ""                             ) ) return FT_Undefined;
3613   else  return FT_Undefined;
3614 }
3615
3616 //=======================================================================
3617 // name    : toFunctorType
3618 // Purpose : Convert LDOMString to value of ElementType enumeration
3619 //=======================================================================
3620 static inline SMESH::ElementType toElementType( const LDOMString& theStr )
3621 {
3622   if      ( theStr.equals( "NODE"   ) ) return SMESH::NODE;
3623   else if ( theStr.equals( "EDGE"   ) ) return SMESH::EDGE;
3624   else if ( theStr.equals( "FACE"   ) ) return SMESH::FACE;
3625   else if ( theStr.equals( "VOLUME" ) ) return SMESH::VOLUME;
3626   else                                  return SMESH::ALL;
3627 }
3628
3629 //=======================================================================
3630 // name    : toString
3631 // Purpose : Convert ElementType to string
3632 //=======================================================================
3633 static inline LDOMString toString( const SMESH::ElementType theType )
3634 {
3635   switch ( theType )
3636   {
3637     case SMESH::NODE   : return "NODE";
3638     case SMESH::EDGE   : return "EDGE";
3639     case SMESH::FACE   : return "FACE";
3640     case SMESH::VOLUME : return "VOLUME";
3641     case SMESH::ALL    : return "ALL";
3642     default            : return "";
3643   }
3644 }
3645
3646 //=======================================================================
3647 // name    : findFilter
3648 // Purpose : Find filter in document
3649 //=======================================================================
3650 static LDOM_Element findFilter( const char* theFilterName,
3651                                 const LDOM_Document& theDoc,
3652                                 LDOM_Node* theParent = 0 )
3653 {
3654   LDOM_Element aRootElement = theDoc.getDocumentElement();
3655   if ( aRootElement.isNull() || !aRootElement.hasChildNodes() )
3656     return LDOM_Element();
3657
3658   for ( LDOM_Node aTypeNode = aRootElement.getFirstChild();
3659         !aTypeNode.isNull(); aTypeNode = aTypeNode.getNextSibling() )
3660   {
3661     for ( LDOM_Node aFilter = aTypeNode.getFirstChild();
3662           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
3663     {
3664       LDOM_Element* anElem = ( LDOM_Element* )&aFilter;
3665       if ( anElem->getTagName().equals( LDOMString( "filter" ) ) &&
3666            anElem->getAttribute( "name" ).equals( LDOMString( theFilterName ) ) )
3667       {
3668         if ( theParent != 0  )
3669           *theParent = aTypeNode;
3670         return (LDOM_Element&)aFilter;
3671       }
3672     }
3673   }
3674   return LDOM_Element();
3675 }
3676
3677 //=======================================================================
3678 // name    : getSectionName
3679 // Purpose : Get name of section of filters
3680 //=======================================================================
3681 static const char* getSectionName( const ElementType theType )
3682 {
3683   switch ( theType )
3684   {
3685     case SMESH::NODE   : return "Filters for nodes";
3686     case SMESH::EDGE   : return "Filters for edges";
3687     case SMESH::FACE   : return "Filters for faces";
3688     case SMESH::VOLUME : return "Filters for volumes";
3689     case SMESH::ALL    : return "Filters for elements";
3690     default            : return "";
3691   }
3692 }
3693
3694 //=======================================================================
3695 // name    : getSection
3696 // Purpose : Create section for filters corresponding to the entity type
3697 //=======================================================================
3698 static LDOM_Node getSection( const ElementType theType,
3699                              LDOM_Document&    theDoc,
3700                              const bool        toCreate = false )
3701 {
3702   LDOM_Element aRootElement = theDoc.getDocumentElement();
3703   if ( aRootElement.isNull() )
3704     return LDOM_Node();
3705
3706   // Find section
3707   bool anExist = false;
3708   const char* aSectionName = getSectionName( theType );
3709   if ( strcmp( aSectionName, "" ) == 0 )
3710     return LDOM_Node();
3711
3712   LDOM_NodeList aSections = theDoc.getElementsByTagName( "section" );
3713   LDOM_Node aNode;
3714   for ( int i = 0, n = aSections.getLength(); i < n; i++ )
3715   {
3716     aNode = aSections.item( i );
3717     LDOM_Element& anItem = ( LDOM_Element& )aNode;
3718     if ( anItem.getAttribute( "name" ).equals( LDOMString( aSectionName ) ) )
3719     {
3720       anExist = true;
3721       break;
3722     }
3723   }
3724
3725   // Create new section if necessary
3726   if ( !anExist )
3727   {
3728     if ( toCreate )
3729     {
3730       LDOM_Element aNewItem = theDoc.createElement( "section" );
3731       aNewItem.setAttribute( "name", aSectionName );
3732       aRootElement.appendChild( aNewItem );
3733       return aNewItem;
3734     }
3735     else
3736       return LDOM_Node();
3737   }
3738   return
3739     aNode;
3740 }
3741
3742 //=======================================================================
3743 // name    : createFilterItem
3744 // Purpose : Create filter item or LDOM document
3745 //=======================================================================
3746 static LDOM_Element createFilterItem( const char*       theName,
3747                                       SMESH::Filter_ptr theFilter,
3748                                       LDOM_Document&    theDoc )
3749 {
3750   // create new filter in document
3751   LDOM_Element aFilterItem = theDoc.createElement( "filter" );
3752   aFilterItem.setAttribute( "name", theName );
3753
3754   // save filter criterions
3755   SMESH::Filter::Criteria_var aCriteria = new SMESH::Filter::Criteria;
3756
3757   if ( !theFilter->GetCriteria( aCriteria ) )
3758     return LDOM_Element();
3759
3760   for ( CORBA::ULong i = 0, n = aCriteria->length(); i < n; i++ )
3761   {
3762     LDOM_Element aCriterionItem = theDoc.createElement( "criterion" );
3763     
3764     aCriterionItem.setAttribute( ATTR_TYPE         , toString(  aCriteria[ i ].Type) );
3765     aCriterionItem.setAttribute( ATTR_COMPARE      , toString(  aCriteria[ i ].Compare ) );
3766     aCriterionItem.setAttribute( ATTR_THRESHOLD    , toString(  aCriteria[ i ].Threshold ) );
3767     aCriterionItem.setAttribute( ATTR_UNARY        , toString(  aCriteria[ i ].UnaryOp ) );
3768     aCriterionItem.setAttribute( ATTR_BINARY       , toString(  aCriteria[ i ].BinaryOp ) );
3769
3770     aCriterionItem.setAttribute( ATTR_THRESHOLD_STR, (const char*)aCriteria[ i ].ThresholdStr );
3771     aCriterionItem.setAttribute( ATTR_TOLERANCE    , toString( aCriteria[ i ].Tolerance ) );
3772     aCriterionItem.setAttribute( ATTR_ELEMENT_TYPE ,
3773       toString( (SMESH::ElementType)aCriteria[ i ].TypeOfElement ) );
3774
3775     aFilterItem.appendChild( aCriterionItem );
3776   }
3777
3778   return aFilterItem;
3779 }
3780
3781 //=======================================================================
3782 // name    : FilterLibrary_i::FilterLibrary_i
3783 // Purpose : Constructor
3784 //=======================================================================
3785 FilterLibrary_i::FilterLibrary_i( const char* theFileName )
3786 {
3787   myFileName = CORBA::string_dup( theFileName );
3788   SMESH::FilterManager_i* aFilterMgr = new SMESH::FilterManager_i();
3789   myFilterMgr = aFilterMgr->_this();
3790
3791   LDOMParser aParser;
3792
3793   // Try to use existing library file
3794   bool anExists = false;
3795   if ( !aParser.parse( myFileName ) )
3796   {
3797     myDoc = aParser.getDocument();
3798     anExists = true;
3799   }
3800   // Create a new XML document if it doesn't exist
3801   else
3802     myDoc = LDOM_Document::createDocument( LDOMString() );
3803
3804   LDOM_Element aRootElement = myDoc.getDocumentElement();
3805   if ( aRootElement.isNull() )
3806   {
3807     // If the existing document is empty --> try to create a new one
3808     if ( anExists )
3809       myDoc = LDOM_Document::createDocument( LDOMString() );
3810   }
3811 }
3812
3813 //=======================================================================
3814 // name    : FilterLibrary_i::FilterLibrary_i
3815 // Purpose : Constructor
3816 //=======================================================================
3817 FilterLibrary_i::FilterLibrary_i()
3818 {
3819   myFileName = 0;
3820   SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
3821   myFilterMgr = aFilter->_this();
3822
3823   myDoc = LDOM_Document::createDocument( LDOMString() );
3824 }
3825
3826 FilterLibrary_i::~FilterLibrary_i()
3827 {
3828   CORBA::string_free( myFileName );
3829   //TPythonDump()<<this<<".UnRegister()";
3830 }
3831
3832 //=======================================================================
3833 // name    : FilterLibrary_i::Copy
3834 // Purpose : Create filter and initialize it with values from library
3835 //=======================================================================
3836 Filter_ptr FilterLibrary_i::Copy( const char* theFilterName )
3837 {
3838   Filter_ptr aRes = Filter::_nil();
3839   LDOM_Node aFilter = findFilter( theFilterName, myDoc );
3840
3841   if ( aFilter.isNull() )
3842     return aRes;
3843
3844   std::list<SMESH::Filter::Criterion> aCriteria;
3845
3846   for ( LDOM_Node aCritNode = aFilter.getFirstChild();
3847         !aCritNode.isNull() ; aCritNode = aCritNode.getNextSibling() )
3848   {
3849     LDOM_Element* aCrit = (LDOM_Element*)&aCritNode;
3850
3851     const char* aTypeStr      = aCrit->getAttribute( ATTR_TYPE          ).GetString();
3852     const char* aCompareStr   = aCrit->getAttribute( ATTR_COMPARE       ).GetString();
3853     const char* aUnaryStr     = aCrit->getAttribute( ATTR_UNARY         ).GetString();
3854     const char* aBinaryStr    = aCrit->getAttribute( ATTR_BINARY        ).GetString();
3855     const char* anElemTypeStr = aCrit->getAttribute( ATTR_ELEMENT_TYPE  ).GetString();
3856
3857     SMESH::Filter::Criterion aCriterion = createCriterion();
3858
3859     aCriterion.Type          = toFunctorType( aTypeStr );
3860     aCriterion.Compare       = toFunctorType( aCompareStr );
3861     aCriterion.UnaryOp       = toFunctorType( aUnaryStr );
3862     aCriterion.BinaryOp      = toFunctorType( aBinaryStr );
3863
3864     aCriterion.TypeOfElement = toElementType( anElemTypeStr );
3865
3866     LDOMString str = aCrit->getAttribute( ATTR_THRESHOLD );
3867     int val = 0;
3868     aCriterion.Threshold = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3869       ? val : atof( str.GetString() );
3870
3871     str = aCrit->getAttribute( ATTR_TOLERANCE );
3872     aCriterion.Tolerance = str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val )
3873       ? val : atof( str.GetString() );
3874
3875     str = aCrit->getAttribute( ATTR_THRESHOLD_STR );
3876     if ( str.Type() == LDOMBasicString::LDOM_Integer && str.GetInteger( val ) )
3877     {
3878       char a[ 255 ];
3879       sprintf( a, "%d", val );
3880       aCriterion.ThresholdStr = CORBA::string_dup( a );
3881     }
3882     else
3883       aCriterion.ThresholdStr = str.GetString();
3884
3885     aCriteria.push_back( aCriterion );
3886   }
3887
3888   SMESH::Filter::Criteria_var aCriteriaVar = new SMESH::Filter::Criteria;
3889   aCriteriaVar->length( aCriteria.size() );
3890
3891   CORBA::ULong i = 0;
3892   std::list<SMESH::Filter::Criterion>::iterator anIter = aCriteria.begin();
3893
3894   for( ; anIter != aCriteria.end(); ++anIter )
3895     aCriteriaVar[ i++ ] = *anIter;
3896
3897   aRes = myFilterMgr->CreateFilter();
3898   aRes->SetCriteria( aCriteriaVar.inout() );
3899
3900   TPythonDump()<<this<<".Copy('"<<theFilterName<<"')";
3901
3902   return aRes;
3903 }
3904
3905 //=======================================================================
3906 // name    : FilterLibrary_i::SetFileName
3907 // Purpose : Set file name for library
3908 //=======================================================================
3909 void FilterLibrary_i::SetFileName( const char* theFileName )
3910 {
3911   CORBA::string_free( myFileName );
3912   myFileName = CORBA::string_dup( theFileName );
3913   TPythonDump()<<this<<".SetFileName('"<<theFileName<<"')";
3914 }
3915
3916 //=======================================================================
3917 // name    : FilterLibrary_i::GetFileName
3918 // Purpose : Get file name of library
3919 //=======================================================================
3920 char* FilterLibrary_i::GetFileName()
3921 {
3922   return CORBA::string_dup( myFileName );
3923 }
3924
3925 //=======================================================================
3926 // name    : FilterLibrary_i::Add
3927 // Purpose : Add new filter to library
3928 //=======================================================================
3929 CORBA::Boolean FilterLibrary_i::Add( const char* theFilterName, Filter_ptr theFilter )
3930 {
3931   // if filter already in library or entry filter is null do nothing
3932   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3933   if ( !aFilterNode.isNull() || theFilter->_is_nil() )
3934     return false;
3935
3936   // get section corresponding to the filter type
3937   ElementType anEntType = theFilter->GetElementType();
3938
3939   LDOM_Node aSection = getSection( anEntType, myDoc, true );
3940   if ( aSection.isNull() )
3941     return false;
3942
3943   // create filter item
3944   LDOM_Element aFilterItem = createFilterItem( theFilterName, theFilter, myDoc );
3945   if ( aFilterItem.isNull() )
3946     return false;
3947   else
3948   {
3949     aSection.appendChild( aFilterItem );
3950     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
3951       TPythonDump()<<this<<".Add('"<<theFilterName<<"',"<<aFilter<<")";
3952     return true;
3953   }
3954 }
3955
3956 //=======================================================================
3957 // name    : FilterLibrary_i::Add
3958 // Purpose : Add new filter to library
3959 //=======================================================================
3960 CORBA::Boolean FilterLibrary_i::AddEmpty( const char* theFilterName, ElementType theType )
3961 {
3962   // if filter already in library or entry filter is null do nothing
3963   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc );
3964   if ( !aFilterNode.isNull() )
3965     return false;
3966
3967   LDOM_Node aSection = getSection( theType, myDoc, true );
3968   if ( aSection.isNull() )
3969     return false;
3970
3971   // create filter item
3972   Filter_var aFilter = myFilterMgr->CreateFilter();
3973
3974   LDOM_Element aFilterItem = createFilterItem( theFilterName, aFilter, myDoc );
3975   if ( aFilterItem.isNull() )
3976     return false;
3977   else
3978   {
3979     aSection.appendChild( aFilterItem );
3980     TPythonDump()<<this<<".AddEmpty('"<<theFilterName<<"',"<<theType<<")";
3981     return true;
3982   }
3983 }
3984
3985 //=======================================================================
3986 // name    : FilterLibrary_i::Delete
3987 // Purpose : Delete filter from library
3988 //=======================================================================
3989 CORBA::Boolean FilterLibrary_i::Delete ( const char* theFilterName )
3990 {
3991   LDOM_Node aParentNode;
3992   LDOM_Node aFilterNode = findFilter( theFilterName, myDoc, &aParentNode );
3993   if ( aFilterNode.isNull() || aParentNode.isNull() )
3994     return false;
3995
3996   aParentNode.removeChild( aFilterNode );
3997   TPythonDump()<<this<<".Delete('"<<theFilterName<<"')";
3998   return true;
3999 }
4000
4001 //=======================================================================
4002 // name      : FilterLibrary_i::Replace
4003 // Purpose   : Replace existing filter with entry filter.
4004 // IMPORTANT : If filter does not exist it is not created
4005 //=======================================================================
4006 CORBA::Boolean FilterLibrary_i::Replace( const char* theFilterName,
4007                                          const char* theNewName,
4008                                          Filter_ptr  theFilter )
4009 {
4010   LDOM_Element aFilterItem = findFilter( theFilterName, myDoc );
4011   if ( aFilterItem.isNull() || theFilter->_is_nil() )
4012     return false;
4013
4014   LDOM_Element aNewItem = createFilterItem( theNewName, theFilter, myDoc );
4015   if ( aNewItem.isNull() )
4016     return false;
4017   else
4018   {
4019     aFilterItem.ReplaceElement( aNewItem );
4020     if(Filter_i* aFilter = DownCast<Filter_i*>(theFilter))
4021       TPythonDump()<<this<<".Replace('"<<theFilterName<<"','"<<theNewName<<"',"<<aFilter<<")";
4022     return true;
4023   }
4024 }
4025
4026 //=======================================================================
4027 // name    : FilterLibrary_i::Save
4028 // Purpose : Save library on disk
4029 //=======================================================================
4030 CORBA::Boolean FilterLibrary_i::Save()
4031 {
4032   if ( myFileName == 0 || strlen( myFileName ) == 0 )
4033     return false;
4034
4035   std::filebuf fb;
4036   fb.open( myFileName, std::ios::out );
4037
4038   Standard_OStream os( &fb );
4039
4040   LDOM_XmlWriter aWriter;
4041   aWriter.SetIndentation( 2 );
4042   aWriter.Write( os, myDoc );
4043   fb.close();
4044
4045   TPythonDump()<<this<<".Save()";
4046   return true;
4047 }
4048
4049 //=======================================================================
4050 // name    : FilterLibrary_i::SaveAs
4051 // Purpose : Save library on disk
4052 //=======================================================================
4053 CORBA::Boolean FilterLibrary_i::SaveAs( const char* aFileName )
4054 {
4055   myFileName = strdup ( aFileName );
4056   TPythonDump()<<this<<".SaveAs('"<<aFileName<<"')";
4057   return Save();
4058 }
4059
4060 //=======================================================================
4061 // name    : FilterLibrary_i::IsPresent
4062 // Purpose : Verify whether filter is in library
4063 //=======================================================================
4064 CORBA::Boolean FilterLibrary_i::IsPresent( const char* theFilterName )
4065 {
4066   return !findFilter( theFilterName, myDoc ).isNull();
4067 }
4068
4069 //=======================================================================
4070 // name    : FilterLibrary_i::NbFilters
4071 // Purpose : Return amount of filters in library
4072 //=======================================================================
4073 CORBA::Long FilterLibrary_i::NbFilters( ElementType theType )
4074 {
4075   string_array_var aNames = GetNames( theType );
4076   return aNames->length();
4077 }
4078
4079 //=======================================================================
4080 // name    : FilterLibrary_i::GetNames
4081 // Purpose : Get names of filters from library
4082 //=======================================================================
4083 string_array* FilterLibrary_i::GetNames( ElementType theType )
4084 {
4085   string_array_var anArray = new string_array;
4086   TColStd_SequenceOfHAsciiString aSeq;
4087
4088   LDOM_Node aSection = getSection( theType, myDoc, false );
4089
4090   if ( !aSection.isNull() )
4091   {
4092     for ( LDOM_Node aFilter = aSection.getFirstChild();
4093           !aFilter.isNull(); aFilter = aFilter.getNextSibling() )
4094     {
4095       LDOM_Element& anElem = ( LDOM_Element& )aFilter;
4096       aSeq.Append( new TCollection_HAsciiString(
4097          (Standard_CString)anElem.getAttribute( "name" ).GetString() ) );
4098     }
4099   }
4100
4101   anArray->length( aSeq.Length() );
4102   for ( int i = 1, n = aSeq.Length(); i <= n; i++ )
4103     anArray[ i - 1 ] = CORBA::string_dup( aSeq( i )->ToCString() );
4104
4105   return anArray._retn();
4106 }
4107
4108 //=======================================================================
4109 // name    : FilterLibrary_i::GetAllNames
4110 // Purpose : Get names of filters from library
4111 //=======================================================================
4112 string_array* FilterLibrary_i::GetAllNames()
4113 {
4114   string_array_var aResArray = new string_array;
4115   for ( int type = SMESH::ALL; type <= SMESH::VOLUME; type++ )
4116   {
4117     SMESH::string_array_var aNames = GetNames( (SMESH::ElementType)type );
4118
4119     int aPrevLength = aResArray->length();
4120     aResArray->length( aPrevLength + aNames->length() );
4121     for ( int i = 0, n = aNames->length(); i < n; i++ )
4122       aResArray[ aPrevLength + i ] = aNames[ i ];
4123   }
4124
4125   return aResArray._retn();
4126 }
4127
4128 //================================================================================
4129 /*!
4130  * \brief Return an array of strings corresponding to items of enum FunctorType
4131  */
4132 //================================================================================
4133
4134 static const char** getFunctNames()
4135 {
4136   static const char* functName[] = {
4137     // IT's necessary to update this array according to enum FunctorType (SMESH_Filter.idl)
4138     // The order is IMPORTANT !!!
4139     "FT_AspectRatio",
4140     "FT_AspectRatio3D",
4141     "FT_Warping",
4142     "FT_MinimumAngle",
4143     "FT_Taper",
4144     "FT_Skew",
4145     "FT_Area",
4146     "FT_Volume3D",
4147     "FT_MaxElementLength2D",
4148     "FT_MaxElementLength3D",
4149     "FT_FreeBorders",
4150     "FT_FreeEdges",
4151     "FT_FreeNodes",
4152     "FT_FreeFaces",
4153     "FT_EqualNodes",
4154     "FT_EqualEdges",
4155     "FT_EqualFaces",
4156     "FT_EqualVolumes",
4157     "FT_MultiConnection",
4158     "FT_MultiConnection2D",
4159     "FT_Length",
4160     "FT_Length2D",
4161     "FT_Length3D",
4162     "FT_Deflection2D",
4163     "FT_NodeConnectivityNumber",
4164     "FT_BelongToMeshGroup",
4165     "FT_BelongToGeom",
4166     "FT_BelongToPlane",
4167     "FT_BelongToCylinder",
4168     "FT_BelongToGenSurface",
4169     "FT_LyingOnGeom",
4170     "FT_RangeOfIds",
4171     "FT_BadOrientedVolume",
4172     "FT_BareBorderVolume",
4173     "FT_BareBorderFace",
4174     "FT_OverConstrainedVolume",
4175     "FT_OverConstrainedFace",
4176     "FT_LinearOrQuadratic",
4177     "FT_GroupColor",
4178     "FT_ElemGeomType",
4179     "FT_EntityType",
4180     "FT_CoplanarFaces",
4181     "FT_BallDiameter",
4182     "FT_ConnectedElements",
4183     "FT_LessThan",
4184     "FT_MoreThan",
4185     "FT_EqualTo",
4186     "FT_LogicalNOT",
4187     "FT_LogicalAND",
4188     "FT_LogicalOR",
4189     "FT_Undefined"};
4190
4191 #ifdef _DEBUG_
4192   // check if functName is complete, compilation failure means that enum FunctorType changed
4193   const int nbFunctors = sizeof(functName) / sizeof(const char*);
4194   int _assert[( nbFunctors == SMESH::FT_Undefined + 1 ) ? 2 : -1 ]; _assert[0]=_assert[1]=0;
4195 #endif
4196
4197   return functName;
4198 }
4199
4200 //================================================================================
4201 /*!
4202  * \brief Return a string corresponding to an item of enum FunctorType
4203  */
4204 //================================================================================
4205
4206 const char* SMESH::FunctorTypeToString(SMESH::FunctorType ft)
4207 {
4208   if ( ft < 0 || ft > SMESH::FT_Undefined )
4209     return "FT_Undefined";
4210   return getFunctNames()[ ft ];
4211 }
4212
4213 //================================================================================
4214 /*!
4215  * \brief Converts a string to FunctorType. This is reverse of FunctorTypeToString()
4216  */
4217 //================================================================================
4218
4219 SMESH::FunctorType SMESH::StringToFunctorType(const char* str)
4220 {
4221   std::string name( str + 3 ); // skip "FT_"
4222   const char** functNames = getFunctNames();
4223   int ft = 0;
4224   for ( ; ft < SMESH::FT_Undefined; ++ft )
4225     if ( name == ( functNames[ft] + 3 ))
4226       break;
4227
4228   //ASSERT( strcmp( str, FunctorTypeToString( SMESH::FunctorType( ft ))) == 0 );
4229
4230   return SMESH::FunctorType( ft );
4231 }
4232
4233 //================================================================================
4234 /*!
4235  * \brief calls OnBaseObjModified(), if who != this, and myWaiters[i]->Modified(who)
4236  */
4237 //================================================================================
4238
4239 void NotifyerAndWaiter::Modified( bool removed, NotifyerAndWaiter* who )
4240 {
4241   if ( who != 0 && who != this )
4242     OnBaseObjModified( who, removed );
4243   else
4244     who = this;
4245
4246   std::list<NotifyerAndWaiter*> waiters = myWaiters; // myWaiters can be changed by Modified()
4247   std::list<NotifyerAndWaiter*>::iterator i = waiters.begin();
4248   for ( ; i != waiters.end(); ++i )
4249     (*i)->Modified( removed, who );
4250 }
4251
4252 //================================================================================
4253 /*!
4254  * \brief Stores an object to be notified on change of predicate
4255  */
4256 //================================================================================
4257
4258 void NotifyerAndWaiter::AddModifWaiter( NotifyerAndWaiter* waiter )
4259 {
4260   if ( waiter )
4261     myWaiters.push_back( waiter );
4262 }
4263
4264 //================================================================================
4265 /*!
4266  * \brief Removes an object to be notified on change of predicate
4267  */
4268 //================================================================================
4269
4270 void NotifyerAndWaiter::RemoveModifWaiter( NotifyerAndWaiter* waiter )
4271 {
4272   myWaiters.remove( waiter );
4273 }
4274
4275 //================================================================================
4276 /*!
4277  * \brief Checks if a waiter is among myWaiters, maybe nested
4278  */
4279 //================================================================================
4280
4281 bool NotifyerAndWaiter::ContainModifWaiter( NotifyerAndWaiter* waiter )
4282 {
4283   bool is = ( waiter == this );
4284
4285   std::list<NotifyerAndWaiter*>::iterator w = myWaiters.begin();
4286   for ( ; !is && w != myWaiters.end(); ++w )
4287     is = (*w)->ContainModifWaiter( waiter );
4288
4289   return is;
4290 }