-
- Option Explicit
- Public Sub MTextTotext()
- On Error Resume Next
- Dim ptInsert As Variant
- Dim txtStr As String
- Dim height As Double
- Dim width As Double, bbg As Double
- Dim k As Double, oScale As Double
- Dim pt1, pt2, pt3
- k = 0.4
- '确定选择范围区以及表格现有的标高*********************************************
- pt1 = ThisDrawing.Utility.GetPoint(, "框选左上角一个点: ")
- pt2 = ThisDrawing.Utility.GetPoint(, "框选右下角一个点: ")
- pt3 = ThisDrawing.Utility.GetPoint(, "将表格变成7mm高,选取左上角下方邻近点,以确定现有表格高度: ")
- bbg = GetDistance(pt1, pt3)
- Dim SSet As AcadSelectionSet
- oScale = 7 / bbg
- '选择多行文字*********************************************
- '安全创建选择集
- 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) = "MText"
- SSet.Select acSelectionSetCrossing, pt1, pt2, filterType, filterData
- '创建单行文字***************************************************************
- Dim ptMin As Variant, ptMax As Variant
- Dim objText As AcadText
- Dim objMText As AcadMText
- For Each objMText In SSet
- '获得文字的主要参数
- height = objMText.height
- ptInsert = objMText.InsertionPoint
- ptInsert(1) = ptInsert(1) - height
- txtStr = MtextStringClearFormat(objMText.TextString)
- '文字的限制框宽度
- Set objText = ThisDrawing.ModelSpace.AddText(txtStr, ptInsert, height)
- objText.ScaleFactor = k
- objMText.Delete '删除原来的多行文字
- Next
- SSet.Delete
- '第二步,在上一步的基础上,实现所有单行文字宽高比,变成K。
- '安全创建选择集
- If Not IsNull(ThisDrawing.SelectionSets.Item("this")) Then
- Set SSet = ThisDrawing.SelectionSets.Item("this")
- SSet.Delete
- End If
- Set SSet = ThisDrawing.SelectionSets.Add("this")
- '定义过滤规则,选持单行文字。
- filterType(0) = 0
- filterData(0) = "Text"
- SSet.Select acSelectionSetCrossing, pt1, pt2, filterType, filterData
- For Each objText In SSet
- objText.ScaleFactor = k
- Next
- SSet.Delete
- '第三步,表格整体缩放,在现在表格标高的基础上,将单表格高度整体缩放为7mm高,此时文字大概的高度为3.5mm,标准化后以便下一步操作。
- '安全创建选择集
- Dim objEnt As AcadEntity
- If Not IsNull(ThisDrawing.SelectionSets.Item("this")) Then
- Set SSet = ThisDrawing.SelectionSets.Item("this")
- SSet.Delete
- End If
- Set SSet = ThisDrawing.SelectionSets.Add("this")
- SSet.Select acSelectionSetCrossing, pt1, pt2
- For Each objEnt In SSet
- objEnt.ScaleEntity pt1, oScale
- Next
- SSet.Delete
- End Sub
- Public Function MtextStringClearFormat(MTextString As String) As String '清除掉多行文字中的格式。
- Dim MyString As String
- MyString = MTextString
- MyString = ReplaceByRegExp(MyString, "\\{", Chr(1))
- MyString = ReplaceByRegExp(MyString, "\\}", Chr(2))
- MyString = ReplaceByRegExp(MyString, "\\", Chr(3))
- MyString = ReplaceByRegExp(MyString, "\\S([^;]*?)(\^|#)([^;]*?);", "$1$3")
- MyString = ReplaceByRegExp(MyString, "\\S([^;]*?);", "$1")
- MyString = ReplaceByRegExp(MyString, "(\\P|\\O|\\o|\\L|\\l|\{|\})", "")
- MyString = ReplaceByRegExp(MyString, "\\[^;]*?;", "")
- MyString = ReplaceByRegExp(MyString, "\x01", "{")
- MyString = ReplaceByRegExp(MyString, "\x02", "}")
- MyString = ReplaceByRegExp(MyString, "\x03", "")
- MtextStringClearFormat = Trim(MyString)
- End Function
- Public Function ReplaceByRegExp(ByVal Mystrig As String, ByVal TxtFind As String, ByVal TxtReplace As String)
- Dim RE As Object
- Set RE = ThisDrawing.Application.GetInterfaceObject("Vbscript.RegExp")
- RE.IgnoreCase = False
- RE.Global = True
- RE.Pattern = TxtFind
- ReplaceByRegExp = RE.Replace(Mystrig, TxtReplace)
- Set RE = Nothing
- End Function
- '计算两点之间距离
- Public Function GetDistance(sp As Variant, ep As Variant) As Double
- Dim x As Double
- Dim y As Double
- Dim z As Double
- x = sp(0) - ep(0)
- y = sp(1) - ep(1)
- z = sp(2) - ep(2)
- GetDistance = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2))
- End Function