码迷,mamicode.com
首页 > 其他好文 > 详细

NX二次开发-测量投影距离

时间:2019-07-26 22:54:48      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:cas   model   类型   section   ane   sprint   char   red   temp   

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日

技术图片

NX二次开发-测量投影距离

标签:cas   model   类型   section   ane   sprint   char   red   temp   

原文地址:https://www.cnblogs.com/nxopen2018/p/11253254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!