Salome HOME
Copyrights update
[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       Engines::Component_var comp = ls->FindOrLoad_Component( "FactoryServer", "GEOM" );
122       GEOM::GEOM_Gen_var geomGen = GEOM::GEOM_Gen::_narrow( comp );
123       if ( !CORBA::is_nil( geomGen ) )
124       {
125         TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape( geomGen, theObject );
126
127         if ( !aTopoDSShape.IsNull() )
128         {
129           theShape = aTopoDSShape;
130           return true;
131         }
132       }
133     }
134   }
135   return false;
136 }
137
138 //=======================================================================
139 // function : contains
140 // purpose  :
141 //=======================================================================
142 bool GEOM_SelectionFilter::contains( const int type ) const
143 {
144   return myTypes.contains( type );
145 }
146
147 //=======================================================================
148 // function : add
149 // purpose  :
150 //=======================================================================
151 void GEOM_SelectionFilter::add( const int type )
152 {
153   if ( !contains( type ) )
154     myTypes.append( type );
155 }
156
157 //=======================================================================
158 // function : remove
159 // purpose  :
160 //=======================================================================
161 void GEOM_SelectionFilter::remove( const int type )
162 {
163   if ( contains( type ) )
164     myTypes.remove( type );
165 }
166
167 //=======================================================================
168 // function : setAll
169 // purpose  :
170 //=======================================================================
171 void GEOM_SelectionFilter::setAll( const bool all )
172 {
173   myAll = all;
174 }
175
176 //=======================================================================
177 // function : isAll
178 // purpose  :
179 //=======================================================================
180 bool GEOM_SelectionFilter::isAll() const
181 {
182   return myAll;
183 }
184
185 //=======================================================================
186 // function : isShapeOk
187 // purpose  :
188 //=======================================================================
189 bool GEOM_SelectionFilter::isShapeOk( const TopoDS_Shape& ) const
190 {
191   return true;
192 }