Salome HOME
SMH: Add forgotten files
[modules/geom.git] / src / GEOMFiltersSelection / GEOM_SelectionFilter.cxx
1 #include "GEOM_SelectionFilter.h"
2
3 #include "GEOM_Client.hxx"
4
5 #include <SalomeApp_DataOwner.h>
6 #include <SalomeApp_Study.h>
7 #include <SalomeApp_Application.h>
8
9 #include <SALOME_LifeCycleCORBA.hxx>
10
11 #include <SUIT_Session.h>
12
13 #include <SALOMEDS_SObject.hxx>
14
15
16
17 //=======================================================================
18 // function : GEOM_SelectionFilter
19 // purpose  : 
20 //=======================================================================
21 GEOM_SelectionFilter::GEOM_SelectionFilter( SalomeApp_Study* study, const bool theAll )
22   : SalomeApp_Filter(study)
23 {
24   myAll = theAll;
25 }
26
27 //=======================================================================
28 // function : ~GEOM_SelectionFilter
29 // purpose  : 
30 //=======================================================================
31 GEOM_SelectionFilter::~GEOM_SelectionFilter()
32 {
33 }
34
35 //=======================================================================
36 // function : isOk
37 // purpose  : 
38 //=======================================================================
39 bool GEOM_SelectionFilter::isOk( const SUIT_DataOwner* sOwner ) const
40 {
41   GEOM::GEOM_Object_var obj = getObject( sOwner );
42   if ( !CORBA::is_nil( obj ) && obj->IsShape() )
43   {
44     if ( isAll() )
45       return true;
46
47     TopoDS_Shape shape;
48     if ( getShape( obj, shape ) )
49       return contains( shape.ShapeType() ) && isShapeOk( shape );
50   }
51   return false;
52 }
53
54 //=======================================================================
55 // function : getObject
56 // purpose  : 
57 //=======================================================================
58 GEOM::GEOM_Object_ptr GEOM_SelectionFilter::getObject( const SUIT_DataOwner* sOwner ) const
59 {
60   GEOM::GEOM_Object_var anObj;
61
62   const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*> ( sOwner );
63   SalomeApp_Study* appStudy = getStudy();
64   if ( owner && appStudy ) 
65   {
66     _PTR(Study) study = appStudy->studyDS();
67     QString entry = owner->entry();
68
69     _PTR(SObject) aSO( study->FindObjectID( entry.latin1() ) );
70     if ( aSO )
71       anObj = GEOM::GEOM_Object::_narrow(dynamic_cast<SALOMEDS_SObject*>(aSO.get())->GetObject());
72   }
73   
74   if ( !CORBA::is_nil( anObj ) )
75     return anObj._retn();
76     
77   return GEOM::GEOM_Object::_nil();
78 }
79
80 //=======================================================================
81 // function : getShape
82 // purpose  : 
83 //=======================================================================
84 bool GEOM_SelectionFilter::getShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape ) const
85 {
86   if ( !CORBA::is_nil( theObject ) )
87   {
88     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
89     if ( app )
90     {
91       SALOME_LifeCycleCORBA* ls = new SALOME_LifeCycleCORBA( app->namingService() );
92       Engines::Component_var comp = ls->FindOrLoad_Component( "FactoryServer", "GEOM" );
93       GEOM::GEOM_Gen_var geomGen = GEOM::GEOM_Gen::_narrow( comp );
94       if ( !CORBA::is_nil( geomGen ) ) 
95       {
96         TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape( geomGen, theObject );
97         
98         if ( !aTopoDSShape.IsNull() )
99         {
100           theShape = aTopoDSShape;
101           return true;
102         }
103       }
104     }
105   }
106   return false;
107 }
108
109 //=======================================================================
110 // function : contains
111 // purpose  : 
112 //=======================================================================
113 bool GEOM_SelectionFilter::contains( const int type ) const
114 {
115   return myTypes.contains( type );
116 }
117
118 //=======================================================================
119 // function : add
120 // purpose  : 
121 //=======================================================================
122 void GEOM_SelectionFilter::add( const int type )
123 {
124   if ( !contains( type ) )
125     myTypes.append( type );
126 }
127
128 //=======================================================================
129 // function : remove
130 // purpose  : 
131 //=======================================================================
132 void GEOM_SelectionFilter::remove( const int type )
133 {
134   if ( contains( type ) )
135     myTypes.remove( type );
136 }
137
138 //=======================================================================
139 // function : setAll
140 // purpose  : 
141 //=======================================================================
142 void GEOM_SelectionFilter::setAll( const bool all )
143 {
144   myAll = all;
145 }
146
147 //=======================================================================
148 // function : isAll
149 // purpose  : 
150 //=======================================================================
151 bool GEOM_SelectionFilter::isAll() const
152 {
153   return myAll;
154 }
155
156 //=======================================================================
157 // function : isShapeOk
158 // purpose  : 
159 //=======================================================================
160 bool GEOM_SelectionFilter::isShapeOk( const TopoDS_Shape& ) const
161 {
162   return true;
163 }
164