How can opencascade support chinese step file?

Brooks

Looking around for some CAD
STEPControl_Reader stepReader;
IFSelect_ReturnStatus status = stepReader.ReadFile("零件.step");
if (status >= IFSelect_RetError)
{
std::cout << "Open Failed" << std::endl;
return NULL;
}
when I want to open a step file where the path has chinese characters like "零件" ,the status return IFSelect_RetError.
 

Quaoar

Administrator
Staff member
Works in Analysis Situs:

1684918428718.png
In should actually be handled by OpenCascade itself, because its translators accept `const char *` for the input filenames assuming that the filename might be represented with UTF-8 encoding. When OpenCascade opens the input stream, the string is converted to unicode representation:

stream.png

To debug it, you may want to try to get into this OSD_OpenStream and check the `aFileNameW` with the watch -- it should give you the valid filename:

stream2.png

Are you on windows btw?
 

Brooks

Looking around for some CAD
Works in Analysis Situs:

View attachment 610
In should actually be handled by OpenCascade itself, because its translators accept `const char *` for the input filenames assuming that the filename might be represented with UTF-8 encoding. When OpenCascade opens the input stream, the string is converted to unicode representation:

View attachment 611

To debug it, you may want to try to get into this OSD_OpenStream and check the `aFileNameW` with the watch -- it should give you the valid filename:

View attachment 612

Are you on windows btw?
Thanks for the tip. I have tried to convert the input string (Unicode) to wstring and then to utf8, and the parts are loaded correctly. code show as below:

#include <windows.h>
#include <string>

char* Unicode2Utf8(std::string string)
{
std::wstring res;
int len = MultiByteToWideChar(CP_ACP, 0, string.c_str(), string.size(), nullptr, 0);
if (len < 0) {
return NULL;
}
wchar_t* buffer = new wchar_t[len + 1];
if (buffer == nullptr) {
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, string.c_str(), string.size(), buffer, len);
buffer[len] = '\0';
res.append(buffer);
delete[] buffer;

len = WideCharToMultiByte(CP_UTF8, 0, res.c_str(), -1, NULL, 0, NULL, NULL);
char* szUtf8 = (char*)malloc(len + 1);
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, res.c_str(), -1, szUtf8, len, NULL, NULL);
return szUtf8;
}

And convert from Unicode to utf8:
std::string name = ”零件.step“;
char* fileName = Unicode2Utf8(name);
STEPControl_Reader stepReader;
IFSelect_ReturnStatus status = stepReader.ReadFile(fileName);
if (status >= IFSelect_RetError)
return;

Thanks again!
 
Top