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