NX二次開發-測量投影距離


NXOPEN方法,直接錄制測量投影距離命令

 1 NX9+VS2012
 2 
 3 #include <NXOpen/Annotations.hxx>
 4 #include <NXOpen/Assemblies_Component.hxx>
 5 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
 6 #include <NXOpen/Body.hxx>
 7 #include <NXOpen/BodyCollection.hxx>
 8 #include <NXOpen/Face.hxx>
 9 #include <NXOpen/Line.hxx>
10 #include <NXOpen/NXException.hxx>
11 #include <NXOpen/Part.hxx>
12 #include <NXOpen/PartCollection.hxx>
13 #include <NXOpen/DirectionCollection.hxx>
14 #include <NXOpen/MeasureBuilder.hxx>
15 #include <NXOpen/MeasureDistance.hxx>
16 #include <NXOpen/MeasureDistanceBuilder.hxx>
17 #include <NXOpen/MeasureManager.hxx>
18 #include <NXOpen/Point.hxx>
19 #include <NXOpen/PointCollection.hxx>
20 #include <NXOpen/ScCollector.hxx>
21 #include <NXOpen/SelectDisplayableObject.hxx>
22 #include <NXOpen/NXObjectManager.hxx>
23 #include <uf_obj.h>
24 #include <uf_part.h>
25 #include <uf_ui.h>
26 
27 
28 
29 UF_initialize();
30 
31 
32 //找名字得到面的tag
33 tag_t FaceTag = NULL_TAG;
34 UF_OBJ_cycle_by_name("MYFACE", &FaceTag);
35 
36 //以下為NXOPEN錄制
37 NXObject *nullNXObject(NULL);
38 MeasureDistanceBuilder *measureDistanceBuilder1;
39 measureDistanceBuilder1 = workPart->MeasureManager()->CreateMeasureDistanceBuilder(nullNXObject);
40 
41 Point3d origin1(0.0, 0.0, 0.0);
42 Vector3d vector1(0.0, 1.0, 0.0);//設置矢量方向
43 Direction *direction1;
44 direction1 = workPart->Directions()->CreateDirection(origin1, vector1, SmartObject::UpdateOptionAfterModeling);
45 
46 measureDistanceBuilder1->SetProjectionVector(direction1);//設置投影
47 
48 //創建點
49 NXOpen::Point3d Point1XYZ(0,0,0);
50 
51 //Point3d轉Point
52 NXOpen::Point *Point1 = workPart->Points()->CreatePoint(Point1XYZ);
53 
54 //設置第一個對象
55 measureDistanceBuilder1->Object1()->SetValue(Point1);
56 
57 //設置第二個對象
58 Face *face1(dynamic_cast<Face *>(NXOpen::NXObjectManager::Get(FaceTag)));
59 measureDistanceBuilder1->Object2()->SetValue(face1);
60 
61 //創建,類型為投影距離
62 MeasureDistance *measureDistance1;
63 measureDistance1 = workPart->MeasureManager()->NewDistance(NULL, Point1, face1, direction1, MeasureManager::ProjectionTypeMinimum);
64 
65 //獲得距離
66 double Distance = measureDistance1->Value();
67 
68 //打印
69 char msg[256];
70 sprintf_s(msg, "%f", Distance);
71 uc1601(msg, 1);
72 
73 //銷毀刪除
74 delete measureDistance1;
75 
76 UF_terminate();
77 
78 Caesar盧尚宇
79 2019年7月26日

UFUN寫的算法

思路:自己指定原點和矢量方向,從原點沿着矢量方向作一條無限長的直線,在對選擇的面獲取面的原點和向量方向,創建一個基准平面(基准平面是無限大的),最后求直線和基准平面的交點到原點距離,就是投影距離。

  1 NX9+VS2012
  2 
  3 #include <uf.h>
  4 #include <uf_ui.h>
  5 #include <uf_csys.h>
  6 #include <uf_curve.h>
  7 #include <uf_obj.h>
  8 #include <uf_mtx.h>
  9 #include <uf_modl.h>
 10 
 11 
 12     
 13 UF_initialize();
 14 
 15 //找名字得到點的tag
 16 tag_t PointTag = NULL_TAG;
 17 UF_OBJ_cycle_by_name("MYPOINT", &PointTag);
 18 
 19 //找名字得到面的tag
 20 tag_t FaceTag = NULL_TAG;
 21 UF_OBJ_cycle_by_name("MYFACE", &FaceTag);
 22 
 23 //由點出發創建直線
 24 
 25 //創建向量方向
 26 double Vec[3] = { 0.0, 0.0, 1.0 };
 27 
 28 //3*3矩陣,輸入Z向量,得到矩陣
 29 double Mtx[9];
 30 UF_MTX3_initialize_z(Vec, Mtx);
 31 
 32 //創建矩陣
 33 tag_t MatrixTag = NULL_TAG;
 34 UF_CSYS_create_matrix(Mtx, &MatrixTag);
 35 
 36 //創建臨時坐標系
 37 double P1[3] = { 0.0, 0.0, 0.0 };//直線起點
 38 tag_t CsysTag = NULL_TAG;
 39 UF_CSYS_create_temp_csys(P1, MatrixTag, &CsysTag);
 40 
 41 //設置WCS
 42 UF_CSYS_set_wcs(CsysTag);
 43 
 44 //創建直線終點
 45 double P2[3] = { P1[0], P1[1] + 10000, P1[2] };
 46 
 47 //從當前工作坐標系轉換到絕對坐標系
 48 int InputCsys = UF_CSYS_ROOT_WCS_COORDS;
 49 int OutputCsys = UF_CSYS_ROOT_COORDS;
 50 double OutputPoint[3];
 51 UF_CSYS_map_point(InputCsys, P2, OutputCsys, OutputPoint);
 52 
 53 //創建直線
 54 UF_CURVE_line_t LineCoods;
 55 LineCoods.start_point[0] = P1[0];
 56 LineCoods.start_point[1] = P1[1];
 57 LineCoods.start_point[2] = P1[2];
 58 LineCoods.end_point[0] = OutputPoint[0];
 59 LineCoods.end_point[1] = OutputPoint[1];
 60 LineCoods.end_point[2] = OutputPoint[2];
 61 tag_t LineTag = NULL_TAG;
 62 UF_CURVE_create_line(&LineCoods, &LineTag);
 63 
 64 //獲得面的原點和向量方向
 65 int Type;
 66 double Point[3];
 67 double Dir[3];
 68 double Box[6];
 69 double Radius[3];
 70 double RadData[3];
 71 int NormDir;
 72 UF_MODL_ask_face_data(FaceTag, &Type, Point, Dir, Box, Radius, RadData, &NormDir);
 73 
 74 //創建基准平面
 75 tag_t Plane_Tag = NULL_TAG;
 76 UF_MODL_create_fixed_dplane(Point, Dir, &Plane_Tag);
 77 
 78 //求直線與基准平面的交點
 79 int IntersectionsNum;
 80 UF_MODL_intersect_info_p_t * Intersections;
 81 UF_MODL_intersect_objects(LineTag, Plane_Tag, 0.01, &IntersectionsNum, &Intersections);//輸入兩個對象tag,找交點
 82 
 83 double IntersectionsPoint[3];//交點坐標
 84 for (int i = 0; i < IntersectionsNum; i++)
 85 {
 86     int type = Intersections[i]->intersect_type;
 87     if (type == UF_MODL_INTERSECT_POINT)
 88     {
 89         IntersectionsPoint[0] = Intersections[i]->intersect.point.coords[0];
 90         IntersectionsPoint[1] = Intersections[i]->intersect.point.coords[1];
 91         IntersectionsPoint[2] = Intersections[i]->intersect.point.coords[2];
 92     }
 93 }
 94 
 95 //刪除直線和基准平面
 96 UF_OBJ_delete_object(LineTag);
 97 UF_OBJ_delete_object(Plane_Tag);
 98 
 99 //打印
100 char msg[256];
101 sprintf_s(msg, "距離為:%f", IntersectionsPoint[1]);
102 uc1601(msg,1);
103 
104 //釋放內存
105 UF_free(Intersections);
106 
107 UF_terminate();
108 
109 caesar盧尚宇
110 2019年7月26日


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM