Hi!
Good day.
Please help using AutoCad VBA. Currently I'm developing a tool to remove XRefs that are un-reference or unloaded.
Below the code that I already started to list all Xref in to the drawing and display it to the listbox. I don't know if this is the proper way to load the Xref's?
Code:
Dim bdef As AcadBlock
Dim bref As AcadBlockReference
Dim xref As AcadExternalReference
Dim blk As AcadBlock
Dim ent As AcadEntity
Dim xEnt As AcadEntity
Dim list As New Collection
Dim item As Variant
Dim msg As String
Dim processCnt As Integer
'' handle errors inline
On Error Resume Next
'' Purge
purgeBlocks
purgeBlocks
'' Settings
pCnt = 1
pCntMax = ThisDrawing.Blocks.Count
ListBox1.ColumnCount = 5
ListBox1.ColumnWidths = "30,150,250,50,30"
mProcessDetached = True
processCnt = 0
ListBox1.Clear
'' iterate block collection
frmProgressBar.show
pCnt = 0
For Each blk In ThisDrawing.Blocks
processCnt = processCnt + 1
'frmProgressBar.progressbar "", processCnt, pCntMax
'frmProgressBar.Repaint
If (blk.IsXRef) Then
pCnt = pCnt + 1
Set xref = blk.XRefDatabase
With ListBox1
.AddItem
.list(.ListCount - 1, 0) = pCnt
.list(.ListCount - 1, 1) = blk.Name
.list(.ListCount - 1, 2) = blk.Path
.list(.ListCount - 1, 3) = IIf(IsBlockNoXRefDatabase(blk), "Unloaded", "Loaded")
If (mProcessDetached) Then
If (IsBlockNoXRefDatabase(blk)) Then
'blk.Unload
'blk.Detach
'blk.Delete
.list(.ListCount - 1, 3) = "Detached"
End If
End If
End With
For Each bdef In blk.XRefDatabase.Blocks
'frmProgressBar.progressbar "", pCnt, pCntMax
'frmProgressBar.Repaint
If Not (bdef Is Nothing) Then
If (bdef.IsXRef) Then
pCnt = pCnt + 1
If (bdef.Name = "") Then
MsgBox bdef.Name
MsgBox (bdef Is Nothing)
End If
With ListBox1
.AddItem
.list(.ListCount - 1, 0) = pCnt
.list(.ListCount - 1, 1) = " -" & bdef.Name
.list(.ListCount - 1, 2) = bdef.Path
.list(.ListCount - 1, 3) = IIf(IsBlockNoXRefDatabase(bdef), "Unloaded", "Loaded")
End With
End If
End If
Next
End If
Next
Thanks and best regards,
Alex