Salome HOME
Bug #490: batch mode error.
[modules/hydro.git] / src / HYDROData / HYDROData_GeomTool.cxx
1 // Copyright (C) 2007-2013  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.
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 #include "HYDROData_GeomTool.h"
24
25 #include <TopoDS_Shape.hxx>
26
27 #include <SALOME_NamingService.hxx>
28 #include <SALOME_LifeCycleCORBA.hxx>
29
30 #include <QStringList>
31 #include <QSet>
32
33 static int _argc = 0;
34 static CORBA::ORB_var _ORB = CORBA::ORB_init( _argc, 0, "omniORB4"/*CORBA::ORB_ID*/ );
35 static SALOME_NamingService _NS( _ORB );
36 static SALOME_LifeCycleCORBA _LCC( &_NS );
37
38
39 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const int theStudyId, const QString& theIOR )
40 {
41   // Note that GEOMBase::GetShape() cause crash in batch mode
42
43   TopoDS_Shape aResShape;
44    
45   CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
46   if ( !CORBA::is_nil( aCorbaObj ) ) {
47     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
48
49     Engines::EngineComponent_var aComp =
50       _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
51     GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
52
53     aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
54   }
55
56   return aResShape;
57 }
58
59 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
60 {
61   Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
62   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
63
64   return aGEOMEngine._retn();
65 }
66
67 SALOMEDS::Study_var HYDROData_GeomTool::GetStudyByID( const int theStudyId )
68 {
69   CORBA::Object_var aSMObject = _NS.Resolve("/myStudyManager");
70   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
71   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
72
73   return aDSStudy._retn();
74 }
75
76 QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QString& theBaseName )
77 {
78   QString aName = theBaseName;
79
80   if ( !theStudy->_is_nil() ) {
81     // collect all object names of GEOM component
82     QSet<QString> anUsedNames;
83     SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
84     if ( !aComponent->_is_nil() ) {
85       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
86       for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
87         anUsedNames.insert( anIter->Value()->GetName() );
88       }
89     }
90
91     // build a unique name
92     int aNumber = 0;
93     bool isUnique = false;
94     QString aPrefix = theBaseName;
95     QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
96     if ( aParts.count() > 1 ) {
97       bool isOk;
98       aNumber = aParts.last().toLong( &isOk );
99       if ( isOk ) {
100         aParts.removeLast();
101         aPrefix = aParts.join( "_" );
102         aNumber--;
103       }
104     }
105     
106     while ( !isUnique ) {
107       aName = aPrefix + "_" + QString::number( ++aNumber );
108       isUnique = ( !anUsedNames.contains( aName ) );
109     }
110   }
111
112   return aName;
113 }