Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[modules/hydro.git] / src / HYDROData / HYDROData_GeomTool.cxx
index 5bfd9e38cd59624afbedf35f3da7c66b13349aa3..a152909c98247f0e26f54693bd0f917b820c8ba7 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
 
 #include "HYDROData_GeomTool.h"
 
+#ifndef LIGHT_MODE
+
 #include <TopoDS_Shape.hxx>
 
+#include <BRepTools.hxx>
+#include <GEOMBase.h>
+
+#ifdef WIN32
+  #pragma warning ( disable: 4251 )
+#endif
+
 #include <SALOME_NamingService.hxx>
 #include <SALOME_LifeCycleCORBA.hxx>
 
+#include <TopTools_MapOfShape.hxx>
+#include <TopExp_Explorer.hxx>
+
+#ifdef WIN32
+  #pragma warning ( default: 4251 )
+#endif
+
 #include <QStringList>
+#include <QVector>
 #include <QSet>
 
 static int _argc = 0;
@@ -88,7 +101,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 +123,140 @@ 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::ExplodeShapeInGEOMandPublish( GEOM::GEOM_Gen_var theGeomEngine, 
+  SALOMEDS::Study_ptr theStudy, 
+  const TopoDS_Shape& theShape,   
+  const NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher>& aFacesToNameModif,  
+  const QString& theName,
+  QString& theGeomObjEntry)
+{ 
+  GEOM::GEOM_Object_ptr aGeomObj = HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, theShape, theName, theGeomObjEntry );
+
+  GEOM::GEOM_IShapesOperations_var anExpOp = theGeomEngine->GetIShapesOperations( theStudy->StudyId() );
+  GEOM::ListOfGO* aFc = anExpOp->MakeExplode(aGeomObj, TopAbs_FACE, false);
+
+  TopTools_MapOfShape mapShape;
+
+  NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFF2N;
+  for (int i = 1; i <= aFacesToNameModif.Extent(); i++)
+  {
+    TopExp_Explorer exp (aFacesToNameModif.FindKey(i), TopAbs_FACE);
+    for (;exp.More(); exp.Next())
+      aFF2N.Add(exp.Current(), aFacesToNameModif.FindFromIndex(i));
+  }
+
+  TopExp_Explorer exp (theShape, TopAbs_FACE);
+  QVector<QString> Names;
+  for (; exp.More(); exp.Next())
+  {
+    const TopoDS_Shape& csh = exp.Current();
+    if (mapShape.Add(csh))
+    {
+      //listShape.Append(csh);
+      QString Qstr = aFF2N.FindFromKey(csh);
+      Names.push_back(Qstr);
+    }
+  }
+
+  for (size_t i = 0; i < aFc->length(); i++)
+  {
+    std::string name = Names[i].toStdString();
+    GEOM::GEOM_Object_ptr anObj = aFc->operator[](i);
+    //GEOMBase::PublishSubObject( anObj, name.c_str() );
+    theGeomEngine->AddInStudy( theStudy, anObj, name.c_str(), aGeomObj );
+  }
+
+  return aGeomObj;
+}
+
+
+ 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;
+}
+
+#endif