1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-02 17:46:22 +03:00

0029069: Samples - handle UNICODE filenames within C++/CLI CSharp sample

This commit is contained in:
kgv 2018-01-29 11:05:20 +03:00 committed by apn
parent 1d53219a28
commit 95f688263d
2 changed files with 80 additions and 76 deletions

View 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")
@ -45,6 +47,24 @@
#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)

View File

@ -39,6 +39,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")
@ -55,6 +57,24 @@
#pragma comment(lib, "D3D9.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)
@ -109,14 +129,14 @@ public:
/// Make dump of current view to file
/// </summary>
/// <param name="theFileName">Name of dump file</param>
bool Dump (const 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>
@ -709,28 +729,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);
delete[] 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)
{
TopoDS_Shape aShape;
BRep_Builder aBuilder;
if (!BRepTools::Read (aShape, theFileName, aBuilder))
if (!BRepTools::Read (aShape, theFileName.ToCString(), aBuilder))
{
return false;
}
@ -746,10 +756,10 @@ public:
///Import Step file
/// </summary>
/// <param name="theFileName">Name of import file</param>
bool ImportStep (char* theFileName)
bool ImportStep (const TCollection_AsciiString& theFileName)
{
STEPControl_Reader aReader;
if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
{
return false;
}
@ -779,10 +789,10 @@ public:
///Import Iges file
/// </summary>
/// <param name="theFileName">Name of import file</param>
bool ImportIges (char* theFileName)
bool ImportIges (const TCollection_AsciiString& theFileName)
{
IGESControl_Reader aReader;
if (aReader.ReadFile (theFileName) != IFSelect_RetDone)
if (aReader.ReadFile (theFileName.ToCString()) != IFSelect_RetDone)
{
return false;
}
@ -798,7 +808,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())
@ -808,14 +818,14 @@ public:
Handle(AIS_Shape) anIS = Handle(AIS_Shape)::DownCast (myAISContext()->SelectedInteractive());
return !anIS.IsNull()
&& BRepTools::Write (anIS->Shape(), theFileName);
&& BRepTools::Write (anIS->Shape(), theFileName.ToCString());
}
/// <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;
STEPControl_Writer aWriter;
@ -833,14 +843,14 @@ public:
return false;
}
}
return aWriter.Write (theFileName) == IFSelect_RetDone;
return aWriter.Write (theFileName.ToCString()) == IFSelect_RetDone;
}
/// <summary>
///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"),
@ -857,14 +867,14 @@ public:
}
aWriter.ComputeModel();
return aWriter.Write (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;
@ -880,7 +890,7 @@ public:
}
VrmlAPI_Writer aWriter;
aWriter.Write (aRes, theFileName);
aWriter.Write (aRes, theFileName.ToCString());
return true;
}
@ -888,7 +898,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;
@ -904,7 +914,7 @@ public:
}
StlAPI_Writer aWriter;
aWriter.Write (aComp, theFileName);
aWriter.Write (aComp, theFileName.ToCString());
return true;
}
@ -917,14 +927,7 @@ public:
bool TranslateModel (System::String^ theFileName, int theFormat, bool theIsImport)
{
bool isResult = false;
int aLength = theFileName->Length;
char* aFilename = new char[aLength + 1];
for (int aCharIter = 0; aCharIter < aLength; ++aCharIter)
{
aFilename[aCharIter] = (char)theFileName->ToCharArray()[aCharIter];
}
aFilename[aLength] = '\0';
const TCollection_AsciiString aFilename = toAsciiString (theFileName);
if (theIsImport)
{
switch (theFormat)
@ -946,7 +949,6 @@ public:
case 5: isResult = Dump (aFilename); break;
}
}
delete[] aFilename;
return isResult;
}