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