How to implementation the block redefinition function with VBA?

How to implementation the block redefinition function with VBA?

Anonymous
Not applicable
2,292 Views
4 Replies
Message 1 of 5

How to implementation the block redefinition function with VBA?

Anonymous
Not applicable

Hi all,

I want to update block definition from another file by using the ADCENTER facility, for example, file1.dwg has some blocks named BLOCK1,BLOCK2,...etc., and file2.dwg has these blocks with the same name, but definition up-to-date. By choosing REDEFINITION in ADCENTER the block can be redefined, but it only can be done manually one by one.

As the number of blocks is large, I want to do this in VBA code, but I cannot find any method to do this, could anyone give me hints or sample code?

Thanks very much.

 

0 Likes
Accepted solutions (1)
2,293 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

Well, you can simply insert the block file into the drawing, the block definition will be updated automatically, with this method

 

AcadDocument.Model[Paper]Space.Inser (point, blockName,...)

 

for the second argument, you can use either a block name, for a block file name with full pah. When use block name, AutoCAD will search the drawing itself for the block definition. In your case, because the block already there, so the inserted block reference would be refernce to the existing block definition. However, if you use a full block file name, AutoCAd will insert the block from the file (in your case, updated block with the same name), and use the block file name (without .dwg) as block name. The newly inserted block definition will replace an existing block definition with the same block name.

 

After inserting, you call AcadDcument.Regen(...), all existing block reference will be updated to reference new block definition, except for attributes in block reference, which you need to use ATTSync command (or your own code) to process it, and may be, some instances of dynamic block references.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks very much for your reply, but it doesn't work when I use the InsertBlock method accordint to your answer, AutoCAD prompt that block redefinition would be ignored while the block has existed.

Can you give some worked example?

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

Without seeing your code, I can not guess how and why it does not work for you. But it should be really simple. Here is the code I do it with detailed explanation as comments:

 

Option Explicit

Public Sub RedefineBlock()

    ''Block file: a rectangle with an attribute at center
    Dim blockFile As String
    blockFile = "C:\Temp\PartName.dwg"
    
    ''Block: a Circle with an attribute at center
    Dim blockName As String
    blockName = "PartName"
    
    Dim blk As AcadBlockReference
    
    ''Insert a block using a block definition, named as "PartName"
    ''already exists in drawing.
    ''Also there are already a few block refernces of this block in drawing
    Dim pt1(0 To 2) As Double
    pt1(0) = 1#: pt1(1) = 1#: pt1(2) = 0#
    
    Set blk = ThisDrawing.ModelSpace.InsertBlock(pt1, blockName, 1#, 1#, 1#, 0#)
    blk.Update
    
    ''Notice that the block references of block "PartName" are
    ''as the block defined: a circle with an attribute at center
    MsgBox "inserted block name=" & blk.Name
    
    ''Insert the block with the same name from file,
    ''so that the existing block definition of "PartName"
    ''will be redefined to become a rectangle with an attribute
    ''at center. All block references to block "PartName" will
    ''all change according to the new definition, a REGEN is required
    Set blk = ThisDrawing.ModelSpace.InsertBlock(pt1, blockFile, 1#, 1#, 1#, 0#)
    
    blk.Update
    
    ''Notice that the block references of block "PartName" are
    ''as the redefined block: a rectnagle with an attribute at center
    MsgBox "inserted block name=" & blk.Name
    
    ''Since the purpose of the inserting is to redefine the block definition
    ''so the inserted block refernce can be erased, if desired
    blk.Delete
    
    ''Regen to update the existing block references
    ThisDrawing.Regen acActiveViewport

End Sub

Here is a video clip showing how the code works.

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5

Anonymous
Not applicable

Really appreciate for your sample code.

When I generate the .dwg file by WBLOCK command, the InsertBlock method works. so I think that the problem is the content of the .dwg file. My file tested before is a ordinary dwg file, including many blocks, when the InsertBlock method run, AutoCAD propmts an error. But when I export a block by WBLOCK command, the generated file is a block file, then the InsertBlock method works well.

Thanks again.

0 Likes