C++ NX二次开发 - 属性工具:创建属性和设置图层
{"title":"C++ NX二次开发 - 属性工具:创建属性和设置图层","description":"使用C++进行NX二次开发,创建属性工具,分别给工作部件和实体创建四个属性(部件名、设计者、日期、QQ),并设置图层。提供完整代码示例,展示如何使用NXOpen库进行API调用。","keywords":"NX二次开发, C++, 属性工具, NXOpen, 属性, 图层, 工作部件, 实体, 代码示例","content":"#include <iostream> #include <vector> #include <NXOpen/NXException.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/PartLoadStatus.hxx> #include <NXOpen/PartSaveStatus.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/TaggedObject.hxx> #include <NXOpen/TaggedObjectCollection.hxx> #include <NXOpen/UI.hxx>
using namespace std; using namespace NXOpen;
// 定义属性类 class Attribute { public: string title; string value; };
// 创建属性 void CreateAttribute(TaggedObject *object, const string &title, const string &value) { try { Attribute *attribute = new Attribute(); attribute->title = title; attribute->value = value;
object->SetUserAttribute("MyAttribute", -1, attribute);
} catch (const NXException &e) { cerr << "Error: " << e.Message() << endl; } }
// 获取属性 Attribute *GetAttribute(TaggedObject *object, const string &title) { try { return static_cast<Attribute *>(object->GetUserAttribute("MyAttribute", -1)); } catch (const NXException &e) { cerr << "Error: " << e.Message() << endl; }
return nullptr;
}
// 设置图层 void SetLayer(TaggedObject *object, const string &layerName) { try { TaggedObjectCollection *objects = new TaggedObjectCollection(); objects->Add(object);
Session::GetSession()->Parts()->SetDisplayLayer(layerName, objects);
} catch (const NXException &e) { cerr << "Error: " << e.Message() << endl; } }
int main() { // 启动NX会话 Session *session = Session::GetSession(); if (session == nullptr) { cerr << "Failed to start NX session." << endl; return 1; }
// 获取UI对象
UI *ui = session->UI();
if (ui == nullptr)
{
cerr << "Failed to get UI object." << endl;
return 1;
}
// 打开当前工作部件
Part *workPart = session->Parts()->Work();
if (workPart == nullptr)
{
cerr << "No work part found." << endl;
return 1;
}
// 创建四个属性
CreateAttribute(workPart, "部件名", workPart->Name().GetText());
CreateAttribute(workPart, "设计者", "John Doe");
CreateAttribute(workPart, "日期", "2022-01-01");
CreateAttribute(workPart, "QQ", "123456789");
// 设置图层
SetLayer(workPart, "工作部件图层");
// 获取当前实体
TaggedObject *currentObject = ui->SelectionManager()->GetSelectedObject();
if (currentObject == nullptr)
{
cerr << "No entity selected." << endl;
return 1;
}
// 创建四个属性
CreateAttribute(currentObject, "部件名", workPart->Name().GetText());
CreateAttribute(currentObject, "设计者", "John Doe");
CreateAttribute(currentObject, "日期", "2022-01-01");
CreateAttribute(currentObject, "QQ", "123456789");
// 设置图层
SetLayer(currentObject, "实体图层");
// 保存工作部件
PartSaveStatus *saveStatus = workPart->Save();
if (saveStatus->NXObjectStatus() != BasePartSaveStatus::SaveStatusOk)
{
cerr << "Failed to save work part." << endl;
return 1;
}
// 关闭工作部件
PartCollection *partCollection = session->Parts();
PartLoadStatus *loadStatus = partCollection->Close(workPart);
if (loadStatus->NXObjectStatus() != BasePartLoadStatus::PartLoadStatusOk)
{
cerr << "Failed to close work part." << endl;
return 1;
}
return 0;
}
"
原文地址: https://www.cveoy.top/t/topic/p4O3 著作权归作者所有。请勿转载和采集!