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