UG NX二次开发(C#)-建模-获取圆柱面信息(轴向半径深度)
Posted GimiGimmy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UG NX二次开发(C#)-建模-获取圆柱面信息(轴向半径深度)相关的知识,希望对你有一定的参考价值。
前言
在一个项目中,需要遍历曲面,获得所有的圆柱面和圆柱面的信息,包括轴向、半径和圆柱面的深度。经过研究,采用uFModl.AskFaceData(face.Tag, out faceTypeInt, point, dir, box, out rad, out rad_data, out normDir);实现,现在把实现方法共享下。
一、在UG NX中面的类型有哪些?
在UG NX中,面的类型有以下几个:
在UG NX中,获取面的类型函数为:
face.SolidFaceType
其值是枚举值:
Face.FaceType.xxxx
如果是圆柱面,则为Face.FaceType.Cylindrical
二、获取圆柱面信息的函数
1.获取半径和轴向
采用 uFModl.AskFaceData(face.Tag, out faceTypeInt, point, dir, box, out rad, out rad_data, out normDir);函数来获取圆柱面的定位点、轴向、半径等信息。
代码为:
/// <summary>
/// 获取面的数据,主要针对圆柱面和圆锥面
/// </summary>
/// <param name="face">选择对象</param>
/// <param name="faceTypeInt">面类型</param>
/// <param name="location">定位点,其位于轴向上,但是不是底圆点</param>
/// <param name="axisVector">面的旋转轴</param>
/// <param name="radius">面的半径,主要针对圆柱体</param>
/// <param name="angle">圆锥面的锥角</param>
public static void GetFaceData(Face face, out int faceTypeInt, out Point3d location, out Vector3d axisVector, out double radius, out double angle)
{
faceTypeInt = 0;
location = new Point3d();
axisVector = new Vector3d();
angle = -1;
radius = -1;
double[] point = new double[3];
double[] dir = new double[3];
double[] box = new double[6];
double rad;
double rad_data;
int normDir;
uFModl.AskFaceData(face.Tag, out faceTypeInt, point, dir, box, out rad, out rad_data, out normDir);
location.X = point[0];
location.Y = point[1];
location.Z = point[2];
axisVector.X = dir[0];
axisVector.Y = dir[1];
axisVector.Z = dir[2];
angle = rad_data * 360 / Math.PI;
radius = rad;
}
2、计算圆柱面的深度值
圆柱面的深度值是通过计算圆柱面两条圆弧曲线的圆心点距离得到的。
首先获得圆柱面的两条边:
Edge[] edges = face.GetEdges();
然后,分别获得两条边的圆心点:
Point3d[] point3Ds = new Point3d[2];
for(int i=0;i<2;i++)
{
NXOpen.Section section = workPart.Sections.CreateSection(edges[i]);
Arc arc = (Arc)workPart.Curves.CreateSmartCompositeCurve(section,SmartObject.UpdateOption.WithinModeling,0.001);
point3Ds[i] = arc.CenterPoint;
}
double dis = Math.Sqrt(Math.Pow(point3Ds[0].X-point3Ds[1].X,2) + Math.Pow(point3Ds[0].Y - point3Ds[1].Y, 2) + Math.Pow(point3Ds[0].Z - point3Ds[1].Z, 2) );
dis就是圆柱面的深度
3、测试用例
测试用的实例如下如所示:
圆柱面的直径为46mm,深度为100mm,其轴向为[0,0,1]。
在以下界面中的面选择,选择圆柱面。
点击操作下的无标题按钮1:
测试用的代码为:
else if (block == button0)
{
//---------Enter your code here-----------
#region 测试获取圆柱面的半径和深度
if(face_select0.GetSelectedObjects().Length==0)
{
eturn 0;
}
TaggedObject []taggedObjects= face_select0.GetSelectedObjects();
theUFUi.OpenListingWindow();
foreach(var to in taggedObjects)
{
Face face = (Face)to;
///圆柱面
if(face.SolidFaceType==Face.FaceType.Cylindrical)
{
int faceTypeInt;
Point3d locationPoint3D;
Vector3d axisDirectionVector3D;
double angle;
double radius;
FaceAdd faceAdd = new FaceAdd(face);
FaceAdd.GetFaceData(face,out faceTypeInt,out locationPoint3D,out axisDirectionVector3D,out radius,out angle);
theUFUi.WriteListingWindow("========面的tag:" + face.Tag.ToString()+",\\t其面类型:" + Face.FaceType.Cylindrical.ToString()+ "\\n\\n");
theUFUi.WriteListingWindow("圆柱面的定位点为:" + locationPoint3D.ToString() + "\\n\\n");
theUFUi.WriteListingWindow("圆柱面的轴向为:" + axisDirectionVector3D.ToString() + "\\n\\n");
theUFUi.WriteListingWindow("圆柱面的直径为:" + (radius * 2).ToString() + "\\n\\n");
//获取圆柱面的深度
Edge[] edges = face.GetEdges();
Point3d[] point3Ds = new Point3d[2];
for(int i=0;i<2;i++)
{
NXOpen.Section section = workPart.Sections.CreateSection(edges[i]);
Arc arc = (Arc)workPart.Curves.CreateSmartCompositeCurve(section,SmartObject.UpdateOption.WithinModeling,0.001);
point3Ds[i] = arc.CenterPoint;
}
double dis = Math.Sqrt(Math.Pow(point3Ds[0].X-point3Ds[1].X,2) + Math.Pow(point3Ds[0].Y - point3Ds[1].Y, 2) + Math.Pow(point3Ds[0].Z - point3Ds[1].Z, 2) );
theUFUi.WriteListingWindow("圆柱面的深度为:" + dis.ToString()+ "\\n\\n");
}
}
#endregion
}
总结
创作不易,如果觉得有用的,请给博主点个赞,如果有什么不足的,请给博主留言或者私信。
如果有初学者想学习NX 二次开发C#,请私信博主,交流咨询是免费的。
以上是关于UG NX二次开发(C#)-建模-获取圆柱面信息(轴向半径深度)的主要内容,如果未能解决你的问题,请参考以下文章
UG NX二次开发(C#)--建模--识别曲面类型(圆柱面)
UG NX二次开发(C#)-建模-判断一张面是孔面还是凸台面