TheSwamp

Code Red => VB(A) => Topic started by: Bob Wahr on October 10, 2005, 05:54:57 PM

Title: Explode and delete block reference on insertion
Post by: Bob Wahr on October 10, 2005, 05:54:57 PM
Here's what I needI was trying to do this using the ObjectAdded event but when I try to delete the block reference, I get an error because it is still open for read.  I tried to do the block reference deletion in a sub that was called by the event sub.  Same result.  Can someone kick me in the head and make me think?
Title: Re: Explode and delete block reference on insertion
Post by: Bob Wahr on October 10, 2005, 06:00:29 PM
Above might not be clear or someone might be able to point me in a slightly different direction here's some more rundown
Title: Re: Explode and delete block reference on insertion
Post by: Chuck Gabriel on October 10, 2005, 06:01:24 PM
By the time the EndCommand event fires, all the objects should be closed, so in the ObjectAdded Event, just store the ObjectId of the block reference in a global variable.  Then, in the EndCommand event, check to see if the global variable for the ObjectId is non-null, and if it is do your processing there.  Then nullify the global variable.

Clear as mud?
Title: Re: Explode and delete block reference on insertion
Post by: Bob Wahr on October 10, 2005, 06:02:34 PM
just exactly the kick I needed.  Thanks Chuck.
Title: Re: Explode and delete block reference on insertion
Post by: Kerry on October 10, 2005, 06:05:30 PM
You may want to investigate GROUPS for containing the 'exploded' objects .. may make your management a little easier.
Title: Re: Explode and delete block reference on insertion
Post by: Bob Wahr on October 10, 2005, 06:34:54 PM
That's a thought.  I was thinking since the explode method writes to a variant array anyway and the objects would never last longer than the current session, I should be fine.  I'll keep the groups in mind if I run into any trouble though.
Title: Re: Explode and delete block reference on insertion
Post by: Dinosaur on October 10, 2005, 10:16:08 PM
Back in the ancient days days of r10 before dialog boxes were, we were inserting exploded blocks at the command line that would not leave a block definition to purge.

All that is necessary is to place an asterisk "*" at the start of your response when the insert command asks for a file name
example - *c:\<path>\<block name.dwg>
It will then prompt for the scale factor (must be uniform in x,y,z), an insert point and lastly the rotation.  I have confirmed that this procedure has indeed survived and still works (with Civil 3D 2006).

Perhaps there is a way to incorporate this into your code?
Title: Re: Explode and delete block reference on insertion
Post by: Murphy on October 11, 2005, 05:53:17 AM
808,

This is what I had to use in AutosXeduLe to delete the block ref. Notice the error handling.

Code: [Select]
Public Function ExplodeEX(oBlkRef As AcadBlockReference)
    Dim objEnt As AcadEntity
    Dim anEnt As AcadEntity
    Dim aBRef As AcadBlockReference
    Dim objBlk As AcadBlock
    Dim objDoc As AcadDocument
    Dim objArray() As AcadEntity
    Dim objSpace As AcadBlock
    Dim intCnt As Integer
    Dim varTemp As Variant
    Dim varPnt As Variant
    Dim dblScale As Double
    Dim dblRot As Double
    Dim strBName As String
On Error GoTo Err_Handler
    strBName = oBlkRef.Name
    'What document is the reference in?
    Set objDoc = oBlkRef.Document
    'Model space or layout?
    Set objSpace = objDoc.ObjectIdToObject(oBlkRef.OwnerID)
    Set objBlk = objDoc.Blocks(strBName)
    objBlk.Delete
DeleteRefs:
    For Each anEnt In ThisDrawing1.ModelSpace
        If TypeOf anEnt Is AcadBlockReference Then
            Set aBRef = anEnt
            If aBRef.Name = strBName Then
                aBRef.Delete
            End If
        End If
    Next
    For Each anEnt In ThisDrawing1.PaperSpace
        If TypeOf anEnt Is AcadBlockReference Then
            Set aBRef = anEnt
            If aBRef.Name = strBName Then
                aBRef.Delete
            End If
        End If
    Next
        oBlkRef.Delete
    'Release memory
    objDoc.PurgeAll
    objDoc.PurgeAll
    Set objDoc = Nothing
    Set objBlk = Nothing
    Set objSpace = Nothing
Exit_Here:
    Exit Function
Err_Handler:
  Select Case Err.Number
    Case -2145386476 ' key not found
        Resume Exit_Here
    Case -2145386420 ' automation error
        Resume Exit_Here
    Case -2145386239 ' object is referenced
        Resume DeleteRefs
    Case Else
        MsgBox Err.Number & Err.Description, vbOKOnly, "Uh Oh!"
        Resume Exit_Here
  End Select
End Function
Title: Re: Explode and delete block reference on insertion
Post by: Jürg Menzi on October 12, 2005, 09:57:39 AM
Bob,
keep in mind that you can't use the explode method with NUS blocks since A2k4... :realmad:
Title: Re: Explode and delete block reference on insertion
Post by: MP on October 12, 2005, 10:42:13 AM
That is indeed a pain Jürg.
Title: Re: Explode and delete block reference on insertion
Post by: Bob Wahr on October 12, 2005, 11:21:37 AM
I will definitely keep that in mind.  although the app I was writing was for a very specific case and it worked fine.