Failed: Loading SU model by a path containing Chinese words

sketchup

#1

I develop with C++ SDK, and I found the interface SUModelCreateFromFile would return SU_ERROR_SERIALIZATION and couldn’t load the model when the path is containing Chinese words like “C:\Users\Administrator\Desktop\testone\风雨雷电a.skp”.

I have tried some ways like and some others ways ,but it seems no wording.,
I am looking forward to your help … (T T)


#2

Can you post a complete example, with a short snippet ready to compile and a file that fails?


#3

That’s interesting.


#4

for example :
CString strPath;
m_pathOfSKP.GetWindowTextW(strPath);

std::string tempName = (LPCSTR)CStringA(strPath);  //CStringA、CStringW、CString对应字符类型分别为char,wchar_t,TCHAR

const char* p = tempName.c_str();

if (SU_ERROR_NONE != SUModelCreateFromFile(&m_model, p))//判断模型创建失败的话,释放空间
{
	return false;
}

SUEntitiesRef entities = SU_INVALID;

if(SU_ERROR_NONE != SUModelGetEntities(m_model, &entities))
{
	return false;
}

#5

Do you have a sample SKP file?


#6

this is a SKP file with Chinese name 中国CTO.skp (53.3 KB)


#7

I have the same problem, too. Looking for solution…


#8

develop in C++ ?


#9

Yes , Visual C++ 2015.


#10

did you work out this problem? I wonder if you have qq ?maybe we can have a deeper talk, myqq is 27925916


#11

I find it’s an encoding problem.SU SDK receives char* encoded with utf-8 only .try this:

char *filename = L"模型.skp"; // an ANSI char array

const int SIZE = 2080; // Bytes , 
WCHAR w[SIZE] = { 0 };

MultiByteToWideChar(CP_ACP, 0, filename, -1, w, SIZE); // ANSI to UNICODE
WideCharToMultiByte(CP_UTF8, 0, w, -1, filename, SIZE, 0, 0); // UNICODE to UTF-8

Now “filename” is char* encoded with utf-8 , could be passed to SDK as file path .
Works for me, good luck!


#12

Thanks,It works!


#13

Yes, that is correct, SketchUp deals with UTF-8. This applies to Ruby API as well. I’m adding issues internally to clarify this in the documentation.


#14

I also found this works on Windows using the SU functions. This avoids using Windows-specific functions so may be more cross-platform?

            SUResult res = SU_ERROR_NONE;
            std::wstring path = L"path.skp";
            SUStringRef out_str = SU_INVALID;
            // Because NTFS uses UTF16 encoding.
            SUStringCreateFromUTF16(&out_str, &path[0]);
            char buffer[2000] = "";
            size_t count;
            SUStringGetUTF8(out_str, 2000, buffer, &count);
            SUStringRelease(&out_str);
            res = SUModelCreateFromFile(&model, buffer);



#15

Windows is UTF16 - so it’ll be doing pretty much the same thing.