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