您可以水平和垂直对齐单行文本。左对齐是默认设置。若要设置水平和垂直对齐选项,请使用 和 属性。HorizontalModeVerticalMode 通常,当文本对象关闭时,会根据其对齐方式和文本样式调整文本对象的位置和对齐点。但是,内存中文本对象的对齐方式不会自动更新。调用该方法以根据文本对象的当前属性值更新其对齐方式。AdjustAlignment 重新对齐文本下面的示例创建一个单行文本 () 对象和一个点 () 对象。点对象设置为文本对齐点,并更改为红色十字准线,以便可见。更改文本对齐方式并显示一个消息框,以便停止宏执行。这使您可以查看更改文本对齐方式的影响。DBTextDBPoint VB.NETImports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry <CommandMethod("TextAlignment")> _ Public Sub TextAlignment() '' Get the current document and database Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database '' Start a transaction Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() '' Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _ OpenMode.ForRead) '' Open the Block table record Model space for write Dim acBlkTblRec As BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _ OpenMode.ForWrite) Dim textString(0 To 2) As String textString(0) = "Left" textString(1) = "Center" textString(2) = "Right" Dim textAlign(0 To 2) As Integer textAlign(0) = TextHorizontalMode.TextLeft textAlign(1) = TextHorizontalMode.TextCenter textAlign(2) = TextHorizontalMode.TextRight Dim acPtIns As Point3d = New Point3d(3, 3, 0) Dim acPtAlign As Point3d = New Point3d(3, 3, 0) Dim nCnt As Integer = 0 For Each strVal As String In textString '' Create a single-line text object Using acText As DBText = New DBText() acText.Position = acPtIns acText.Height = 0.5 acText.TextString = strVal '' Set the alignment for the text acText.HorizontalMode = textAlign(nCnt) If acText.HorizontalMode <> TextHorizontalMode.TextLeft Then acText.AlignmentPoint = acPtAlign End If acBlkTblRec.AppendEntity(acText) acTrans.AddNewlyCreatedDBObject(acText, True) End Using '' Create a point over the alignment point of the text Using acPoint As DBPoint = New DBPoint(acPtAlign) acPoint.ColorIndex = 1 acBlkTblRec.AppendEntity(acPoint) acTrans.AddNewlyCreatedDBObject(acPoint, True) '' Adjust the insertion and alignment points acPtIns = New Point3d(acPtIns.X, acPtIns.Y + 3, 0) acPtAlign = acPtIns End Using nCnt = nCnt + 1 Next '' Set the point style to crosshair Application.SetSystemVariable("PDMODE", 2) '' Save the changes and dispose of the transaction acTrans.Commit() End Using End Sub C#using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; [CommandMethod("TextAlignment")] public static void TextAlignment() { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; string[] textString = new string[3]; textString[0] = "Left"; textString[1] = "Center"; textString[2] = "Right"; int[] textAlign = new int[3]; textAlign[0] = (int)TextHorizontalMode.TextLeft; textAlign[1] = (int)TextHorizontalMode.TextCenter; textAlign[2] = (int)TextHorizontalMode.TextRight; Point3d acPtIns = new Point3d(3, 3, 0); Point3d acPtAlign = new Point3d(3, 3, 0); int nCnt = 0; foreach (string strVal in textString) { // Create a single-line text object using (DBText acText = new DBText()) { acText.Position = acPtIns; acText.Height = 0.5; acText.TextString = strVal; // Set the alignment for the text acText.HorizontalMode = (TextHorizontalMode)textAlign[nCnt]; if (acText.HorizontalMode != TextHorizontalMode.TextLeft) { acText.AlignmentPoint = acPtAlign; } acBlkTblRec.AppendEntity(acText); acTrans.AddNewlyCreatedDBObject(acText, true); } // Create a point over the alignment point of the text using (DBPoint acPoint = new DBPoint(acPtAlign)) { acPoint.ColorIndex = 1; acBlkTblRec.AppendEntity(acPoint); acTrans.AddNewlyCreatedDBObject(acPoint, true); // Adjust the insertion and alignment points acPtIns = new Point3d(acPtIns.X, acPtIns.Y + 3, 0); acPtAlign = acPtIns; } nCnt = nCnt + 1; } // Set the point style to crosshair Application.SetSystemVariable("PDMODE", 2); // Save the changes and dispose of the transaction acTrans.Commit(); } } VBA/ActiveX 代码参考Sub TextAlignment() ' Set the point style to crosshair ThisDrawing.SetVariable "PDMODE", 2 Dim textObj As AcadText Dim pointObj As AcadPoint ' Define the text strings and insertion point for the text objects Dim textString(0 To 2) As String textString(0) = "Left" textString(1) = "Center" textString(2) = "Right" Dim textAlign(0 To 2) As Integer textAlign(0) = acAlignmentLeft textAlign(1) = acAlignmentCenter textAlign(2) = acAlignmentRight Dim insertionPoint(0 To 2) As Double insertionPoint(0) = 3: insertionPoint(1) = 0: insertionPoint(2) = 0 Dim alignmentPoint(0 To 2) As Double alignmentPoint(0) = 3: alignmentPoint(1) = 0: alignmentPoint(2) = 0 Dim nCnt As Integer For Each strVal In textString ' Create the Text object in model space Set textObj = ThisDrawing.ModelSpace. _ AddText(strVal, insertionPoint, 0.5) ' Set the alignment for the text textObj.Alignment = textAlign(nCnt) On Error Resume Next textObj.TextAlignmentPoint = alignmentPoint ' Create a point over the alignment point of the text Set pointObj = ThisDrawing.ModelSpace.AddPoint(alignmentPoint) pointObj.color = acRed ' Adjust the insertion and alignment points insertionPoint(1) = insertionPoint(1) + 3 alignmentPoint(1) = insertionPoint(1) nCnt = nCnt + 1 Next End Sub 相关概念父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-1-8 19:32
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.