Salome HOME
10c0bf4ca90b029ea409f5201125a0c3b8199ae6
[modules/hydro.git] / src / HYDROData / HYDROData_GeomTool.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_GeomTool.h"
20
21 #include <TopoDS_Shape.hxx>
22
23 #include <SALOME_NamingService.hxx>
24 #include <SALOME_LifeCycleCORBA.hxx>
25
26 #include <QStringList>
27 #include <QSet>
28
29 static int _argc = 0;
30 static CORBA::ORB_var _ORB = CORBA::ORB_init( _argc, 0, "omniORB4"/*CORBA::ORB_ID*/ );
31 static SALOME_NamingService _NS( _ORB );
32 static SALOME_LifeCycleCORBA _LCC( &_NS );
33
34
35 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const int theStudyId, const QString& theIOR )
36 {
37   // Note that GEOMBase::GetShape() cause crash in batch mode
38
39   TopoDS_Shape aResShape;
40    
41   CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
42   if ( !CORBA::is_nil( aCorbaObj ) ) {
43     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
44
45     Engines::EngineComponent_var aComp =
46       _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
47     GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
48
49     aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
50   }
51
52   return aResShape;
53 }
54
55 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
56 {
57   Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
58   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
59
60   return aGEOMEngine._retn();
61 }
62
63 SALOMEDS::Study_var HYDROData_GeomTool::GetStudyByID( const int theStudyId )
64 {
65   CORBA::Object_var aSMObject = _NS.Resolve("/myStudyManager");
66   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
67   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
68
69   return aDSStudy._retn();
70 }
71
72 QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QString& theBaseName )
73 {
74   QString aName = theBaseName;
75
76   if ( !theStudy->_is_nil() ) {
77     // collect all object names of GEOM component
78     QSet<QString> anUsedNames;
79     SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
80     if ( !aComponent->_is_nil() ) {
81       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
82       for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
83         anUsedNames.insert( anIter->Value()->GetName() );
84       }
85     }
86
87     // build an unique name
88     int aNumber = 0;
89     bool isUnique = false;
90     QString aPrefix = theBaseName;
91     QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
92     if ( aParts.count() > 1 ) {
93       bool isOk;
94       aNumber = aParts.last().toLong( &isOk );
95       if ( isOk ) {
96         aParts.removeLast();
97         aPrefix = aParts.join( "_" );
98         aNumber--;
99       }
100     }
101     
102     while ( !isUnique ) {
103       aName = aPrefix + "_" + QString::number( ++aNumber );
104       isUnique = ( !anUsedNames.contains( aName ) );
105     }
106   }
107
108   return aName;
109 }