‘绑定 AutoCAD
Dim I As Integer, J As Integer
Dim oAutoCAD As Object ‘AutoCAD Application 本身
Call BindAutoCAD(True)
Dim oDraw As Object ‘AutoCAD 多文档之一
‘打开并绑定文件 D:/Test.dwg
Set oDraw = oAutoCAD.Application.Documents.Open(“D:/Test.dwg”)
‘绑定 Microsoft Excel
Dim oExcel As Object ‘Microsoft Excel Application 本身
Call BindExcel(True)
Dim oBook As Object ‘Microsoft Excel 多工作薄之一
Dim oSheet As Object ‘工作薄的工作表之一
‘打开并绑定文件 D:/Test.xls
Set oBook = oExcel.Application.WorkBooks.Open(“D:/Test.xls”)
‘绑定已有工作表 Sheet1
Set oSheet = oBook.Sheets(“Sheet1”)
‘输入 App 名称
Dim sApp As String
sApp = InputBox(“Enter the app name of XData!”, “”, sApp, Me.Left + 1800, Me.Top + 2400)
‘sApp 为空表示所有 App

‘屏幕选择图元
Dim PointPicked As Variant
Dim oEntity As Object
oDraw.Activate
oDraw.Utility.GetEntity oEntity, PointPicked
‘读取扩展数据
Dim iType As Variant
Dim sValue As Variant
iType = DetachXData(oEntity, sApp, “Type”)
sValue = DetachXData(oEntity, sApp, “Value”)
‘指向 TargetRow 行第一列
TargetRow = MaxRow(oSheet) + 1 ‘有文字的最大行
oSheet.Cells(TargetRow, 1).Select
oSheet.Rows(TargetRow).NumberFormatLocal = “@” ‘文本模式
‘展示扩展数据
Dim I As Integer, J As Integer
For I = LBound(iType) To UBound(iType)
oSheet.Cells(1, GetColumnNumber(oSheet, iType(I), 1)) = iType(I)
oSheet.Cells(TargetRow, GetColumnNumber(oSheet, iType(I), 1)) = sValue(I)
Next I
For J = MinColumn(oSheet) To MaxColumn(oSheet)
oSheet.Columns(J).AutoFit ‘调整列宽
Next J

Public Function DetachXData(ByVal oEntity As Object, Optional ByVal sApp As String = “”, Optional ByVal sScope As String = “”) As Variant
Dim I As Integer, J As Integer
‘ Return the xdata for the entity
Dim XTypeOut As Variant
Dim XValueOut As Variant
oEntity.GetXData sApp, XTypeOut, XValueOut
If Not IsArray(XTypeOut) Then Exit Function
If Not IsArray(XValueOut) Then Exit Function
Dim iArray() As Integer
Dim sArray() As String
Dim vTemp As Variant
Dim iCount As Integer
For I = LBound(XTypeOut) To UBound(XTypeOut)
If Not IsInArray(XTypeOut(I), iArray) Then
iCount = iCount + 1
ReDim Preserve iArray(0 To iCount – 1)
ReDim Preserve sArray(0 To iCount – 1)
iArray(iCount – 1) = XTypeOut(I)
End If
Next I
‘IsMissing 只对 Variant 变量有效
If IsMissing(sScope) Or sScope = “” Or InStr(1, sScope, “Type”, vbTextCompare) > 0 Then ‘DataType
DetachXData = iArray
Else ‘DataValue
For I = LBound(iArray) To UBound(iArray)
For J = LBound(XTypeOut) To UBound(XTypeOut)
If XTypeOut(J) = iArray(I) Then
If iArray(I) = 1010 Or iArray(I) = 1011 Or iArray(I) = 1012 Or iArray(I) = 1013 Then ‘三维
vTemp = XValueOut(J)
sArray(I) = sArray(I) & “|” & vTemp(0) & “,” & vTemp(1) & “,” & vTemp(2)
Else
sArray(I) = sArray(I) & “|” & XValueOut(J)
End If
sArray(I) = CutTerminal(sArray(I), “|”)
Else
‘Nothing
End If
Next J
Next I
DetachXData = sArray
End If
End Function