部件属性操作篇:
NXOpen C++ API为NX软件的二次开发提供了强大的工具,特别是在参数化设计方面。通过对部件(Part)属性的操作,开发者可以实现更加灵活和高效的设计流程。
在参数化设计中,部件属性属于产品的标准属性,是通过模型或图纸传递数据的重要媒介。部件属性篇将会大家带来关于部件属性新增,修改,删除,匹配、锁定等系列化的操作及相关代码展示。
NXOpen C++ API为NX软件的二次开发提供了强大的工具,特别是在参数化设计方面。通过对部件(Part)属性的操作,开发者可以实现更加灵活和高效的设计流程。
在NX中,部件文件不仅包含了零件的几何信息,还包含了大量的非几何信息,如材料、尺寸参数等。这些属性对于参数化设计至关重要。NXOpen API通过基类NXObject,提供了对NX对象属性的全面操作,包括Integer(整型)、Real(实数型)、String(字符串型)等多种类型。
在参数化设计中,这些属性操作使得开发者能够动态地调整部件的参数,从而生成满足不同需求的零件模型。例如,通过修改部件的某个尺寸参数,可以快速地生成一系列具有不同尺寸的零件模型,大大提高了设计效率。
1、获取所有部件属性的代码
std::vector Attributelock::GetAttribute()
{
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());
std::vector callback;
//遍历NX对象上某个类型的所有属性的方法
theSession->ListingWindow()->Open();
std::vectorNXObject::AttributeInformation attr1 = workPart->GetAttributeTitlesByType(NXObject::AttributeTypeAny);
for (int i = 0; i < attr1.size(); i++)
{
NXObject::AttributeType type1 = attr1[i].Type;//类型
NXString title1 = attr1[i].Title;//名称
NXString title2 = attr1[i].StringValue;
callback.push_back(title1);
//打印内容
//theSession->ListingWindow()->WriteLine(title1);
//theSession->ListingWindow()->WriteLine(title2);
}
return callback;
}
2、设置锁定/解锁代码
bool Attributelock::SetLockUnlock(NXString Attribute)
{
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());
NXString Lock = this->LockUnlock->GetProperties()->GetEnumAsString(“Value”);
bool Judge = workPart->HasUserAttribute(Attribute, NXObject::AttributeTypeAny, -1);
if (Judge)
{
string lockstr = Lock.GetLocaleText();
if (lockstr =="锁定")
{
workPart->NXObject::SetUserAttributeLock(Attribute,NXObject::AttributeTypeAny,true);
Attributelock::theUI->NXMessageBox()->Show("属性操作", NXOpen::NXMessageBox::DialogTypeInformation, "属性锁定成功");
return 1;
}
else
{
workPart->NXObject::SetUserAttributeLock(Attribute, NXObject::AttributeTypeAny, false);
Attributelock::theUI->NXMessageBox()->Show("属性操作", NXOpen::NXMessageBox::DialogTypeInformation, "属性解锁成功");
}
}
else
{
Attributelock::theUI->NXMessageBox()->Show("属性操作", NXOpen::NXMessageBox::DialogTypeError,"未找到对应属性");
return 2;
}
}
3、删除所有非系统部件属性
void DeletePartAttributes::UnlockAllattributes()
{
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());
std::vectorNXObject::AttributeInformation attr1 = workPart->GetAttributeTitlesByType(NXObject::AttributeTypeAny);
for (int i = 0; i < attr1.size(); i++)
{
NXObject::AttributeType type1 = attr1[i].Type;//类型
NXString title1 = attr1[i].Title;//名称
SetLockUnlock(title1);
try
{
workPart->DeleteAttributeByTypeAndTitle(NXObject::AttributeTypeAny, title1);
}
catch (const std::exception&)
{
}
}
}
在这里插入图片描述
以上是代码的介绍,具体内容详见附件资料,本资料没有经过过细的设计。也是初次上传,希望大家体谅