Salome HOME
merge BR_LAND_COVER_MAP
[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 #ifndef LIGHT_MODE
22
23 #include <TopoDS_Shape.hxx>
24
25 #include <BRepTools.hxx>
26
27 #ifdef WIN32
28   #pragma warning ( disable: 4251 )
29 #endif
30
31 #include <SALOME_NamingService.hxx>
32 #include <SALOME_LifeCycleCORBA.hxx>
33
34 #ifdef WIN32
35   #pragma warning ( default: 4251 )
36 #endif
37
38 #include <QStringList>
39 #include <QSet>
40
41 static int _argc = 0;
42 static CORBA::ORB_var _ORB = CORBA::ORB_init( _argc, 0, "omniORB4"/*CORBA::ORB_ID*/ );
43 static SALOME_NamingService _NS( _ORB );
44 static SALOME_LifeCycleCORBA _LCC( &_NS );
45
46
47 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const int theStudyId, const QString& theIOR )
48 {
49   // Note that GEOMBase::GetShape() cause crash in batch mode
50
51   TopoDS_Shape aResShape;
52    
53   CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
54   if ( !CORBA::is_nil( aCorbaObj ) ) {
55     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
56
57     Engines::EngineComponent_var aComp =
58       _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
59     GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
60
61     aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
62   }
63
64   return aResShape;
65 }
66
67 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
68 {
69   Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
70   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
71
72   return aGEOMEngine._retn();
73 }
74
75 SALOMEDS::Study_var HYDROData_GeomTool::GetStudyByID( const int theStudyId )
76 {
77   CORBA::Object_var aSMObject = _NS.Resolve("/myStudyManager");
78   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
79   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
80
81   return aDSStudy._retn();
82 }
83
84 QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QString& theBaseName )
85 {
86   QString aName = theBaseName;
87
88   if ( !theStudy->_is_nil() ) {
89     // collect all object names of GEOM component
90     QSet<QString> anUsedNames;
91     SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
92     if ( !aComponent->_is_nil() ) {
93       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
94       for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
95         anUsedNames.insert( anIter->Value()->GetName() );
96       }
97     }
98
99     // build an unique name
100     int aNumber = 0;
101     bool isUnique = false;
102     QString aPrefix = theBaseName;
103     QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
104     if ( aParts.count() > 1 ) {
105       bool isOk;
106       aNumber = aParts.last().toLong( &isOk );
107       if ( isOk ) {
108         aParts.removeLast();
109         aPrefix = aParts.join( "_" );
110         aNumber--;
111       }
112     }
113     
114     while ( !isUnique ) {
115       aName = aPrefix + "_" + QString::number( ++aNumber );
116       isUnique = ( !anUsedNames.contains( aName ) );
117     }
118   }
119
120   return aName;
121 }
122
123 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM( 
124   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
125   const TopoDS_Shape& theShape, const QString& theName,
126   QString& theGeomObjEntry )
127 {
128   theGeomObjEntry = "";
129   GEOM::GEOM_Object_var aGeomObj;
130
131   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
132        theShape.IsNull() ) {
133     return aGeomObj._retn();
134   }
135
136   std::ostringstream aStreamShape;
137   // Write TopoDS_Shape in ASCII format to the stream
138   BRepTools::Write( theShape, aStreamShape );
139   // Returns the number of bytes that have been stored in the stream's buffer.
140   int aSize = aStreamShape.str().size();
141   // Allocate octect buffer of required size
142   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
143   // Copy ostrstream content to the octect buffer
144   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
145   // Create TMPFile
146   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
147
148   // Restore shape from the stream and get the GEOM object
149   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
150   aGeomObj = anInsOp->RestoreShape( aSeqFile );
151   
152   // Publish the GEOM object
153   theGeomObjEntry = publishGEOMObject( theGeomEngine, theStudy, aGeomObj, theName );
154   
155   return aGeomObj._retn();
156 }
157
158  GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
159                                                              SALOMEDS::Study_ptr theStudy,
160                                                              const int theWidth,
161                                                              const int theHeight,
162                                                              const QString& theName,
163                                                              QString& theFaceEntry )
164 {
165   theFaceEntry = "";
166   GEOM::GEOM_Object_var aGeomObj;
167
168   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
169     return aGeomObj._retn();
170   }
171
172   GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
173   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
174          
175   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
176   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
177   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
178   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
179   
180   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
181     
182   // Publish the face
183   theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
184
185   return aFace._retn();
186 }
187
188 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
189                                                SALOMEDS::Study_ptr theStudy,
190                                                GEOM::GEOM_Object_ptr theGeomObj,
191                                                const QString& theName )
192 {
193   QString anEntry;
194
195   if ( !theGeomObj->_is_nil() ) {
196     QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
197
198     SALOMEDS::SObject_var aResultSO = 
199       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
200                                      theGeomObj, qPrintable( aName ) );
201     if ( !aResultSO->_is_nil() ) {
202       anEntry = aResultSO->GetID();
203     }
204   }
205
206   return anEntry;
207 }
208
209 #endif