Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / SMESHFiltersSelection / SMESH_NumberFilter.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
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   : SMESH_NumberFilter.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESH_NumberFilter.hxx"
29 #include "GEOMBase.h"
30 #include "TopTools_MapOfShape.hxx"
31 #include "TopExp_Explorer.hxx"
32 #include "SALOME_InteractiveObject.hxx"
33 #include "QAD_Application.h"
34 #include "QAD_Desktop.h"
35
36
37 /*
38   Class       : SMESH_NumberFilter
39   Description : Filter for geom objects.
40                 Filter geom objects by number of subshapes of the given type
41 */
42
43 IMPLEMENT_STANDARD_HANDLE( SMESH_NumberFilter, SALOME_TypeFilter )
44 IMPLEMENT_STANDARD_RTTIEXT( SMESH_NumberFilter, SALOME_TypeFilter )
45
46 //=======================================================================
47 // name    : SMESH_NumberFilter::SMESH_NumberFilter
48 // Purpose : Constructor
49 //=======================================================================
50 SMESH_NumberFilter::SMESH_NumberFilter( const char*            theKind,
51                                         const TopAbs_ShapeEnum theSubShapeType,
52                                         const int              theNumber,
53                                         const TopAbs_ShapeEnum theShapeType,
54                                         GEOM::GEOM_Object_ptr  theMainObj,
55                                         const bool             theIsClosedOnly )
56 : SALOME_TypeFilter( (char*)theKind )
57 {
58   mySubShapeType = theSubShapeType;
59   myNumber = theNumber;
60   myIsClosedOnly = theIsClosedOnly;
61   myShapeTypes.Add( theShapeType );
62   myMainObj = GEOM::GEOM_Object::_duplicate( theMainObj );
63 }
64
65 //=======================================================================
66 // name    : SMESH_NumberFilter::SMESH_NumberFilter
67 // Purpose : Constructor
68 //=======================================================================
69 SMESH_NumberFilter::SMESH_NumberFilter( const char*                 theKind,
70                                         const TopAbs_ShapeEnum      theSubShapeType,
71                                         const int                   theNumber,
72                                         const TColStd_MapOfInteger& theShapeTypes,
73                                         GEOM::GEOM_Object_ptr       theMainObj,
74                                         const bool                  theIsClosedOnly )
75 : SALOME_TypeFilter( (char*)theKind )
76 {
77   mySubShapeType = theSubShapeType;
78   myNumber = theNumber;
79   myIsClosedOnly = theIsClosedOnly;
80   myShapeTypes = theShapeTypes;
81   myMainObj = GEOM::GEOM_Object::_duplicate( theMainObj );
82 }
83
84 SMESH_NumberFilter::~SMESH_NumberFilter()
85 {
86 }
87
88 //=======================================================================
89 // name    : SMESH_NumberFilter::SMESH_NumberFilter
90 // Purpose : Verify validity of entry object
91 //=======================================================================
92 Standard_Boolean SMESH_NumberFilter::IsOk( const Handle(SALOME_InteractiveObject)& theObj ) const
93 {
94   if ( theObj.IsNull() || !SALOME_TypeFilter::IsOk( theObj ) )
95     return false;
96
97   // Get geom object from IO
98   GEOM::GEOM_Object_var aGeomObj = getGeom( theObj );
99   if ( aGeomObj->_is_nil() )
100     return false;
101
102   // Get shape from geom object and verify its parameters
103   TopoDS_Shape aShape;
104   if ( !GEOMBase::GetShape( aGeomObj, aShape ) ||
105         aShape.IsNull() ||
106         !myShapeTypes.Contains( aShape.ShapeType() ) ||
107         myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed() )
108     return false;
109                         
110   // Verify whether shape of entry object is sub-shape of myMainObj
111   if ( !myMainObj->_is_nil() )
112   {
113     TopoDS_Shape aMainShape;
114     if ( !GEOMBase::GetShape( myMainObj, aMainShape ) )
115       return false;
116
117     bool isFound = false;
118     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
119     TopExp_Explorer anExp( aMainShape, aShapeType );
120     for ( ; anExp.More(); anExp.Next() )
121       if ( anExp.Current() == aShape )
122       {
123         isFound = true;
124         break;
125       }
126     if ( !isFound )
127       return false;
128   }
129   // Verify number of sub-shapes
130   if ( mySubShapeType == TopAbs_SHAPE );
131     return true;
132
133   int nbShapes = 0;
134   TopExp_Explorer anExp2( aShape, mySubShapeType );
135   TopTools_MapOfShape aMap;
136   for ( ; anExp2.More(); anExp2.Next() )
137     aMap.Add( anExp2.Current() );
138
139   return myNumber == aMap.Extent();
140 }
141
142 //=======================================================================
143 // name    : SMESH_NumberFilter::getGeom
144 // Purpose : Retrieve geom object from SALOME_InteractiveObject
145 //=======================================================================
146 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom(
147   const Handle(SALOME_InteractiveObject)& theObj ) const
148 {
149   if ( theObj->isComponentType( "GEOM" ) )
150   {
151     Standard_Boolean aRes = Standard_False;
152     GEOM::GEOM_Object_var aGeomObj = GEOMBase::ConvertIOinGEOMObject( theObj, aRes );
153     return aRes ? aGeomObj._retn() : GEOM::GEOM_Object::_nil();
154   }
155   else
156   {
157     // Get geom object corresponding to the mesh
158     SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
159     SALOMEDS::SObject_var aSO = aStudy->FindObjectID( theObj->getEntry() );
160     if ( aSO->_is_nil() )
161       return GEOM::GEOM_Object::_nil();
162
163     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( aSO );
164     for( ; anIter->More(); anIter->Next() )
165     {
166       SALOMEDS::SObject_var aSO = anIter->Value();
167       SALOMEDS::SObject_var aRefSO;
168
169       GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(
170         aSO->ReferencedObject( aRefSO )? aRefSO->GetObject() : aSO->GetObject() );
171
172       if ( !aMeshShape->_is_nil() )
173         return aMeshShape._retn();
174     }
175
176     return GEOM::GEOM_Object::_nil();
177   }
178 }
179
180
181 void SMESH_NumberFilter::SetSubShapeType( const TopAbs_ShapeEnum theSubShapeType )
182 {
183   mySubShapeType = theSubShapeType;
184 }
185
186 void SMESH_NumberFilter::SetNumber( const int theNumber )
187 {
188   myNumber = theNumber;
189 }
190
191 void SMESH_NumberFilter::SetClosedOnly( const bool theIsClosedOnly )
192 {
193   myIsClosedOnly = theIsClosedOnly;
194 }
195
196 void SMESH_NumberFilter::SetShapeType( const TopAbs_ShapeEnum theShapeType )
197 {
198   myShapeTypes.Add( theShapeType );
199 }
200
201 void SMESH_NumberFilter::SetMainShape( GEOM::GEOM_Object_ptr theMainObj )
202 {
203   myMainObj = GEOM::GEOM_Object::_duplicate( theMainObj );
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225