自动刻字功能是建模过程中必须用到的,但是网上没有找到完整的刻字功能实现,所以我花了一些时间写了一套简易程序,希望能给初学者带来一点帮助
先介绍一下建模环境下刻字功能的实现,分三步走(本节主要介绍打印文本):
第一步:打印文本
第二步:拉伸
第三步:布尔求差
/// <summary>
/// 创建文本
/// </summary>
/// <param name="p"></param>
/// <param name="faceTag"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string TextBuilderByPoint(Point3d p, Tag faceTag , string content)
{
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;
Part displayPart = theSession.Parts.Display;
theUFSession = UFSession.GetUFSession();
int type;
double[] point = new double[3]; //面的中心点
double[] dir = new double[3]; //面的法向量
double[] box = new double[6]; ;
double radius;
double rad_data;
int norm_dir;
theUFSession.Modl.AskFaceData(faceTag, out type, point, dir, box, out radius, out rad_data, out norm_dir);
NXOpen.Features.Text nullFeatures_Text = null;
NXOpen.Features.TextBuilder textBuilder1;
textBuilder1 = workPart.Features.CreateTextBuilder(nullFeatures_Text);
Point3d origin1 = new Point3d(0.0, 0.0, 0.0);
Vector3d normal1 = new Vector3d(0.0, 0.0, 1.0);
Plane plane1;
plane1 = workPart.Planes.CreatePlane(origin1, normal1, NXOpen.SmartObject.UpdateOption.WithinModeling);
textBuilder1.SectionPlane = plane1;
Unit unit1;
unit1 = textBuilder1.PlanarFrame.Length.Units;
textBuilder1.FrameOnPath.AnchorPosition.Expression.RightHandSide = "50";
textBuilder1.Type = NXOpen.Features.TextBuilder.Types.OnFace;
textBuilder1.Script = NXOpen.Features.TextBuilder.ScriptOptions.Western;
textBuilder1.PlanarFrame.AnchorLocation = NXOpen.GeometricUtilities.RectangularFrameBuilder.AnchorLocationType.BottomCenter;
textBuilder1.PlanarFrame.Length.RightHandSide = "22.169352775646";
textBuilder1.PlanarFrame.Height.RightHandSide = "10"; //标记高度
textBuilder1.PlanarFrame.Shear.RightHandSide = "0";
textBuilder1.OnFacePlacementMethod = NXOpen.Features.TextBuilder.OnFacePlacementMethodOptions.SectionPlane;
textBuilder1.FrameOnPath.AnchorPosition.Expression.RightHandSide = "50";
textBuilder1.FrameOnPath.Offset.RightHandSide = "20";
textBuilder1.FrameOnPath.Length.RightHandSide = "30"; //标记长度
textBuilder1.FrameOnPath.Height.RightHandSide = "10";
textBuilder1.FrameOnPath.WScale = 99.9999999999998;
textBuilder1.SelectFont("Arial", NXOpen.Features.TextBuilder.ScriptOptions.Western);
//textBuilder1.TextString = DateTime.Now.ToString("yyyy-MM-dd") ;
textBuilder1.TextString = textBuilder1.Tag.ToString();
textBuilder1.Type = NXOpen.Features.TextBuilder.Types.Planar;
textBuilder1.PlanarFrame.Length.RightHandSide = "22.1693527756459";
//通过创建智能点,确保刻字在面上
Scalar scalar1;
scalar1 = workPart.Scalars.CreateScalar(0.5, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling);
Scalar scalar2;
scalar2 = workPart.Scalars.CreateScalar(0.5, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling);
Face face1 = (Face)NXOpen.Utilities.NXObjectManager.Get(faceTag);
Point point1;
point1 = workPart.Points.CreatePoint(face1, scalar1, scalar2, NXOpen.SmartObject.UpdateOption.WithinModeling);
NXObject nXObject1;
Xform xform1;
xform1 = workPart.Xforms.CreateExtractXform(face1, NXOpen.SmartObject.UpdateOption.WithinModeling, false, out nXObject1);
Point3d point2 = new Point3d(p.X, p.Y, p.Z);
textBuilder1.PlanarFrame.AnchorLocator.SetValue(point1, workPart.ModelingViews.WorkView, point2);
//显示智能点的位置
//Tag point11;
//double[] Vertex1 = new double[3] { point1.Coordinates.X, point1.Coordinates.Y, point1.Coordinates.Z };
//theUFSession.Curve.CreatePoint(Vertex1, out point11);
//更新笛卡尔坐标系,摆正刻字
CartesianCoordinateSystem cartesianCoordinateSystem1 = (CartesianCoordinateSystem)workPart.FindObject("ENTITY 45 1");
cartesianCoordinateSystem1.RemoveParameters();
Point3d origin2 = new Point3d(p.X, p.Y, p.Z);
cartesianCoordinateSystem1.Origin = origin2;
Vector3d xDirection1 = new Vector3d(1, 0, 0);
Vector3d yDirection1 = new Vector3d(0, 1, 0);
if (dir[2] == 1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, 1, 0);
}
else if (dir[2] == -1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, -1, 0);
}
else if (dir[1] == 1)
{
xDirection1 = new Vector3d(-1, 0, -0);
yDirection1 = new Vector3d(-0, -0, 1);
}
else if (dir[1] == -1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, 0, 1);
}
else if (dir[0] == 1)
{
xDirection1 = new Vector3d(-0.0, 1.0, -0.0);
yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
}
else if (dir[0] == -1)
{
xDirection1 = new Vector3d(0.0, -1.0, 0.0);
yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
}
cartesianCoordinateSystem1.SetDirections(xDirection1, yDirection1);
textBuilder1.PlanarFrame.UpdateOnCoordinateSystem();
//显示笛卡尔坐标系
//cartesianCoordinateSystem1.SetVisibility(SmartObject.VisibilityOption.Visible);
List<Feature> feaList = new List<Feature>();
feaList = GetFeatureList();
NXObject nXObject2;
nXObject2 = textBuilder1.Commit();
textBuilder1.Destroy();
foreach (Feature fea in workPart.Features)
{
if (!feaList.Contains(fea))
{
Extrude(fea.JournalIdentifier,dir);//执行拉伸命令
return fea.JournalIdentifier;
}
}
return "";
}
打印文本的源码是经过录制修改而来,所以有部分源码我也还没完全整明白;
主要实现了NXOpen.Features.TextBuilder这个类来完成文本打印。
其中,需要注意的地方有:
1、要确保文本打印在面上,这里实现了一个智能点
point1 = workPart.Points.CreatePoint(face1, scalar1, scalar2, NXOpen.SmartObject.UpdateOption.WithinModeling);
2、文本在面上之后,怎么摆正文本又是一个问题,所以用到了笛卡尔坐标系
CartesianCoordinateSystem cartesianCoordinateSystem1 = (CartesianCoordinateSystem)workPart.FindObject("ENTITY 45 1");
cartesianCoordinateSystem1.RemoveParameters();
Point3d origin2 = new Point3d(p.X, p.Y, p.Z);
cartesianCoordinateSystem1.Origin = origin2;
Vector3d xDirection1 = new Vector3d(1, 0, 0);
Vector3d yDirection1 = new Vector3d(0, 1, 0);
if (dir[2] == 1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, 1, 0);
}
else if (dir[2] == -1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, -1, 0);
}
else if (dir[1] == 1)
{
xDirection1 = new Vector3d(-1, 0, -0);
yDirection1 = new Vector3d(-0, -0, 1);
}
else if (dir[1] == -1)
{
xDirection1 = new Vector3d(1, 0, 0);
yDirection1 = new Vector3d(0, 0, 1);
}
else if (dir[0] == 1)
{
xDirection1 = new Vector3d(-0.0, 1.0, -0.0);
yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
}
else if (dir[0] == -1)
{
xDirection1 = new Vector3d(0.0, -1.0, 0.0);
yDirection1 = new Vector3d(-0.0, -0.0, 1.0);
}
cartesianCoordinateSystem1.SetDirections(xDirection1, yDirection1);
textBuilder1.PlanarFrame.UpdateOnCoordinateSystem();
开发过程中,大家可以用下面的代码显示出笛卡尔坐标系,以辅助开发cartesianCoordinateSystem1.SetVisibility(SmartObject.VisibilityOption.Visible);
3、这里还有一个大大坑,就是创建的文本没有返回Tag,所以我这里通过在Commit()方法前后分别获取所有的特征对象的方式,才获取到创建的文本
List<Feature> feaList = new List<Feature>();
feaList = GetFeatureList();
NXObject nXObject2;
nXObject2 = textBuilder1.Commit();
textBuilder1.Destroy();
foreach (Feature fea in workPart.Features)
{
if (!feaList.Contains(fea))
{
Extrude(fea.JournalIdentifier,dir);//执行拉伸命令
return fea.JournalIdentifier;
}
}
运行结果如图: