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