Salome HOME
298385bed2467964c7079db305f375499d151f97
[modules/geom.git] / src / GEOMFiltersSelection / GEOM_SelectionFilter.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 #include "GEOM_SelectionFilter.h"
24
25 #include "GEOM_Client.hxx"
26
27 #include <LightApp_DataOwner.h>
28 #include <SalomeApp_Study.h>
29 #include <SalomeApp_Application.h>
30
31 #include <SALOME_LifeCycleCORBA.hxx>
32
33 #include <SUIT_Session.h>
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
70   // IMP 0020435: fine local selection
71   {
72     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(sOwner);
73     if (owner) {
74       QString entry = owner->entry();
75       int index = entry.lastIndexOf("_");
76       if (index > 0) {
77         return true;
78       }
79     }
80   }
81
82   return false;
83 }
84
85 //=======================================================================
86 // function : getObject
87 // purpose  :
88 //=======================================================================
89 GEOM::GEOM_Object_ptr GEOM_SelectionFilter::getObject (const SUIT_DataOwner* sOwner,
90                                                        const bool extractReference) const
91 {
92   GEOM::GEOM_Object_var anObj;
93
94   const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>(sOwner);
95   SalomeApp_Study* appStudy = getStudy();
96   if (owner && appStudy)
97   {
98     _PTR(Study) study = appStudy->studyDS();
99     QString entry = owner->entry();
100
101     _PTR(SObject) aSO (study->FindObjectID(entry.toStdString())), aRefSO;
102     if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
103       aSO = aRefSO;
104
105     if (aSO) {
106       std::string aValue = aSO->GetIOR();
107       if (strcmp(aValue.c_str(), "") != 0) {
108         CORBA::ORB_ptr anORB = SalomeApp_Application::orb();
109         CORBA::Object_var aCorbaObj = anORB->string_to_object(aValue.c_str());
110         anObj = GEOM::GEOM_Object::_narrow(aCorbaObj);
111       }
112     }
113   }
114
115   if (!CORBA::is_nil(anObj))
116     return anObj._retn();
117
118   return GEOM::GEOM_Object::_nil();
119 }
120
121 //=======================================================================
122 // function : getShape
123 // purpose  :
124 //=======================================================================
125 bool GEOM_SelectionFilter::getShape (const GEOM::GEOM_Object_ptr& theObject,
126                                      TopoDS_Shape&                theShape) const
127 {
128   if ( !CORBA::is_nil( theObject ) )
129   {
130     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
131       ( SUIT_Session::session()->activeApplication() );
132     if ( app )
133     {
134       SALOME_LifeCycleCORBA* ls = new SALOME_LifeCycleCORBA( app->namingService() );
135       static GEOM::GEOM_Gen_var geomGen;
136       if(CORBA::is_nil( geomGen )) {
137         Engines::EngineComponent_var comp = ls->FindOrLoad_Component( "FactoryServer", "GEOM" );
138         geomGen = GEOM::GEOM_Gen::_narrow( comp );
139       }
140       if ( !CORBA::is_nil( geomGen ) )
141       {
142         TopoDS_Shape aTopoDSShape = GEOM_Client::get_client().GetShape( geomGen, theObject );
143
144         if ( !aTopoDSShape.IsNull() )
145         {
146           theShape = aTopoDSShape;
147           return true;
148         }
149       }
150     }
151   }
152   return false;
153 }
154
155 //=======================================================================
156 // function : contains
157 // purpose  :
158 //=======================================================================
159 bool GEOM_SelectionFilter::contains( const int type ) const
160 {
161   return myTypes.contains( type );
162 }
163
164 //=======================================================================
165 // function : add
166 // purpose  :
167 //=======================================================================
168 void GEOM_SelectionFilter::add( const int type )
169 {
170   if ( !contains( type ) )
171     myTypes.append( type );
172 }
173
174 //=======================================================================
175 // function : remove
176 // purpose  :
177 //=======================================================================
178 void GEOM_SelectionFilter::remove( const int type )
179 {
180   if ( contains( type ) )
181     myTypes.removeAll( type );
182 }
183
184 //=======================================================================
185 // function : setAll
186 // purpose  :
187 //=======================================================================
188 void GEOM_SelectionFilter::setAll( const bool all )
189 {
190   myAll = all;
191 }
192
193 //=======================================================================
194 // function : isAll
195 // purpose  :
196 //=======================================================================
197 bool GEOM_SelectionFilter::isAll() const
198 {
199   return myAll;
200 }
201
202 //=======================================================================
203 // function : isShapeOk
204 // purpose  :
205 //=======================================================================
206 bool GEOM_SelectionFilter::isShapeOk( const TopoDS_Shape& ) const
207 {
208   return true;
209 }