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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_GeomTool.h"
23 #include <TopoDS_Shape.hxx>
25 #include <BRepTools.hxx>
29 #pragma warning ( disable: 4251 )
32 #include <SALOME_NamingService.hxx>
33 #include <SALOME_LifeCycleCORBA.hxx>
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
39 #pragma warning ( default: 4251 )
42 #include <QStringList>
47 static CORBA::ORB_var _ORB = CORBA::ORB_init( _argc, 0, "omniORB4"/*CORBA::ORB_ID*/ );
48 static SALOME_NamingService _NS( _ORB );
49 static SALOME_LifeCycleCORBA _LCC( &_NS );
52 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const int theStudyId, const QString& theIOR )
54 // Note that GEOMBase::GetShape() cause crash in batch mode
56 TopoDS_Shape aResShape;
58 CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
59 if ( !CORBA::is_nil( aCorbaObj ) ) {
60 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
62 Engines::EngineComponent_var aComp =
63 _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
64 GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
66 aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
72 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
74 Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
75 GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
77 return aGEOMEngine._retn();
80 SALOMEDS::Study_var HYDROData_GeomTool::GetStudyByID( const int theStudyId )
82 CORBA::Object_var aSMObject = _NS.Resolve("/myStudyManager");
83 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
84 SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
86 return aDSStudy._retn();
89 QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QString& theBaseName )
91 QString aName = theBaseName;
93 if ( !theStudy->_is_nil() ) {
94 // collect all object names of GEOM component
95 QSet<QString> anUsedNames;
96 SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
97 if ( !aComponent->_is_nil() ) {
98 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
99 for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
100 anUsedNames.insert( anIter->Value()->GetName() );
104 // build an unique name
106 bool isUnique = false;
107 QString aPrefix = theBaseName;
108 QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
109 if ( aParts.count() > 1 ) {
111 aNumber = aParts.last().toLong( &isOk );
114 aPrefix = aParts.join( "_" );
119 while ( !isUnique ) {
120 aName = aPrefix + "_" + QString::number( ++aNumber );
121 isUnique = ( !anUsedNames.contains( aName ) );
128 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM(
129 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
130 const TopoDS_Shape& theShape, const QString& theName,
131 QString& theGeomObjEntry )
133 theGeomObjEntry = "";
134 GEOM::GEOM_Object_var aGeomObj;
136 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
137 theShape.IsNull() ) {
138 return aGeomObj._retn();
141 std::ostringstream aStreamShape;
142 // Write TopoDS_Shape in ASCII format to the stream
143 BRepTools::Write( theShape, aStreamShape );
144 // Returns the number of bytes that have been stored in the stream's buffer.
145 int aSize = aStreamShape.str().size();
146 // Allocate octect buffer of required size
147 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
148 // Copy ostrstream content to the octect buffer
149 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
151 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
153 // Restore shape from the stream and get the GEOM object
154 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
155 aGeomObj = anInsOp->RestoreShape( aSeqFile );
157 // Publish the GEOM object
158 theGeomObjEntry = publishGEOMObject( theGeomEngine, theStudy, aGeomObj, theName );
160 return aGeomObj._retn();
164 GEOM::GEOM_Object_ptr HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( GEOM::GEOM_Gen_var theGeomEngine,
165 SALOMEDS::Study_ptr theStudy,
166 const TopoDS_Shape& theShape,
167 const NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher>& aFacesToNameModif,
168 const QString& theName,
169 QString& theGeomObjEntry)
171 GEOM::GEOM_Object_ptr aGeomObj = HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, theShape, theName, theGeomObjEntry );
173 GEOM::GEOM_IShapesOperations_var anExpOp = theGeomEngine->GetIShapesOperations( theStudy->StudyId() );
174 GEOM::ListOfGO* aFc = anExpOp->MakeExplode(aGeomObj, TopAbs_FACE, false);
176 TopTools_MapOfShape mapShape;
178 TopExp_Explorer exp (theShape, TopAbs_FACE);
179 QVector<QString> Names;
180 for (; exp.More(); exp.Next())
182 const TopoDS_Shape& csh = exp.Current();
183 if (mapShape.Add(csh))
185 //listShape.Append(csh);
186 QString Qstr = aFacesToNameModif.FindFromKey(csh);
187 Names.push_back(Qstr);
191 for (size_t i = 0; i < aFc->length(); i++)
192 GEOMBase::PublishSubObject(aFc->operator[](i), Names[i]);
198 GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
199 SALOMEDS::Study_ptr theStudy,
202 const QString& theName,
203 QString& theFaceEntry )
206 GEOM::GEOM_Object_var aGeomObj;
208 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
209 return aGeomObj._retn();
212 GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
213 GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
215 GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
216 GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ( 0.5 * theWidth, -0.5 * theHeight, 0 );
217 GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ( 0.5 * theWidth, 0.5 * theHeight, 0 );
218 GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, 0.5 * theHeight, 0 );
220 GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
223 theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
225 return aFace._retn();
228 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
229 SALOMEDS::Study_ptr theStudy,
230 GEOM::GEOM_Object_ptr theGeomObj,
231 const QString& theName )
235 if ( !theGeomObj->_is_nil() ) {
236 QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
238 SALOMEDS::SObject_var aResultSO =
239 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
240 theGeomObj, qPrintable( aName ) );
241 if ( !aResultSO->_is_nil() ) {
242 anEntry = aResultSO->GetID();