Salome HOME
IPAL19834: Incorrect result after make Partition of 2 objects. A fix by PKV.
[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/ or email : webmaster.salome@opencascade.com
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 //=======================================================================
33 // function : GEOM_SelectionFilter
34 // purpose  :
35 //=======================================================================
36 GEOM_SelectionFilter::GEOM_SelectionFilter( SalomeApp_Study* study, const bool theAll )
37   : SalomeApp_Filter(study)
38 {
39   myAll = theAll;
40 }
41
42 //=======================================================================
43 // function : ~GEOM_SelectionFilter
44 // purpose  :
45 //=======================================================================
46 GEOM_SelectionFilter::~GEOM_SelectionFilter()
47 {
48 }
49
50 //=======================================================================
51 // function : isOk
52 // purpose  :
53 //=======================================================================
54 bool GEOM_SelectionFilter::isOk( const SUIT_DataOwner* sOwner ) const
55 {
56   GEOM::GEOM_Object_var obj = getObject( sOwner );
57   if ( !CORBA::is_nil( obj ) && obj->IsShape() )
58   {
59     if ( isAll() )
60       return true;
61
62     TopoDS_Shape shape;
63     if ( getShape( obj, shape ) )
64       return contains( shape.ShapeType() ) && isShapeOk( shape );
65   }
66   return false;
67 }
68
69 //=======================================================================
70 // function : getObject
71 // purpose  :
72 //=======================================================================
73 GEOM::GEOM_Object_ptr GEOM_SelectionFilter::getObject( const SUIT_DataOwner* sOwner, const bool extractReference ) const
74 {
75   GEOM::GEOM_Object_var anObj;
76
77   const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(sOwner);
78   SalomeApp_Study* appStudy = getStudy();
79   if (owner && appStudy)
80   {
81     _PTR(Study) study = appStudy->studyDS();
82     QString entry = owner->entry();
83
84     _PTR(SObject) aSO (study->FindObjectID(entry.toStdString())), aRefSO;
85     if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
86       aSO = aRefSO;
87
88     if (aSO) {
89       std::string aValue = aSO->GetIOR();
90       if (strcmp(aValue.c_str(), "") != 0) {
91         CORBA::ORB_ptr anORB = SalomeApp_Application::orb();
92         CORBA::Object_var aCorbaObj = anORB->string_to_object(aValue.c_str());
93         anObj = GEOM::GEOM_Object::_narrow(aCorbaObj);
94       }
95     }
96   }
97
98   if (!CORBA::is_nil(anObj))
99     return anObj._retn();
100
101   return GEOM::GEOM_Object::_nil();
102 }
103
104 //=======================================================================
105 // function : getShape
106 // purpose  :
107 //=======================================================================
108 bool GEOM_SelectionFilter::getShape (const GEOM::GEOM_Object_ptr& theObject,
109                                      TopoDS_Shape&                theShape) const
110 {
111   if ( !CORBA::is_nil( theObject ) )
112   {
113     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
114       ( SUIT_Session::session()->activeApplication() );
115     if ( app )
116     {
117       SALOME_LifeCycleCORBA* ls = new SALOME_LifeCycleCORBA( app->namingService() );
118       static GEOM::GEOM_Gen_var geomGen;
119       if(CORBA::is_nil( geomGen )) {
120         Engines::Component_var comp = ls->FindOrLoad_Component( "FactoryServer", "GEOM" );
121         geomGen = GEOM::GEOM_Gen::_narrow( comp );
122       }
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.removeAll( 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 }