NX二次开发-UFUN发射线函数UF_MODL_trace_a_ray的用法

今天是国庆节,放假休息懒得动,没有出去玩,在家研究一下发射线函数UF_MODL_trace_a_ray。小弟以前在软件公司混的时候,当时我做的那个项目就用到了UF_MODL_trace_a_ray,当时为了赶项目,从别处抄了代码,改吧改吧。自己也没有仔细的去研究一下这个函数的用法。UF_MODL_trace_a_ray在NX二次开发中算是一个用到比较高频的函数,今天研究了一下,写了一道例题,UFUN和NXOpen C++简单混合去用了一下,大致介绍下函数的用法。如有错误,还请各位前辈多多指教小弟!
在这里插入图片描述

NX11+VS2013

#include <uf.h>
#include <uf_ui.h>
#include <uf_modl.h>
#include <uf_mtx.h>
#include <uf_curve.h>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <uf_obj.h>
#include <NXOpen/ListingWindow.hxx>

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

UF_initialize();

//创建点
double Point1[3] = { -300.0, 50.0, 50.0 };
tag_t PointTag = NULL_TAG;
UF_CURVE_create_point(Point1, &PointTag);

//创建块
UF_FEATURE_SIGN Sign = UF_NULLSIGN;
double Corner_pt[3] = { 0.0, 0.0, 0.0 };
char *Edge_len[3] = { "100", "100", "100"};
tag_t BlkTag = NULL_TAG;
UF_MODL_create_block1(Sign, Corner_pt, Edge_len, &BlkTag);

//特征找体
tag_t BodyTag = NULL_TAG;
UF_MODL_ask_feat_body(BlkTag, &BodyTag);

//创建射线(从点出发,向X方向,发射到块上)
tag_t Bodies[1] = { BodyTag };
double Direction[3] = { 1.0, 0.0, 0.0 };
double TransForm[16];
UF_MTX4_identity(TransForm);
int num_results;
UF_MODL_ray_hit_point_info_p_t  hit_list;
UF_MODL_trace_a_ray(1, Bodies, Point1, Direction, TransForm, 0, &num_results, &hit_list);

//判断射线与块是否有交点
if ( num_results == 0 )
{
	uc1601("提示:找不到射线交点", 1);
}

if ( num_results != 0 )
{
	//创建直线(连接发射点和射线与块的第一个交点)
	NXOpen::Point3d StartPoint1{ Point1[0], Point1[1], Point1[2] };
	NXOpen::Point3d EndPoint1{ hit_list[0].hit_point[0], hit_list[0].hit_point[1], hit_list[0].hit_point[2] };
	NXOpen::Line *Line1;
	Line1 = workPart->Curves()->CreateLine(StartPoint1, EndPoint1);
	
	//赋予直线红色
	UF_OBJ_set_color(Line1->Tag(), 186);

	//创建直线(连接射线与块的第一个交点与第二个交点)
	NXOpen::Point3d EndPoint2{ hit_list[1].hit_point[0], hit_list[1].hit_point[1], hit_list[1].hit_point[2] };
	NXOpen::Line *Line2;
	Line2 = workPart->Curves()->CreateLine(EndPoint1, EndPoint2);

	//赋予直线绿色
	UF_OBJ_set_color(Line2->Tag(), 108);

	//赋予第一个交点所在面为蓝色
	tag_t FirstFaceTag = { hit_list[0].hit_face };
	UF_OBJ_set_color(FirstFaceTag, 211);

	//赋予第二个交点所在面为紫色
	tag_t SecondFaceTag = { hit_list[1].hit_face };
	UF_OBJ_set_color(SecondFaceTag, 164);

	//打印坐标点和方向
	char msg[256];
	sprintf_s(msg, "交点数量为:%d\n射线与块第一个交点坐标为:\nX坐标:%f\nY坐标:%f\nZ坐标:%f\n射线与块第二个交点坐标为:\nX坐标:%f\nY坐标:%f\nZ坐标:%f",						
		num_results,hit_list[0].hit_point[0], hit_list[0].hit_point[1], hit_list[0].hit_point[2], hit_list[1].hit_point[0], hit_list[1].hit_point[1], hit_list[1].hit_point[2]);
		
	lw->Open();
	lw->WriteLine(msg);
}

UF_terminate();

Caesar卢尚宇  [email protected]

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lu1287580078/article/details/82916477