代码改变世界

AutoCAD VBA单行文字转换为多行文字

2011-03-22 16:41  精诚所至 金石为开  阅读(1187)  评论(0编辑  收藏  举报

AutoCAD VBA单行文字转换为多行文字,多行文字便于编辑,代码如下。

Public Sub TextToMtext()
On Error Resume Next
Dim ptInsert As Variant
Dim txtStr As String
Dim height As Double
Dim width As Double
Dim SSet As AcadSelectionSet
If Not IsNull(ThisDrawing.SelectionSets.Item("this")) Then
Set SSet = ThisDrawing.SelectionSets.Item("this")
SSet.Delete
End If
Set SSet = ThisDrawing.SelectionSets.Add("this")
Dim filterType(0) As Integer
Dim filterData(0) As Variant
filterType(0) = 0
filterData(0) = "Text"
SSet.SelectOnScreen filterType, filterData
Dim ptMin As Variant, ptMax As Variant
Dim objText As AcadText
Dim objMtext As AcadMText
For Each objText In SSet
ptInsert = objText.InsertionPoint
txtStr = objText.height
objText.GetBoundingBox ptMin, ptMax
width = (ptMax(0) - ptMin(0)) * 1.2
Set objMtext = ThisDrawing.ModelSpace.AddMText(ptInsert, width, txtStr)
objMtext.height = height
objMtext.AttachmentPoint = acAttachmentPointBottomLeft
objMtext.InsertionPoint = ptInsert
objText.Delete
Next
SSet.Delete
End Sub

代码完。