循环组件中的重名对象

这是一篇测试代码,其中有很多条件的测试,尤其注意注释部分,知识 点较多

UF_initialize();
char msg[111];
theSession->ListingWindow()->Open();

Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());

//////组件tag遍历 子类型
tag_t part_tag;
tag_t occ_tag;
Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT WT57-10H0-001 1")));
occ_tag = component1->Tag(); //但是此处只是一个occ Tag.不是原零件的tag
part_tag = UF_ASSEM_ask_prototype_of_occ(occ_tag);
sprintf(msg,"组件的part_Tag:%d",part_tag);
theSession->ListingWindow()->WriteLine(msg);

/*////体选择传递
std::vector<TaggedObject*>objects=this->selection0->GetProperties()->GetTaggedObjectVector("SelectedObjects"); //但是此处只是一个occ Tag.不是原零件的tag
///Body *body = dynamic_cast<Body *>(objects[0]); ///////由于返回的是TaggedObject类型的一个vector,故需进行类型转换。

sprintf(msg,"选择的tag%d",objects[0]->Tag()); //选择时注意是compont还是body ,得到的tag会不同
theSession->ListingWindow()->WriteLine(msg);*/

char part_fspec[MAX_FSPEC_SIZE+1];
int curr_part, num_parts;
int count_1 = 0;
int type = UF_line_type; //可以查询line, sketch,其他的edge,feature,solid摸不清规律
tag_t part;
/*当前共打开 多少部件
num_parts = UF_PART_ask_num_parts();
sprintf(msg,"数量为:%d",num_parts);
theSession->ListingWindow()->WriteLine(msg);
////t第几个部件是什么,此两函数配合使用
part = UF_PART_ask_nth_part( 0 );
UF_PART_ask_part_name( part, part_fspec );
sprintf(msg,"第一个名称为:%s,tag为%d",part_fspec,part);
theSession->ListingWindow()->WriteLine(msg);*/

/* Start the cycling process by passing in a NULL_TAG. */
tag_t feature = NULL_TAG;
/*///不重复的Featture优先用名称,不是的再用name_and_type
int count = 0;
tag_t foundThis = NULL_TAG;
tag_t * object ;
UF_OBJ_cycle_by_name("line4",&foundThis);
while ( foundThis != NULL_TAG )
{
count++;
UF_OBJ_cycle_by_name("k1",&foundThis);
}
sprintf(msg,"查询名称的数量tag:%d",count);
theSession->ListingWindow()->WriteLine(msg);
////不重复的Featture优先用名称,不是的再用名称和类型 */

tag_t parentPart = UF_PART_ask_display_part();

const char * name= "LINE7" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名

UF_OBJ_cycle_by_name_and_type(parentPart,name,type, TRUE, &feature);
sprintf(msg,"tag:%d",feature);
theSession->ListingWindow()->WriteLine(msg);

/*tag_t * part_occs;
int occ_count = 0;
occ_count = UF_ASSEM_ask_occs_of_part( parentPart,part_tag,&part_occs);
for (int i = 0; i < occ_count; i++)
{
sprintf(msg,"%d",part_occs[i]);
theSession->ListingWindow()->WriteLine(msg);
}
UF_free(part_occs); */
tag_t * parent_parts ;
while ( feature != NULL_TAG )
{
count_1++;
//int cc;
//cc = UF_ASSEM_where_is_part_used(42637,&parent_parts); //这个只能用部件名找,不能用下面的特征,所以不适用此处。
sprintf(msg,"N:%d",parent_parts[0]);
theSession->ListingWindow()->WriteLine(msg);
UF_OBJ_cycle_by_name_and_type(part_tag, name,type, TRUE, &feature);//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
}

/////////////////
//UF_OBJ_cycle_objs_in_part(part_tag, type, &feature ); //此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
///* Keep cycling until there are no more features to cycle. */
//while ( feature != NULL_TAG )
//{
// count_1++;
// UF_OBJ_cycle_objs_in_part(part_tag, type, &feature );//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
//}
sprintf(msg,"Line数量:%d",count_1);
theSession->ListingWindow()->WriteLine(msg);

UF_free(parent_parts);
UF_terminate();

posted @ 2021-10-09 16:20  光量子  阅读(83)  评论(0编辑  收藏  举报