CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

设置多行文本格式 (.NET)

2023-1-1 12:38| 发布者: admin| 查看: 794| 评论: 0|来自: AutoCAD

新创建的多行文本会自动采用当前文本样式的特征。默认文本样式为“标准”。可以通过将格式应用于单个字符并将属性应用于多行文本对象来覆盖默认文本样式。您还可以使用本节中介绍的方法指示格式或特殊字符。

方向选项(如样式、对齐、宽度和旋转)会影响多行文本边界内的所有文本,而不是特定的单词或字符。使用 属性 更改多行文本对象的对齐方式,使用 属性 控制旋转角度。AttachmentRotation

该属性设置多行文本对象的字体和格式特征。创建多行文本时,可以从现有样式列表中选择要使用的样式。更改将字符格式应用于文本任何部分的多行文本对象的样式时,该样式将应用于整个对象,并且可能不会保留某些字符格式。例如,从 TrueType 样式更改为使用 SHX 字体的样式或另一种 TrueType 字体会导致多行文本对整个对象使用新字体,并且任何字符格式都将丢失。TextStyleId

下划线、堆叠文本或字体等格式选项可以应用于段落中的单个单词或字符。您还可以更改颜色、字体和文本高度。您可以更改文本字符之间的空格或增加字符的宽度。

使用大括号 ({ }) 仅对大括号内的文本应用格式更改。您可以嵌套深达八层的牙套。

您还可以为行或段落中的控制代码输入 ASCII 等效值,以指示格式或特殊字符,如公差或尺寸标注符号。

以下控制字符可用于创建图中的文本。(有关此字符串的 ASCII 等效项,请参阅下图下面的示例。

{{\H1.5x;大文本} \a2;文本\A1;/\A0;在文本下}

使用控制字符设置文本格式

下面的示例创建一个多行文本对象并设置其格式。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("FormatMText")> _
Public Sub FormatMText()
    '' 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)

        '' Create a multiline text object
        Using acMText As MText = New MText()
            acMText.Location = New Point3d(2, 2, 0)
            acMText.Width = 4.5
            acMText.Contents = "{{\H1.5x; Big text}\A2; over text\A1;/\A0;under text}"

            acBlkTblRec.AppendEntity(acMText)
            acTrans.AddNewlyCreatedDBObject(acMText, True)
        End Using

        '' 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("FormatMText")]
public static void FormatMText()
{
    // 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;

        // Create a multiline text object
        using (MText acMText = new MText())
        {
            acMText.Location = new Point3d(2, 2, 0);
            acMText.Width = 4.5;
            acMText.Contents = "{{\\H1.5x; Big text}\\A2; over text\\A1;/\\A0;under text}";

            acBlkTblRec.AppendEntity(acMText);
            acTrans.AddNewlyCreatedDBObject(acMText, true);
        }

        // Save the changes and dispose of the transaction
        acTrans.Commit();
    }
}

VBA/ActiveX Code Reference

Sub FormatMText()
    Dim mtextObj As AcadMText
    Dim insertPoint(0 To 2) As Double
    Dim width As Double
    Dim textString As String
 
    insertPoint(0) = 2
    insertPoint(1) = 2
    insertPoint(2) = 0
    width = 4.5
 
    ' Define the ASCII characters for the control characters
    Dim OB As Long  ' Open Bracket  {
    Dim CB As Long  ' Close Bracket }
    Dim BS As Long  ' Back Slash    \
    Dim FS As Long  ' Forward Slash /
    Dim SC As Long  ' Semicolon     ;
    OB = Asc("{")
    CB = Asc("}")
    BS = Asc("\")
    FS = Asc("/")
    SC = Asc(";")
 
    ' Assign the text string the following line of control
    ' characters and text characters:
    ' {{\H1.5x; Big text}\A2; over text\A1;/\A0;under text}
 
    textString = Chr(OB) + Chr(OB) + Chr(BS) + "H1.5x" _
    + Chr(SC) + "Big text" + Chr(CB) + Chr(BS) + "A2" _
    + Chr(SC) + "over text" + Chr(BS) + "A1" + Chr(SC) _
    + Chr(FS) + Chr(BS) + "A0" + Chr(SC) + "under text" _
    + Chr(CB)
 
    ' Create a text Object in model space
    Set mtextObj = ThisDrawing.ModelSpace. _
                       AddMText(insertPoint, width, textString)
    ZoomAll
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 14:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部