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