这篇博客内容是紧接着上一篇的NX二次开发-使用数学方法计算两个向量之间的夹角(计算顺逆判断锐角还是钝角)
探讨的内容,也是一样的。就是区分夹角是锐角还是钝角,区分方法,就是这个函数UF_VEC3_dot,点乘的值。函数是QQ群的一位网友发的
我不确定用的对不对,用的不对,请底部留言给我。
//a·b > 0 点乘大于0 两个向量方向基本相同 夹角在0°90°之间
//a·b = 0 点乘等于0 正交
//a·b < 0 点乘小于0 两个向量方向基本相反 夹角在90°-180°之间
NX9+VS2012 //NX9_NXOpenCPP_Wizard1 // Mandatory UF Includes #include <uf.h> #include <uf_object_types.h> // Internal Includes #include <NXOpen/ListingWindow.hxx> #include <NXOpen/NXMessageBox.hxx> #include <NXOpen/UI.hxx> // Internal+External Includes #include <NXOpen/Annotations.hxx> #include <NXOpen/Assemblies_Component.hxx> #include <NXOpen/Assemblies_ComponentAssembly.hxx> #include <NXOpen/Body.hxx> #include <NXOpen/BodyCollection.hxx> #include <NXOpen/Face.hxx> #include <NXOpen/Line.hxx> #include <NXOpen/NXException.hxx> #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Session.hxx> #include <uf.h> #include <uf_ui.h> #include <uf_vec.h> #include <uf_curve.h> // Std C++ Includes #include <iostream> #include <sstream> using namespace NXOpen; using std::string; using std::exception; using std::stringstream; using std::endl; using std::cout; using std::cerr; //------------------------------------------------------------------------------ // NXOpen c++ test class //------------------------------------------------------------------------------ class MyClass { // class members public: static Session *theSession; static UI *theUI; MyClass(); ~MyClass(); void do_it(); void print(const NXString &); void print(const string &); void print(const char*); private: Part *workPart, *displayPart; NXMessageBox *mb; ListingWindow *lw; LogFile *lf; }; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(MyClass::theSession) = NULL; UI *(MyClass::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ MyClass::MyClass() { // Initialize the NX Open C++ API environment MyClass::theSession = NXOpen::Session::GetSession(); MyClass::theUI = UI::GetUI(); mb = theUI->NXMessageBox(); lw = theSession->ListingWindow(); lf = theSession->LogFile(); workPart = theSession->Parts()->Work(); displayPart = theSession->Parts()->Display(); } //------------------------------------------------------------------------------ // Destructor //------------------------------------------------------------------------------ MyClass::~MyClass() { } //------------------------------------------------------------------------------ // Print string to listing window or stdout //------------------------------------------------------------------------------ void MyClass::print(const NXString &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const string &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const char * msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { // TODO: add your code here UF_initialize(); //创建直线1 UF_CURVE_line_t LineCoords1; LineCoords1.start_point[0] = 0.0; LineCoords1.start_point[1] = 0.0; LineCoords1.start_point[2] = 0.0; LineCoords1.end_point[0] = 0.0; LineCoords1.end_point[1] = 100.0; LineCoords1.end_point[2] = 0.0; tag_t Line1Tag = NULL_TAG; UF_CURVE_create_line(&LineCoords1, &Line1Tag); //创建直线2 UF_CURVE_line_t LineCoords2; LineCoords2.start_point[0] = 0.0; LineCoords2.start_point[1] = 0.0; LineCoords2.start_point[2] = 0.0; LineCoords2.end_point[0] = 110.0; LineCoords2.end_point[1] = 80.0; LineCoords2.end_point[2] = 0.0; tag_t Line2Tag = NULL_TAG; UF_CURVE_create_line(&LineCoords2, &Line2Tag); //直线1的向量方向,终点减起点 double Vec1[3] = {LineCoords1.end_point[0]-LineCoords1.start_point[0], LineCoords1.end_point[1]-LineCoords1.start_point[1], LineCoords1.end_point[2]-LineCoords1.start_point[2]}; //直线2的向量方向,终点减起点 double Vec2[3] = {LineCoords2.end_point[0]-LineCoords2.start_point[0], LineCoords2.end_point[1]-LineCoords2.start_point[1], LineCoords2.end_point[2]-LineCoords2.start_point[2]}; //求两个向量的叉乘 double CrossProduct[3]; UF_VEC3_cross(Vec1, Vec2, CrossProduct); //已知两个向量求夹角(输出的是弧度) double Angle; UF_VEC3_angle_between(Vec1, Vec2, CrossProduct, &Angle); //向量点乘 double dot_product = 0; UF_VEC3_dot(Vec1, Vec2, &dot_product); //a·b > 0 点乘大于0 两个向量方向基本相同 夹角在0°90°之间 //a·b = 0 点乘等于0 正交 //a·b < 0 点乘小于0 两个向量方向基本相反 夹角在90°-180°之间 double ss22 = RADEG*Angle; if (dot_product < 0) { ss22 = 360 - RADEG*Angle;//角度=180度/π*弧度,或者用宏定义RADEG*弧度 } //打印 char msg[256]; sprintf(msg, "%f", ss22); uc1601(msg, 1); UF_terminate(); } //------------------------------------------------------------------------------ // Entry point(s) for unmanaged internal NXOpen C/C++ programs //------------------------------------------------------------------------------ // Explicit Execution extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) { try { // Create NXOpen C++ class instance MyClass *theMyClass; theMyClass = new MyClass(); theMyClass->do_it(); delete theMyClass; } catch (const NXException& e1) { UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); } catch (const exception& e2) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); } catch (...) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); } } //------------------------------------------------------------------------------ // Unload Handler //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; } 阿飞 2021年10月17日
阿飞 2021年10月17日