Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESHFiltersSelection / SMESH_NumberFilter.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SMESH_NumberFilter.cxx
21 //  Module : SMESH
22
23 #include "SMESH_NumberFilter.hxx"
24
25 #include "GEOM_Client.hxx"
26 #include "GeometryGUI.h"
27
28 #include "SUIT_Application.h"
29 #include "SUIT_Session.h"
30
31 #include "SalomeApp_Study.h"
32 #include "LightApp_DataOwner.h"
33
34 #include "SALOME_InteractiveObject.hxx"
35 #include "SALOMEDSClient_SObject.hxx"
36 #include "SALOMEDS_SObject.hxx"
37
38 #include <TopExp_Explorer.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopExp.hxx>
41
42 /*!
43  *  Class       : SMESH_NumberFilter
44  *  Description : Filter for geom objects.
45  *                Filter geom objects by number of subshapes of the given type
46  */
47
48 //=======================================================================
49 // name    : SMESH_NumberFilter::SMESH_NumberFilter
50 // Purpose : Constructor
51 //=======================================================================
52 SMESH_NumberFilter::SMESH_NumberFilter (const char*            theKind,
53                                         const TopAbs_ShapeEnum theSubShapeType,
54                                         const int              theNumber,
55                                         const TopAbs_ShapeEnum theShapeType,
56                                         GEOM::GEOM_Object_ptr  theMainObj,
57                                         const bool             theIsClosedOnly)
58 {
59   myKind = (char*)theKind;
60   mySubShapeType = theSubShapeType;
61   myNumber = theNumber;
62   myIsClosedOnly = theIsClosedOnly;
63   myShapeTypes.Add(theShapeType);
64   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
65 }
66
67 //=======================================================================
68 // name    : SMESH_NumberFilter::SMESH_NumberFilter
69 // Purpose : Constructor
70 //=======================================================================
71 SMESH_NumberFilter::SMESH_NumberFilter (const char*                 theKind,
72                                         const TopAbs_ShapeEnum      theSubShapeType,
73                                         const int                   theNumber,
74                                         const TColStd_MapOfInteger& theShapeTypes,
75                                         GEOM::GEOM_Object_ptr       theMainObj,
76                                         const bool                  theIsClosedOnly )
77 {
78   myKind = (char*)theKind;
79   mySubShapeType = theSubShapeType;
80   myNumber = theNumber;
81   myIsClosedOnly = theIsClosedOnly;
82   myShapeTypes = theShapeTypes;
83   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
84 }
85
86 SMESH_NumberFilter::~SMESH_NumberFilter()
87 {
88 }
89
90 //=======================================================================
91 // name    : SMESH_NumberFilter::SMESH_NumberFilter
92 // Purpose : Verify validity of entry object
93 //=======================================================================
94 bool SMESH_NumberFilter::isOk (const SUIT_DataOwner* theDataOwner) const
95 {
96   if (!theDataOwner)
97     return false;
98
99   // Get geom object from IO
100   GEOM::GEOM_Object_var aGeomObj = getGeom(theDataOwner);
101   if (aGeomObj->_is_nil())
102     return false;
103
104   // Get shape from geom object and verify its parameters
105   GEOM_Client aGeomClient;
106   if ( CORBA::is_nil( GeometryGUI::GetGeomGen() ) && !GeometryGUI::InitGeomGen() )
107     return false;
108   TopoDS_Shape aShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), aGeomObj);
109   if (aShape.IsNull() ||
110       !myShapeTypes.Contains(aShape.ShapeType()))
111     return false;
112
113   if (myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed())
114     return false;
115
116   // Verify whether shape of entry object is sub-shape of myMainObj
117   if (!myMainObj->_is_nil()) {
118     TopoDS_Shape aMainShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), myMainObj);
119     if (aMainShape.IsNull())
120       return false;
121
122     bool isFound = false;
123     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
124     TopExp_Explorer anExp (aMainShape, aShapeType);
125     for (; anExp.More(); anExp.Next()) {
126       if (anExp.Current() == aShape) {
127         isFound = true;
128         break;
129       }
130     }
131     if (!isFound)
132       return false;
133   }
134
135   // Verify number of sub-shapes
136   if (mySubShapeType == TopAbs_SHAPE);
137     return true;
138
139   TopTools_IndexedMapOfShape aMap;
140   TopExp::MapShapes(aShape, mySubShapeType, aMap);
141
142   if ( myNumber )
143     return myNumber == aMap.Extent(); // given number
144
145   return aMap.Extent(); // at least one?
146 }
147
148 //=======================================================================
149 // name    : SMESH_NumberFilter::getGeom
150 // Purpose : Retrieve geom object from SALOME_InteractiveObject
151 //=======================================================================
152 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom
153   (const SUIT_DataOwner* theDataOwner, const bool extractReference ) const
154 {
155   const LightApp_DataOwner* owner =
156     dynamic_cast<const LightApp_DataOwner*>(theDataOwner);
157   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
158     (SUIT_Session::session()->activeApplication()->activeStudy());
159
160   GEOM::GEOM_Object_var anObj;
161
162   if (!owner || !appStudy)
163     return GEOM::GEOM_Object::_nil();
164
165   _PTR(Study) study = appStudy->studyDS();
166   QString entry = owner->entry();
167
168   _PTR(SObject) aSO( study->FindObjectID( entry.latin1() ) ), aRefSO;
169   if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
170     aSO = aRefSO;
171
172   if (!aSO)
173     return GEOM::GEOM_Object::_nil();
174
175   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
176   anObj = GEOM::GEOM_Object::_narrow(anObject);
177   if (!CORBA::is_nil(anObj))
178     return anObj._retn();
179
180   // Get geom object corresponding to the mesh
181   if ( myKind == "SMESH" ) {
182     _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
183     for (; anIter->More(); anIter->Next()) {
184       _PTR(SObject) aSO = anIter->Value();
185       if (!aSO)
186         continue;
187       _PTR(SObject) aRefSO;
188       _PTR(SObject) anObj;
189       if (aSO->ReferencedObject(aRefSO))
190         anObj = aRefSO;
191
192       if (!anObj)
193         anObj = aSO;
194
195       anObject = _CAST(SObject,anObj)->GetObject();
196       GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
197
198       if (!aMeshShape->_is_nil())
199         return aMeshShape._retn();
200     }
201   }
202
203   return GEOM::GEOM_Object::_nil();
204 }
205
206 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
207 {
208   mySubShapeType = theSubShapeType;
209 }
210
211 void SMESH_NumberFilter::SetNumber (const int theNumber)
212 {
213   myNumber = theNumber;
214 }
215
216 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
217 {
218   myIsClosedOnly = theIsClosedOnly;
219 }
220
221 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
222 {
223   myShapeTypes.Add( theShapeType );
224 }
225
226 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
227 {
228   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
229 }