码迷,mamicode.com
首页 > 其他好文 > 详细

NX 二次开发 装配环境遍历体按图层过滤

时间:2021-07-07 17:55:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:dllexport   cycle   HERE   locale   void   获取   variable   names   ack   

VS2019 NX1946

 

//CycleLlayer

// 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>

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


//用户代码
#include <uf_layer.h>
#include<uf_obj.h>

#include<algorithm>
#include <iomanip>
#include <atlstr.h>
#include <fstream>

//获取运行程序的头文件
#include <string>
#include <atlstr.h>//非MFC工程中
//#include <afx.h> //MFC工程中
using namespace std;


using namespace NXOpen;
using std::string;
using std::exception;
using std::stringstream;
using std::endl;
using std::cout;
using std::cerr;


//------------------------------------------------------------------------------
// NXOpen c++ test class
//------------------------------------------------------------------------------
class MyClass
{
// class members
public:
static Session *theSession;
static UI *theUI;

MyClass();
~MyClass();

void do_it();
void print(const NXString &);
void print(const string &);
void print(const char*);

//用户代码
NXString doubleToNXString(double value);//double转换NXString
NXString intToNXString(int value);//int转换NXString

void YNCycleLayer();//遍历图层
void CycleBody(Assemblies::Component* component);//遍历工作部件所有体的显示属性

std::vector<string> componentNameOriginal;
std::vector<int> componentOriginalTag;
void CycleComponent();//遍历装配
void getchildren(Assemblies::Component* root);//遍历子组件

void SetWorkPart(Assemblies::Component* component1); //设置工作部件


private:
BasePart *workPart, *displayPart;
NXMessageBox *mb;
ListingWindow *lw;
LogFile *lf;
};

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
Session *(MyClass::theSession) = NULL;
UI *(MyClass::theUI) = NULL;

//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
MyClass::MyClass()
{

// Initialize the NX Open C++ API environment
MyClass::theSession = NXOpen::Session::GetSession();
MyClass::theUI = UI::GetUI();
mb = theUI->NXMessageBox();
lw = theSession->ListingWindow();
lf = theSession->LogFile();

workPart = theSession->Parts()->BaseWork();
displayPart = theSession->Parts()->BaseDisplay();

}

//------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------
MyClass::~MyClass()
{
}

//------------------------------------------------------------------------------
// Print string to listing window or stdout
//------------------------------------------------------------------------------
void MyClass::print(const NXString &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const string &msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}
void MyClass::print(const char * msg)
{
if(! lw->IsOpen() ) lw->Open();
lw->WriteLine(msg);
}

 


//------------------------------------------------------------------------------
// Do something
//------------------------------------------------------------------------------
void MyClass::do_it()
{

// TODO: add your code here
//YNCycleLayer();//遍历图层
//CycleBody(); ////遍历工作部件所有体的显示属性

CycleComponent();//遍历装配
}

//------------------------------------------------------------------------------
// Entry point(s) for unmanaged internal NXOpen C/C++ programs
//------------------------------------------------------------------------------
// Explicit Execution
extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
try
{
// Create NXOpen C++ class instance
MyClass *theMyClass;
theMyClass = new MyClass();
theMyClass->do_it();
delete theMyClass;
}
catch (const NXException& e1)
{
UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
}
catch (const exception& e2)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
}
catch (...)
{
UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
}
}


//------------------------------------------------------------------------------
// Unload Handler
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
}


NXString MyClass::doubleToNXString(double value)//double转换NXString
{
stringstream tmpValue1;
tmpValue1 << setprecision(8) << value;
return NXString(tmpValue1.str());
}

NXString MyClass::intToNXString(int value)//int转换NXString
{
stringstream tmpValue2;
tmpValue2 << setprecision(8) << value;
return NXString(tmpValue2.str());
}


void MyClass::YNCycleLayer()//遍历图层
{

UF_initialize();

theSession->ListingWindow()->Open();

std::vector<int> ObjTagVector;//对象TAG容器
ObjTagVector.clear();

char msg[256];

int i = -1;
tag_t objTAG = NULL_TAG;
do
{
UF_LAYER_cycle_by_layer(0, &objTAG);//0是所有图层,指定图层1~256
i++;
if (objTAG !=0)
{
ObjTagVector.push_back(objTAG);
}
}
while (objTAG != NULL_TAG);

for (int i = 0; i < ObjTagVector.size(); i++)
{
sprintf (msg, "%d", ObjTagVector[i]);
//theSession->ListingWindow()->WriteLine(msg);
}

UF_terminate();

}

void MyClass::CycleBody(Assemblies::Component* component) //遍历工作部件所有体的显示属性
{
UF_initialize();

Part* workPart;//定义工作部件为全局变量
workPart = theSession->Parts()->Work();//获取工作部件

Part* part1 = dynamic_cast<Part*>(component->Prototype()); //component强制转换为part

char msg[256];

std::vector<int> BodyTagVector;//体的TAG容器
BodyTagVector.clear();

int PartBodyLayer;

//遍历工作部件所有体的显示属性
BodyCollection* bodys = part1->Bodies();
for (BodyCollection::iterator ite = bodys->begin(); ite != bodys->end(); ite++)
{
Body* bodys = (*ite);

//获得对象信息
UF_OBJ_disp_props_s dispprops;
UF_OBJ_ask_display_properties(bodys->Tag(), &dispprops); //获取对象的显示属性(图层, 颜色, 隐藏状态, 线宽, 字体, 高亮状态)
//sprintf(msg, "体的TAG: %d", bodys->Tag());
//theSession->ListingWindow()->WriteLine(msg);
//sprintf(msg, "体的图层:%d", dispprops.layer);
//theSession->ListingWindow()->WriteLine(msg);
//sprintf(msg, "体的颜色:%d", dispprops.color);
//theSession->ListingWindow()->WriteLine(msg);
//sprintf(msg, "体的隐藏状态:%d", dispprops.blank_status);
//theSession->ListingWindow()->WriteLine(msg);
int PartBodyLayer = dispprops.layer;
if (PartBodyLayer > 9 && PartBodyLayer <150 ) //如果体在图层10~140
{
BodyTagVector.push_back(bodys->Tag());
theSession->ListingWindow()->WriteLine("部件名字:" + component->DisplayName() + " 部件真体TAG:" + intToNXString(bodys->Tag())+ " 体的图层:" + intToNXString(dispprops.layer));
}
}

UF_terminate();
}


void MyClass::CycleComponent()//遍历装配
{
try
{
//---- Enter your callback code here -----
//遍历装配
BasePart* basedisplaypart;//定义顶层显示部件为全局变量
basedisplaypart = theSession->Parts()->BaseDisplay();//获取顶层显示部件
Assemblies::ComponentAssembly* componentassembly = basedisplaypart->ComponentAssembly();//获取装配底层(BaseWork为当前工作装配,basedisplaypart为当前显示的装配 两种结果是不一样的)
Assemblies::Component* rootcomponent = componentassembly->RootComponent();//获取rootcomponent

if (rootcomponent == NULL)//判断rootcomponent是否为空,空即提示不是装配,是就执行遍历
{
theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeWarning, "当前没在装配环境!"); //弹出消息框提示
}
else if (rootcomponent != NULL)//不为空就打印装配的名字并遍历子组件
{
getchildren(rootcomponent);
}

//SetWorkPart(rootcomponent); //设置工作部件

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("遍历装配", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}


//遍历子组件
void MyClass::getchildren(Assemblies::Component* root)
{
try
{
//用户代码

std::vector<Assemblies::Component*> children = root->GetChildren();//获取第一层Component

std::vector<NXString> childrenName;//定义NXString类型子组件名字容器

//循环把工件名塞到容器
for (int i = 0; i < children.size(); i++)
{

Assemblies::Component* component = children[i];//获取第一层第i个Component
childrenName.push_back(component->DisplayName());//收集子组件的名字

//获取部件加载状态
bool isloading = true;
Part* resCurrentProtype = dynamic_cast<Part*>(component->Prototype());

if (resCurrentProtype == NULL)
{
isloading = false;// 不加载为false 全加载和部份加载为true
}

//把加载的部件名和TAG塞到容器
if (isloading == true)
{
//theSession->ListingWindow()->WriteLine("加载组件:"+children[i]->DisplayName()+":true");//信息窗口写入
componentNameOriginal.push_back(childrenName[i].GetLocaleText());//部件名塞到容器
componentOriginalTag.push_back(children[i]->Tag());//部件TAG塞到容器
//SetWorkPart(component); //设置工作部件
CycleBody(component); //遍历工作部件所有体的显示属性
}

//获取组件抑制状态
bool suppressedState = true;
if (component->IsSuppressed())
{
suppressedState = false; // 抑制的和空的为false
//theSession->ListingWindow()->WriteLine("抑制部件:"+component->DisplayName());
}
//如果部件没抑制就进行下一级遍历
if (suppressedState == true)
{
//theSession->ListingWindow()->WriteLine("正常部件:"+component->DisplayName());
getchildren(component);
}
}
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("遍历子组件", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}


void MyClass::SetWorkPart(Assemblies::Component* component1) //设置工作部件
{
try
{
//把装配子部件设置成工作部件
NXOpen::PartLoadStatus* partLoadStatus1;
theSession->Parts()->SetWorkComponent(component1, NXOpen::PartCollection::RefsetOptionEntire, NXOpen::PartCollection::WorkComponentOptionVisible, &partLoadStatus1);
workPart = theSession->Parts()->Work();
delete partLoadStatus1;
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("设置工作部件", NXOpen::NXMessageBox::DialogTypeError, "程序错误,请检查代码");
}
}

 技术图片

NX 二次开发 装配环境遍历体按图层过滤

标签:dllexport   cycle   HERE   locale   void   获取   variable   names   ack   

原文地址:https://www.cnblogs.com/hqsalanhuang/p/14981036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!