Back to index

salome-smesh  6.5.0
SMESHGUI_MeshUtils.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 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_MeshUtils.cxx
00025 // Author : Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_MeshUtils.h"
00029 
00030 #include "SMESHGUI.h"
00031 #include "SMESHGUI_Utils.h"
00032 
00033 // SALOME KERNEL includes
00034 #include <SALOMEDSClient_Study.hxx>
00035 
00036 // Qt includes
00037 #include <QStringList>
00038 
00039 // IDL includes
00040 #include <SALOMEconfig.h>
00041 #include CORBA_SERVER_HEADER(SMESH_Group)
00042 #include CORBA_SERVER_HEADER(SMESH_Measurements)
00043 
00044 namespace SMESH
00045 {
00046   SMESH_Mesh_var GetMeshByIO(const Handle(SALOME_InteractiveObject)& theIO)
00047   {
00048     CORBA::Object_var anObj = IObjectToObject(theIO);
00049     if(!CORBA::is_nil(anObj)){
00050       SMESH_Mesh_var aMesh = SMESH_Mesh::_narrow(anObj);
00051       if(!CORBA::is_nil(aMesh))
00052         return aMesh;
00053       SMESH_GroupBase_var aGroup = SMESH_GroupBase::_narrow(anObj);
00054       if(!CORBA::is_nil(aGroup))
00055         return aGroup->GetMesh();
00056       SMESH_subMesh_var aSubMesh = SMESH_subMesh::_narrow(anObj);
00057       if(!CORBA::is_nil(aSubMesh))
00058         return aSubMesh->GetFather();
00059     }
00060     return SMESH_Mesh::_nil();
00061   }
00062 
00063   QString UniqueMeshName(const QString& theBaseName, const QString& thePostfix)
00064   {
00065     QString baseName = thePostfix.isEmpty() ? 
00066       theBaseName : theBaseName + "_" + thePostfix;
00067     if ( _PTR(Study) aStudy = GetActiveStudyDocument() ) {
00068       QString name = baseName;
00069       while ( !aStudy->FindObjectByName( name.toLatin1().data(), "SMESH" ).empty() ) {
00070         int nb = 0;
00071         QStringList names = name.split("_", QString::KeepEmptyParts);
00072         if ( names.count() > 0 ) {
00073           bool ok;
00074           int index = names.last().toInt( &ok );
00075           if ( ok ) {
00076             nb = index;
00077             names.removeLast();
00078           }
00079         }
00080         names.append( QString::number( nb+1 ) );
00081         name = names.join( "_" );
00082       }
00083       return name;
00084     }
00085     return baseName;
00086   }
00087 
00088   QString UniqueName(const QString& theBaseName, _PTR(SObject) theParent, const QString& thePostfix)
00089   {
00090     QString baseName = thePostfix.isEmpty() ? 
00091       theBaseName : theBaseName + "_" + thePostfix;
00092     QString name = baseName;
00093     if ( _PTR(Study) aStudy = GetActiveStudyDocument() ) {
00094       _PTR(SObject) p = theParent;
00095       if ( !p ) p = aStudy->FindComponent( "SMESH" );
00096       if ( p ) {
00097         _PTR(ChildIterator) iter = aStudy->NewChildIterator( p );
00098         int idx = 0;
00099         while( true ) {
00100           bool found = false;
00101           for ( ; iter->More(); iter->Next() ) {
00102             _PTR(SObject) so = iter->Value();
00103             if ( !so ) continue; // skip bad objects
00104             _PTR(SObject) ref;
00105             if ( so->ReferencedObject( ref ) ) continue; // skip references
00106             QString n = so->GetName().c_str();
00107             if ( !n.isEmpty() && n == name ) {
00108               QStringList names = name.split("_", QString::KeepEmptyParts);
00109               if ( names.count() > 0 ) {
00110                 bool ok;
00111                 names.last().toInt( &ok );
00112                 if ( ok )
00113                   names.removeLast();
00114               }
00115               names.append( QString::number( ++idx ) );
00116               name = names.join( "_" );
00117               found = true;
00118               break;
00119             }
00120           }
00121           if ( !found ) break;
00122         }
00123       }
00124     }
00125     return name;
00126   }
00127 
00128   SMESH::Measurements_var& GetMeasurements()
00129   {
00130     static SMESH::Measurements_var aMeasurements;
00131     if (CORBA::is_nil(aMeasurements)) {
00132       aMeasurements = SMESHGUI::GetSMESHGen()->CreateMeasurements();
00133     }
00134     return aMeasurements;
00135   }
00136 } // end of namespace SMESH