<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Copy block definition from a file to the current drawing in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891580#M62139</link>
    <description>&lt;P&gt;I've seen lots of examples in this forum that do something similar, and a few google examples, but not many of them are similar - nor do any of them do specifically what I want. Some of them use deep clone, some of them use a wblockClone method, others iterate throught the sub objects of an external database and construct a new block.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am making a program that will insert a block. My hope is that I can check if the block table has the block, and if not insert the block definition from an external file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are three mangled attempts at doing this (all of which end up in run-time errors despite the try-catch statements).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My ideal result is:&lt;BR /&gt;If BT.Has("MyBlock") then&lt;BR /&gt;&amp;nbsp; '&amp;nbsp;&amp;nbsp; Go about my business&lt;BR /&gt;Else&lt;BR /&gt;&amp;nbsp; InsertBlock("Myfile.dwg","MyBlock")&lt;BR /&gt;end if&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("Test")&amp;gt; _
        Public Shared Sub Test()
            CopyBlockDef3("C:\myBlock.dwg", MySettings.BlockName)
        End Sub

        Public Shared Sub CopyBlockDef3(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim id As ObjectId = db.Insert(blockName, extDB, False)
                        tr.Commit()
                        extTr.Dispose()
                    End Using
                End Using
                db.Dispose()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try
        End Sub

        Public Shared Sub CopyBlockDef2(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)
                        If extBT.Has(blockName) Then
                            extDB.WblockCloneObjects(New ObjectIdCollection(New ObjectId() {extBT(blockName)}), db.BlockTableId, New IdMapping(), DuplicateRecordCloning.Replace, False)
                        End If
                        tr.Commit()
                        extTr.Abort()
                    End Using
                End Using
                db.Dispose()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try
        End Sub

        Public Shared Sub CopyBlockDef(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)

                        Dim objIds As ObjectIdCollection = New ObjectIdCollection()

                        objIds.Add(extBT.Item(blockName))

                        Dim idMap As IdMapping = New IdMapping

                        db.DeepCloneObjects(objIds, db.CurrentSpaceId, idMap, False)

                        'For Each idP As IdPair In idMap
                        '    Dim copyent As Entity = CType(tr.GetObject(idP.Value, OpenMode.ForRead), Entity)
                        'Next

                        ed.Regen()

                        db.Dispose()

                        tr.Commit()

                    End Using
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try

        End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2011 20:40:09 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2011-01-24T20:40:09Z</dc:date>
    <item>
      <title>Copy block definition from a file to the current drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891580#M62139</link>
      <description>&lt;P&gt;I've seen lots of examples in this forum that do something similar, and a few google examples, but not many of them are similar - nor do any of them do specifically what I want. Some of them use deep clone, some of them use a wblockClone method, others iterate throught the sub objects of an external database and construct a new block.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am making a program that will insert a block. My hope is that I can check if the block table has the block, and if not insert the block definition from an external file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below are three mangled attempts at doing this (all of which end up in run-time errors despite the try-catch statements).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My ideal result is:&lt;BR /&gt;If BT.Has("MyBlock") then&lt;BR /&gt;&amp;nbsp; '&amp;nbsp;&amp;nbsp; Go about my business&lt;BR /&gt;Else&lt;BR /&gt;&amp;nbsp; InsertBlock("Myfile.dwg","MyBlock")&lt;BR /&gt;end if&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&amp;lt;CommandMethod("Test")&amp;gt; _
        Public Shared Sub Test()
            CopyBlockDef3("C:\myBlock.dwg", MySettings.BlockName)
        End Sub

        Public Shared Sub CopyBlockDef3(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim id As ObjectId = db.Insert(blockName, extDB, False)
                        tr.Commit()
                        extTr.Dispose()
                    End Using
                End Using
                db.Dispose()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try
        End Sub

        Public Shared Sub CopyBlockDef2(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForWrite)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)
                        If extBT.Has(blockName) Then
                            extDB.WblockCloneObjects(New ObjectIdCollection(New ObjectId() {extBT(blockName)}), db.BlockTableId, New IdMapping(), DuplicateRecordCloning.Replace, False)
                        End If
                        tr.Commit()
                        extTr.Abort()
                    End Using
                End Using
                db.Dispose()
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try
        End Sub

        Public Shared Sub CopyBlockDef(ByVal blockFile As String, ByVal blockName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim extDB As New Database(False, True)
            extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Using extTr As Transaction = extDB.TransactionManager.StartTransaction
                        Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                        Dim extBT As BlockTable = extDB.BlockTableId.GetObject(OpenMode.ForRead)

                        Dim objIds As ObjectIdCollection = New ObjectIdCollection()

                        objIds.Add(extBT.Item(blockName))

                        Dim idMap As IdMapping = New IdMapping

                        db.DeepCloneObjects(objIds, db.CurrentSpaceId, idMap, False)

                        'For Each idP As IdPair In idMap
                        '    Dim copyent As Entity = CType(tr.GetObject(idP.Value, OpenMode.ForRead), Entity)
                        'Next

                        ed.Regen()

                        db.Dispose()

                        tr.Commit()

                    End Using
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)
            End Try

        End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2011 20:40:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891580#M62139</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-01-24T20:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: Copy block definition from a file to the current drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891650#M62140</link>
      <description>&lt;P&gt;Looks like I should have tried just one more search before posting. The article linked below contains a very helpful class that seems to work great (even after being converted to VB.Net from an online converter.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="http://forums.autodesk.com/t5/NET/Code-to-insert-a-dynamic-block-into-the-current-drawing/td-p/2798420/highlight/true" rel="nofollow" target="_self"&gt;http://forums.autodesk.com/t5/NET/Code-to-insert-a-dynamic-block-into-the-current-drawing/td-p/2798420/highlight/true&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Looks like i was close on some of my attempts, but they still need work on transaction and object management.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2011 21:22:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891650#M62140</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-01-24T21:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: Copy block definition from a file to the current drawing</title>
      <link>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891682#M62141</link>
      <description>&lt;BLOCKQUOTE&gt;Try this version, with un-needed stuff removed, (and I think the primary problem, don't dispose the working database)&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Shared Sub CopyBlockDef3(ByVal blockFile As String, ByVal blockName As String)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim db As Database = HostApplicationServices.WorkingDatabase&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim extDB As New Database(False, True)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extDB.ReadDwgFile(blockFile, IO.FileShare.Read, True, "")&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Try&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim id As ObjectId = db.Insert(blockName, extDB, False)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Catch ex As Autodesk.AutoCAD.Runtime.Exception&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MsgBox(ex.Message &amp;amp; vbCr &amp;amp; ex.StackTrace)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Finally&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extDB.CloseInput(True)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extDB.Dispose&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Try&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 24 Jan 2011 21:46:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/copy-block-definition-from-a-file-to-the-current-drawing/m-p/2891682#M62141</guid>
      <dc:creator>chiefbraincloud</dc:creator>
      <dc:date>2011-01-24T21:46:26Z</dc:date>
    </item>
  </channel>
</rss>

