Home AutoCad VB Replace Block
08 | 02 | 2012
Main Menu
Search
Paypal Donation
So we can continue and expand this site
Content View Hits : 455708
Replace Block PDF Print E-mail
User Rating: / 3
PoorBest 
Tutorials

The code below can be used to look for a block, delete it and then replace it at the same insert points with another block.

Show/Hidden vbnet code

View source
 
 
	Function Replaceblock(ByVal Block2replace As String, ByVal Block2add As String)
 
 
 
	'****************************************
 
	'*** Code from VisibleVisual.com ********
 
	'****************************************
 
 
 
	Dim ent As AcadEntity
 
	Dim oBkRef As AcadBlockReference
 
	Dim Insertpoints As Variant
 
 
 
	Dim v As Variant
 
	Dim I As Long
 
 
 
	'First we go over every object in the modelspace
 
	For Each ent In ThisDrawing.ModelSpace
 
 
 
	'Check if the object is a block
 
	If ent.ObjectName = "AcDbBlockReference" Then
 
	Set oBkRef = ent
 
 
 
	'If the object is a block then check if its the block we are looking for
 
	If oBkRef.EffectiveName = Block2replace Then
 
 
 
	'get the blocks insertion points
 
	Insertpoints = oBkRef.insertionpoint
 
 
 
	'delete the old instance
 
	oBkRef.Delete
 
 
 
	'insert the new block at the same location
 
	InsertBlock Block2add, Insertpoints, 0
 
 
 
	End If
 
	End If
 
 
 
	'Next object
 
	Next ent
 
 
 
	End Function
 
 
 
	Function InsertBlock(ByVal blockname As String, ByVal insertionpoint As Variant, ByVal rotation As Double)
 
	'This function inserts a new block
 
 
 
	Dim blockobj As AcadBlockReference
 
	Dim insertionPnt As Variant
 
	Dim prompt1 As String
 
 
 
	'set rotation Angle
 
	rotateAngle = rotation
 
	rotateAngle = rotation * 3.141592 / 180#
 
 
 
	ThisDrawing.ActiveSpace = acModelSpace
 
 
 
	Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionpoint, blockname, 1#, 1#, 1#, rotateAngle)
 
 
 
	End Function
 
 
 
	Sub TestReplaceBlock()
 
	'Test the functions
 
	Replaceblock "Blockname1", "Blockname2"
 
 
 
	End Sub

The code will search all blocks in modelspace, if the name is the same as the block that you are looking for then it will get the insert points and insert a new block.


Below an example of the result, left the original block called 'blockname1", right the replaced block called "blockname2"

blockname1  blockname2


Attachments:
FileDescriptionFile sizeDownloads
Download this file (ReplaceBlock.zip)Replace BlockReplace a Block by another Block7 Kb280
 

Comments  

 
0 #4 2011-12-20 11:58
TO #1 2010-10-27 12:31 - very smartly done indeed! I think your way is more safe, retaining all dynamic blocks as they are.
Quote
 
 
0 #3 2011-03-16 12:25
How about replacing blocks with attributed text?

Where the new block have different tag names, but similar such as "DWGNUMBER" instead of "DRGNo".

It will also have to read the value of the old tag and re-enter it in the new block.

This is not so simple as you need to
1- Find the block
2- Read the block insert, scale
3- Read attribute value
4- Delete old block
5- purge old block as (yes same block name and Atttribute definition won't get re-define)
6- Insert new block using insert point and scale
7- re-enter attribute data

Loop on both model space and all layout space.
Quote
 
 
0 #2 2010-10-27 12:57
And regen all
Quote
 
 
0 #1 2010-10-27 12:31
Sub test()

Replaceblock "blockname1", "blockname2"

End Sub

Function Replaceblock(By Val Block2replace As String, ByVal Block2add As String)

Dim blockobj As AcadBlockRefere nce

For Each ent In ThisDrawing.Blocks(Block2re place)
ent.Delete
Next

Set blockRefObj = ThisDrawing.Blocks(Block2re place).insertblock(Thi sDrawing.Blocks(Block2ad d).Origin, Block2add, 1, 1, 1, 0)

blockRefObj.Explode
blockRefObj.Delete

End Function
Quote
 

Add comment


Security code
Refresh

Advertising

LOGON
Sponsored Links