Salome HOME
aca4648e4dc10863a9d1a90bdef38d6019f9638b
[modules/geom.git] / src / GEOMFiltersSelection / GEOM_FaceFilter.cxx
1 //  GEOM GEOMFiltersSelection : filter selector for the viewer
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOM_FaceFilter.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GEOM_FaceFilter.ixx"
31 #include "GEOM_Client.hxx"
32
33 #include "SALOME_InteractiveObject.hxx"
34 #include "GEOM_InteractiveObject.hxx"
35 #include "GEOM_ShapeTypeFilter.hxx"
36 #include "SALOME_TypeFilter.hxx"
37
38 #include "utilities.h"
39 #include "QAD_Application.h"
40 #include "QAD_Desktop.h"
41 #include "QAD_Study.h"
42
43 // Open CASCADE Includes
44 #include <BRepAdaptor_Surface.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS.hxx>
48 #include <TopAbs.hxx>
49
50
51 static GEOM_Client  ShapeReader;
52
53 /*!
54   enumeration TypeOfFace is AnyFace,Plane,Cylinder,Sphere,Torus,Revol,Cone;
55 */
56 GEOM_FaceFilter::GEOM_FaceFilter(const StdSelect_TypeOfFace Face,
57                                  GEOM::GEOM_Gen_ptr geom) 
58 {
59   myKind = Face;
60   myComponentGeom = GEOM::GEOM_Gen::_narrow(geom);
61 }
62
63 Standard_Boolean GEOM_FaceFilter::IsOk(const Handle(SALOME_InteractiveObject)& anObj) const 
64 {
65   Handle(SALOME_TypeFilter) GeomFilter = new SALOME_TypeFilter( "GEOM" );
66   if ( !GeomFilter->IsOk(anObj) ) 
67     return false;
68
69   Handle(GEOM_ShapeTypeFilter) GeomShapeTypeFilter = new GEOM_ShapeTypeFilter( TopAbs_FACE, myComponentGeom );
70   if ( !GeomShapeTypeFilter->IsOk(anObj) ) 
71     return false;
72
73   if ( anObj->hasEntry() ) {
74     QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
75     SALOMEDS::Study_var aStudy = ActiveStudy->getStudyDocument();
76     SALOMEDS::SObject_var obj = aStudy->FindObjectID( anObj->getEntry() );
77     SALOMEDS::GenericAttribute_var anAttr;
78     SALOMEDS::AttributeIOR_var     anIOR;
79     if ( !obj->_is_nil() ) {
80        if (obj->FindAttribute(anAttr, "AttributeIOR")) {
81          anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
82          GEOM::GEOM_Shape_var aShape = myComponentGeom->GetIORFromString( anIOR->Value() );  
83          if ( aShape->_is_nil() )
84            return false;
85      
86          TopoDS_Shape    Shape = ShapeReader.GetShape( myComponentGeom, aShape );
87          if ( Shape.IsNull() )
88            return false;
89          
90          switch (myKind) {
91          case StdSelect_AnyFace:
92            return Standard_True;
93          case StdSelect_Plane:
94            {
95              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
96              return (surf.GetType() == GeomAbs_Plane);
97            }
98          case StdSelect_Cylinder:
99            {
100              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
101              return (surf.GetType() == GeomAbs_Cylinder);
102            }
103          case StdSelect_Sphere: 
104            {
105              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
106              return (surf.GetType() == GeomAbs_Sphere);      
107            }
108          case StdSelect_Torus: 
109            {
110              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
111              return ( surf.GetType() == GeomAbs_Torus);      
112            }
113          case StdSelect_Revol: 
114            {
115              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
116              return ( surf.GetType() == GeomAbs_Cylinder || 
117                       surf.GetType() == GeomAbs_Cone     ||
118                       surf.GetType() == GeomAbs_Torus    ||
119                       surf.GetType() == GeomAbs_Sphere    || 
120                       surf.GetType() == GeomAbs_SurfaceOfRevolution ); 
121            }
122          case StdSelect_Cone: // en attendant la liberation du cdl, on l'utilise pour Cone
123            {
124              BRepAdaptor_Surface surf(TopoDS::Face(Shape));
125              return (surf.GetType() == GeomAbs_Cone);      
126            }     
127          }
128        }
129     }
130   }
131     
132   if ( anObj->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject)) ) {
133     Handle(GEOM_InteractiveObject) GObject =
134       Handle(GEOM_InteractiveObject)::DownCast(anObj);
135     
136     GEOM::GEOM_Shape_var aShape = myComponentGeom->GetIORFromString( GObject->getIOR() );  
137     if ( aShape->_is_nil() )
138       return false;
139     
140     TopoDS_Shape    Shape = ShapeReader.GetShape( myComponentGeom, aShape );
141     if ( Shape.IsNull() )
142       return false;
143     
144     switch (myKind) {
145     case StdSelect_AnyFace:
146       return Standard_True;
147     case StdSelect_Plane:
148       {
149         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
150         return (surf.GetType() == GeomAbs_Plane);
151       }
152     case StdSelect_Cylinder:
153       {
154         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
155         return (surf.GetType() == GeomAbs_Cylinder);
156       }
157     case StdSelect_Sphere: 
158       {
159         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
160         return (surf.GetType() == GeomAbs_Sphere);      
161       }
162     case StdSelect_Torus: 
163       {
164         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
165         return ( surf.GetType() == GeomAbs_Torus);      
166       }
167     case StdSelect_Revol: 
168       {
169         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
170         return ( surf.GetType() == GeomAbs_Cylinder || 
171                  surf.GetType() == GeomAbs_Cone     ||
172                  surf.GetType() == GeomAbs_Torus    ||
173                  surf.GetType() == GeomAbs_Sphere         || 
174                  surf.GetType() == GeomAbs_SurfaceOfRevolution ); 
175       }
176     case StdSelect_Cone: // en attendant la liberation du cdl, on l'utilise pour Cone
177       {
178         BRepAdaptor_Surface surf(TopoDS::Face(Shape));
179         return (surf.GetType() == GeomAbs_Cone);      
180       }  
181     }
182   }
183   return false;
184 }