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