NX/UG二次开发孔特征查找

孔特征查找

在二次开发过程中会遇到需要获取模型孔特征的Tag值,以方便对获取到的孔特征进行编辑。下面介绍一种获取孔特征的方法。主要分为三个过程:1.获得模型的所有圆柱面的Tag值。2.获得圆柱面的所有边。3.判断每个边是否为整圆。

代码所需包含的额外头文件

#include <uf_obj.h>
#include <uf_assem.h>
#include <uf_modl.h>
#include <uf_ui.h>
#include <uf_eval.h>

1.获得模型的所有圆柱面的Tag值。

std::vector<tag_t> get_cylindrical_face()//创建一个获得所有圆柱面的方法,返回值为所有圆柱面Tag的vector容器。
{
	tag_t objectTAG = NULL_TAG;
	int thetype, thesubtype, face_type;
	std::vector<tag_t> face_collection;

	UF_OBJ_cycle_objs_in_part(UF_ASSEM_ask_work_part(), UF_solid_type, &objectTAG);//遍历获得工作部件上类型为solid_type的Tag。
	while (objectTAG != NULL_TAG)
	{
		UF_OBJ_ask_type_and_subtype(objectTAG, &thetype, &thesubtype);//获得块的类型和子类型。
		if (thesubtype == UF_solid_face_subtype)//判断子类型是否为UF_solid_face_subtype。
		{
			UF_MODL_ask_face_type(objectTAG, &face_type);//获得子类型为UF_solid_face_subtype的对象的面类型。

			if (face_type == UF_MODL_CYLINDRICAL_FACE)
			{
				face_collection.push_back(objectTAG);//如果面类型为圆柱面,将该Tag存入vector容器。
			}
		}
		UF_OBJ_cycle_objs_in_part(UF_ASSEM_ask_work_part(), UF_circle_type, &objectTAG);
	}
	return face_collection;//返回容器。
}

2.获得圆柱面的所有边。

std::vector<tag_t> get_circle_edge(std::vector<tag_t> face_collection)//创建一个获得圆柱面的所有边的方法。形参为所有圆柱面的vector容器,返回值为所有圆柱面所有边Tag的vector容器。
{
	std::vector<tag_t> edge_collection;
	for (int i = 0; i < size(face_collection); i++)
	{
		uf_list_p_t  edge_list;
		int count;
		tag_t edge_tag;

		UF_MODL_ask_face_edges(face_collection[i], &edge_list);//获得每个圆柱面包含所有边的链表。
		UF_MODL_ask_list_count(edge_list, &count);//获得链表的大小。
		for (int j = 0; j < count; j++)
		{
			UF_MODL_ask_list_item(edge_list, j, &edge_tag);//获得链表里的Tag值
			edge_collection.push_back(edge_tag);//将该Tag值存入vector容器。
		}
		UF_MODL_delete_list(&edge_list);//删除链表。
	}
	return edge_collection;//返回所有边的vector容器。
}

3.判断每个边是否为整圆。

bool judge_circle_edge(tag_t edge_tag)//创建判断每个边是否为整圆的方法。形参是边的Tag,返回值为布尔值。
{
	UF_EVAL_p_t evaluator;
	UF_EVAL_initialize(edge_tag, &evaluator);//使用UF_EVAL_ask_arc()函数需要先进行初始化

	UF_EVAL_arc_t arcCoords;
	UF_EVAL_ask_arc(evaluator, &arcCoords);//arc结构体中有曲线的参数(圆中,半径,是否整圆等相关信息)

	bool boolVal = false;
	if (arcCoords.is_periodic)//判断是否为圆
	{
		double arcCenter[3] = { 0.00 };
		arcCenter[0] = arcCoords.center[0];
		arcCenter[1] = arcCoords.center[1];
		arcCenter[2] = arcCoords.center[2];
		double arcx_axis[3] = { 0.00 };
		arcx_axis[0] = arcCoords.x_axis[0];
		arcx_axis[1] = arcCoords.x_axis[1];
		arcx_axis[2] = arcCoords.x_axis[2];
		double arcy_axis[3] = { 0.00 };
		arcy_axis[0] = arcCoords.y_axis[0];
		arcy_axis[1] = arcCoords.y_axis[1];
		arcy_axis[2] = arcCoords.y_axis[2];
		double arcR[1] = { 0.00 };
		arcR[0] = arcCoords.radius;
		//确定给定的数据是否来自圆形曲线或边缘
		bool boolIsArc;
		UF_EVAL_is_arc(evaluator, &boolIsArc);
		if (fabs(arcCoords.limits[0]) < 0.001 && fabs(arcCoords.limits[1] - 2 * PI) < 0.001)//判断是否是整圆
		{
			boolVal = true;//此圆是整圆。
		}

		UF_EVAL_free(evaluator);//释放评估器。
	}

	return boolVal;
}

do_it函数

void MyClass::do_it()
{

	// TODO: add your code here
	
	UF_initialize();
	UF_UI_open_listing_window();
	char msg[256];
	std::vector<tag_t> face_collection = get_cylindrical_face();
	std::vector<tag_t> edges_collection = get_circle_edge(face_collection);
	std::vector<tag_t> circle_edge_collection;

	bool boolVal;

	for (int i = 0; i < size(edges_collection); i++)//循环包含圆柱体所有的边的容器。
	{
		boolVal = judge_circle_edge(edges_collection[i]);//判断是否为整圆。
		if (boolVal)
		{
			circle_edge_collection.push_back(edges_collection[i]);//如果是整圆,将该Tag值收集到vector容器中。
			sprintf(msg, "圆的tag为%d", edges_collection[i]);//打印
			UF_UI_write_listing_window(msg);
		}
	}
	sprintf(msg, "整圆的个数为%d", (int)size(circle_edge_collection));//打印。
	UF_UI_write_listing_window(msg);

	UF_terminate();
}

  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值