ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NXOpen 遍历部件并把子部件设置成工作部件加属性后恢复到装配部件

2021-01-02 23:35:48  阅读:296  来源: 互联网

标签:遍历 Component attributePropertiesBuilder1 部件 theSession NXOpen include Assemblies


C++

 

// Mandatory UF Includes
#include <uf.h>
#include <uf_object_types.h>

// Internal Includes
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/UI.hxx>

// Internal+External Includes
#include <NXOpen/Annotations.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Session.hxx>

#include <NXOpen/BasePart.hxx>
#include <NXOpen/AttributeManager.hxx>
#include <NXOpen/AttributePropertiesBaseBuilder.hxx>
#include <NXOpen/AttributePropertiesBuilder.hxx>
#include <NXOpen/TaggedObject.hxx>
#include <NXOpen/NXObjectManager.hxx>

// Std C++ Includes
#include <iostream>
#include <sstream>

using namespace NXOpen;
using namespace std;

 

void MyClass::do_it()
{

// TODO: add your code here

//用户代码

Session *theSession = Session::GetSession();//获取绘画客口
Part *workPart(theSession->Parts()->Work());//获取工作部件
Part *displayPart(theSession->Parts()->Display());//获取显示部件

//BasePart *baseworkpart = theSession->Parts()->BaseWork();//定义baseworkpart
BasePart *basedisplaypart = theSession->Parts()->BaseDisplay();//定义basedisplaypart
//遍历
Assemblies::ComponentAssembly *componentassembly = basedisplaypart->ComponentAssembly();//获取装配底层(BaseWork为当前工作装配,basedisplaypart为当前显示的装配 两种结果是不一样的)
Assemblies::Component *rootcomponent = componentassembly->RootComponent();//获取rootcomponent
if (rootcomponent==NULL)//判断rootcomponent是否为空,空即提示不是装配,是就执行遍历
{
//弹出消息框(四种消息框 错误:DialogTypeError 警告:DialogTypeWarning 提示:DialogTypeInformation 询问:DialogTypeQuestion
theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeWarning, "当前没在装配环境!"); //弹出消息框提示
}
else//
{

getchildren(rootcomponent);

}

}


void MyClass::getchildren(Assemblies::Component *root)
{

//用户代码

std::vector<Assemblies::Component *> children = root->GetChildren();//获取第一层Component
for (int i = 0 ; i<children.size();i++)

{
Assemblies::Component *component = children[i];//获取第一层第i个Component

//把装配子部件设置成工作部件
NXOpen::Assemblies::Component *component1(dynamic_cast<NXOpen::Assemblies::Component *>(NXOpen::NXObjectManager::Get(children[i]->Tag())));
NXOpen::PartLoadStatus *partLoadStatus1;
theSession->Parts()->SetWorkComponent(component1, NXOpen::PartCollection::RefsetOptionEntire, NXOpen::PartCollection::WorkComponentOptionVisible, &partLoadStatus1);
workPart = theSession->Parts()->Work();
delete partLoadStatus1;

//NXOpen对当前工作部件加属性
std::vector<NXObject *> objects1(1);
objects1[0] = workPart;
AttributePropertiesBuilder *attributePropertiesBuilder1;
attributePropertiesBuilder1 = theSession->AttributeManager()->CreateAttributePropertiesBuilder(workPart, objects1, AttributePropertiesBuilder::OperationTypeNone);
attributePropertiesBuilder1->SetTitle("PartNo");
attributePropertiesBuilder1->SetStringValue(component->DisplayName()); //属性值为部件名字

NXObject *nXObject1;
nXObject1 = attributePropertiesBuilder1->Commit();

attributePropertiesBuilder1->SetTitle("PartName");
attributePropertiesBuilder1->SetStringValue("后模仁");

NXObject *nXObject2;
nXObject2 = attributePropertiesBuilder1->Commit();

attributePropertiesBuilder1->Destroy();

//恢复到装配部件
NXOpen::Assemblies::Component *nullNXOpen_Assemblies_Component(NULL);
NXOpen::PartLoadStatus *partLoadStatus2;
theSession->Parts()->SetWorkComponent(nullNXOpen_Assemblies_Component, NXOpen::PartCollection::RefsetOptionEntire, NXOpen::PartCollection::WorkComponentOptionVisible, &partLoadStatus2);
workPart = theSession->Parts()->Work();
delete partLoadStatus2;

 

getchildren(component);

}

 

}

 

 

 

标签:遍历,Component,attributePropertiesBuilder1,部件,theSession,NXOpen,include,Assemblies
来源: https://www.cnblogs.com/hqsalanhuang/p/14224428.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有