Salome HOME
fix bug 10959. Initialize GEOM_Gen if necessary
[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   if ( CORBA::is_nil( GeometryGUI::GetGeomGen() ) && !GeometryGUI::InitGeomGen() )
106     return false;
107   TopoDS_Shape aShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), aGeomObj);
108   if (aShape.IsNull() ||
109       !myShapeTypes.Contains(aShape.ShapeType()))
110     return false;
111
112   if (myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed())
113     return false;
114
115   // Verify whether shape of entry object is sub-shape of myMainObj
116   if (!myMainObj->_is_nil()) {
117     TopoDS_Shape aMainShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), myMainObj);
118     if (aMainShape.IsNull())
119       return false;
120
121     bool isFound = false;
122     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
123     TopExp_Explorer anExp (aMainShape, aShapeType);
124     for (; anExp.More(); anExp.Next()) {
125       if (anExp.Current() == aShape) {
126         isFound = true;
127         break;
128       }
129     }
130     if (!isFound)
131       return false;
132   }
133
134   // Verify number of sub-shapes
135   if (mySubShapeType == TopAbs_SHAPE);
136     return true;
137
138   TopExp_Explorer anExp2 (aShape, mySubShapeType);
139   TopTools_MapOfShape aMap;
140   for (; anExp2.More(); anExp2.Next())
141     aMap.Add(anExp2.Current());
142
143   return myNumber == aMap.Extent();
144 }
145
146 //=======================================================================
147 // name    : SMESH_NumberFilter::getGeom
148 // Purpose : Retrieve geom object from SALOME_InteractiveObject
149 //=======================================================================
150 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom
151   (const SUIT_DataOwner* theDataOwner, const bool extractReference ) const
152 {
153   const LightApp_DataOwner* owner =
154     dynamic_cast<const LightApp_DataOwner*>(theDataOwner);
155   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
156     (SUIT_Session::session()->activeApplication()->activeStudy());
157
158   GEOM::GEOM_Object_var anObj;
159
160   if (!owner || !appStudy)
161     return GEOM::GEOM_Object::_nil();
162
163   _PTR(Study) study = appStudy->studyDS();
164   QString entry = owner->entry();
165
166   _PTR(SObject) aSO( study->FindObjectID( entry.latin1() ) ), aRefSO;
167   if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
168     aSO = aRefSO;
169
170   if (!aSO)
171     return GEOM::GEOM_Object::_nil();
172
173   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
174   anObj = GEOM::GEOM_Object::_narrow(anObject);
175   if (!CORBA::is_nil(anObj))
176     return anObj._retn();
177
178   // Get geom object corresponding to the mesh
179   _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
180   for (; anIter->More(); anIter->Next()) {
181     _PTR(SObject) aSO = anIter->Value();
182     if (!aSO)
183       continue;
184     _PTR(SObject) aRefSO;
185     _PTR(SObject) anObj;
186     if (aSO->ReferencedObject(aRefSO))
187       anObj = aRefSO;
188
189     if (!anObj)
190       anObj = aSO;
191
192     anObject = _CAST(SObject,anObj)->GetObject();
193     GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
194
195     if (!aMeshShape->_is_nil())
196       return aMeshShape._retn();
197   }
198
199   return GEOM::GEOM_Object::_nil();
200 }
201
202 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
203 {
204   mySubShapeType = theSubShapeType;
205 }
206
207 void SMESH_NumberFilter::SetNumber (const int theNumber)
208 {
209   myNumber = theNumber;
210 }
211
212 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
213 {
214   myIsClosedOnly = theIsClosedOnly;
215 }
216
217 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
218 {
219   myShapeTypes.Add( theShapeType );
220 }
221
222 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
223 {
224   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
225 }