Back to index

salome-smesh  6.5.0
SMESH_NumberFilter.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SMESH_NumberFilter.cxx
00024 //  Module : SMESH
00025 //
00026 #include "SMESH_NumberFilter.hxx"
00027 
00028 #include "GEOM_Client.hxx"
00029 #include "GeometryGUI.h"
00030 
00031 #include "SUIT_Application.h"
00032 #include "SUIT_Session.h"
00033 
00034 #include "SalomeApp_Study.h"
00035 #include "LightApp_DataOwner.h"
00036 
00037 #include "SALOME_InteractiveObject.hxx"
00038 #include "SALOMEDSClient_SObject.hxx"
00039 #include "SALOMEDS_SObject.hxx"
00040 
00041 #include <TopExp_Explorer.hxx>
00042 #include <TopTools_IndexedMapOfShape.hxx>
00043 #include <TopExp.hxx>
00044 
00051 //=======================================================================
00052 // name    : SMESH_NumberFilter::SMESH_NumberFilter
00053 // Purpose : Constructor
00054 //=======================================================================
00055 SMESH_NumberFilter::SMESH_NumberFilter (const char*            theKind,
00056                                         const TopAbs_ShapeEnum theSubShapeType,
00057                                         const int              theNumber,
00058                                         const TopAbs_ShapeEnum theShapeType,
00059                                         GEOM::GEOM_Object_ptr  theMainObj,
00060                                         const bool             theIsClosedOnly)
00061 {
00062   myKind = (char*)theKind;
00063   mySubShapeType = theSubShapeType;
00064   myNumber = theNumber;
00065   myIsClosedOnly = theIsClosedOnly;
00066   myShapeTypes.Add(theShapeType);
00067   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
00068 }
00069 
00070 //=======================================================================
00071 // name    : SMESH_NumberFilter::SMESH_NumberFilter
00072 // Purpose : Constructor
00073 //=======================================================================
00074 SMESH_NumberFilter::SMESH_NumberFilter (const char*                 theKind,
00075                                         const TopAbs_ShapeEnum      theSubShapeType,
00076                                         const int                   theNumber,
00077                                         const TColStd_MapOfInteger& theShapeTypes,
00078                                         GEOM::GEOM_Object_ptr       theMainObj,
00079                                         const bool                  theIsClosedOnly )
00080 {
00081   myKind = (char*)theKind;
00082   mySubShapeType = theSubShapeType;
00083   myNumber = theNumber;
00084   myIsClosedOnly = theIsClosedOnly;
00085   myShapeTypes = theShapeTypes;
00086   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
00087 }
00088 
00089 SMESH_NumberFilter::~SMESH_NumberFilter()
00090 {
00091 }
00092 
00093 //=======================================================================
00094 // name    : SMESH_NumberFilter::SMESH_NumberFilter
00095 // Purpose : Verify validity of entry object
00096 //=======================================================================
00097 bool SMESH_NumberFilter::isOk (const SUIT_DataOwner* theDataOwner) const
00098 {
00099   if (!theDataOwner)
00100     return false;
00101 
00102   // Get geom object from IO
00103   GEOM::GEOM_Object_var aGeomObj = getGeom(theDataOwner);
00104   if (aGeomObj->_is_nil())
00105     return false;
00106 
00107   // Get shape from geom object and verify its parameters
00108   GEOM_Client aGeomClient;
00109   if ( CORBA::is_nil( GeometryGUI::GetGeomGen() ) && !GeometryGUI::InitGeomGen() )
00110     return false;
00111   TopoDS_Shape aShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), aGeomObj);
00112   if (aShape.IsNull() ||
00113       !myShapeTypes.Contains(aShape.ShapeType()))
00114     return false;
00115 
00116   if (myIsClosedOnly && aShape.ShapeType() == TopAbs_SHELL && !aShape.Closed())
00117     return false;
00118 
00119   // Verify whether shape of entry object is sub-shape of myMainObj
00120   if (!myMainObj->_is_nil()) {
00121     TopoDS_Shape aMainShape = aGeomClient.GetShape(GeometryGUI::GetGeomGen(), myMainObj);
00122     if (aMainShape.IsNull())
00123       return false;
00124 
00125     bool isFound = false;
00126     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
00127     TopExp_Explorer anExp (aMainShape, aShapeType);
00128     for (; anExp.More(); anExp.Next()) {
00129       if (anExp.Current() == aShape) {
00130         isFound = true;
00131         break;
00132       }
00133     }
00134     if (!isFound)
00135       return false;
00136   }
00137 
00138   // Verify number of sub-shapes
00139   if (mySubShapeType == TopAbs_SHAPE);
00140     return true;
00141 
00142   TopTools_IndexedMapOfShape aMap;
00143   TopExp::MapShapes(aShape, mySubShapeType, aMap);
00144 
00145   if ( myNumber )
00146     return myNumber == aMap.Extent(); // given number
00147 
00148   return aMap.Extent(); // at least one?
00149 }
00150 
00151 //=======================================================================
00152 // name    : SMESH_NumberFilter::getGeom
00153 // Purpose : Retrieve geom object from SALOME_InteractiveObject
00154 //=======================================================================
00155 GEOM::GEOM_Object_ptr SMESH_NumberFilter::getGeom
00156   (const SUIT_DataOwner* theDataOwner, const bool extractReference ) const
00157 {
00158   const LightApp_DataOwner* owner =
00159     dynamic_cast<const LightApp_DataOwner*>(theDataOwner);
00160   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
00161     (SUIT_Session::session()->activeApplication()->activeStudy());
00162 
00163   GEOM::GEOM_Object_var anObj;
00164 
00165   if (!owner || !appStudy)
00166     return GEOM::GEOM_Object::_nil();
00167 
00168   _PTR(Study) study = appStudy->studyDS();
00169   QString entry = owner->entry();
00170 
00171   _PTR(SObject) aSO( study->FindObjectID( entry.toLatin1().data() ) ), aRefSO;
00172   if( extractReference && aSO && aSO->ReferencedObject( aRefSO ) )
00173     aSO = aRefSO;
00174 
00175   if (!aSO)
00176     return GEOM::GEOM_Object::_nil();
00177 
00178   CORBA::Object_var anObject = _CAST(SObject,aSO)->GetObject();
00179   anObj = GEOM::GEOM_Object::_narrow(anObject);
00180   if (!CORBA::is_nil(anObj))
00181     return anObj._retn();
00182 
00183   // Get geom object corresponding to the mesh
00184   if ( myKind == "SMESH" ) {
00185     _PTR(ChildIterator) anIter = study->NewChildIterator(aSO);
00186     for (; anIter->More(); anIter->Next()) {
00187       _PTR(SObject) aSO = anIter->Value();
00188       if (!aSO)
00189         continue;
00190       _PTR(SObject) aRefSO;
00191       _PTR(SObject) anObj;
00192       if (aSO->ReferencedObject(aRefSO))
00193         anObj = aRefSO;
00194 
00195       if (!anObj)
00196         anObj = aSO;
00197 
00198       anObject = _CAST(SObject,anObj)->GetObject();
00199       GEOM::GEOM_Object_var aMeshShape = GEOM::GEOM_Object::_narrow(anObject);
00200 
00201       if (!aMeshShape->_is_nil())
00202         return aMeshShape._retn();
00203     }
00204   }
00205 
00206   return GEOM::GEOM_Object::_nil();
00207 }
00208 
00209 void SMESH_NumberFilter::SetSubShapeType (const TopAbs_ShapeEnum theSubShapeType)
00210 {
00211   mySubShapeType = theSubShapeType;
00212 }
00213 
00214 void SMESH_NumberFilter::SetNumber (const int theNumber)
00215 {
00216   myNumber = theNumber;
00217 }
00218 
00219 void SMESH_NumberFilter::SetClosedOnly (const bool theIsClosedOnly)
00220 {
00221   myIsClosedOnly = theIsClosedOnly;
00222 }
00223 
00224 void SMESH_NumberFilter::SetShapeType (const TopAbs_ShapeEnum theShapeType)
00225 {
00226   myShapeTypes.Add( theShapeType );
00227 }
00228 
00229 void SMESH_NumberFilter::SetMainShape (GEOM::GEOM_Object_ptr theMainObj)
00230 {
00231   myMainObj = GEOM::GEOM_Object::_duplicate(theMainObj);
00232 }