UG二次开发主要使用NXOpen和UGopen(ufun),这并不是标准的C/C++语言他们建立了大量专有的变量类型,数据类型转化就成了制约新手入门的门槛之一
简单转换
Body NXOjectNXString JI= nxobject1->JournalIdentifier(); Body *body1(dynamic_cast<Body *>(workPart->Bodies()->FindObject(JI)));NXObject tag_t
tag_t grp_tag=*grp_p_tag;// grp_tag=nxobject1->GetTag(); TaggedObject *grp_tag_object=NXObjectManager::Get(grp_tag); Group *group1(dynamic_caststring NXString(grp_tag_object));
str_str=nxstring->GetLocalText();const char* NXString
char_str=nxstring->GetLocalText();//不推荐 char_str=str_str.c_str();//str_str为string类型 NXString str1(charBHRT,NXString::Locale);char * const char*
uc1601((char*)char_str,1);string Number
str_str=std::to_string((long double)int_count); float_x=std::atof(str_str);_bstr_t string
bt=str_str.c_str();
封装的函数
string ——> LPCWSTR
wchar_t* NXFunction::StringToWchar(string str)
{
size_t size=str.length();
wchar_t *wchar_path = new wchar_t[size+1];
MultiByteToWideChar( CP_ACP, 0,str.c_str(), size, wchar_path
, size * sizeof(wchar_t) );
wchar_path[size] = 0; // 确保以 '\0' 结尾
return wchar_path;
}
LPCWSTR ——> nxstring
NXString NXFunction::WcharToNXString(wchar_t *wchar)
{
wchar_t * wText = wchar;//std::string& szDst,
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
string szDst = psText;// std::string赋值
delete []psText;// psText的清除
NXString str1(szDst.c_str(),NXString::Locale);
return str1;
}
LPCWSTR ——> string
string NXFunction::WcharToString(wchar_t *wchar)
{
wchar_t * wText = wchar;//std::string& szDst,
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用
char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用
string szDst = psText;// std::string赋值
delete []psText;// psText的清除
return szDst;
}