Anda di halaman 1dari 3

Option Explicit

Sub SortDims()
Dim oSset As AcadSelectionSet
Dim oEnt As AcadEntity
Dim oDim As AcadDimension
Dim oDimAln As AcadDimAligned
Dim oDimRot As AcadDimRotated
Dim eCnt As Integer
Dim iCnt As Integer
Dim rCnt As Integer
Dim iNdx As Integer
Dim insPnt() As Double
Dim fcode(0) As Integer
Dim fdata(0) As Variant
Dim dxfCode, dxfdata
fcode(0) = 0: fdata(0) = "DIMENSION"
dxfCode = fcode
dxfdata = fdata
Set oSset = ThisDrawing.PickfirstSelectionSet
oSset.Clear
oSset.SelectOnScreen dxfCode, dxfdata
iCnt = oSset.Count
ReDim SelPnt(0 To iCnt - 1, 0 To 3) As Variant
eCnt = 0
For Each oEnt In oSset
Set oDim = oEnt
insPnt = oDim.TextPosition
SelPnt(eCnt, 0) = insPnt(0)
SelPnt(eCnt, 1) = insPnt(1)
SelPnt(eCnt, 2) = insPnt(2)
If TypeOf oDim Is AcadDimAligned Then
Set oDimRot = oEnt
SelPnt(eCnt, 3) = oDimRot.Measurement
ElseIf TypeOf oDim Is AcadDimRotated Then
Set oDimRot = oEnt
SelPnt(eCnt, 3) = oDimRot.Measurement
End If
eCnt = eCnt + 1
Next oEnt
ReDim sortPnt(0 To iCnt - 1, 0 To 3) As Variant
sortPnt = ColSort(SelPnt, 1)
Dim xlApp As Excel.Application
Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim strFilePath As String
strFilePath = "C:\ExtractDims.xls" '<-- must be exist
On Error Resume Next
Err.Clear
Set xlApp = GetObject(, "Excel.Application")
If Err 0 Then
Err.Clear
Set xlApp = CreateObject("Excel.Application")
If Err 0 Then
MsgBox "Cannot start Excel", vbExclamation
End
End If
End If
xlApp.Visible = True
On Error GoTo 0
Dim CheckOpen As Boolean
Dim OpenCnt As Long
Dim strFilePath_Name As String
For OpenCnt = 1 To xlApp.Workbooks.Count
If xlApp.Workbooks(OpenCnt).FullName = strFilePath Then
CheckOpen = True
strFilePath_Name = xlApp.Workbooks(OpenCnt).Name
ElseIf CheckOpen = True Then
CheckOpen = True
Else
CheckOpen = False
End If
Next
If CheckOpen Then
Set xlBook = xlApp.Workbooks(strFilePath_Name)
Set xlSheet = xlBook.Worksheets(1)
Else
Set xlBook = xlApp.Workbooks.Open(strFilePath)
Set xlSheet = xlBook.Worksheets(1)
End If
Dim irow As Long
irow = 1
With xlSheet
.Range("A:A").NumberFormat = "0.00#"
For iNdx = 0 To UBound(sortPnt, 1)
.Cells(irow, 1) = CStr(sortPnt(iNdx, 3))
irow = irow + 1
Next iNdx
End With
End Sub
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
' written by Fatty T.O.H. (c)2006 * all rights removed '
' SourceArr - two dimensional array '
' iPos - physical position of item in the sublist (starting from 1) '
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
Public Function ColSort(SourceArr As Variant, iPos As Integer) As Variant
Dim Check As Boolean
ReDim tmpArr(UBound(SourceArr, 2)) As Variant
Dim iCount As Integer
Dim jCount As Integer
Dim nCount As Integer
iPos = iPos - 1
Check = False
Do Until Check
Check = True
For iCount = LBound(SourceArr, 1) To UBound(SourceArr, 1) - 1
If SourceArr(iCount, iPos) > SourceArr(iCount + 1, iPos) Then
For jCount = LBound(SourceArr, 2) To UBound(SourceArr, 2)
tmpArr(jCount) = SourceArr(iCount, jCount)
SourceArr(iCount, jCount) = SourceArr(iCount + 1, jCount)
SourceArr(iCount + 1, jCount) = tmpArr(jCount)
Check = False
Next
End If
Next
Loop
ColSort = SourceArr
End Function