Home AutoCad VB Count Blocks
20 | 05 | 2012
This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade

Count Blocks PDF Print E-mail
User Rating: / 1
PoorBest 
Tutorials

The following code will show all blocks in modelspace

OPTION 1


Show/Hidden vbnet code

View source
Sub Countblocks()
 
 
 
	'****************************************
 
	'*** Code from VisibleVisual.com ********
 
	'****************************************
 
 
 
	Dim oBkRef As AcadBlockReference
 
	Dim ent As AcadEntity
 
	Dim a As Variant
 
	Dim Value As String
 
 
 
	Value = "The following blocks are in modelspace" & vbCr
 
 
 
	For Each ent In ThisDrawing.ModelSpace
 
 
 
	If ent.ObjectName = "AcDbBlockReference" Then
 
	Set oBkRef = ent
 
 
 
	Value = Value & "- " & oBkRef.EffectiveName & vbCr
 
	End If
 
	Next ent
 
 
 
	MsgBox Value
 
 
 
	End Sub

OPTION 2


Code above only displayes the blocks in modelspace but if multiple of the same blocks are in modelspace then double items exist. If we modificate the code and add a userform and listbox (called LstBlocks) then the code blow can display how many off the same blocks are in the drawing. (Make sure that the ColumnCount of the LstCount is set to 2)

 

Show/Hidden vbnet code

View source
Sub Countblocks()
 
 
 
	'****************************************
 
	'*** Code from VisibleVisual.com ********
 
	'****************************************
 
 
 
	Dim oBkRef As AcadBlockReference
 
	Dim ent As AcadEntity
 
 
 
	For Each ent In ThisDrawing.ModelSpace
 
 
 
	If ent.ObjectName = "AcDbBlockReference" Then
 
	Set oBkRef = ent
 
 
 
 
 
	With LstBlocks
 
 
 
	'Search if item exists in listbox
 
	For n = 0 To .ListCount - 1
 
	'Raise the amount with 1
 
	If .list(n) = oBkRef.EffectiveName Then
 
	.list(n, 1) = .list(n, 1) + 1
 
	GoTo Nextn
 
	End If
 
	Next n
 
 
 
	'If item does not exist then add item
 
	LstBlocks.AddItem oBkRef.EffectiveName
 
	LstBlocks.list(n, 1) = 1
 
 
 
	Nextn:
 
 
 
	End With
 
 
 
	End If
 
 
 
	Next ent
 
 
 
	End Sub

The code will result in the following view:


Attachments:
FileDescriptionFile sizeDownloads
Download this file (Count Blocks.dvb)Count BlocksMacro file to count blocks in AutoCad26 Kb537
 

Comments   

 
0 #1 Guest 2011-02-11 13:29
It is an amazing answer for me and help me so much. I'd like to be the practitioner of this blog and as a honest fan about it. Welcome to share and reprint.
Quote
 

Add comment


Security code
Refresh

This site has been updated and renewed. You have followed an old link.

Click here to go to the new site

http://www.visiblevisual.com/jupgrade