Results 1 to 4 of 4

Thread: Delete block

  1. #1
    Member
    Join Date
    2011-02
    Posts
    17
    Login to Give a bone
    0

    Default Delete block

    Hi
    I want to create a program witch delete blocks (Block defitions and block refrence) in multiple drawings.
    Here is my code:
    Code:
    Public Class Form1
    
        
    
    
        Function GetTopLevelBlocks(ByVal DatabaseIn As Database) As List(Of String)
            Dim myList As New List(Of String)
            Using myTrans As Transaction = DatabaseIn.TransactionManager.StartTransaction
                Dim myBT As BlockTable = DatabaseIn.BlockTableId.GetObject(OpenMode.ForRead)
                For Each myBTRid As ObjectId In myBT
                    Dim myBTR As BlockTableRecord = myBTRid.GetObject(OpenMode.ForRead)
                    If myBTR.IsAnonymous = False And myBTR.IsLayout = False And _
        myBTR.IsFromExternalReference = False And _
        myBTR.IsDependent = False Then
                        myList.Add(myBTR.Name)
                    End If
                Next
            End Using
            Return myList
        End Function
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myOFD As New OpenFileDialog("Select DWG", "", "dwg", "Select DWG", _
            OpenFileDialog.OpenFileDialogFlags.AllowMultiple)
            If myOFD.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                For Each myFileName As String In myOFD.GetFilenames
                    'Debug.Print("***EXPORT FOR " & myFileName)
                    Dim myDB As New Database(False, True)
                    myDB.ReadDwgFile(myFileName, FileOpenMode.OpenForReadAndAllShare, True, "")
                    Using mytrans As Transaction = myDB.TransactionManager.StartTransaction
                        For Each myBlockName As String In GetTopLevelBlocks(myDB)
                            Debug.WriteLine(myBlockName)
                            Lblokova.Items.Add(myBlockName)
                        Next
                    End Using
                    myDB.Dispose()
                Next
            End If
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            For i As Integer = 0 To Lblokova.SelectedItems.Count - 1
                
            Next
        End Sub
    
    End Class
    How I delete Block table selected in Listbox Lblokova, and all its references???

    Please help !!!!

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Delete block

    Just a pseudocode:
    Code:
      For Each myFileName As String In myOFD.GetFilenames
            
     Using Dim myDB As New Database(False, True)
     
                    myDB.ReadDwgFile(myFileName, FileOpenMode.OpenForReadAndAllShare, True, "")
                    Using mytrans As Transaction = myDB.TransactionManager.StartTransaction
                        For Each myBlockName As String In GetTopLevelBlocks(myDB)
                   Using myBt as BlockTable= mytrans.GetObject(myDB.BlockTableId,OpenMode.ForRead)
                    If myBt.Has(myBlockName) then
                            Dim btrId as ObjectID=myBt(myBlockName)
    
     Using myBtr as BlockTableRecord=mytrans.GetObject(btrId ,OpenMode.ForRead)
    ' ' you probably might be want to check before if it's dynamic:
    ' ' if myBtr.IsDynamicBlock.... etc...
    ' ' and also you need to check if BlockReferece layer is not locked etc...
    ' ' First delete all block references
     Dim blkIds as objectidCollection= myBtr.GetBlockReferenceIds
    
    For each blkID as objectid in blkIds
    Using  bref as blockreference= mytrans.GetObject(blkID  ,OpenMode.ForWrite)
    bref.erase()
    End Using
    Next
    End If
    ' ' Now delete the block record itself
    myBtr.UpgradeOpen()
    myBtr.Erase()
    End if
    End Using
    
    Next
    mytrans.Commit()
    End Using ' ' transaction
    myDB.SaveAs(myFileName,DwgVersion.Current)'<-- 2009
    myDB.SaveAs(myFileName,DwgVersion.Current,Database.SecurityParameters)'<-- 2010?
    End Using ' ' myDB
    
    Next ' ' myFileName
    Note
    You do not have to use obj.Dispose() inside statement: Using obj as .... End Using
    And at the end
    Hence I was writing this pseudocode in Notepad
    so you have to check code syntax by yourself

  3. #3
    Member
    Join Date
    2011-02
    Posts
    17
    Login to Give a bone
    0

    Default Re: Delete block

    Thank you very much fixo!!!! I will try it when I get home.

  4. #4
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Delete block

    Quote Originally Posted by krkeec763189 View Post
    Thank you very much fixo!!!! I will try it when I get home.
    I was playing a little with your code
    Assuming Lblokova is listbox with block names
    you want to delete
    Here is working part, tested on 2009:
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'populate list of blocks to be deleted:
            Dim names As List(Of String) = New List(Of String)(New String() {"Block1", "Block2", "Block3", "BlockETC"})
            'Or use like this instead
            'Dim names As New List(Of String)
            'names.Add("Block1")
            'names.Add("Block2")
            'names.Add("Block3")
            Me.Lblokova.DataSource = names
            Me.Lblokova.ClearSelected()
        End Sub
     
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.Lblokova.SelectedItems.Count = 0 Then
                MessageBox.Show("Select blocks from listbox first")
                Return
            End If
            Try
                Dim myOFD As New Autodesk.AutoCAD.Windows.OpenFileDialog("Select DWG", "", "dwg", "Select DWG", _
            Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple)
                If myOFD.ShowDialog = System.Windows.Forms.DialogResult.OK Then
                    For Each myFileName As String In myOFD.GetFilenames
                        Using myDB As New Database(False, True)
                            myDB.ReadDwgFile(myFileName, FileOpenMode.OpenForReadAndAllShare, True, "")
                            Using mytrans As Transaction = myDB.TransactionManager.StartTransaction
                                Using myBt As BlockTable = mytrans.GetObject(myDB.BlockTableId, OpenMode.ForRead)
                                    For i = 0 To Me.Lblokova.SelectedItems.Count - 1
                                        Dim myBlockName As String = Me.Lblokova.SelectedItems.Item(i).ToString
                                        If myBt.Has(myBlockName) Then
                                            Dim btrId As ObjectId = myBt(myBlockName)
                                            Using myBtr As BlockTableRecord = mytrans.GetObject(btrId, OpenMode.ForRead)
                                                ' ' you probably might be want to check before if it's dynamic:
                                                ' ' if myBtr.IsDynamicBlock.... etc...
                                                ' ' and also you need to check if BlockReferece layer is not locked etc...
                                                ' ' First delete all block references
                                                Dim blkIds As ObjectIdCollection = myBtr.GetBlockReferenceIds(True, True)
                                                For Each blkID As ObjectId In blkIds
                                                    Using bref As BlockReference = mytrans.GetObject(blkID, OpenMode.ForWrite)
                                                        bref.Erase()
                                                    End Using
                                                Next
                                                ' ' Now delete the block record itself
                                                myBtr.UpgradeOpen()
                                                myBtr.Erase()
                                            End Using
                                        End If
                                    Next
                                End Using
                                mytrans.Commit()
                            End Using
                            myDB.SaveAs(myFileName, DwgVersion.Current) '<-- 2009
                            'myDB.SaveAs(myFileName, DwgVersion.Current, Database.SecurityParameters) '<-- 2010?
                        End Using
                    Next
                End If
            Catch ex As System.Exception
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace)
            Finally
                MessageBox.Show("done")
                Me.Close()
            End Try
        End Sub

Similar Threads

  1. DELETE BLOCK CODE HELP
    By jayhay35365091 in forum AutoLISP
    Replies: 4
    Last Post: 2013-11-14, 01:16 AM
  2. delete elements outside a block
    By matthew.221135 in forum AutoCAD General
    Replies: 3
    Last Post: 2012-10-16, 07:09 AM
  3. Delete Block LSP
    By 1966ford in forum AutoCAD Customization
    Replies: 1
    Last Post: 2011-02-03, 01:24 PM
  4. Can you delete a mask block?
    By bercherdbrc138323 in forum ACA General
    Replies: 2
    Last Post: 2007-05-02, 01:52 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
  •