1 NX11+VS2013 2 3 #include <NXOpen/Line.hxx> 4 #include <NXOpen/NXException.hxx> 5 #include <NXOpen/PartCollection.hxx> 6 #include <NXOpen/Session.hxx> 7 #include <uf.h> 8 #include <uf_curve.h> 9 #include <uf_ui.h> 10 #include <NXOpen/Edge.hxx> 11 #include <NXOpen/NXObjectManager.hxx> 12 #include <uf_eval.h> 13 14 NXOpen::Session *theSession = NXOpen::Session::GetSession(); 15 NXOpen::Part *workPart(theSession->Parts()->Work()); 16 NXOpen::Part *displayPart(theSession->Parts()->Display()); 17 18 UF_initialize(); 19 20 //创建一条直线 21 UF_CURVE_line_t Line_Coords; 22 Line_Coords.start_point[0] = 10.0; 23 Line_Coords.start_point[1] = 0.0; 24 Line_Coords.start_point[2] = 0.0; 25 Line_Coords.end_point[0] = 100.0; 26 Line_Coords.end_point[1] = 0.0; 27 Line_Coords.end_point[2] = 0.0; 28 tag_t LineTag = NULL_TAG; 29 UF_CURVE_create_line(&Line_Coords, &LineTag); 30 31 //方法1:UFUN方法 32 //获得一条直线两个端点坐标、直线长度、向量方向 33 34 //函数1(UF_CURVE_ask_line_data) 35 UF_CURVE_line_t AskLinePoint; 36 UF_CURVE_ask_line_data(LineTag, &AskLinePoint); 37 double UF1LinePoint1[3] = { AskLinePoint.start_point[0], AskLinePoint.start_point[1], AskLinePoint.start_point[2] };//获得直线起点坐标 38 double UF1LinePoint2[3] = { AskLinePoint.end_point[0], AskLinePoint.end_point[1], AskLinePoint.end_point[2] };//获得直线终点坐标 39 40 //函数2(UF_EVAL_ask_line) 41 UF_EVAL_p_t evaluator; 42 UF_EVAL_initialize(LineTag, &evaluator); 43 44 UF_EVAL_line_t line; 45 UF_EVAL_ask_line(evaluator, &line); 46 double UFLineLength = line.length;//获得直线长度 47 double UF2LinePoint1[3] = { line.start[0], line.start[1], line.start[2] };//获得直线起点坐标 48 double UF2LinePoint2[3] = { line.end[0], line.end[1], line.end[2] };//获得直线终点坐标 49 double UFLineVec[3] = { line.unit[0], line.unit[1], line.unit[2] };//获得直线向量方向 50 51 char msg[256]; 52 sprintf_s(msg, "直线的长度为:%.3f\n直线的起点坐标X为:%.3f 直线的起点坐标Y为:%.3f 直线的起点坐标Z为:%.3f\n直线的终点坐标X为%.3f 直线的终点坐标Y为:%.3f 直线的终点坐标Z为:%.3f\n直线的向量方向为:%.0f,%.0f,%.0f", 53 UFLineLength, UF2LinePoint1[0], UF2LinePoint1[1], UF2LinePoint1[2], UF2LinePoint2[0], UF2LinePoint2[1], UF2LinePoint2[2], UFLineVec[0], UFLineVec[1], UFLineVec[2]); 54 55 lw->Open(); 56 lw->WriteLine(msg); 57 58 UF_EVAL_free(evaluator); 59 60 //方法2:NXOpen方法 61 //获得一条直线两个端点坐标、直线长度 62 std::vector<Line*> LineVector; 63 LineVector.push_back(dynamic_cast<Line*>(NXObjectManager::Get(LineTag))); 64 65 for (int i = 0; i < LineVector.size(); i++) 66 { 67 double NXLineLength = LineVector[i]->GetLength();//获得直线长度 68 Point3d NXLinePoint1 = LineVector[i]->StartPoint();//获得直线起点坐标 69 //char msg[200]; 70 //sprintf_s(msg, "%.3f,%.3f,%.3f", NXLinePoint1.X, NXLinePoint1.Y, NXLinePoint1.Z); 71 //uc1601(msg, 1); 72 Point3d NXLinePoint2 = LineVector[i]->EndPoint();//获得直线终点坐标 73 //sprintf_s(msg, "%.3f,%.3f,%.3f", NXLinePoint2.X, NXLinePoint2.Y, NXLinePoint2.Z); 74 //uc1601(msg, 1); 75 } 76 77 UF_terminate();