创建文字的核心代码
说明:MyText是一个文字类,记录了文字的基本特征,代码实现在manip坐标控件原点上生成一个文字对象;
代码示例
/// <summary>
/// 创建文字对象
/// </summary>
/// <param name="args"></param>
/// <param name="manip"></param>
/// <param name="textsNxObject"></param>
/// <param name="textObject"></param>
public static void CreateText1(SpecifyOrientation manip, Text textsNxObject, MyText textObject)
{
string[] valueStrings = textObject.ValueStrings;
Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;
if (valueStrings.Length >= 1)
{
Vector3d xVector3d = manip.XAxis;
Vector3d yVector3d = manip.YAxis;
TextBuilder textBuilder1 = workPart.Features.CreateTextBuilder(textsNxObject);
textBuilder1.TextString = valueStrings[0]; //刻字内容(第一行)
textBuilder1.PlanarFrame.AnchorLocation = RectangularFrameBuilder.AnchorLocationType.MiddleCenter; //锚点居中
textBuilder1.SelectFont(textObject.TheFont, GetFontScript(textObject)); //字体
SetTextFontStyle(textBuilder1, textObject); //设置字体粗细类型
textBuilder1.PlanarFrame.Height.RightHandSide = textObject.FontHeight.ToString(CultureInfo.InvariantCulture); //字高
textBuilder1.PlanarFrame.WScale = textObject.FontWScale; //设置长宽比
textBuilder1.PlanarFrame.Shear.RightHandSide = "0";
textBuilder1.FrameOnPath.AnchorPosition.Expression.RightHandSide = "0";
Xform xForm = workPart.Xforms.CreateXform(manip.Origin, xVector3d, yVector3d, SmartObject.UpdateOption.WithinModeling, 1); //文字位置
textBuilder1.OnFacePlacementProfile.ReverseDirection();
CartesianCoordinateSystem cartesianCoordinate = workPart.CoordinateSystems.CreateCoordinateSystem(xForm, SmartObject.UpdateOption.WithinModeling);
textBuilder1.PlanarFrame.CoordinateSystem = cartesianCoordinate;
var markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "text");
textsNxObject = (Text) textBuilder1.Commit();
textBuilder1.Destroy();
theSession.UpdateManager.DoUpdate(markId1);
theSession.DeleteUndoMark(markId1, null);
}
}
var markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "text");
textsNxObject = (Text)textBuilder1.Commit();
//这里的Markd1用于文字对象的更新,如果没有则文字不会被刷新出现
textBuilder1.Destroy();
theSession.UpdateManager.DoUpdate(markId1);
theSession.DeleteUndoMark(markId1, null);