Results 1 to 5 of 5

Thread: Purging Blocks in managed code?

  1. #1
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,397
    Login to Give a bone
    0

    Default Purging Blocks in managed code?

    I've tried several methods to delete a BlockTableRecord, but all corrupt the dwg. You can use the same method to delete a layer, but it doesn't seem to work for blocks. So far, I've had to use com interop. Has anyone found a way to purge blocks in managed code?
    C:> ED WORKING....

  2. #2
    I could stop if I wanted to
    Join Date
    2003-03
    Location
    Alberta
    Posts
    260
    Login to Give a bone
    0

    Default Re: Purging Blocks in managed code?

    Try the Dwg.Database.Purge method. This will only work to purge out unused objects however. If you have a block used somewhere in the drawing the block will not be able to be purged. Here's a sub (havn't tried it, cut and pasted from an existing sub that does). It's missing setting a few variables. The code keeps looping until all items that can be purged have been.

    Code:
    
    Sub Purge()
        Dim Layers As Autodesk.AutoCAD.DatabaseServices.LayerTable
        Dim Blocks As Autodesk.AutoCAD.DatabaseServices.BlockTable
        Dim TextStyles As Autodesk.AutoCAD.DatabaseServices.TextStyleTable
        Dim LineTypes As Autodesk.AutoCAD.DatabaseServices.LinetypeTable
        Dim ObjId As Autodesk.AutoCAD.DatabaseServices.ObjectId
        Dim PurgeCollection As New Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection
        Dim NumberPurged(1) As Integer
        Dim Database As Autodesk.AutoCAD.DatabaseServices.Database
        Dim Trans As Autodesk.AutoCAD.DatabaseServices.Transaction
    
        Do
            ' Update the layers so we can purge all the unused layers.
            Layers.GenerateUsageData()
    
            For Each ObjId In Blocks
                PurgeCollection.Add(ObjId)
            Next
    
            For Each ObjId In Layers
                PurgeCollection.Add(ObjId)
            Next
    
            For Each ObjId In TextStyles
                PurgeCollection.Add(ObjId)
            Next
    
            For Each ObjId In LineTypes
                PurgeCollection.Add(ObjId)
            Next
    
            Database.Purge(PurgeCollection)
            NumberPurged(0) = PurgeCollection.Count
    
            For Each ObjId In PurgeCollection
                Dim Obj As AcadDb.DBObject = Trans.GetObject(ObjId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, True, True)
                Obj.Erase()
            Next
    
            ' Check if there where more items purged from the last attempt or not.
            If NumberPurged(0) = NumberPurged(1) Then
                ' No new items purged so we exit the loop.
                Exit Do
            Else
                NumberPurged(1) = NumberPurged(0)
            End If
        Loop
    
    End Sub
    

  3. #3
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,397
    Login to Give a bone
    0

    Default Re: Purging Blocks in managed code?

    Thank you. I may be wrong but from what I've read, the purge method doesn't do anything other than let you know which items can be erased. I searches the db for dependencies and removes items from your collection that have dependencies. Its then up to you to do the actual deletion on the items still remaining in the collection. In my case, I already know. I just need to erase it.
    C:> ED WORKING....

  4. #4
    100 Club
    Join Date
    2000-11
    Location
    Adelaide, South Australia
    Posts
    116
    Login to Give a bone
    0

    Default Re: Purging Blocks in managed code?

    I have experienced the same problem but have not tried to hard to find a solution. Out of curiosity have you tried the purge method to see whether it indicates your item can be deleted.

    Regards - Nathan

  5. #5
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,397
    Login to Give a bone
    0

    Default Re: Purging Blocks in managed code?

    No. I'm working on a plot stamp program. I assumed that since I inserted the block on the BeginDocument event and erased it on the EndDocument event, that nothing else had a chance to reverence the file. I have a deadline to meet now, so I will leave the com interop fix for now. But I plan to go back later and figure this out. First thing I planned trying was to use the purge method. You know what happens when you assume something.
    C:> ED WORKING....

Similar Threads

  1. Replies: 0
    Last Post: 2013-09-19, 07:26 PM
  2. Finding and purging blocks not created by me
    By Many Irons in forum AutoCAD General
    Replies: 2
    Last Post: 2013-08-19, 07:37 PM
  3. Purging viral blocks
    By SCR73 in forum AMEP General
    Replies: 9
    Last Post: 2009-11-03, 02:23 PM
  4. Purging / Removing Blocks from a Template file
    By .chad in forum ACA General
    Replies: 14
    Last Post: 2006-09-26, 12:14 AM
  5. purge all not purging *__ (blocks)
    By michael.byrne in forum CAD Management - General
    Replies: 6
    Last Post: 2004-06-10, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •