本实例主要实现了通过ufun创建圆柱和简单孔,主要使用了一下几个ufun函数:
theUFSession.Modl.CreateCyl1 创建圆柱
theUFSession.Modl.AskFeatFaces 获取面列表
theUFSession.Modl.CreateSimpleHole 创建简单孔
最终实现效果图如下:
一、主程序
先看实现简单孔的主程序如下:
using System;
using NXOpen;
using NXOpen.UF;
using NxLibrary;
public class Program
{
private static Session theSession;
private static UFSession theUFSession;
public static Program theProgram;
private static UI theUI = null;
public static int Main(string[] args)
{
theUI = UI.GetUI();
int retValue = 0;
try
{
#region 简单孔
double[] origin = new double[3] { 0.0, 0.0, 0.0 };
double[] direction = new double[3] { 0.0, 0.0, 1.0 };
string height = "10";
string diam = "50";
Tag cylTag = ModlHelper.CreateCylinder(origin, height, diam, direction);
double[] location = new double[3]{0, 0, 20};
double[] direction1 = new double[3] { 0.0, 0.0, -1.0 };
diam = "20";
string depth = "100";
string angle = "0";
theUFSession = UFSession.GetUFSession();
Tag[] face_list;
int count;
theUFSession.Modl.AskFeatFaces(cylTag, out face_list);
theUFSession.Modl.AskListCount(face_list, out count);
for (int i = 0; i < count; i++)
{
Tag faceTag;
theUFSession.Modl.AskListItem(face_list, i ,out faceTag);
theUFSession.Obj.SetName(faceTag,"face"+i);
}
Tag placement_face, thru_face;
theUFSession.Modl.AskListItem(face_list, 0, out placement_face);
theUFSession.Modl.AskListItem(face_list, 2, out thru_face);
ModlHelper.CreateSimpleHole(location, direction1, diam, depth, angle, placement_face, thru_face);
#endregion
}
catch (NXOpen.NXException ex)
{
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return retValue;
}
}
根据主程序的思路,简单孔的实现分一下几步:
1、先创建一个圆柱
2、获取圆柱的上、下两个面,用来打孔
这里获取圆柱的上下面有一个技巧,使用了theUFSession.Obj.SetName给圆柱的每一个面做了name标记,所以才把face_list编号0和2的面赋值给placement_face、thru_face。 如果这两个面弄错了是无法正常打孔的。
3、初始化打孔需要的参数:孔位置、方向向量、直径、孔深、尖角角度、孔开始面、孔结束面
二、创建圆柱方法
/// <summary>
/// 创建圆柱体
/// </summary>
/// <param name="origin">原点</param>
/// <param name="height">高度</param>
/// <param name="diam">直径</param>
/// <param name="direction">方向向量</param>
/// <returns></returns>
public static Tag CreateCylinder(double[] origin, string height, string diam, double[] direction)
{
theUFSession = UFSession.GetUFSession();
FeatureSigns signs = FeatureSigns.Nullsign;
Tag cylTag;
theUFSession.Modl.CreateCyl1(signs, origin, height, diam, direction, out cylTag);
return cylTag;
}
创建圆柱使用了ufun函数:theUFSession.Modl.CreateCyl1
有原点、圆柱的高度、直径、方向向量即可创建
三、创建简单孔的方法
/// <summary>
/// 创建简单孔
/// </summary>
/// <param name="location">孔位置</param>
/// <param name="direction">方向向量</param>
/// <param name="diame">直径</param>
/// <param name="depth">孔深</param>
/// <param name="angle">尖角角度</param>
/// <param name="face_li">孔开始面(尾)</param>
/// <param name="face_t1">孔结束面(头)</param>
/// <returns></returns>
public static Tag CreateSimpleHole(double[] location, double[] direction, string diame, string depth, string angle, Tag face_li, Tag face_t1)
{
theUFSession = UFSession.GetUFSession();
Tag holeTag;
theUFSession.Modl.CreateSimpleHole(location, direction, diame, depth, angle, face_li, face_t1, out holeTag);
return holeTag;
}
创建简单孔使用了ufun函数:theUFSession.Modl.CreateSimpleHole
主要使用参数有:孔位置、方向向量、直径、孔深、尖角角度、孔开始面、孔结束面