Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMESHFiltersSelection / SMESH_NumberFilter.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 //  File   : SMESH_NumberFilter.cxx
23 //  Module : SMESH
24 //
25 #include "SMESH_NumberFilter.hxx"
26
27 #include "GEOM_Client.hxx"
28 #include "GeometryGUI.h"
29
30 #include "SUIT_Application.h"
31 #include "SUIT_Session.h"
32
33 #include "SalomeApp_Study.h"
34 #include "LightApp_DataOwner.h"
35
36 #include "SALOME_InteractiveObject.hxx"
37 #include "SALOMEDSClient_SObject.hxx"
38 #include "SALOMEDS_SObject.hxx"
39
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TopExp.hxx>
43
44 /*!
45  *  Class       : SMESH_NumberFilter
46  *  Description : Filter for geom objects.
47  *                Filter geom objects by number of subshapes of the given type
48  */
49
50 //=======================================================================
51 // name    : SMESH_NumberFilter::SMESH_NumberFilter
52 // Purpose : Constructor
53 //=======================================================================
54 SMESH_NumberFilter::SMESH_NumberFilter (const char*            theKind,
55                                         const TopAbs_ShapeEnum theSubShapeType,
56                                         const int              theNumber,
57                                         const TopAbs_ShapeEnum theShapeType,
58                                         GEOM::GEOM_Object_ptr  theMainObj,
59                                         const bool             theIsClosedOnly)
60 {
61   myKind = (char*)theKind;
62   mySubShapeType = theSubShapeType;
63   myNumber = theNumber;
64   myIsClosedOnly = theIsClosedOnly;
65   myShapeTypes.Add(theShapeType);
66   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
67 }
68
69 //=======================================================================
70 // name    : SMESH_NumberFilter::SMESH_NumberFilter
71 // Purpose : Constructor
72 //=======================================================================
73 SMESH_NumberFilter::SMESH_NumberFilter (const char*                 theKind,
74                                         const TopAbs_ShapeEnum      theSubShapeType,
75                                         const int                   theNumber,
76                                         const TColStd_MapOfInteger& theShapeTypes,
77                                         GEOM::GEOM_Object_ptr       theMainObj,
78                                         const bool                  theIsClosedOnly )
79 {
80   myKind = (char*)theKind;
81   mySubShapeType = theSubShapeType;
82   myNumber = theNumber;
83   myIsClosedOnly = theIsClosedOnly;
84   myShapeTypes = theShapeTypes;
85   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
86 }
87
88 SMESH_NumberFilter::~SMESH_NumberFilter()
89 {
90 }
91
92 //=======================================================================
93 // name    : SMESH_NumberFilter::SMESH_NumberFilter
94 // Purpose : Verify validity of entry object
95 //=======================================================================
96 bool SMESH_NumberFilter::isOk (const SUIT_DataOwner* theDataOwner) const
97 {
98   if (!theDataOwner)
99     return false;
100
101   // Get geom object from IO
102   GEOM::GEOM_Object_var aGeomObj = getGeom(theDataOwner);
103   if (aGeomObj->_is_nil())
104     return false;
105
106   // Get shape from geom object and verify its parameters
107   GEOM_Client aGeomClient;
108   if ( CORBA::is_nil( GeometryGUI::GetGeomGen() ) && !GeometryGUI::InitGeomGen() )
109     return false;
110   TopoDS_Shape aShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), aGeomObj);
111   if (aShape.IsNull() ||
112       !myShapeTypes.Contains(aShape.ShapeType()))
113     return false;
114
115   if (myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed())
116     return false;
117
118   // Verify whether shape of entry object is sub-shape of myMainObj
119   if (!myMainObj->_is_nil()) {
120     TopoDS_Shape aMainShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), myMainObj);
121     if (aMainShape.IsNull())
122       return false;
123
124     bool isFound = false;
125     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
126     TopExp_Explorer anExp (aMainShape, aShapeType);
127     for (; anExp.More(); anExp.Next()) {
128       if (anExp.Current() == aShape) {
129         isFound = true;
130         break;
131       }
132     }
133     if (!isFound)
134       return false;
135   }
136
137   // Verify number of sub-shapes
138   if (mySubShapeType == TopAbs_SHAPE);
139     return true;
140
141   TopTools_IndexedMapOfShape aMap;
142   TopExp::MapShapes(aShape, mySubShapeType, aMap);
143
144   if ( myNumber )
145     return myNumber == aMap.Extent(); // given number
146
147   return aMap.Extent(); // at least one?
148 }
149
150 //=======================================================================
151 // name    : SMESH_NumberFilter::getGeom
152 // Purpose : Retrieve geom object from SALOME_InteractiveObject
153 //=======================================================================
154 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom
155   (const SUIT_DataOwner* theDataOwner, const bool extractReference ) const
156 {
157   const LightApp_DataOwner* owner =
158     dynamic_cast<const LightApp_DataOwner*>(theDataOwner);
159   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
160     (SUIT_Session::session()->activeApplication()->activeStudy());
161
162   GEOM::GEOM_Object_var anObj;
163
164   if (!owner || !appStudy)
165     return GEOM::GEOM_Object::_nil();
166
167   _PTR(Study) study = appStudy->studyDS();
168   QString entry = owner->entry();
169
170   _PTR(SObject) aSO( study->FindObjectID( entry.toLatin1().data() ) ), aRefSO;
171   if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
172     aSO = aRefSO;
173
174   if (!aSO)
175     return GEOM::GEOM_Object::_nil();
176
177   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
178   anObj = GEOM::GEOM_Object::_narrow(anObject);
179   if (!CORBA::is_nil(anObj))
180     return anObj._retn();
181
182   // Get geom object corresponding to the mesh
183   if ( myKind == "SMESH" ) {
184     _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
185     for (; anIter->More(); anIter->Next()) {
186       _PTR(SObject) aSO = anIter->Value();
187       if (!aSO)
188         continue;
189       _PTR(SObject) aRefSO;
190       _PTR(SObject) anObj;
191       if (aSO->ReferencedObject(aRefSO))
192         anObj = aRefSO;
193
194       if (!anObj)
195         anObj = aSO;
196
197       anObject = _CAST(SObject,anObj)->GetObject();
198       GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
199
200       if (!aMeshShape->_is_nil())
201         return aMeshShape._retn();
202     }
203   }
204
205   return GEOM::GEOM_Object::_nil();
206 }
207
208 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
209 {
210   mySubShapeType = theSubShapeType;
211 }
212
213 void SMESH_NumberFilter::SetNumber (const int theNumber)
214 {
215   myNumber = theNumber;
216 }
217
218 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
219 {
220   myIsClosedOnly = theIsClosedOnly;
221 }
222
223 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
224 {
225   myShapeTypes.Add( theShapeType );
226 }
227
228 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
229 {
230   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
231 }