- 文章讨论主题
昨天有位朋友问我,已知一个点,距离,矢量方向,怎样求出另外个点的坐标?
- 方法1
使用UF函数UF_CSYS_map_point进行坐标系转换
代码
NX11+VS2013
#include <uf.h>
#include <uf_ui.h>
#include <uf_csys.h>
#include <uf_mtx.h>
#include <uf_curve.h>
#include <uf_vec.h>
UF_initialize();
//获取WCS标识
tag_t WcsId = NULL_TAG;
UF_CSYS_ask_wcs(&WcsId);
//向量
double Vec[3] = { cos(30 * DEGRA), sin(30 * DEGRA), 0 };
//3*3矩阵,输入Z向量,得到矩阵
double Mtx[9];
UF_MTX3_initialize_z(Vec, Mtx);
//创建矩阵
tag_t MatrixTag = NULL_TAG;
UF_CSYS_create_matrix(Mtx, &MatrixTag);
//创建临时坐标系
double P1[3] = { 15.0, 10.0, 0.0 };//起始点
tag_t CsysTag = NULL_TAG;
UF_CSYS_create_temp_csys(P1, MatrixTag, &CsysTag);
//设置WCS
UF_CSYS_set_wcs(CsysTag);
double distance = 30.0;//移动的距离
double P2[3] = { 0, 0, distance };//这里要注意,转换前的点是从0,0,0开始加distance的,不是从P1开始去加distance
//从当前工作坐标系转换到绝对坐标系
int InputCsys = UF_CSYS_ROOT_WCS_COORDS;
int OutputCsys = UF_CSYS_ROOT_COORDS;
double OutputPoint[3];//移动后点坐标
UF_CSYS_map_point(InputCsys, P2, OutputCsys, OutputPoint);
//创建直线
UF_CURVE_line_t LineCoods;
LineCoods.start_point[0] = P1[0];
LineCoods.start_point[1] = P1[1];
LineCoods.start_point[2] = P1[2];
LineCoods.end_point[0] = OutputPoint[0];
LineCoods.end_point[1] = OutputPoint[1];
LineCoods.end_point[2] = OutputPoint[2];
tag_t LineTag = NULL_TAG;
UF_CURVE_create_line(&LineCoods, &LineTag);
char msg[256];
sprintf(msg, "坐标:( %f, %f, %f)", OutputPoint[0], OutputPoint[1], OutputPoint[2]);
UF_UI_open_listing_window();
UF_UI_write_listing_window(msg);
//设置WCS
UF_CSYS_set_wcs(WcsId);
UF_terminate();
阿飞
2022年4月17日
演示
- 方法2
使用UF函数UF_VEC3_affine_comb
转载自唐工的这篇 NX二次开中UF_VEC3_affine_comb函数的数学原理!
代码
NX11+VS2013
#include <uf.h>
#include <uf_ui.h>
#include <uf_csys.h>
#include <uf_mtx.h>
#include <uf_curve.h>
#include <uf_vec.h>
UF_initialize();
//方法2 函数 UF_VEC3_affine_comb
double oldPoint[3] = { 15.0, 10.0, 0.0 };
double vec_to_scale[3] = { cos(30 * DEGRA), sin(30 * DEGRA), 0 };//向量方向
double newPoint[3];//移动后点坐标
UF_VEC3_affine_comb(oldPoint, 30.0, vec_to_scale, newPoint);
//创建直线
UF_CURVE_line_t LineCoods;
LineCoods.start_point[0] = oldPoint[0];
LineCoods.start_point[1] = oldPoint[1];
LineCoods.start_point[2] = oldPoint[2];
LineCoods.end_point[0] = newPoint[0];
LineCoods.end_point[1] = newPoint[1];
LineCoods.end_point[2] = newPoint[2];
tag_t LineTag = NULL_TAG;
UF_CURVE_create_line(&LineCoods, &LineTag);
char msg[256];
sprintf(msg, "坐标:( %f, %f, %f)", newPoint[0], newPoint[1], newPoint[2]);
UF_UI_open_listing_window();
UF_UI_write_listing_window(msg);
UF_terminate();
演示
- 方法3
使用数学方法计算
数学方法是我的开发老师苏工告诉我的。
已知一个点,距离,矢量方向,怎样求出另外个点的坐标?
这个是基本的数学几何运算,自己写一个函数就可以。
就是一个点沿着一个矢量方向移动一段距离
就是点的xyz坐标分别+对应矢量*距离
比如点1(0,0,0)沿矢向(0,0,1)移动5得到的坐标就是(0,0,5)
x=0+0*5
y=0+0*5
z=0+1*5=5
前提向量要是单位矢量,长度为1
矢量的x*x+y*y+z*z = 1
代码
NX11+VS2013
#include <uf.h>
#include <uf_ui.h>
#include <uf_csys.h>
#include <uf_mtx.h>
#include <uf_curve.h>
#include <uf_vec.h>
UF_initialize();
//方法3 数学计算
double distance = 30.0;//移动的距离
double oldPoint[3] = { 15.0, 10.0, 0.0 };//起始点
double vec_to_scale[3] = { cos(30 * DEGRA), sin(30 * DEGRA), 0 };//向量方向
double newPoint[3];//移动后点坐标
newPoint[0] = oldPoint[0] + vec_to_scale[0] * distance;
newPoint[1] = oldPoint[1] + vec_to_scale[1] * distance;
newPoint[2] = oldPoint[2] + vec_to_scale[2] * distance;
//创建直线
UF_CURVE_line_t LineCoods;
LineCoods.start_point[0] = oldPoint[0];
LineCoods.start_point[1] = oldPoint[1];
LineCoods.start_point[2] = oldPoint[2];
LineCoods.end_point[0] = newPoint[0];
LineCoods.end_point[1] = newPoint[1];
LineCoods.end_point[2] = newPoint[2];
tag_t LineTag = NULL_TAG;
UF_CURVE_create_line(&LineCoods, &LineTag);
char msg[256];
sprintf(msg, "坐标:( %f, %f, %f)", newPoint[0], newPoint[1], newPoint[2]);
UF_UI_open_listing_window();
UF_UI_write_listing_window(msg);
UF_terminate();
阿飞
2022年4月17日
演示
阿飞
2022年4月17日