NX二次开发读属性/表达式封装函数

int Read_ATTR_Type(int ObjTag, char* Attr_Title); //读取属性返回属性类型
string Read_ATTR_StringValue(int ObjTag, char* Attr_Title);//读取属性返回属性字符串值
string Read_PartExprDoubleValue(int ComponentTag, char * Expressions_Title); //遍历部件获取表达式值 Double型
string Read_PartExprIntValue(int ComponentTag, char* Expressions_Title); //遍历部件获取表达式值 Int型


int YN_BOM::Read_ATTR_Type(int ObjTag, char * Attr_Title) //读取属性返回属性类型
{
try
{
UF_initialize();
UF_ATTR_value_t Attr_Value;//定义结构体
Attr_Value.value.string = new char[UF_ATTR_MAX_STRING_LEN + 1];
UF_ATTR_read_value(ObjTag, Attr_Title, UF_ATTR_string, &Attr_Value);//获取属性
int TypeResults = Attr_Value.type;

return TypeResults;

//释放内存
delete Attr_Value.value.string;
UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_BOM::theUI->NXMessageBox()->Show("属性类型", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

string YN_BOM::Read_ATTR_StringValue(int ObjTag, char* Attr_Title) //读取属性返回属性字符串值
{
try
{
UF_initialize();
UF_ATTR_value_t Attr_Value;//定义结构体
Attr_Value.value.string = new char[UF_ATTR_MAX_STRING_LEN + 1];
UF_ATTR_read_value(ObjTag, Attr_Title, UF_ATTR_string, &Attr_Value);//获取属性
string StringValeResults = Attr_Value.value.string;

return StringValeResults;

//释放内存
delete Attr_Value.value.string;
UF_terminate();
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_BOM::theUI->NXMessageBox()->Show("属性字符串值", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}
string YN_BOM::Read_PartExprDoubleValue(int ComponentTag , char* Expressions_Title) //遍历部件获取表达式值 Double型
{
try
{
Assemblies::Component* component = dynamic_cast<NXOpen::Assemblies::Component*>(NXObjectManager::Get(ComponentTag));//获取Component
NXOpen::Part* ThePart = dynamic_cast<Part*>(component->Prototype()); //component强制转换为part
//遍历表达式
std::vector<Expression*> PartExprs = ThePart->Expressions()->GetVisibleExpressions();
Expression* PartExpression;

char PartExpCharValue[256];
string PartExpStrValue;

for (int i = 0 ; i < PartExprs.size(); i++)
{
PartExpression = dynamic_cast<Expression*>(PartExprs[i]);
NXString PartExpressionName = PartExpression->Name();
double PartExpressionValue = PartExpression->Value();
if (strcmp(PartExpressionName.GetLocaleText(), Expressions_Title) == 0)
{
sprintf(PartExpCharValue, "%.2f", PartExpressionValue);
}
}
PartExpStrValue = PartExpCharValue;
//theSession->ListingWindow()->WriteLine(component->DisplayName()+"表达式:" + Expressions_Title +"值=" + PartExpStrValue );
return PartExpStrValue;
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_BOM::theUI->NXMessageBox()->Show("获取表达式Double值", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

string YN_BOM::Read_PartExprIntValue(int ComponentTag, char* Expressions_Title) //遍历部件获取表达式值 Int型
{
try
{
Assemblies::Component* component = dynamic_cast<NXOpen::Assemblies::Component*>(NXObjectManager::Get(ComponentTag));//获取Component
NXOpen::Part* ThePart = dynamic_cast<Part*>(component->Prototype()); //component强制转换为part
//遍历表达式
std::vector<Expression*> PartExprs = ThePart->Expressions()->GetVisibleExpressions();
Expression* PartExpression;

char PartExpCharValue[256];
string PartExpStrValue;

for (int i = 0; i < PartExprs.size(); i++)
{
PartExpression = dynamic_cast<Expression*>(PartExprs[i]);
NXString PartExpressionName = PartExpression->Name();
int PartExpressionValue = PartExpression->Value();
if (strcmp(PartExpressionName.GetLocaleText(), Expressions_Title) == 0)
{
sprintf(PartExpCharValue, "%d", PartExpressionValue);
//theSession->ListingWindow()->WriteLine(component->DisplayName() + "表达式:" + Expressions_Title + "值=" + PartExpCharValue);
}
}
PartExpStrValue = PartExpCharValue;

return PartExpStrValue;
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
YN_BOM::theUI->NXMessageBox()->Show("获取表达式Int值", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

怡宁塑胶模具设计
原文地址:https://www.cnblogs.com/hqsalanhuang/p/14993889.html