一、概述
? ? ? ? 最近作了一个案例是通过遍历整个显示部件窗口的所有实体合并成一个部件,并且在导航器上也显示一个体,主要的思路是遍历当前所有实体,然后进行一键合并如图1所示,最后去除参数,这时导航器中显示一个体的记录。如下图2所示:
图1.一键合并
图2.一键去参
二、代码记录
注意:本次代码对遍历整个显示部件窗口实体函数进行封装,如需要优化代码,请自行封装。
///
//一键合并
///
NXOpen::Session *theSession = NXOpen::Session::GetSession();
NXOpen::Part *workPart(theSession->Parts()->Work());
NXOpen::Part *displayPart(theSession->Parts()->Display());
vector<tag_t> thecycleTAG;
//找到工作部件所有的体
tag_t WorkPartTag1 = UF_PART_ask_display_part();
tag_t objTag1 = NULL_TAG;
UF_OBJ_cycle_objs_in_part1(WorkPartTag1, UF_solid_type, &objTag1);
int type1;
int subtype1;
while (objTag1 != NULL_TAG)
{
UF_OBJ_ask_type_and_subtype(objTag1, &type1, &subtype1);
if (!subtype1)
{
thecycleTAG.push_back(objTag1);
}
UF_OBJ_cycle_objs_in_part1(WorkPartTag1, UF_solid_type, &objTag1);
}
NXOpen::Features::BooleanFeature *nullNXOpen_Features_BooleanFeature(NULL);
NXOpen::Features::BooleanBuilder *booleanBuilder1;
booleanBuilder1 = workPart->Features()->CreateBooleanBuilderUsingCollector(nullNXOpen_Features_BooleanFeature);
//建模公差
booleanBuilder1->SetTolerance(0.001);
//选择布尔类型——求和BooleanTypeUnite
booleanBuilder1->SetOperation(NXOpen::Features::Feature::BooleanTypeUnite);
//目标体的设置
NXOpen::Body *body1(dynamic_cast<NXOpen::Body *>(NXOpen::NXObjectManager::Get(thecycleTAG[0])));
bool added1;
added1 = booleanBuilder1->Targets()->Add(body1);
//工具体设置
NXOpen::ScCollector *scCollector2;//定义一个工具收集器
scCollector2 = workPart->ScCollectors()->CreateCollector();
std::vector<NXOpen::Body *> bodies1;//获得工具体的body
for (int i = 1; i < thecycleTAG.size(); i++)
{
NXOpen::Body *body(dynamic_cast<NXOpen::Body *>(NXOpen::NXObjectManager::Get(thecycleTAG[i])));
bodies1.push_back(body);
}
NXOpen::BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1, true);
std::vector<NXOpen::SelectionIntentRule *> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector2->ReplaceRules(rules1, false);
//设置目标体
booleanBuilder1->SetToolBodyCollector(scCollector2);
//创建、销毁
NXOpen::NXObject *nXObject1;
nXObject1 = booleanBuilder1->Commit();
booleanBuilder1->Destroy();
//
//移除参数
//
UF_initialize();
//找到工作部件所有的体
tag_t WorkPartTag = UF_ASSEM_ask_work_part();
tag_t objTag = NULL_TAG;
UF_OBJ_cycle_objs_in_part1(WorkPartTag, UF_solid_type, &objTag);
int type,subtype;
//创建链表
uf_list_p_t list;
UF_MODL_create_list(&list);
while (objTag != NULL_TAG)
{
UF_OBJ_ask_type_and_subtype(objTag, &type, &subtype);
if (!subtype)
{
UF_MODL_put_list_item(list, objTag);
}
UF_OBJ_cycle_objs_in_part1(WorkPartTag, UF_solid_type, &objTag);
}
int count;
UF_MODL_ask_list_count(list, &count);
if (count>0)
{
//移除参数
UF_MODL_delete_object_parms(list);
}
else
{
uc1601("温馨提示:未找到要移除参数对象。", 1);
}
//删除链表,释放内存
UF_MODL_delete_list(&list);
UF_terminate();