Salome HOME
Join modifications from branch BR_3_1_0deb
[modules/geom.git] / src / GEOMFiltersSelection / GEOM_SelectionFilter.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 #include "GEOM_SelectionFilter.h"
21
22 #include "GEOM_Client.hxx"
23
24 #include <LightApp_DataOwner.h>
25 #include <SalomeApp_Study.h>
26 #include <SalomeApp_Application.h>
27
28 #include <SALOME_LifeCycleCORBA.hxx>
29
30 #include <SUIT_Session.h>
31
32 #include <SALOMEDSClient.hxx>
33
34
35 //=======================================================================
36 // function : GEOM_SelectionFilter
37 // purpose  :
38 //=======================================================================
39 GEOM_SelectionFilter::GEOM_SelectionFilter( SalomeApp_Study* study, const bool theAll )
40   : SalomeApp_Filter(study)
41 {
42   myAll = theAll;
43 }
44
45 //=======================================================================
46 // function : ~GEOM_SelectionFilter
47 // purpose  :
48 //=======================================================================
49 GEOM_SelectionFilter::~GEOM_SelectionFilter()
50 {
51 }
52
53 //=======================================================================
54 // function : isOk
55 // purpose  :
56 //=======================================================================
57 bool GEOM_SelectionFilter::isOk( const SUIT_DataOwner* sOwner ) const
58 {
59   GEOM::GEOM_Object_var obj = getObject( sOwner );
60   if ( !CORBA::is_nil( obj ) && obj->IsShape() )
61   {
62     if ( isAll() )
63       return true;
64
65     TopoDS_Shape shape;
66     if ( getShape( obj, shape ) )
67       return contains( shape.ShapeType() ) && isShapeOk( shape );
68   }
69   return false;
70 }
71
72 //=======================================================================
73 // function : getObject
74 // purpose  :
75 //=======================================================================
76 GEOM::GEOM_Object_ptr GEOM_SelectionFilter::getObject( const SUIT_DataOwner* sOwner, const bool extractReference ) const
77 {
78   GEOM::GEOM_Object_var anObj;
79
80   const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(sOwner);
81   SalomeApp_Study* appStudy = getStudy();
82   if (owner && appStudy)
83   {
84     _PTR(Study) study = appStudy->studyDS();
85     QString entry = owner->entry();
86
87     _PTR(SObject) aSO (study->FindObjectID(entry.latin1())), aRefSO;
88     if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
89       aSO = aRefSO;
90
91     if (aSO) {
92       std::string aValue = aSO->GetIOR();
93       if (strcmp(aValue.c_str(), "") != 0) {
94         CORBA::ORB_ptr anORB = SalomeApp_Application::orb();
95         CORBA::Object_var aCorbaObj = anORB->string_to_object(aValue.c_str());
96         anObj = GEOM::GEOM_Object::_narrow(aCorbaObj);
97       }
98     }
99   }
100
101   if (!CORBA::is_nil(anObj))
102     return anObj._retn();
103
104   return GEOM::GEOM_Object::_nil();
105 }
106
107 //=======================================================================
108 // function : getShape
109 // purpose  :
110 //=======================================================================
111 bool GEOM_SelectionFilter::getShape (const GEOM::GEOM_Object_ptr& theObject,
112                                      TopoDS_Shape&                theShape) const
113 {
114   if ( !CORBA::is_nil( theObject ) )
115   {
116     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
117       ( SUIT_Session::session()->activeApplication() );
118     if ( app )
119     {
120       SALOME_LifeCycleCORBA* ls = new SALOME_LifeCycleCORBA( app->namingService() );
121       static GEOM::GEOM_Gen_var geomGen;
122       if(CORBA::is_nil( geomGen )) {
123         Engines::Component_var comp = ls->FindOrLoad_Component( "FactoryServer", "GEOM" );
124         geomGen = GEOM::GEOM_Gen::_narrow( comp );
125       }
126       if ( !CORBA::is_nil( geomGen ) )
127       {
128         TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape( geomGen, theObject );
129
130         if ( !aTopoDSShape.IsNull() )
131         {
132           theShape = aTopoDSShape;
133           return true;
134         }
135       }
136     }
137   }
138   return false;
139 }
140
141 //=======================================================================
142 // function : contains
143 // purpose  :
144 //=======================================================================
145 bool GEOM_SelectionFilter::contains( const int type ) const
146 {
147   return myTypes.contains( type );
148 }
149
150 //=======================================================================
151 // function : add
152 // purpose  :
153 //=======================================================================
154 void GEOM_SelectionFilter::add( const int type )
155 {
156   if ( !contains( type ) )
157     myTypes.append( type );
158 }
159
160 //=======================================================================
161 // function : remove
162 // purpose  :
163 //=======================================================================
164 void GEOM_SelectionFilter::remove( const int type )
165 {
166   if ( contains( type ) )
167     myTypes.remove( type );
168 }
169
170 //=======================================================================
171 // function : setAll
172 // purpose  :
173 //=======================================================================
174 void GEOM_SelectionFilter::setAll( const bool all )
175 {
176   myAll = all;
177 }
178
179 //=======================================================================
180 // function : isAll
181 // purpose  :
182 //=======================================================================
183 bool GEOM_SelectionFilter::isAll() const
184 {
185   return myAll;
186 }
187
188 //=======================================================================
189 // function : isShapeOk
190 // purpose  :
191 //=======================================================================
192 bool GEOM_SelectionFilter::isShapeOk( const TopoDS_Shape& ) const
193 {
194   return true;
195 }