PDA

View Full Version : Batch Inserting Blocks with VBA Hangs up on second drawing?



muck
9th Sep 2010, 07:11 pm
AutoCAD 2010 vba.

The following code is for a batch block insert.
Batch Inserting Blocks with VBA Hangs up on second drawing?
The process stops on the second drawing at line
Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(startPnt, "C:\BlockInsert.Dwg", 1#, 1#, 1#, 0)
If I rename C:\BlockInsert.Dwg to C:\BlockInsert1.dwg durring debug the code will execute.
I don't see where I have duplicate blocks my drawing because of my purges.
Anyone have any suggestion to fix this?
Thank you,


Private Sub CommandButton3_Click()
Dim toggle As Boolean
toggle = True
Me.Hide
Dim zcount As Integer
DrawingList.VBDwgFileList
AutoCAD.Documents.Close
For zcount = 0 To DrawingList.filecount - 1
FileName = DrawingList.VBDwgFileNames(zcount)

If FileName <> "" Then AutoCAD.Documents.Open (FileName)
If Application.Documents.Count = 0 Then
MsgBox "Empty AutoCAD Editor"
Exit Sub
End If
InsertBlockName = "BlockInsert.Dwg"
'****
ThisDrawing.PurgeAll
ThisDrawing.SendCommand "-Purge" & vbCr & "B" & vbCr & "*" & vbCr & "N" & vbCr
startPnt = ThisDrawing.Utility.GetPoint(, prompt1)
Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(startPnt, "C:\BlockInsert.Dwg", 1#, 1#, 1#, 0)
BlockRefObj.Explode
BlockRefObj.Delete
ThisDrawing.PurgeAll
Debug.Print FileName
ThisDrawing.Save 'Save the drawing
ThisDrawing.Close
'Debug.Print zcount
If zcount = DrawingList.filecount - 1 Then Exit Sub
Next
Me.Show
End Sub

SEANT
10th Sep 2010, 08:30 am
The problem may be due to the VBA quirk where the SendCommand execution is delayed until the end of the routine.

A possible option to avoid the Purge requirement would be to use the CopyObject method. An example of which is shown here:

http://www.cadtutor.net/forum/showthread.php?26666

muck
10th Sep 2010, 02:38 pm
So if I have my routine to run through all the files and purge them then go
back and insert the blocks. That might work? I will give that a try
I notice that the VBA statement Thisdrawing.purgeall does not get all blocks.
The command send for purge does. That is why I am using the command send here.
Any other suggestions?
Thank you,

muck
13th Sep 2010, 12:23 pm
I saperated the routines into purge then insert block as follows.
That did not work.

Private Sub CommandButton3_Click()
Dim toggle As Boolean
toggle = True
Me.Hide
Dim zcount As Integer
DrawingList.VBDwgFileList
AutoCAD.Documents.Close
For zcount = 0 To DrawingList.filecount - 1
FileName = DrawingList.VBDwgFileNames(zcount)

If FileName <> "" Then AutoCAD.Documents.Open (FileName)
If Application.Documents.Count = 0 Then
MsgBox "Empty AutoCAD Editor"
Exit Sub
End If
InsertBlockName = "BlockInsert.Dwg"
ThisDrawing.PurgeAll
ThisDrawing.SendCommand "-Purge" & vbCr & "B" & vbCr & "*" & vbCr & "N" & vbCr
Debug.Print FileName
ThisDrawing.Save 'Save the drawing
ThisDrawing.Close
If zcount = DrawingList.filecount - 1 Then Exit Sub
Next
Me.Show
End Sub
Private Sub CommandButton11_Click()
Dim toggle As Boolean
toggle = True
Me.Hide
Dim zcount As Integer
DrawingList.VBDwgFileList
AutoCAD.Documents.Close
For zcount = 0 To DrawingList.filecount - 1
FileName = DrawingList.VBDwgFileNames(zcount)

If FileName <> "" Then AutoCAD.Documents.Open (FileName)
If Application.Documents.Count = 0 Then
MsgBox "Empty AutoCAD Editor"
Exit Sub
End If

Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(startPnt, "C:\BlockInsert.Dwg", 1#, 1#, 1#, 0)
BlockRefObj.Explode
BlockRefObj.Delete
ThisDrawing.PurgeAll
Debug.Print FileName
ThisDrawing.Save 'Save the drawing
ThisDrawing.Close
If zcount = DrawingList.filecount - 1 Then Exit Sub
Next
Me.Show
End Sub
Any other suggestions?

SEANT
14th Sep 2010, 10:01 am
Does the incoming drawing, "BlockInsert.Dwg", itself contain Blocks.

At the database level (the level at which VBA operates) a purge operations only affects the top most block layer, nested block are still considered referenced until after that top layer is deleted. ThisDrawing.PurgeAll would have to be called for each nesting level.*

Sadly, VBA does not handle ThisDrawing.SendCommand well so any result from a SendCommand call will not be available until after the entire routine has run.

What VBA should be able to do, though, is continually loop a PurgeAll call until none of the blocks associated with BlockInsert.Dwg are present.



* Presumably, there is a ObjectARX multi-iteration purge happening behind the scenes with AutoCAD’s Command: -Purge.