Salome HOME
Merge multi-study removal branch.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshUtils.cxx
1 // Copyright (C) 2007-2016  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 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_MeshUtils.cxx
25 // Author : Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_MeshUtils.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32
33 // SALOME KERNEL includes
34 #include <SALOMEDSClient_Study.hxx>
35
36 // Qt includes
37 #include <QStringList>
38
39 // IDL includes
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(SMESH_Group)
42 #include CORBA_SERVER_HEADER(SMESH_Measurements)
43
44 namespace SMESH
45 {
46   SMESH_Mesh_var GetMeshByIO(const Handle(SALOME_InteractiveObject)& theIO)
47   {
48     CORBA::Object_var anObj = IObjectToObject(theIO);
49     if(!CORBA::is_nil(anObj)){
50       SMESH_Mesh_var aMesh = SMESH_Mesh::_narrow(anObj);
51       if(!CORBA::is_nil(aMesh))
52         return aMesh;
53       SMESH_GroupBase_var aGroup = SMESH_GroupBase::_narrow(anObj);
54       if(!CORBA::is_nil(aGroup))
55         return aGroup->GetMesh();
56       SMESH_subMesh_var aSubMesh = SMESH_subMesh::_narrow(anObj);
57       if(!CORBA::is_nil(aSubMesh))
58         return aSubMesh->GetFather();
59     }
60     return SMESH_Mesh::_nil();
61   }
62
63   QString UniqueMeshName(const QString& theBaseName, const QString& thePostfix)
64   {
65     QString baseName = thePostfix.isEmpty() ? 
66       theBaseName : theBaseName + "_" + thePostfix;
67     if ( _PTR(Study) aStudy = getStudy() ) {
68       QString name = baseName;
69       while ( !aStudy->FindObjectByName( name.toLatin1().data(), "SMESH" ).empty() ) {
70         int nb = 0;
71         QStringList names = name.split("_", QString::KeepEmptyParts);
72         if ( names.count() > 0 ) {
73           bool ok;
74           int index = names.last().toInt( &ok );
75           if ( ok ) {
76             nb = index;
77             names.removeLast();
78           }
79         }
80         names.append( QString::number( nb+1 ) );
81         name = names.join( "_" );
82       }
83       return name;
84     }
85     return baseName;
86   }
87
88   QString UniqueName(const QString& theBaseName, _PTR(SObject) theParent, const QString& thePostfix)
89   {
90     QString baseName = thePostfix.isEmpty() ? 
91       theBaseName : theBaseName + "_" + thePostfix;
92     QString name = baseName;
93     if ( _PTR(Study) aStudy = getStudy() ) {
94       _PTR(SObject) p = theParent;
95       if ( !p ) p = aStudy->FindComponent( "SMESH" );
96       if ( p ) {
97         _PTR(ChildIterator) iter = aStudy->NewChildIterator( p );
98         iter->InitEx(/*allLevels=*/true);
99         int idx = 0;
100         while( true ) {
101           bool found = false;
102           for ( ; iter->More(); iter->Next() ) {
103             _PTR(SObject) so = iter->Value();
104             if ( !so ) continue; // skip bad objects
105             _PTR(SObject) ref;
106             if ( so->ReferencedObject( ref ) ) continue; // skip references
107             QString n = so->GetName().c_str();
108             if ( !n.isEmpty() && n == name ) {
109               QStringList names = name.split("_", QString::KeepEmptyParts);
110               if ( names.count() > 0 ) {
111                 bool ok;
112                 names.last().toInt( &ok );
113                 if ( ok )
114                   names.removeLast();
115               }
116               names.append( QString::number( ++idx ) );
117               name = names.join( "_" );
118               found = true;
119               break;
120             }
121           }
122           if ( !found ) break;
123         }
124       }
125     }
126     return name;
127   }
128
129   SMESH::Measurements_var& GetMeasurements()
130   {
131     static SMESH::Measurements_var aMeasurements;
132     if (CORBA::is_nil(aMeasurements)) {
133       aMeasurements = SMESHGUI::GetSMESHGen()->CreateMeasurements();
134     }
135     return aMeasurements;
136   }
137 } // end of namespace SMESH