您的位置:首页 > 其它

03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(2)使用单行文字

2012-04-17 11:23 531 查看

2、UseSingle-Line Text (Text) 使用单行文字(Text命令)

The text you add to your drawings conveysa variety of information. It may be a complex specification, title blockinformation, a label, or even part of the drawing. For shorter entries that donot require multiple fonts
or lines, create an instance of a DBText objectwhich represents a single-line of text and is convenient for labels.

添加到图形中的文字可以表示好多意思,可以是复杂的技术说明、标题栏信息、标签,甚至图形的部件。对于不需要多种字体和文本行的较短文字,可以通过创建一个DBText对象的实例来创建一个单行文字,用于标签时尤其省事儿。
Topics in this section
· Create Single-Line Text创建单行文字
· Format Single-Line Text格式化单行文字
· Align Single-Line Text对齐单行文字
· Change Single-Line Text修改单行文字

2.1、CreateSingle-Line Text创建单行文字

Each individual line of text is a distinctobject when using single-line text. To create a single-line text object, youcreate an instance of a DBText object and then add it to either the block tablerecord that represents
Model or Paper space. When you create a new instance ofa DBText object, you do not pass the constructor any parameters.

使用单行文字时,每一行文字都是一个单独的对象。创建单行文字的方法是:首先创建DBText对象的实例,然后将其添加到代表模型空间或图纸空间的块表记录中。创建DBText对象的实例时,不用给构造函数传递任何参数。

To Create Line Text
创建单行文字


The following example creates asingle-line text object in Model space at the coordinate (2, 2, 0) with aheight of 0.5 and the text string "Hello, World.".

下面这个例子在模型空间创建一个单行文字对象,起点坐标为(2,2,0),文字高度为0.5,文字内容为“Hello,
World.”。
VB.NET

ImportsAutodesk.AutoCAD.Runtime
ImportsAutodesk.AutoCAD.ApplicationServices
ImportsAutodesk.AutoCAD.DatabaseServices
ImportsAutodesk.AutoCAD.Geometry

<CommandMethod("CreateText")>_
Public SubCreateText()
'' 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 single-line text object

Dim acText As DBText = New DBText()

acText.Position = New Point3d(2, 2, 0)

acText.Height = 0.5

acText.TextString = "Hello,World."

acBlkTblRec.AppendEntity(acText)

acTrans.AddNewlyCreatedDBObject(acText,True)

'' Save the changes and dispose of thetransaction

acTrans.Commit()
End Using
End Sub
C#

usingAutodesk.AutoCAD.Runtime;
usingAutodesk.AutoCAD.ApplicationServices;
usingAutodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("CreateText")]
public static voidCreateText()
{
// 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以读的方式打开Block表

BlockTable acBlkTbl;

acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Modelspace for write以写的方式打开Block表记录模型空间
BlockTableRecord acBlkTblRec;

acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create a single-line text object 创建一个单行文字;

DBText acText = new DBText();

acText.Position = new Point3d(2, 2, 0);

acText.Height = 0.5;

acText.TextString = "Hello,World.";

//
添加到模型空间

acBlkTblRec.AppendEntity(acText);

acTrans.AddNewlyCreatedDBObject(acText,true);

// Save the changes and dispose of thetransaction保存修改,关闭事务

acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub CreateText()
Dim textObj As AcadText
Dim textString As String
Dim insertionPoint(0 To 2) As Double
Dim height As Double

' Create the text object
textString = "Hello, World."
insertionPoint(0) = 2
insertionPoint(1) = 2
insertionPoint(2) = 0
height = 0.5

Set textObj = ThisDrawing.ModelSpace. _

AddText(textString,insertionPoint, height)
textObj.Update
End Sub

2.2、FormatSingle-Line Text格式化单行文字

A single-text object is created using theactive text style. You can change the formatting of a single-line text objectby changing the text style associated with it, or by editing the properties ofthe single-line text
object directly. You cannot apply formats toindividual words and characters with single-line text objects.

单行文字是使用当前文字样式创建的,我们可以通过修改与之关联的文字样式来修改单行文字的格式,或者通过直接编辑单行文字对象的属性来修改其格式。我们不能对单行文字对象里的单个字和字符进行格式修改。
To change a text style associated with anindividual single-line text object, set the
TextStyleId property to a new text style. Once youhave changed the text style, you must regenerate the drawing or update theobject to see the changes in your drawing.

要修改与某个单行文字对象关联的文字样式,将其TextStyleId属性设置为新文字样式即可。修改文字样式后,要想看到其效果,需重新生成图形或更新一下该文字对象。
In addition to the standard editableproperties for entities (color, layer, linetype, and so forth), otherproperties that you can change on a single-line text object include thefollowing:

除了标准的可编辑实体属性(颜色、图层、线型,等等)外,单行文字还有一些我们可以修改的属性,如下:
HorizontalMode
Specifies the horizontal alignment for the text.
表示文字水平对齐;
VerticalMode
Specifies the vertical alignment for the text.
表示文字垂直对齐;
Position
Specifies the insertion point for the text.
表示文字的插入点;
Oblique
Specifies the oblique angle of the individual textobject.
表示每个文字对象的倾角;
Rotation
Specifies the rotation angle in radians for the text.
表示文字的旋转角度(弧度);
WidthFactor
Specifies the scale factor for the text.
表示文字的放大系数;
AlignmentPoint
Specifies the alignment point for the text.
表示文字的对起点;
IsMirroredInX
Specifies whether the text is displayed backwards.
表示文字是否反向显示;
IsMirroredInY
Specifies whether the text is displayed upside-down.
表示文字是否倒置显示;
TextString
Specifies the actual text string displayed.
表示实际显示的文字内容;
Once you have changed a property,regenerate the drawing or update the object to see the changes made.

每次修改文字属性后,都要重新生成图形或对该文字对象进行更新,这样才能看到所作的修改。

2.3、AlignSingle-Line Text 对齐单行文字

You can justify single-line texthorizontally and vertically. Left alignment is the default. To set thehorizontal and vertical alignment options, use the
HorizontalMode and
VerticalMode properties.

我们可以对单行文字进行水平和垂直调整。单行文字默认情况下是左对齐的。设置水平对齐和垂直对齐选项,用HorizontalMode属性和VerticalMode属性。
Normally when a text object is closed, theposition and alignment points of the text object are adjusted according to itsjustification and text style. However, the
alignment of an in memory textobject will not automatically be updated. Call the
AdjustAlignment method to update the alignment of thetext object based on its current property values.

通常创建完文字对象时,文字对象的位置和对齐点会依据排版和文字样式作相应调整。不过内存里的文字对象的对齐不会自动更新。可以调用AdjustAlignment方法在当前属性值的基础上更新文字对象的对齐特性。

Realign text
重新排列文字


The following example creates asingle-line text (DBText) object and a point (DBPoint) object. The point objectis set to the text alignment point, and is changed to a red crosshair so thatit is visible. The text alignment
is changed and you can see the impact ofchanging the text alignment.
下面的例子创建一个单行文字对象(DBText)和一个点对象(DBPoint)。将点对象设置成文字的对齐点,并将其外形改为红色十字以便可见。修改文字的排列对齐,并查看修改效果。
VB.NET

ImportsAutodesk.AutoCAD.Runtime
ImportsAutodesk.AutoCAD.ApplicationServices
ImportsAutodesk.AutoCAD.DatabaseServices
ImportsAutodesk.AutoCAD.Geometry

<CommandMethod("TextAlignment")>_
Public SubTextAlignment()
'' 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 Modelspace 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

Dim 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)

'' Create a point over the alignmentpoint of the text

Dim acPoint As DBPoint = NewDBPoint(acPtAlign)

acPoint.ColorIndex = 1

acBlkTblRec.AppendEntity(acPoint)
acTrans.AddNewlyCreatedDBObject(acPoint, True)

'' Adjust the insertion and alignmentpoints

acPtIns = New Point3d(acPtIns.X,acPtIns.Y + 3, 0)

acPtAlign = acPtIns

nCnt = nCnt + 1

Next

'' Set the point style to crosshair
Application.SetSystemVariable("PDMODE", 2)

'' Save the changes and dispose of thetransaction

acTrans.Commit()
End Using
End Sub
C#

usingAutodesk.AutoCAD.Runtime;
usingAutodesk.AutoCAD.ApplicationServices;
usingAutodesk.AutoCAD.DatabaseServices;
usingAutodesk.AutoCAD.Geometry;

[CommandMethod("TextAlignment")]
public static voidTextAlignment()
{
// 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以读打开Block表

BlockTable acBlkTbl;

acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Modelspace for write以写打开Block表记录Model空间

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创建单行文字

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 alignmentpoint of the text在文字的对齐点上创建一个点对象

DBPoint acPoint = newDBPoint(acPtAlign);

acPoint.ColorIndex = 1; //红色

acBlkTblRec.AppendEntity(acPoint);
acTrans.AddNewlyCreatedDBObject(acPoint, true);

// Adjust the insertion and alignmentpoints

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 thetransaction保存修改并关闭事务

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 insertionpoint 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 alignmentpoint of the text

Set pointObj = ThisDrawing.ModelSpace.AddPoint(alignmentPoint)

pointObj.color = acRed

' Adjust the insertion and alignmentpoints

insertionPoint(1) = insertionPoint(1) +3

alignmentPoint(1) = insertionPoint(1)

nCnt = nCnt + 1
Next
End Sub

2.4、ChangeSingle-Line Text修改单行文字

Like any other object, text objects can bemoved, rotated, erased, and copied. You also can mirror text. If you do notwant the text to be reversed when you mirror it, you can set the MIRRTEXTsystem variable to 0. You
move, rotate and copy objects using the TransformBy and
Clone methods. For more information on the
TransformBy and
Clone methods, see
EditNamed and 2D Objects.

就像操作其他对象一样,我们也可以对文字对象进行移动、旋转、删除和复制等修改。还可以对文字进行镜像操作。镜像文字时,如果不想反向文字,可以将系统变量MIRRTEXT设置为0。移动、旋转和复制对象使用TransformBy方法和Clone方法。关于TransformBy方法和Clone方法的详细内容,见“编辑命名对象和二维对象”一节。

/article/8703017.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐