1. 程式人生 > 其它 >NX二次開發-已知一個點,距離,向量方向,怎樣求出另外個點的座標?

NX二次開發-已知一個點,距離,向量方向,怎樣求出另外個點的座標?

  • 文章討論主題

昨天有位朋友問我,已知一個點,距離,向量方向,怎樣求出另外個點的座標?

  • 方法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();


//向量
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); 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日