自动刻字功能是建模过程中必须用到的,但是网上没有找到完整的刻字功能实现,所以我花了一些时间写了一套简易程序,希望能给初学者带来一点帮助
目前成都创新互联已为超过千家的企业提供了网站建设、域名、网站空间、绵阳服务器托管、企业网站设计、乌翠网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。先介绍一下建模环境下刻字功能的实现,分三步走(本节主要介绍打印文本):
第一步:打印文本
第二步:拉伸
第三步:布尔求差
////// 创建文本
/// //////////// 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);
ListfeaList = new List();
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()方法前后分别获取所有的特征对象的方式,才获取到创建的文本
ListfeaList = new List();
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;
}
}
运行结果如图:
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧