NX二次开发 批量导出X_T(包括部件里的装配体)

VS2019 NX1946

UF_initialize();

string filepath = strDir + "\";//拼接路径
//获取图层过滤
PropertyList* stringLayerProps = stringLayer->GetProperties();
NXString filelayer = stringLayerProps->GetString("Value");
delete stringLayerProps;
stringLayerProps = NULL;

string filelayer_Str = filelayer.GetLocaleText() ;
//int Num = (int)filelayer_Str.length();//找到过滤字符串长度
string filelayerBegin = filelayer_Str.substr( 0, filelayer_Str.find_first_of("-"));//分割部件名获取过滤字符串
string filelayerEnd = filelayer_Str.substr(filelayer_Str.find_first_of("-")+1, filelayer_Str.find_last_of(" "));//分割部件名获取过滤字符串
//theSession->ListingWindow()->WriteLine(filelayerBegin);
//theSession->ListingWindow()->WriteLine(filelayerEnd);

std::vector<string> partname;

for (int i = 0; i < listboxselectStrings.size(); i++)
{
partname.push_back(listboxselectStrings[i].GetLocaleText());
int Num = (int)partname[i].length();//找到文件名长度
string newpartName = partname[i].substr(0, Num - 4);//分割部件名获取过滤字符串

string ExportPatFilePath = filepath + listboxselectStrings[i].GetLocaleText();
string ExportX_TFilePath = filepath + newpartName + ".x_t";
//
//打开文件(没打开直接打开,已打开重新打开)
BasePart* basePart1;
PartLoadStatus* partLoadStatus1;
try
{
// 文件已存在
basePart1 = theSession->Parts()->OpenBaseDisplay(ExportPatFilePath, &partLoadStatus1);
}
catch (const NXException& ex)
{
ex.AssertErrorCode(1020004);
}

Part* part1(dynamic_cast<Part*>(theSession->Parts()->FindObject(listboxselectStrings[i].GetLocaleText())));
BasePart* basePart2;
PartReopenReport* partReopenReport1;
basePart2 = part1->Reopen(BasePart::CloseWholeTreeFalse, BasePart::CloseModifiedCloseModified, NULL, &partReopenReport1);

delete partReopenReport1;
Session::UndoMarkId markId1;
markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Change Display Part");

Part* part2(dynamic_cast<Part*>(basePart2));
PartLoadStatus* partLoadStatus2;
PartCollection::SdpsStatus status1;
status1 = theSession->Parts()->SetDisplay(part2, true, true, &partLoadStatus2);

delete partLoadStatus2;

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

//遍历显示部件所有体的显示属性(包括装配体)
tag_t BodyTag = NULL_TAG;
UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_solid_type, &BodyTag); // UF_component_type ,UF_MODL_SOLID_BODY, UF_solid_type , UF_solid_body_subtype
int Type, SubType, Body_Type;
while (BodyTag != NULL_TAG)
{
//查询对象的类型和子类型
UF_OBJ_ask_type_and_subtype(BodyTag, &Type, &SubType);
if (SubType == UF_solid_body_subtype)
{
//查询体的类型
UF_MODL_ask_body_type(BodyTag, &Body_Type);
if (Body_Type == UF_MODL_SOLID_BODY)//为实体
{
//获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)
UF_OBJ_disp_props_t DispProps;
UF_OBJ_ask_display_properties(BodyTag, &DispProps);
int PartBodyLayer = DispProps.layer;
if (PartBodyLayer > atoi(filelayerBegin.c_str()) && PartBodyLayer <= atoi(filelayerEnd.c_str())) //用图层过滤体
{
BodyTagVector.push_back(BodyTag);
//sprintf(msg, "%d", BodyTag);
//theSession->ListingWindow()->WriteLine("部件名字:" + newpartName + " 体的TAG:" + msg);
}
}
}

UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_solid_type, &BodyTag);
}

//测试文件或目录是否存在
int STPPathStatus = 0;
UF_CFI_ask_file_exist(ExportX_TFilePath.c_str(), &STPPathStatus);
//如果文件已存在,先删除掉
if (STPPathStatus == 0) // 0表示文件存在 1表示文件不存在
{
DeleteFile(ExportX_TFilePath.c_str());
}

//导出
uf_list_p_t Bodylist = NULL;//定义链表
Bodylist = NULL;//定义链表
UF_MODL_create_list(&Bodylist); //创建链表
for (int j = 0; j < BodyTagVector.size(); j++)
{
tag_t tagBody = NULL_TAG;
tagBody = BodyTagVector[j];
UF_MODL_put_list_item(Bodylist, tagBody);
}
UF_MODL_delete_feature(Bodylist);
char szPartNameNewTemp[256];
sprintf_s(szPartNameNewTemp, "%s", ExportX_TFilePath.c_str());
UF_PS_export_data(Bodylist, szPartNameNewTemp);
UF_free(Bodylist);
Bodylist = NULL;
}
UF_terminate();

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