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