Salome HOME
PAL11238: Crash in ExportMED.
[modules/smesh.git] / src / SMESH_I / SMESH_Mesh_i.cxx
index 2f8c2c306efb24cbc88ad8970ad987afc5f6c146..d6b5b420d0971bdf4702fd0985a2b334f4e6ca3a 100644 (file)
 #include "Utils_SINGLETON.hxx"
 #include "OpUtil.hxx"
 
-#include "TCollection_AsciiString.hxx"
 #include "SMESHDS_Command.hxx"
 #include "SMESHDS_CommandType.hxx"
 #include "SMESH_MeshEditor_i.hxx"
 #include "SMESH_Gen_i.hxx"
 #include "DriverMED_R_SMESHDS_Mesh.h"
 
+// OCCT Includes
+#include <OSD_Path.hxx>
+#include <OSD_File.hxx>
+#include <OSD_Directory.hxx>
+#include <OSD_Protection.hxx>
 #include <TColStd_MapOfInteger.hxx>
 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
 #include <TColStd_SequenceOfInteger.hxx>
+#include <TCollection_AsciiString.hxx>
 
+// STL Includes
 #include <string>
 #include <iostream>
 #include <sstream>
@@ -76,7 +82,8 @@ SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
                            CORBA::Long studyId )
 : SALOME::GenericObj_i( thePOA )
 {
-  INFOS("SMESH_Mesh_i");
+  INFOS("SMESH_Mesh_i; this = "<<this);
+  _impl = NULL;
   _gen_i = gen_i;
   _id = myIdGenerator++;
   _studyId = studyId;
@@ -91,7 +98,7 @@ SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
 
 SMESH_Mesh_i::~SMESH_Mesh_i()
 {
-  INFOS("~SMESH_Mesh_i");
+  INFOS("~SMESH_Mesh_i; this = "<<this);
   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
@@ -262,6 +269,12 @@ int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshNam
   list<int> aGroupIds = _impl->GetGroupIds();
   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
+
+    // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
+    SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
+    aGroupImpl->Register();
+    // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
+
     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
 
@@ -1051,17 +1064,59 @@ SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
 
 //=============================================================================
 /*!
- *  
+ *  Export
  */
 //=============================================================================
 
+static void PrepareForWriting (const char* file)
+{
+  TCollection_AsciiString aFullName ((char*)file);
+  OSD_Path aPath (aFullName);
+  OSD_File aFile (aPath);
+  if (aFile.Exists()) {
+    // existing filesystem node
+    if (aFile.KindOfFile() == OSD_FILE) {
+      if (aFile.IsWriteable()) {
+        aFile.Reset();
+        aFile.Remove();
+        if (aFile.Failed()) {
+          TCollection_AsciiString msg ("File ");
+          msg += aFullName + " cannot be replaced.";
+          THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
+        }
+      } else {
+        TCollection_AsciiString msg ("File ");
+        msg += aFullName + " cannot be overwritten.";
+        THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
+      }
+    } else {
+      TCollection_AsciiString msg ("Location ");
+      msg += aFullName + " is not a file.";
+      THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
+    }
+  } else {
+    // nonexisting file; check if it can be created
+    aFile.Reset();
+    aFile.Build(OSD_WriteOnly, OSD_Protection());
+    if (aFile.Failed()) {
+      TCollection_AsciiString msg ("You cannot create the file ");
+      msg += aFullName + ". Check the directory existance and access rights.";
+      THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
+    } else {
+      aFile.Close();
+      aFile.Remove();
+    }
+  }
+}
+
 void SMESH_Mesh_i::ExportToMED( const char* file, 
                                CORBA::Boolean auto_groups, 
                                SMESH::MED_VERSION theVersion )
   throw(SALOME::SALOME_Exception)
 {
   Unexpect aCatch(SALOME_SalomeException);
-  
+
+  PrepareForWriting(file);
   char* aMeshName = "Mesh";
   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
   if ( !aStudy->_is_nil() ) {
@@ -1103,17 +1158,20 @@ void SMESH_Mesh_i::ExportMED( const char* file,
 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
 {
   Unexpect aCatch(SALOME_SalomeException);
+  PrepareForWriting(file);
   _impl->ExportDAT(file);
 }
 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
 {
   Unexpect aCatch(SALOME_SalomeException);
+  PrepareForWriting(file);
   _impl->ExportUNV(file);
 }
 
 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
 {
   Unexpect aCatch(SALOME_SalomeException);
+  PrepareForWriting(file);
   _impl->ExportSTL(file, isascii);
 }