Author Topic: Explode and delete block reference on insertion  (Read 2961 times)

0 Members and 1 Guest are viewing this topic.

Bob Wahr

  • Guest
Explode and delete block reference on insertion
« on: October 10, 2005, 05:54:57 PM »
Here's what I need
  • When a specific block is inserted, block reference is exploded
  • Elements that make up block are added to a public array so that they can be deleted later
  • block reference is deleted (explode method leaves it)
  • block definition is deleted
I 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?

Bob Wahr

  • Guest
Re: Explode and delete block reference on insertion
« Reply #1 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
  • One of two blocks will be inserted into drawing manually
  • The block will ALWAYS be exploded
  • The drawing will be saved with the elements from the block in it
  • the drawing will NOT be closed with the elements from the block in it
  • The block definition and all elements from the block must be gone from the drawing database when the drawing is closed

Chuck Gabriel

  • Guest
Re: Explode and delete block reference on insertion
« Reply #2 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?

Bob Wahr

  • Guest
Re: Explode and delete block reference on insertion
« Reply #3 on: October 10, 2005, 06:02:34 PM »
just exactly the kick I needed.  Thanks Chuck.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Explode and delete block reference on insertion
« Reply #4 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.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Bob Wahr

  • Guest
Re: Explode and delete block reference on insertion
« Reply #5 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.

DinØsaur

  • Certified Bovine Scat Distributor...I lay it on THICK
  • Needs a day job
  • Posts: 5424
  • But I don't WANT a Day Job
Re: Explode and delete block reference on insertion
« Reply #6 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?
« Last Edit: October 10, 2005, 10:20:47 PM by Dinosaur »
Stephen R. Sherrill,
The DinØsaur
Civil 3D Specialist Emeritus

YAY ME !!!  I made it out alive !

Murph

  • Bull Frog
  • Posts: 402
  • P.I.T.A.
Re: Explode and delete block reference on insertion
« Reply #7 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
Murphy's Law applies to all.
Whatever can go wrong, will, and at the most inopportune time.

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Re: Explode and delete block reference on insertion
« Reply #8 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:
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

MP

  • Seagull
  • Posts: 17529
Re: Explode and delete block reference on insertion
« Reply #9 on: October 12, 2005, 10:42:13 AM »
That is indeed a pain Jürg.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

Bob Wahr

  • Guest
Re: Explode and delete block reference on insertion
« Reply #10 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.