Salome HOME
merge from V4_1_0_maintainance branch (from tag mergeto_BR_QT4_Dev_29Jul08)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.cxx
index 520eba58fd4392c6b5242fd1fab6188487f222c4..3dbe49e5dd0e3fc6386227cced2045d237c98df9 100644 (file)
@@ -1,27 +1,48 @@
-using namespace std;
-
-#include "GEOMImpl_IInsertOperations.hxx"
+// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// 
+// 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.
+// 
+// This library is distributed in the hope that it will be useful 
+// but WITHOUT ANY WARRANTY; without even the implied warranty of 
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public  
+// License along with this library; if not, write to the Free Software 
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+#include <Standard_Stream.hxx>
+
+#include <GEOMImpl_IInsertOperations.hxx>
 
 #include "utilities.h"
-#include "OpUtil.hxx"
-#include "Utils_ExceptHandlers.hxx"
+#include <OpUtil.hxx>
+#include <Utils_ExceptHandlers.hxx>
 
 #include <TFunction_DriverTable.hxx>
 #include <TFunction_Driver.hxx>
 #include <TFunction_Logbook.hxx>
 #include <TDF_Tool.hxx>
 
-#include "GEOM_Function.hxx"
+#include <GEOM_Function.hxx>
+#include <GEOM_PythonDump.hxx>
 
-#include "GEOMImpl_CopyDriver.hxx"
-#include "GEOMImpl_ExportDriver.hxx"
-#include "GEOMImpl_ImportDriver.hxx"
+#include <GEOMImpl_CopyDriver.hxx>
+#include <GEOMImpl_ExportDriver.hxx>
+#include <GEOMImpl_ImportDriver.hxx>
 
-#include "GEOMImpl_ICopy.hxx"
-#include "GEOMImpl_IImportExport.hxx"
+#include <GEOMImpl_ICopy.hxx>
+#include <GEOMImpl_IImportExport.hxx>
 
-#include "GEOMImpl_Types.hxx"
+#include <GEOMImpl_Types.hxx>
 
+#include <Standard_Failure.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
 
 //=============================================================================
@@ -78,6 +99,9 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) the
 
   //Compute the Copy value
   try {
+#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
+    OCC_CATCH_SIGNALS;
+#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Copy driver failed");
       return NULL;
@@ -90,13 +114,7 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) the
   }
 
   //Make a Python command
-  TCollection_AsciiString anEntry, aDescr;
-  TDF_Tool::Entry(aCopy->GetEntry(), anEntry);
-  aDescr += (anEntry+" = IInsertOperations.MakeCopy(");
-  TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
-  aDescr += (anEntry+")");
-
-  aFunction->SetDescription(aDescr);
+  GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
 
   SetErrorCode(OK);
   return aCopy;
@@ -108,9 +126,9 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy(Handle(GEOM_Object) the
  */
 //=============================================================================
 void GEOMImpl_IInsertOperations::Export
-                     (const Handle(GEOM_Object) theOriginal,
-                     const char*               theFileName,
-                     const char*               theFormatName)
+                     (const Handle(GEOM_Object)      theOriginal,
+                     const TCollection_AsciiString& theFileName,
+                     const TCollection_AsciiString& theFormatName)
 {
   SetErrorCode(KO);
 
@@ -126,24 +144,26 @@ void GEOMImpl_IInsertOperations::Export
   //Check if the function is set correctly
   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
 
-  //Set parameters
-  GEOMImpl_IImportExport aCI (aFunction);
-  aCI.SetOriginal(aRefFunction);
-  char* aFileName = (char*)theFileName;
-  aCI.SetFileName(aFileName);
-
-  char* aFormatName = (char*)theFormatName;
   Handle(TCollection_HAsciiString) aHLibName;
-  if (!IsSupported(Standard_False, aFormatName, aHLibName)) {
+  if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
     return;
   }
   TCollection_AsciiString aLibName = aHLibName->String();
+
+  //Set parameters
+  GEOMImpl_IImportExport aCI (aFunction);
+  aCI.SetOriginal(aRefFunction);
+  aCI.SetFileName(theFileName);
+  aCI.SetFormatName(theFormatName);
   aCI.SetPluginName(aLibName);
 
   //Perform the Export
   try {
+#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
+    OCC_CATCH_SIGNALS;
+#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
-      SetErrorCode("Export driver failed");
+      SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
       return;
     }
   }
@@ -154,14 +174,8 @@ void GEOMImpl_IInsertOperations::Export
   }
 
   //Make a Python command
-  TCollection_AsciiString anEntry, aDescr;
-  aDescr = "IInsertOperations.Export(";
-  TDF_Tool::Entry(theOriginal->GetEntry(), anEntry);
-  aDescr += (anEntry + ", ");
-  aDescr += (TCollection_AsciiString(aFileName) + ", ");
-  aDescr += (TCollection_AsciiString(aFormatName) + ")");
-
-  aFunction->SetDescription(aDescr);
+  GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
+    << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
 
   SetErrorCode(OK);
 }
@@ -172,12 +186,12 @@ void GEOMImpl_IInsertOperations::Export
  */
 //=============================================================================
 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
-                     (const char* theFileName,
-                     const char* theFormatName)
+                                 (const TCollection_AsciiString& theFileName,
+                                 const TCollection_AsciiString& theFormatName)
 {
   SetErrorCode(KO);
 
-  if (!theFileName || !theFormatName) return NULL;
+  if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
 
   //Add a new result object
   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
@@ -189,21 +203,23 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   //Check if the function is set correctly
   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
 
-  //Set parameters
-  GEOMImpl_IImportExport aCI (aFunction);
-  char* aFileName = (char*)theFileName;
-  aCI.SetFileName(aFileName);
-
-  char* aFormatName = (char*)theFormatName;
   Handle(TCollection_HAsciiString) aHLibName;
-  if (!IsSupported(Standard_True, aFormatName, aHLibName)) {
+  if (!IsSupported(Standard_True, theFormatName, aHLibName)) {
     return result;
   }
   TCollection_AsciiString aLibName = aHLibName->String();
+
+  //Set parameters
+  GEOMImpl_IImportExport aCI (aFunction);
+  aCI.SetFileName(theFileName);
+  aCI.SetFormatName(theFormatName);
   aCI.SetPluginName(aLibName);
 
   //Perform the Import
   try {
+#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
+    OCC_CATCH_SIGNALS;
+#endif
     if (!GetSolver()->ComputeFunction(aFunction)) {
       SetErrorCode("Import driver failed");
       return NULL;
@@ -216,13 +232,8 @@ Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
   }
 
   //Make a Python command
-  TCollection_AsciiString anEntry, aDescr;
-  TDF_Tool::Entry(result->GetEntry(), anEntry);
-  aDescr += (anEntry + " = IInsertOperations.Import(");
-  aDescr += (TCollection_AsciiString(aFileName) + ", ");
-  aDescr += (TCollection_AsciiString(aFormatName) + ")");
-
-  aFunction->SetDescription(aDescr);
+  GEOM::TPythonDump(aFunction) << result << " = geompy.Import(\""
+    << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
 
   SetErrorCode(OK);
   return result;
@@ -249,7 +260,7 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
 
   if (!InitResMgr()) return Standard_False;
 
-  // Read Import formats list
+  // Read Import formats list from install directory
   if (myResMgr->Find("Import")) {
     TCollection_AsciiString aFormats (myResMgr->Value("Import"));
     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
@@ -259,17 +270,44 @@ Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
     }
   }
 
+  // Read Import formats from user directory
+  if (myResMgrUser->Find("Import")) {
+    TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
+    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
+    int i = 1;
+    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
+      int aLenFormats = theFormats->Length();
+      bool isFound = false;
+      for(int aInd=1;aInd<=aLenFormats;aInd++){
+       if( theFormats->Value(aInd) == aToken){
+         isFound = true;
+         break;
+       }
+      }
+      if(!isFound)
+       theFormats->Append(aToken);
+    }
+  }
+
   // Read Patterns for each supported format
   int j = 1, len = theFormats->Length();
   for (; j <= len; j++) {
-    TCollection_AsciiString aPattern;
-    TCollection_AsciiString aKey (theFormats->Value(j));
-    aKey += ".Pattern";
+    TCollection_AsciiString aKey, aPattern;
+    aKey = theFormats->Value(j) + ".ImportPattern";
     if (myResMgr->Find(aKey.ToCString()))
       aPattern = myResMgr->Value(aKey.ToCString());
+    else if(myResMgrUser->Find(aKey.ToCString()))
+      aPattern = myResMgrUser->Value(aKey.ToCString());
     else {
-      aPattern = theFormats->Value(j);
-      aPattern += " Files ( *.* )";
+      aKey = theFormats->Value(j) + ".Pattern";
+      if (myResMgr->Find(aKey.ToCString()))
+        aPattern = myResMgr->Value(aKey.ToCString());
+      else if(myResMgrUser->Find(aKey.ToCString()))
+       aPattern = myResMgrUser->Value(aKey.ToCString());
+      else {
+        aPattern = theFormats->Value(j);
+        aPattern += " Files ( *.* )";
+      }
     }
     thePatterns->Append(aPattern);
   }
@@ -298,7 +336,7 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
 
   if (!InitResMgr()) return Standard_False;
 
-  // Read Export formats list
+  // Read Export formats list from install directory
   if (myResMgr->Find("Export")) {
     TCollection_AsciiString aFormats (myResMgr->Value("Export"));
     TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
@@ -308,17 +346,44 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
     }
   }
 
+  // Read Export formats list from user directory
+  if (myResMgrUser->Find("Export")) {
+    TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
+    TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
+    int i = 1;
+    for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
+      int aLenFormats = theFormats->Length();
+      bool isFound = false;
+      for(int aInd=1;aInd<=aLenFormats;aInd++){
+       if( theFormats->Value(aInd) == aToken){
+         isFound = true;
+         break;
+       }
+      }
+      if(!isFound)
+       theFormats->Append(aToken);
+    }
+  }
+
   // Read Patterns for each supported format
   int j = 1, len = theFormats->Length();
   for (; j <= len; j++) {
-    TCollection_AsciiString aPattern;
-    TCollection_AsciiString aKey (theFormats->Value(j));
-    aKey += ".Pattern";
+    TCollection_AsciiString aKey, aPattern;
+    aKey = theFormats->Value(j) + ".ExportPattern";
     if (myResMgr->Find(aKey.ToCString()))
       aPattern = myResMgr->Value(aKey.ToCString());
+    else if (myResMgrUser->Find(aKey.ToCString()))
+      aPattern = myResMgrUser->Value(aKey.ToCString());
     else {
-      aPattern = theFormats->Value(j);
-      aPattern += " Files ( *.* )";
+      aKey = theFormats->Value(j) + ".Pattern";
+      if (myResMgr->Find(aKey.ToCString()))
+        aPattern = myResMgr->Value(aKey.ToCString());
+      else if (myResMgrUser->Find(aKey.ToCString()))
+       aPattern = myResMgrUser->Value(aKey.ToCString());
+      else {
+        aPattern = theFormats->Value(j);
+        aPattern += " Files ( *.* )";
+      }
     }
     thePatterns->Append(aPattern);
   }
@@ -333,32 +398,55 @@ Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
 //=============================================================================
 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
                             (const Standard_Boolean isImport,
-                             const TCollection_AsciiString theFormat,
+                             const TCollection_AsciiString& theFormat,
                              Handle(TCollection_HAsciiString)& theLibName)
 {
   if (!InitResMgr()) return Standard_False;
 
   // Import/Export mode
-  Standard_CString aMode;
+  TCollection_AsciiString aMode;
+  //Standard_CString aMode;
   if (isImport) aMode = "Import";
   else aMode = "Export";
 
-  // Read supported formats for the certain mode
-  if (myResMgr->Find(aMode)) {
-    TCollection_AsciiString aFormats (myResMgr->Value(aMode));
+  
+  // Read supported formats for the certain mode from install directory
+  if (myResMgr->Find(aMode.ToCString())) {
+    TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
     if (aFormats.Search(theFormat) > -1) {
       // Read library name for the supported format
       TCollection_AsciiString aKey (theFormat);
       aKey += ".";
       aKey += aMode;
       if (myResMgr->Find(aKey.ToCString())) {
-        TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
+        TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));        
+#ifndef WNT
+        aLibName += ".so";     
+#else
+        aLibName += ".dll";
+#endif
         theLibName = new TCollection_HAsciiString (aLibName);
         return Standard_True;
       }
     }
   }
-
+  
+  // Read supported formats for the certain mode from user directory
+  if (myResMgrUser->Find(aMode.ToCString())) {
+    TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
+    if (aFormats.Search(theFormat) > -1) {
+      // Read library name for the supported format
+      TCollection_AsciiString aKey (theFormat);
+      aKey += ".";
+      aKey += aMode;
+      if (myResMgrUser->Find(aKey.ToCString())) {
+        TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
+        theLibName = new TCollection_HAsciiString (aLibName);
+        return Standard_True;
+      }
+    }
+  }
+  
   return Standard_False;
 }
 
@@ -369,25 +457,65 @@ Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
 //=============================================================================
 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
 {
+  bool isResourceFound     = false;
+  bool isResourceFoundUser = false;
+  TCollection_AsciiString aUserResDir,aResDir;
+  
   if (myResMgr.IsNull()) {
     // Initialize the Resource Manager
-    TCollection_AsciiString aResDir (getenv("GEOM_ROOT_DIR"));
+    TCollection_AsciiString aNull;
+    aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
 #ifdef WNT
-    aResDir += "\\share\\salome\\resources";
+    aResDir += "\\share\\salome\\resources\\geom";
 #else
-    aResDir += "/share/salome/resources";
+    aResDir += "/share/salome/resources/geom";
 #endif
-    TCollection_AsciiString aUserResDir (getenv("HOME"));
+    
+    myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
+
+    isResourceFound = true;
+    if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
+      // instead of complains in Resource_Manager
+      isResourceFound = false;
+      INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
+    }
+  } else
+    isResourceFound = true;
+
+  if (myResMgrUser.IsNull()) {
+    char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
+    TCollection_AsciiString aNull;
+    if ( dir )
+    {
+      aUserResDir = dir;
+    }
+    else
+    {
+      aUserResDir = getenv("HOME");
 #ifdef WNT
-    aUserResDir += "\\.salome\\resources";
+      aUserResDir += "\\.salome\\resources";
 #else
-    aUserResDir += "/.salome/resources";
+      aUserResDir += "/.salome/resources";
 #endif
-    myResMgr = new Resource_Manager ("ImportExport", aResDir, aUserResDir, Standard_True);
-  }
+    }
 
-  if (myResMgr->Find("Import") || myResMgr->Find("Export"))
-    return Standard_True;
+    myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
 
-  return Standard_False;
+    isResourceFoundUser = true;
+    
+    if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
+      // instead of complains in Resource_Manager
+      isResourceFoundUser = false;
+    }
+      
+  } else
+    isResourceFoundUser = true;
+    
+  if(!isResourceFound && !isResourceFoundUser){
+    INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
+    INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
+  }
+
+  return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
+          myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
 }