0029069: Samples - handle UNICODE filenames within C++/CLI CSharp sample
[occt.git] / samples / CSharp / OCCTProxy / OCCTProxy.cpp
index 91d4364..cb0b026 100644 (file)
@@ -32,6 +32,8 @@
 //wrapper of pure C++ classes to ref classes
 #include <NCollection_Haft.h>
 
+#include <vcclr.h>
+
 // list of required OCCT libraries
 #pragma comment(lib, "TKernel.lib")
 #pragma comment(lib, "TKMath.lib")
 #pragma comment(lib, "TKStl.lib")
 #pragma comment(lib, "TKVrml.lib")
 
+//! Auxiliary tool for converting C# string into UTF-8 string.
+static TCollection_AsciiString toAsciiString (String^ theString)
+{
+  if (theString == nullptr)
+  {
+    return TCollection_AsciiString();
+  }
+
+  pin_ptr<const wchar_t> aPinChars = PtrToStringChars (theString);
+  const wchar_t* aWCharPtr = aPinChars;
+  if (aWCharPtr == NULL
+  || *aWCharPtr == L'\0')
+  {
+    return TCollection_AsciiString();
+  }
+  return TCollection_AsciiString (aWCharPtr);
+}
+
 /// <summary>
 /// Proxy class encapsulating calls to OCCT C++ classes within 
 /// C++/CLI class visible from .Net (CSharp)
@@ -93,14 +113,14 @@ public:
   /// Make dump of current view to file
   /// </summary>
   /// <param name="theFileName">Name of dump file</param>
-  bool Dump(char *theFileName)
+  bool Dump(const TCollection_AsciiString& theFileName)
   {
     if (myView().IsNull())
     {
       return false;
     }
     myView()->Redraw();
-    return myView()->Dump(theFileName) != Standard_False;
+    return myView()->Dump(theFileName.ToCString()) != Standard_False;
   }
 
   /// <summary>
@@ -731,28 +751,18 @@ public:
   /// <param name="theFileName">Name of import file</param>
   bool ImportBrep(System::String^ theFileName)
   {
-    bool isResult = false;
-    int aLength = theFileName->Length;
-    char* aFilename = new char[aLength+1];
-    for(int i = 0; i<aLength; i++)
-    {
-      aFilename[i] = (char)theFileName->ToCharArray()[i];
-    }
-    aFilename[aLength] = '\0';
-    isResult = ImportBrep(aFilename);
-    return isResult;
+    return ImportBrep (toAsciiString (theFileName));
   }
 
   /// <summary>
   ///Import BRep file
   /// </summary>
   /// <param name="theFileName">Name of import file</param>
-  bool ImportBrep(char* theFileName)
+  bool ImportBrep (const TCollection_AsciiString& theFileName)
   {
-    Standard_CString aFileName = (Standard_CString) theFileName;
     TopoDS_Shape aShape;
     BRep_Builder aBuilder;
-    Standard_Boolean isResult = BRepTools::Read(aShape,aFileName,aBuilder);
+    Standard_Boolean isResult = BRepTools::Read(aShape,theFileName.ToCString(),aBuilder);
     if (!isResult)
     {
       return false;
@@ -766,11 +776,10 @@ public:
   ///Import Step file
   /// </summary>
   /// <param name="theFileName">Name of import file</param>
-  bool ImportStep(char* theFileName)
+  bool ImportStep(const TCollection_AsciiString& theFileName)
   {
-    Standard_CString aFileName = (Standard_CString) theFileName;
     STEPControl_Reader aReader;
-    IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileName);
+    IFSelect_ReturnStatus aStatus = aReader.ReadFile(theFileName.ToCString());
     if ( aStatus == IFSelect_RetDone )
     {
       bool isFailsonly = false;
@@ -805,11 +814,10 @@ public:
   ///Import Iges file
   /// </summary>
   /// <param name="theFileName">Name of import file</param>
-  bool ImportIges(char* theFileName)
+  bool ImportIges(const TCollection_AsciiString& theFileName)
   {
-    Standard_CString aFileName = (Standard_CString) theFileName;
     IGESControl_Reader aReader;
-    int aStatus = aReader.ReadFile( aFileName );
+    int aStatus = aReader.ReadFile( theFileName.ToCString() );
 
     if ( aStatus == IFSelect_RetDone )
     {
@@ -830,7 +838,7 @@ public:
   ///Export BRep file
   /// </summary>
   /// <param name="theFileName">Name of export file</param>
-  bool ExportBRep(char* theFileName)
+  bool ExportBRep(const TCollection_AsciiString& theFileName)
   {
     myAISContext()->InitSelected();
     if (!myAISContext()->MoreSelected())
@@ -840,14 +848,14 @@ public:
 
     Handle(AIS_InteractiveObject) anIO = myAISContext()->SelectedInteractive();
     Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast(anIO);
-    return BRepTools::Write (anIS->Shape(), (Standard_CString)theFileName) != Standard_False;
+    return BRepTools::Write (anIS->Shape(), theFileName.ToCString()) != Standard_False;
   }
 
   /// <summary>
   ///Export Step file
   /// </summary>
   /// <param name="theFileName">Name of export file</param>
-  bool ExportStep(char* theFileName)
+  bool ExportStep(const TCollection_AsciiString& theFileName)
   {
     STEPControl_StepModelType aType = STEPControl_AsIs;
     IFSelect_ReturnStatus aStatus;
@@ -864,7 +872,7 @@ public:
       }
     }
 
-    aStatus = aWriter.Write( (Standard_CString)theFileName );
+    aStatus = aWriter.Write(theFileName.ToCString());
     if ( aStatus != IFSelect_RetDone )
     {
       return false;
@@ -877,7 +885,7 @@ public:
   ///Export Iges file
   /// </summary>
   /// <param name="theFileName">Name of export file</param>
-  bool ExportIges(char* theFileName)
+  bool ExportIges(const TCollection_AsciiString& theFileName)
   {
     IGESControl_Controller::Init();
     IGESControl_Writer aWriter( Interface_Static::CVal( "XSTEP.iges.unit" ),
@@ -892,14 +900,14 @@ public:
     }
 
     aWriter.ComputeModel();
-    return aWriter.Write( (Standard_CString)theFileName) != Standard_False;
+    return aWriter.Write(theFileName.ToCString()) != Standard_False;
   }
 
   /// <summary>
   ///Export Vrml file
   /// </summary>
   /// <param name="theFileName">Name of export file</param>
-  bool ExportVrml(char* theFileName)
+  bool ExportVrml(const TCollection_AsciiString& theFileName)
   {
     TopoDS_Compound aRes;
     BRep_Builder aBuilder;
@@ -919,7 +927,7 @@ public:
     }
 
     VrmlAPI_Writer aWriter;
-    aWriter.Write( aRes, (Standard_CString)theFileName );
+    aWriter.Write(aRes, theFileName.ToCString());
 
     return true;
   }
@@ -928,7 +936,7 @@ public:
   ///Export Stl file
   /// </summary>
   /// <param name="theFileName">Name of export file</param>
-  bool ExportStl(char* theFileName)
+  bool ExportStl(const TCollection_AsciiString& theFileName)
   {
     TopoDS_Compound aComp;
     BRep_Builder aBuilder;
@@ -947,7 +955,7 @@ public:
     }
 
     StlAPI_Writer aWriter;
-    aWriter.Write( aComp, (Standard_CString)theFileName );
+    aWriter.Write(aComp, theFileName.ToCString());
     return true;
   }
 
@@ -960,14 +968,8 @@ public:
   bool TranslateModel(System::String^ theFileName, int theFormat, bool theIsImport)
   {
     bool isResult;
-    int aLength = theFileName->Length;
-    char* aFilename = new char[aLength+1];
-    for(int i = 0; i<aLength; i++)
-    {
-      aFilename[i] = (char)theFileName->ToCharArray()[i];
-    }
-    aFilename[aLength] = '\0';
 
+    const TCollection_AsciiString aFilename = toAsciiString (theFileName);
     if (theIsImport)
     {
       switch(theFormat)