muck Posted September 9, 2010 Share Posted September 9, 2010 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 Quote Link to comment Share on other sites More sharing options...
SEANT Posted September 10, 2010 Share Posted September 10, 2010 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 Quote Link to comment Share on other sites More sharing options...
muck Posted September 10, 2010 Author Share Posted September 10, 2010 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, Quote Link to comment Share on other sites More sharing options...
muck Posted September 13, 2010 Author Share Posted September 13, 2010 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? Quote Link to comment Share on other sites More sharing options...
SEANT Posted September 14, 2010 Share Posted September 14, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.