Issues with STEPControl_Reader with Japanese Characters in them

Hello!

I'm having trouble with OCC version 7.8 with reading STEP files, specifically with reading folder paths with Japanese characters in them.

For example if I have the following C++ code snippet:

CString file = "C:\Users\jgregory\Desktop\新しいフォルダー\stepfile.stp"
Standard_CString FileName = (Standard_CString)(LPCTSTR)file;

STEPControl_Reader Reader;
IFSelect_ReturnStatus status = aReader.ReadFile(FileName);

If I run the above, then I always get the error code "IFSelect_RetError". However, if I run the above with a filepath with only english characters,
("C:\Users\jgregory\Desktop\test\stepfile.stp") then I never see this issue and the step file is opened properly. All of this is done on a Japanese Windows 10 machine.

Are there any ideas that I could try? I did not have this issue formerly with OCD 6.5.3 but only noticed that it happens with OCC 7.8

Any help would be appreciated, I've tried lots of things to no avail.

gkv311 n's picture

I suppose that CString is a class from MFC. In that case:

  • Make sure that your application is build using MFC Unicode, not legacy MFC MBCS.
  • OCCT expects strings in either UTF-8 (Standard_CString or TCollection_AsciiString) or UTF-16 (TCollection_ExtendedString). Make sure to implement a proper conversion between MFC CString and OCCT strings - you may examine MFC sample coming with OCCT as a reference.
  • While hard-coding Unicode string in C++ file, make sure that file encoding is one from Unicode subset (not local codepage), MSVC compiler is aware in which encoding the file (probably file should start with BOM), C++ string has proper encoding prefix (like L"" or u8""). In general, it is better to avoid hard-coding Unicode string into C++ source code to avoid portability issues.
CString theFile = ...;
TCollection_AsciiString aFileUtf8 ((const wchar_t* )theFile);

STEPControl_Reader aReader;
IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileUtf8.ToCString());
John gregory's picture

Thank you for your assistance. When I try to convert the character set to use unicode in out mfc application, I get lots of compiler errors. To use MFC Unicode, I switched this setting "Character Set" to "Use Unicode Character Set".

Is there a workaround to still use MFC multibyte while also still being able to use the STEPControl_Reader with OCC 7.8?

Dmitrii Pasukhin's picture

Hello. If you are having trouble with path string, then you can use "iostream" to read by open stream instead.

But originally, UTF-8 must to be work without issues. You can use as a bridge TCollection_ExtendedString then create AsciiString. Or use constructor, which takes pointer and length.

Best regards, Dmitrii.

Patrik Mueller's picture

Hi,

you could try to convert CString to Unicode via MultiByteToWideChar and CP_ACP as codepage parameter to wchar_t.

Then create a TCollection_AsciiString as shown above:

TCollection_AsciiString aFileUtf8 ((const wchar_t* )theFile);

Greets,

Patrik

gkv311 n's picture

John gregory wrote:

Is there a workaround to still use MFC multibyte while also still being able to use the STEPControl_Reader with OCC 7.8?

You may convert string from locale codepage into UTF-8 using NCollection_Utf8String from NCollection_UtfString.hxx:

CString theFile = ...;

NCollection_Utf8String aFileUtf8;
aFileUtf8.FromLocale((const char* )theFile);

STEPControl_Reader aReader;
IFSelect_ReturnStatus aStatus = aReader.ReadFile(aFileUtf8.ToCString());
gkv311 n's picture

I did not have this issue formerly with OCD 6.5.3 but only noticed that it happens with OCC 7.8

OCCT 7.5.3 introduced handling of Unicode file paths in STEP reader (UTF-8 strings are expected since that).