Salome HOME
middle z for 3D guideline
[modules/hydro.git] / src / HYDROData / HYDROData_GeomTool.cxx
index 5bfd9e38cd59624afbedf35f3da7c66b13349aa3..a83258e61f7bff4b896da5ca198a886e923faee6 100644 (file)
@@ -1,12 +1,8 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -24,6 +20,8 @@
 
 #include <TopoDS_Shape.hxx>
 
+#include <BRepTools.hxx>
+
 #include <SALOME_NamingService.hxx>
 #include <SALOME_LifeCycleCORBA.hxx>
 
@@ -88,7 +86,7 @@ QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QSt
       }
     }
 
-    // build a unique name
+    // build an unique name
     int aNumber = 0;
     bool isUnique = false;
     QString aPrefix = theBaseName;
@@ -110,4 +108,90 @@ QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QSt
   }
 
   return aName;
-}
\ No newline at end of file
+}
+
+GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM( 
+  GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
+  const TopoDS_Shape& theShape, const QString& theName,
+  QString& theGeomObjEntry )
+{
+  theGeomObjEntry = "";
+  GEOM::GEOM_Object_var aGeomObj;
+
+  if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
+       theShape.IsNull() ) {
+    return aGeomObj._retn();
+  }
+
+  std::ostringstream aStreamShape;
+  // Write TopoDS_Shape in ASCII format to the stream
+  BRepTools::Write( theShape, aStreamShape );
+  // Returns the number of bytes that have been stored in the stream's buffer.
+  int aSize = aStreamShape.str().size();
+  // Allocate octect buffer of required size
+  CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
+  // Copy ostrstream content to the octect buffer
+  memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
+  // Create TMPFile
+  SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
+
+  // Restore shape from the stream and get the GEOM object
+  GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
+  aGeomObj = anInsOp->RestoreShape( aSeqFile );
+  
+  // Publish the GEOM object
+  theGeomObjEntry = publishGEOMObject( theGeomEngine, theStudy, aGeomObj, theName );
+  
+  return aGeomObj._retn();
+}
+
+ GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
+                                                             SALOMEDS::Study_ptr theStudy,
+                                                             const int theWidth,
+                                                             const int theHeight,
+                                                             const QString& theName,
+                                                             QString& theFaceEntry )
+{
+  theFaceEntry = "";
+  GEOM::GEOM_Object_var aGeomObj;
+
+  if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
+    return aGeomObj._retn();
+  }
+
+  GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
+  GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
+         
+  GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
+  GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
+  GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
+  GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
+  
+  GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
+    
+  // Publish the face
+  theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
+
+  return aFace._retn();
+}
+
+QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
+                                               SALOMEDS::Study_ptr theStudy,
+                                               GEOM::GEOM_Object_ptr theGeomObj,
+                                               const QString& theName )
+{
+  QString anEntry;
+
+  if ( !theGeomObj->_is_nil() ) {
+    QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
+
+    SALOMEDS::SObject_var aResultSO = 
+      theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
+                                     theGeomObj, qPrintable( aName ) );
+    if ( !aResultSO->_is_nil() ) {
+      anEntry = aResultSO->GetID();
+    }
+  }
+
+  return anEntry;
+}