Salome HOME
Merge from BR_siman_phase1 14/02/2013
authorvsr <vsr@opencascade.com>
Thu, 14 Feb 2013 12:33:53 +0000 (12:33 +0000)
committervsr <vsr@opencascade.com>
Thu, 14 Feb 2013 12:33:53 +0000 (12:33 +0000)
src/SMESH_I/SMESH_Gen_i.cxx
src/SMESH_I/SMESH_Gen_i.hxx

index 8fb303ad35ce421875c92c6f75190fc78a4ae206..e901455925c92869c121275528aa043c4b94720e 100644 (file)
 #include <GEOM_Client.hxx>
 
 #include <Basics_Utils.hxx>
+#include <Basics_DirUtils.hxx>
 #include <HDFOI.hxx>
 #include <OpUtil.hxx>
 #include <SALOMEDS_Tool.hxx>
 #include <Utils_ExceptHandlers.hxx>
 #include <Utils_SINGLETON.hxx>
 #include <utilities.h>
+#include <SALOME_DataContainer_i.hxx>
 
 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
 #include CORBA_CLIENT_HEADER(SALOME_Session)
@@ -293,6 +295,9 @@ SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr            orb,
   myIsHistoricalPythonDump = true;
   myToForgetMeshDataOnHypModif = false;
 
+  myImportedStudyChanged = true;
+  myImportedStudyId      = 0;
+
   // set it in standalone mode only
   //OSD::SetSignal( true );
 
@@ -4903,6 +4908,9 @@ int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
 {
   StudyContext* myStudyContext = GetCurrentStudyContext();
   if ( myStudyContext && !CORBA::is_nil( theObject )) {
+    if (GetCurrentStudyID() == myImportedStudyId)
+      myImportedStudyChanged = true;
+    
     CORBA::String_var iorString = GetORB()->object_to_string( theObject );
     return myStudyContext->addObject( string( iorString.in() ) );
   }
@@ -4961,6 +4969,149 @@ char* SMESH_Gen_i::getVersion()
 #endif
 }
 
+//=================================================================================
+// function : importData
+// purpose  : imports mesh data file (the med one) into the SMESH internal data structure
+//=================================================================================
+Engines::ListOfIdentifiers* SMESH_Gen_i::importData(
+  CORBA::Long studyId, Engines::DataContainer_ptr data, const Engines::ListOfOptions& options)
+{
+  Engines::ListOfIdentifiers_var aResultIds = new Engines::ListOfIdentifiers;
+  list<string> aResultList;
+
+  CORBA::Object_var aSMObject = myNS->Resolve( "/myStudyManager" );
+  SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow( aSMObject );
+  SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID( studyId );
+  SetCurrentStudy(aStudy);
+
+  // load and store temporary imported file
+  string aFileName = Kernel_Utils::GetTmpFileName();
+  aFileName += string(".") + data->extension();
+  Engines::TMPFile* aFileStream = data->get();
+  const char *aBuffer = (const char*)aFileStream->NP_data();
+#ifdef WIN32
+  std::ofstream aFile(aFileName.c_str(), std::ios::binary);
+#else
+  std::ofstream aFile(aFileName.c_str());
+#endif
+  aFile.write(aBuffer, aFileStream->length());
+  aFile.close();
+
+  // Retrieve mesh names from the file
+  DriverMED_R_SMESHDS_Mesh aReader;
+  aReader.SetFile( aFileName );
+  aReader.SetMeshId(-1);
+  Driver_Mesh::Status aStatus;
+  list<string> aNames = aReader.GetMeshNames(aStatus);
+  SMESH::mesh_array_var aResult = new SMESH::mesh_array();
+  SMESH::DriverMED_ReadStatus aStatus2 = (SMESH::DriverMED_ReadStatus)aStatus;
+  if (aStatus2 == SMESH::DRS_OK) {
+    // Iterate through all meshes and create mesh objects
+    for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
+      // create mesh
+      SMESH::SMESH_Mesh_var mesh = createMesh();
+
+      // publish mesh in the study
+      SALOMEDS::SObject_var aSO;
+      if (CanPublishInStudy(mesh)) {
+        aSO = PublishMesh(aStudy, mesh.in(), (*it).c_str());
+        aResultList.push_back(aSO->GetID());
+      }
+      // Read mesh data (groups are published automatically by ImportMEDFile())
+      SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
+      ASSERT( meshServant );
+      meshServant->ImportMEDFile( aFileName.c_str(), (*it).c_str() );
+      //meshServant->GetImpl().GetMeshDS()->Modified();
+    }
+  } else {
+    MESSAGE("Opening MED file problems "<<aFileName.c_str())
+    return aResultIds._retn();
+  }
+
+  // remove temporary file 
+#ifdef WIN32
+  DeleteFileA(aFileName.c_str());
+#else
+  unlink(aFileName.c_str());
+#endif
+
+  if (!aResultList.empty()) {
+    aResultIds->length(aResultList.size());
+    list<string>::iterator aListIter = aResultList.begin();
+    for(int a = 0; aListIter != aResultList.end(); aListIter++, a++)
+      aResultIds[a] = aListIter->c_str();
+  }
+  
+  myImportedStudyId = studyId;
+  myImportedStudyChanged = false;
+
+  return aResultIds._retn();
+}
+
+//=================================================================================
+// function : getModifiedData
+// purpose  : exports all geometry of this GEOM module into one BRep file
+//=================================================================================
+Engines::ListOfData* SMESH_Gen_i::getModifiedData(CORBA::Long studyId)
+{
+  Engines::ListOfData_var aResult = new Engines::ListOfData;
+  
+  if (myImportStudyId == 0 || !myImportedStudyChanged) {
+    MESSAGE("Study is not changed")
+    return aResult._retn();
+  }
+
+  CORBA::Object_var aSMObject = myNS->Resolve("/myStudyManager");
+  SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
+  SALOMEDS::Study_var aStudy = aStudyManager->GetStudyByID(studyId);
+  SetCurrentStudy(aStudy);
+  SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("SMESH");
+  
+  if (CORBA::is_nil(aComponent))
+    return aResult._retn();
+
+  std::string aFullPath(Kernel_Utils::GetTmpFileName());
+  aFullPath += ".med";
+  DriverMED_W_SMESHDS_Mesh aWriter;
+  aWriter.SetFile(aFullPath.c_str());
+  StudyContext* myStudyContext = GetCurrentStudyContext();
+
+  SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(aComponent); // check only published meshes
+  int aNumMeshes = 0; // number of meshes in result
+  for(; anIter->More(); anIter->Next()) {
+    SALOMEDS::SObject_var aSO = anIter->Value();
+    CORBA::Object_var anObj = aSO->GetObject();
+    if (!CORBA::is_nil(anObj)) {
+      SMESH::SMESH_Mesh_var aCORBAMesh = SMESH::SMESH_Mesh::_narrow(anObj);
+      if(!aCORBAMesh->_is_nil()) {
+        SMESH_Mesh_i* myImpl = dynamic_cast<SMESH_Mesh_i*>(GetServant(aCORBAMesh).in());
+        if (myImpl) {
+          CORBA::String_var objStr = GetORB()->object_to_string(anObj);
+          int id = myStudyContext->findId(string(objStr.in()));
+          SMESHDS_Mesh* mySMESHDSMesh = myImpl->GetImpl().GetMeshDS();
+          if (mySMESHDSMesh->NbNodes() > 0) {
+            // write mesh data to med file
+            aWriter.SetMesh(mySMESHDSMesh);
+            aWriter.SetMeshId(id);
+            aNumMeshes++;
+          } else {
+            MESSAGE("Mesh has zero nodes and can not be exported");
+          }
+        }
+      }
+    }
+  }
+  if (aNumMeshes > 0) { // compund is correct, write it to the temporary file
+    MESSAGE("Write "<<aNumMeshes<<" meshes to "<<aFullPath.c_str());
+    aWriter.Perform();
+    aResult->length(1);
+    Engines::DataContainer_var aData = (new Engines_DataContainer_i(
+                    aFullPath.c_str(), "", "", true))->_this();
+    aResult[0] = aData;
+  }
+  return aResult._retn();
+}
+
 //=============================================================================
 /*!
  *  SMESHEngine_factory
index c78122345a1a3040e423c272cb2a78752622919f..0028ef9d4858db105e657bb552f55e252f88024c 100644 (file)
@@ -478,7 +478,14 @@ public:
 
   void CleanPythonTrace (int theStudyID);
 
-
+  // ============
+  // Check In / Check Out
+  // ============
+  virtual Engines::ListOfIdentifiers* importData(CORBA::Long studyId,
+                                                Engines::DataContainer_ptr data,
+                                                const Engines::ListOfOptions& options);
+  virtual Engines::ListOfData* getModifiedData(CORBA::Long studyId);
+    
   // *****************************************
   // Internal methods
   // *****************************************
@@ -621,6 +628,9 @@ private:
   std::map < int, Handle(TColStd_HSequenceOfAsciiString) > myPythonScripts;
   bool                                                     myIsHistoricalPythonDump;
   std::vector< std::string >                               myLastParameters;
+  
+  int myImportedStudyId; // identifier of the imported in importData study to keep no-modifiection flag for getModifiedData method
+  int myImportedStudyChanged; // flag that indicates that the imported study has been changed (by creation of the additional mesh)
 };