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 );
50 static SALOMEDS::Study_var _STUDY = SALOMEDS::Study::_nil();
52 QString HYDROData_GeomTool::myContainerName = "FactoryServer";
54 void HYDROData_GeomTool::SetContainerName( const QString& theContainerName )
56 myContainerName = theContainerName;
59 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const QString& theIOR )
61 // Note that GEOMBase::GetShape() cause crash in batch mode
63 TopoDS_Shape aResShape;
65 CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
66 if ( !CORBA::is_nil( aCorbaObj ) ) {
67 GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
69 Engines::EngineComponent_var aComp =
70 _LCC.FindOrLoad_Component( myContainerName.toStdString().c_str(), "GEOM" );
71 GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
73 aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
79 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
81 Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( myContainerName.toStdString().c_str(), "GEOM" );
82 GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
84 return aGEOMEngine._retn();
87 SALOMEDS::Study_var HYDROData_GeomTool::GetStudy()
89 if (_STUDY->_is_nil())
91 CORBA::Object_var aStudyObject = _NS.Resolve("/Study");
92 _STUDY = SALOMEDS::Study::_narrow(aStudyObject);
94 return _STUDY._retn();
97 QString HYDROData_GeomTool::GetFreeName( const QString& theBaseName )
99 QString aName = theBaseName;
101 SALOMEDS::Study_var theStudy = GetStudy();
102 if ( !theStudy->_is_nil() ) {
103 // collect all object names of GEOM component
104 QSet<QString> anUsedNames;
105 SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
106 if ( !aComponent->_is_nil() ) {
107 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
108 for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
109 anUsedNames.insert( anIter->Value()->GetName() );
113 // build an unique name
115 bool isUnique = false;
116 QString aPrefix = theBaseName;
117 QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
118 if ( aParts.count() > 1 ) {
120 aNumber = aParts.last().toLong( &isOk );
123 aPrefix = aParts.join( "_" );
128 while ( !isUnique ) {
129 aName = aPrefix + "_" + QString::number( ++aNumber );
130 isUnique = ( !anUsedNames.contains( aName ) );
137 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM(
138 GEOM::GEOM_Gen_var theGeomEngine,
139 const TopoDS_Shape& theShape, const QString& theName,
140 QString& theGeomObjEntry )
142 theGeomObjEntry = "";
143 GEOM::GEOM_Object_var aGeomObj;
145 if ( theGeomEngine->_is_nil() ||
146 theShape.IsNull() ) {
147 return aGeomObj._retn();
150 std::ostringstream aStreamShape;
151 // Write TopoDS_Shape in ASCII format to the stream
152 BRepTools::Write( theShape, aStreamShape );
153 // Returns the number of bytes that have been stored in the stream's buffer.
154 int aSize = aStreamShape.str().size();
155 // Allocate octect buffer of required size
156 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
157 // Copy ostrstream content to the octect buffer
158 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
160 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
162 // Restore shape from the stream and get the GEOM object
163 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations();
164 aGeomObj = anInsOp->RestoreShape( aSeqFile );
166 // Publish the GEOM object
167 theGeomObjEntry = publishGEOMObject( theGeomEngine, aGeomObj, theName );
169 return aGeomObj._retn();
173 GEOM::GEOM_Object_ptr HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( GEOM::GEOM_Gen_var theGeomEngine,
174 const TopoDS_Shape& theShape,
175 const NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher>& aFacesToNameModif,
176 const QString& theName,
177 QString& theGeomObjEntry)
179 GEOM::GEOM_Object_ptr aGeomObj = HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theShape, theName, theGeomObjEntry );
181 GEOM::GEOM_IShapesOperations_var anExpOp = theGeomEngine->GetIShapesOperations();
182 GEOM::ListOfGO* aFc = anExpOp->MakeExplode(aGeomObj, TopAbs_FACE, false);
184 TopTools_MapOfShape mapShape;
186 NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFF2N;
187 for (int i = 1; i <= aFacesToNameModif.Extent(); i++)
189 TopExp_Explorer exp (aFacesToNameModif.FindKey(i), TopAbs_FACE);
190 for (;exp.More(); exp.Next())
191 aFF2N.Add(exp.Current(), aFacesToNameModif.FindFromIndex(i));
194 TopExp_Explorer exp (theShape, TopAbs_FACE);
195 QVector<QString> Names;
196 for (; exp.More(); exp.Next())
198 const TopoDS_Shape& csh = exp.Current();
199 if (mapShape.Add(csh))
201 //listShape.Append(csh);
202 QString Qstr = aFF2N.FindFromKey(csh);
203 Names.push_back(Qstr);
207 for (size_t i = 0; i < aFc->length(); i++)
209 std::string name = Names[i].toStdString();
210 GEOM::GEOM_Object_ptr anObj = aFc->operator[](i);
211 //GEOMBase::PublishSubObject( anObj, name.c_str() );
212 theGeomEngine->AddInStudy( anObj, name.c_str(), aGeomObj );
219 GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
222 const QString& theName,
223 QString& theFaceEntry )
226 GEOM::GEOM_Object_var aGeomObj;
228 if ( theGeomEngine->_is_nil() ) {
229 return aGeomObj._retn();
232 GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations();
233 GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations();
235 GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
236 GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ( 0.5 * theWidth, -0.5 * theHeight, 0 );
237 GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ( 0.5 * theWidth, 0.5 * theHeight, 0 );
238 GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, 0.5 * theHeight, 0 );
240 GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
243 theFaceEntry = publishGEOMObject( theGeomEngine, aFace, theName );
245 return aFace._retn();
248 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
249 GEOM::GEOM_Object_ptr theGeomObj,
250 const QString& theName )
254 if ( !theGeomObj->_is_nil() ) {
255 QString aName = HYDROData_GeomTool::GetFreeName( theName );
257 SALOMEDS::SObject_var aResultSO =
258 theGeomEngine->PublishInStudy( SALOMEDS::SObject::_nil(),
259 theGeomObj, qPrintable( aName ) );
260 if ( !aResultSO->_is_nil() ) {
261 anEntry = aResultSO->GetID();