Salome HOME
custom test runner to run only a subset of complete test suite
[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 #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 an 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 }
114
115 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM( 
116   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
117   const TopoDS_Shape& theShape, const QString& theName,
118   QString& theGeomObjEntry )
119 {
120   theGeomObjEntry = "";
121   GEOM::GEOM_Object_var aGeomObj;
122
123   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
124        theShape.IsNull() ) {
125     return aGeomObj._retn();
126   }
127
128   std::ostringstream aStreamShape;
129   // Write TopoDS_Shape in ASCII format to the stream
130   BRepTools::Write( theShape, aStreamShape );
131   // Returns the number of bytes that have been stored in the stream's buffer.
132   int aSize = aStreamShape.str().size();
133   // Allocate octect buffer of required size
134   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
135   // Copy ostrstream content to the octect buffer
136   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
137   // Create TMPFile
138   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
139
140   // Restore shape from the stream and get the GEOM object
141   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
142   aGeomObj = anInsOp->RestoreShape( aSeqFile );
143   
144   // Publish the GEOM object
145   theGeomObjEntry = publishGEOMObject( theGeomEngine, theStudy, aGeomObj, theName );
146   
147   return aGeomObj._retn();
148 }
149
150  GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
151                                                              SALOMEDS::Study_ptr theStudy,
152                                                              const int theWidth,
153                                                              const int theHeight,
154                                                              const QString& theName,
155                                                              QString& theFaceEntry )
156 {
157   theFaceEntry = "";
158   GEOM::GEOM_Object_var aGeomObj;
159
160   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
161     return aGeomObj._retn();
162   }
163
164   GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
165   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
166          
167   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
168   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
169   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
170   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
171   
172   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
173     
174   // Publish the face
175   theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
176
177   return aFace._retn();
178 }
179
180 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
181                                                SALOMEDS::Study_ptr theStudy,
182                                                GEOM::GEOM_Object_ptr theGeomObj,
183                                                const QString& theName )
184 {
185   QString anEntry;
186
187   if ( !theGeomObj->_is_nil() ) {
188     QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
189
190     SALOMEDS::SObject_var aResultSO = 
191       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
192                                      theGeomObj, qPrintable( aName ) );
193     if ( !aResultSO->_is_nil() ) {
194       anEntry = aResultSO->GetID();
195     }
196   }
197
198   return anEntry;
199 }
200
201 #endif