//判断表达式类型
if (!strcmp(expression1->Type().getLocaleText(), "String"))
{
uc1601("是字符串类型",1);
}
//获取表达式完整类型
UF_UI_write_listing_window((char*)express->Equation().getLocaleText());
//创建字符串类型的表达式
string equation = "a=\"ABCD\"";
Expression* new_Str_Exp = workPart->Expressions()->CreateExpression("String", equation);
//根据名称获取表达式Expression
Expression* get_exps(NXString& titles)
{
if (!workPart) return 0;
Expression* expression1 = NULL;
try
{
expression1 = workPart->Expressions()->FindObject(titles);
}
catch (exception& ex)
{
//xt_msgtxt(titles.GetLocaleText());
return 0;
}
return expression1;
}
void changeExp(NXString& title, NXString& exp)
{
Expression* exp1 = get_exps(title);
if (!exp1) return;
char zii[256];
sprintf_s(zii, 256, "%s", exp.getLocaleText());
if (isUseUTF8Text(exp.GetUTF8Text()))
sprintf_s(zii, 256, "%s", exp.GetUTF8Text());
sprintf_s(ex_type, 256, "%s", exp1->Type().GetLocaleText());
if (isUseUTF8Text(exp1->Type().GetUTF8Text()))
sprintf_s(ex_type, 256, "%s", exp1->Type().GetUTF8Text());
char* strs = strstr(zii, "!");
if (strs) strs[0] = '\0';
Unit* nullUnit(NULL);
if (strstr(ex_type, "Number"))
{
bool isSame = 1;
if (isNumber(zii))
{
if (exp1->Value() != atof(zii)) isSame = 0;
}
else
{
if (stricmp(exp1->RightHandSide().getLocaleText(), zii) != 0) isSame = 0;
}
if (!isSame)
{
string tmp_title = title.getLocaleText();
string tmp_exp = tmp_title + "=" + zii;
UF_MODL_edit_exp((char*)tmp_exp.c_str());
}
}
else if (strstr(ex_type, "String"))
{
if (zii[0] != '"')
{
sprintf_s(ex_type, 256, "%s", zii);
sprintf_s(zii, "\"%s\"", ex_type);
}
string old_expRhs = exp1->StringValue().getLocaleText();
if (old_expRhs[0] != '"')
{
old_expRhs = "\"" + old_expRhs + "\"";
}
if (stricmp(old_expRhs.c_str(), zii) != 0)
{
//exp1->SetRightHandSide(zii);--不要采取这种方式,采取下面的方法
workPart->Expressions()->Edit(exp1, zii);
}
}
}
//迭代器遍历所有表达式
{
tag_t part_occ_tag = UF_PART_ask_part_tag(new_NULLPartPath);
tag_t pre_work_tag = NULL_TAG;
UF_ASSEM_set_work_part_quietly(part_occ_tag, &pre_work_tag);
Session* theSession1 = Session::GetSession();
ExpressionCollection* exp_collection = theSession1->Parts()->Work()->Expressions();
vector< NXOpen::Expression*> tmpExpsVec;
for (NXOpen::ExpressionCollection::iterator it = exp_collection->begin(); it != exp_collection->end(); it++)
{
NXOpen::Expression* exp_ = (*it);
tmpExpsVec.push_back(exp_);
}
tag_t pre_work_tag_ = NULL_TAG;
UF_ASSEM_set_work_part_quietly(pre_work_tag, &pre_work_tag_);
}
【UG\NX二次开发】NXOpen 表达式相关操作
于 2023-11-09 15:46:30 首次发布