<?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 Re: Nobody seems to have solved the insert block problem in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402712#M84133</link>
    <description>I converted your code to VB because I know and needed then got rid of error by specifying file extension.&lt;BR /&gt;
&lt;BR /&gt;
You'll probably want to add more testing to check that the file exists or error trapping around FindFile part to prevent error.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Good luck.&lt;BR /&gt;
&lt;BR /&gt;
   &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
   Sub altBlkInsert()&lt;BR /&gt;
      Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
      Dim fname As String = "c:\MyBlockFile.dwg"&lt;BR /&gt;
      HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default)&lt;BR /&gt;
      ' Using &lt;BR /&gt;
      Dim db As Database = New Database(False, False)&lt;BR /&gt;
      Try&lt;BR /&gt;
         db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)&lt;BR /&gt;
         ' Using &lt;BR /&gt;
         Dim t As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
         Try&lt;BR /&gt;
            Dim idBTR As ObjectId = doc.Database.Insert("MyBlockFile", db, False)&lt;BR /&gt;
            Dim bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)&lt;BR /&gt;
            Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)&lt;BR /&gt;
            Dim origin As Point3d = New Point3d(0, 0, 0)&lt;BR /&gt;
            ' Using &lt;BR /&gt;
            Dim bref As BlockReference = New BlockReference(origin, idBTR)&lt;BR /&gt;
            Try&lt;BR /&gt;
               btr.AppendEntity(bref)&lt;BR /&gt;
               t.TransactionManager.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
            Finally&lt;BR /&gt;
               CType(bref, IDisposable).Dispose()&lt;BR /&gt;
            End Try&lt;BR /&gt;
            t.Commit()&lt;BR /&gt;
            t.Dispose()&lt;BR /&gt;
         Finally&lt;BR /&gt;
            CType(t, IDisposable).Dispose()&lt;BR /&gt;
         End Try&lt;BR /&gt;
      Finally&lt;BR /&gt;
         CType(db, IDisposable).Dispose()&lt;BR /&gt;
      End Try&lt;BR /&gt;
   End Sub&lt;/COMMANDMETHOD&gt;</description>
    <pubDate>Fri, 12 Aug 2005 16:31:21 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2005-08-12T16:31:21Z</dc:date>
    <item>
      <title>Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402711#M84132</link>
      <description>Hi again (I know, I post like crazy.  Sorry!),&lt;BR /&gt;
&lt;BR /&gt;
There have been many threads about inserting a block (WBLOCKed as a .dwg &lt;BR /&gt;
file) into a drawing, but the final post in each thread is always the same - &lt;BR /&gt;
they result in a fatal error.  Has anyone solved this problem?  The method &lt;BR /&gt;
that all of the attempts suggest is along these lines:&lt;BR /&gt;
&lt;BR /&gt;
Document doc=Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;
string fname = "PathAndFilenameOfBlockFile";&lt;BR /&gt;
HostApplicationServices.Current.FindFile(fname, doc.Database,&lt;BR /&gt;
                                                                FindFileHint.Default);&lt;BR /&gt;
&lt;BR /&gt;
using(Database db = new Database(false, false))&lt;BR /&gt;
{&lt;BR /&gt;
  db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);&lt;BR /&gt;
  using(Transaction t = doc.TransactionManager.StartTransaction())&lt;BR /&gt;
  {&lt;BR /&gt;
     ObjectId idBTR = doc.Database.Insert("BlockName",db,false);&lt;BR /&gt;
     BlockTable bt = (BlockTable)t.GetObject&lt;BR /&gt;
                                                    (doc.Database.BlockTableId,&lt;BR /&gt;
                                                    OpenMode.ForRead);&lt;BR /&gt;
     BlockTableRecord btr = (BlockTableRecord)t.GetObject&lt;BR /&gt;
             (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);&lt;BR /&gt;
&lt;BR /&gt;
     Point3d origin = new Point3d(0, 0, 0);&lt;BR /&gt;
     using(BlockReference bref = new BlockReference(origin, idBTR))&lt;BR /&gt;
     {&lt;BR /&gt;
        btr.AppendEntity(bref);&lt;BR /&gt;
        t.TransactionManager.AddNewlyCreatedDBObject(bref, true);&lt;BR /&gt;
      }&lt;BR /&gt;
      t.Commit();&lt;BR /&gt;
      t.Dispose();&lt;BR /&gt;
      }&lt;BR /&gt;
}&lt;BR /&gt;
&lt;BR /&gt;
The fatal error is:&lt;BR /&gt;
&lt;BR /&gt;
    Unhandled Access Violation Reading 0xfffffff Exception&lt;BR /&gt;
                                at 7ef399f9h&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Carlos</description>
      <pubDate>Wed, 10 Aug 2005 23:42:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402711#M84132</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-08-10T23:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402712#M84133</link>
      <description>I converted your code to VB because I know and needed then got rid of error by specifying file extension.&lt;BR /&gt;
&lt;BR /&gt;
You'll probably want to add more testing to check that the file exists or error trapping around FindFile part to prevent error.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Good luck.&lt;BR /&gt;
&lt;BR /&gt;
   &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
   Sub altBlkInsert()&lt;BR /&gt;
      Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
      Dim fname As String = "c:\MyBlockFile.dwg"&lt;BR /&gt;
      HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default)&lt;BR /&gt;
      ' Using &lt;BR /&gt;
      Dim db As Database = New Database(False, False)&lt;BR /&gt;
      Try&lt;BR /&gt;
         db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)&lt;BR /&gt;
         ' Using &lt;BR /&gt;
         Dim t As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
         Try&lt;BR /&gt;
            Dim idBTR As ObjectId = doc.Database.Insert("MyBlockFile", db, False)&lt;BR /&gt;
            Dim bt As BlockTable = CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)&lt;BR /&gt;
            Dim btr As BlockTableRecord = CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)&lt;BR /&gt;
            Dim origin As Point3d = New Point3d(0, 0, 0)&lt;BR /&gt;
            ' Using &lt;BR /&gt;
            Dim bref As BlockReference = New BlockReference(origin, idBTR)&lt;BR /&gt;
            Try&lt;BR /&gt;
               btr.AppendEntity(bref)&lt;BR /&gt;
               t.TransactionManager.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
            Finally&lt;BR /&gt;
               CType(bref, IDisposable).Dispose()&lt;BR /&gt;
            End Try&lt;BR /&gt;
            t.Commit()&lt;BR /&gt;
            t.Dispose()&lt;BR /&gt;
         Finally&lt;BR /&gt;
            CType(t, IDisposable).Dispose()&lt;BR /&gt;
         End Try&lt;BR /&gt;
      Finally&lt;BR /&gt;
         CType(db, IDisposable).Dispose()&lt;BR /&gt;
      End Try&lt;BR /&gt;
   End Sub&lt;/COMMANDMETHOD&gt;</description>
      <pubDate>Fri, 12 Aug 2005 16:31:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402712#M84133</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-08-12T16:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402713#M84134</link>
      <description>I've actually got the file extension in the filename, but I'll try adding &lt;BR /&gt;
more error trapping.&lt;BR /&gt;
&lt;BR /&gt;
I also noticed that you dispose of things like the bref and db, which I &lt;BR /&gt;
wasn't doing.  I'll add that stuff too.&lt;BR /&gt;
&lt;BR /&gt;
Thanks very much for your help.  I really appreciate that you would take the &lt;BR /&gt;
time.&lt;BR /&gt;
&lt;BR /&gt;
-Carlos&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;MR_MELLOW&gt; wrote in message news:4927502@discussion.autodesk.com...&lt;BR /&gt;
I converted your code to VB because I know and needed then got rid of error &lt;BR /&gt;
by specifying file extension.&lt;BR /&gt;
&lt;BR /&gt;
You'll probably want to add more testing to check that the file exists or &lt;BR /&gt;
error trapping around FindFile part to prevent error.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Good luck.&lt;BR /&gt;
&lt;BR /&gt;
   &lt;COMMANDMETHOD&gt; _&lt;BR /&gt;
   Sub altBlkInsert()&lt;BR /&gt;
      Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
      Dim fname As String = "c:\MyBlockFile.dwg"&lt;BR /&gt;
      HostApplicationServices.Current.FindFile(fname, doc.Database, &lt;BR /&gt;
FindFileHint.Default)&lt;BR /&gt;
      ' Using&lt;BR /&gt;
      Dim db As Database = New Database(False, False)&lt;BR /&gt;
      Try&lt;BR /&gt;
         db.ReadDwgFile(fname, System.IO.FileShare.Read, True, Nothing)&lt;BR /&gt;
         ' Using&lt;BR /&gt;
         Dim t As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
         Try&lt;BR /&gt;
            Dim idBTR As ObjectId = doc.Database.Insert("MyBlockFile", db, &lt;BR /&gt;
False)&lt;BR /&gt;
            Dim bt As BlockTable = &lt;BR /&gt;
CType(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)&lt;BR /&gt;
            Dim btr As BlockTableRecord = &lt;BR /&gt;
CType(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), &lt;BR /&gt;
BlockTableRecord)&lt;BR /&gt;
            Dim origin As Point3d = New Point3d(0, 0, 0)&lt;BR /&gt;
            ' Using&lt;BR /&gt;
            Dim bref As BlockReference = New BlockReference(origin, idBTR)&lt;BR /&gt;
            Try&lt;BR /&gt;
               btr.AppendEntity(bref)&lt;BR /&gt;
               t.TransactionManager.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
            Finally&lt;BR /&gt;
               CType(bref, IDisposable).Dispose()&lt;BR /&gt;
            End Try&lt;BR /&gt;
            t.Commit()&lt;BR /&gt;
            t.Dispose()&lt;BR /&gt;
         Finally&lt;BR /&gt;
            CType(t, IDisposable).Dispose()&lt;BR /&gt;
         End Try&lt;BR /&gt;
      Finally&lt;BR /&gt;
         CType(db, IDisposable).Dispose()&lt;BR /&gt;
      End Try&lt;BR /&gt;
   End Sub&lt;/COMMANDMETHOD&gt;&lt;/MR_MELLOW&gt;</description>
      <pubDate>Fri, 12 Aug 2005 17:38:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402713#M84134</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-08-12T17:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402714#M84135</link>
      <description>The error is not from inserting a drawing into a drawing, that works fine. It happens when you try to insert a block from an unopened drawing that has several blocks in it and you only want to insert one specified block from that drawing. Anyhow that is what my experience has been.  See http://discussion.autodesk.com/thread.jspa?threadID=416114 for some code that I've tried.</description>
      <pubDate>Fri, 12 Aug 2005 19:17:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402714#M84135</guid>
      <dc:creator>Mikko</dc:creator>
      <dc:date>2005-08-12T19:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402715#M84136</link>
      <description>Hi!&lt;BR /&gt;
&lt;BR /&gt;
I also had the problem with inserting a block from one drawing to another. I solved it with an Lisp-Script, which my .net-code has called.&lt;BR /&gt;
&lt;BR /&gt;
But then I found the following manual in the Autodesk-Blogs:&lt;BR /&gt;
http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html&lt;BR /&gt;
&lt;BR /&gt;
... and it is so simple and wonderful. It works perfekt!</description>
      <pubDate>Thu, 24 Aug 2006 18:39:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402715#M84136</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2006-08-24T18:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Nobody seems to have solved the insert block problem</title>
      <link>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402716#M84137</link>
      <description>dear mabe2k .&lt;BR /&gt;
can you share me the method code :&lt;BR /&gt;
i can't enter into the wetsite you recommend.&lt;BR /&gt;
thanks at advance!</description>
      <pubDate>Fri, 23 May 2008 07:45:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/nobody-seems-to-have-solved-the-insert-block-problem/m-p/1402716#M84137</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2008-05-23T07:45:09Z</dc:date>
    </item>
  </channel>
</rss>

