<?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 how to copy block from an unopened  docment? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380645#M84346</link>
    <description>I tried many times and use several method to copy a block in an unopened file to current document. com method is ok,but net arx method always failed .who can help me and post a sample of net arx.&lt;BR /&gt;
&lt;BR /&gt;
the following is my code of arx method&lt;BR /&gt;
        private static ObjectId GetBlockArx(string blockFile, String name )&lt;BR /&gt;
        {&lt;BR /&gt;
            ObjectId block = ObjectId.Null;&lt;BR /&gt;
            Database db = new Database();&lt;BR /&gt;
            db.ReadDwgFile(blockFile, System.IO.FileShare.Read, false, null);&lt;BR /&gt;
            ArrayList list = new ArrayList();&lt;BR /&gt;
            Transaction dbTrans = db.TransactionManager.StartTransaction();  &lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                BlockTable dbbt = dbTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;&lt;BR /&gt;
                BlockTableRecord dbBtr = dbTrans.GetObject(dbbt[name], OpenMode.ForRead) as BlockTableRecord;&lt;BR /&gt;
                foreach (ObjectId entId in dbBtr)&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity ent = dbTrans.GetObject(entId, OpenMode.ForRead) as Entity;&lt;BR /&gt;
                    Entity newEnt = ent.Clone() as Entity;&lt;BR /&gt;
                    list.Add(newEnt);&lt;BR /&gt;
                }&lt;BR /&gt;
                dbTrans.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
             finally&lt;BR /&gt;
            {&lt;BR /&gt;
                dbTrans.Dispose();&lt;BR /&gt;
                db.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            Transaction trans = Arx.Tm.StartTransaction();  &lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                BlockTable bt = trans.GetObject(Arx.Db.BlockTableId, OpenMode.ForWrite) as BlockTable;&lt;BR /&gt;
                foreach (ObjectId idd in bt)&lt;BR /&gt;
                {&lt;BR /&gt;
                    BlockTableRecord btrr = trans.GetObject(idd, OpenMode.ForWrite) as BlockTableRecord;&lt;BR /&gt;
                    if (btrr.Name.ToUpper() == name.ToUpper())&lt;BR /&gt;
                    {&lt;BR /&gt;
                       btrr.Erase();&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                 BlockTableRecord newBtr = new BlockTableRecord();&lt;BR /&gt;
                newBtr.Name = name;&lt;BR /&gt;
                block = bt.Add(newBtr);&lt;BR /&gt;
                trans.AddNewlyCreatedDBObject(newBtr, true);&lt;BR /&gt;
                foreach (object obj in list)&lt;BR /&gt;
                {&lt;BR /&gt;
                    newBtr.AppendEntity(obj as Entity);&lt;BR /&gt;
                    trans.AddNewlyCreatedDBObject(obj as Entity, true);&lt;BR /&gt;
                    &lt;BR /&gt;
                }&lt;BR /&gt;
                foreach (ObjectId nentId in newBtr)&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity nent = trans.GetObject(nentId, OpenMode.ForRead) as Entity;&lt;BR /&gt;
                }&lt;BR /&gt;
                trans.Commit();  // a fatal error here&lt;BR /&gt;
            }&lt;BR /&gt;
             finally { trans.Dispose(); }&lt;BR /&gt;
&lt;BR /&gt;
            return block;&lt;BR /&gt;
        }</description>
    <pubDate>Fri, 15 Jul 2005 13:25:43 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2005-07-15T13:25:43Z</dc:date>
    <item>
      <title>how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380645#M84346</link>
      <description>I tried many times and use several method to copy a block in an unopened file to current document. com method is ok,but net arx method always failed .who can help me and post a sample of net arx.&lt;BR /&gt;
&lt;BR /&gt;
the following is my code of arx method&lt;BR /&gt;
        private static ObjectId GetBlockArx(string blockFile, String name )&lt;BR /&gt;
        {&lt;BR /&gt;
            ObjectId block = ObjectId.Null;&lt;BR /&gt;
            Database db = new Database();&lt;BR /&gt;
            db.ReadDwgFile(blockFile, System.IO.FileShare.Read, false, null);&lt;BR /&gt;
            ArrayList list = new ArrayList();&lt;BR /&gt;
            Transaction dbTrans = db.TransactionManager.StartTransaction();  &lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                BlockTable dbbt = dbTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;&lt;BR /&gt;
                BlockTableRecord dbBtr = dbTrans.GetObject(dbbt[name], OpenMode.ForRead) as BlockTableRecord;&lt;BR /&gt;
                foreach (ObjectId entId in dbBtr)&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity ent = dbTrans.GetObject(entId, OpenMode.ForRead) as Entity;&lt;BR /&gt;
                    Entity newEnt = ent.Clone() as Entity;&lt;BR /&gt;
                    list.Add(newEnt);&lt;BR /&gt;
                }&lt;BR /&gt;
                dbTrans.Commit();&lt;BR /&gt;
            }&lt;BR /&gt;
             finally&lt;BR /&gt;
            {&lt;BR /&gt;
                dbTrans.Dispose();&lt;BR /&gt;
                db.Dispose();&lt;BR /&gt;
            }&lt;BR /&gt;
&lt;BR /&gt;
            Transaction trans = Arx.Tm.StartTransaction();  &lt;BR /&gt;
            try&lt;BR /&gt;
            {&lt;BR /&gt;
                BlockTable bt = trans.GetObject(Arx.Db.BlockTableId, OpenMode.ForWrite) as BlockTable;&lt;BR /&gt;
                foreach (ObjectId idd in bt)&lt;BR /&gt;
                {&lt;BR /&gt;
                    BlockTableRecord btrr = trans.GetObject(idd, OpenMode.ForWrite) as BlockTableRecord;&lt;BR /&gt;
                    if (btrr.Name.ToUpper() == name.ToUpper())&lt;BR /&gt;
                    {&lt;BR /&gt;
                       btrr.Erase();&lt;BR /&gt;
                    }&lt;BR /&gt;
                }&lt;BR /&gt;
&lt;BR /&gt;
                 BlockTableRecord newBtr = new BlockTableRecord();&lt;BR /&gt;
                newBtr.Name = name;&lt;BR /&gt;
                block = bt.Add(newBtr);&lt;BR /&gt;
                trans.AddNewlyCreatedDBObject(newBtr, true);&lt;BR /&gt;
                foreach (object obj in list)&lt;BR /&gt;
                {&lt;BR /&gt;
                    newBtr.AppendEntity(obj as Entity);&lt;BR /&gt;
                    trans.AddNewlyCreatedDBObject(obj as Entity, true);&lt;BR /&gt;
                    &lt;BR /&gt;
                }&lt;BR /&gt;
                foreach (ObjectId nentId in newBtr)&lt;BR /&gt;
                {&lt;BR /&gt;
                    Entity nent = trans.GetObject(nentId, OpenMode.ForRead) as Entity;&lt;BR /&gt;
                }&lt;BR /&gt;
                trans.Commit();  // a fatal error here&lt;BR /&gt;
            }&lt;BR /&gt;
             finally { trans.Dispose(); }&lt;BR /&gt;
&lt;BR /&gt;
            return block;&lt;BR /&gt;
        }</description>
      <pubDate>Fri, 15 Jul 2005 13:25:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380645#M84346</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-07-15T13:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380646#M84347</link>
      <description>Here is a VB example based on what was posted by Albert Szilvasy.&lt;BR /&gt;
&lt;BR /&gt;
&amp;lt;CommandMethod("BlkTest")&amp;gt; _&lt;BR /&gt;
Public Sub blkTest()&lt;BR /&gt;
Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
Dim ed As Editor = doc.Editor&lt;BR /&gt;
Dim tr As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
Try&lt;BR /&gt;
Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myLines.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)&lt;BR /&gt;
Dim db As Database = New Database(False, False)&lt;BR /&gt;
db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")&lt;BR /&gt;
Dim NewBlkId As ObjectId&lt;BR /&gt;
NewBlkId = doc.Database.Insert(dwgName, db, False)&lt;BR /&gt;
Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)&lt;BR /&gt;
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)&lt;BR /&gt;
Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), NewBlkId)&lt;BR /&gt;
btr.AppendEntity(bref)&lt;BR /&gt;
tr.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
tr.Commit()&lt;BR /&gt;
Catch ex As Exception&lt;BR /&gt;
ed.WriteMessage(ex.ToString)&lt;BR /&gt;
End Try&lt;BR /&gt;
End Sub</description>
      <pubDate>Fri, 15 Jul 2005 18:17:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380646#M84347</guid>
      <dc:creator>Mikko</dc:creator>
      <dc:date>2005-07-15T18:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380647#M84348</link>
      <description>what I mean is to insert a block in an unopened drawing to current drawing, not to insert an unopened drawing as a block to current drawing.</description>
      <pubDate>Sat, 16 Jul 2005 06:01:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380647#M84348</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2005-07-16T06:01:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380648#M84349</link>
      <description>myTest.dwg has a block called circles in it.&lt;BR /&gt;
&lt;BR /&gt;
&amp;lt;CommandMethod("blkInblk")&amp;gt; _&lt;BR /&gt;
Public Sub blkInblk()&lt;BR /&gt;
Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
Dim ed As Editor = doc.Editor&lt;BR /&gt;
Dim tr As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
Try&lt;BR /&gt;
Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myTest.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)&lt;BR /&gt;
Dim db As Database = New Database(False, False)&lt;BR /&gt;
db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")&lt;BR /&gt;
Dim takeBlkId As ObjectId&lt;BR /&gt;
Dim tbt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)&lt;BR /&gt;
If (tbt.Has("Circles")) Then&lt;BR /&gt;
takeBlkId = tbt("Circles")&lt;BR /&gt;
Else&lt;BR /&gt;
ed.WriteMessage("This aint happening")&lt;BR /&gt;
Exit Sub&lt;BR /&gt;
End If&lt;BR /&gt;
Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, True)&lt;BR /&gt;
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)&lt;BR /&gt;
Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), takeBlkId)&lt;BR /&gt;
btr.AppendEntity(bref)&lt;BR /&gt;
tr.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
tr.Commit()&lt;BR /&gt;
Catch ex As Exception&lt;BR /&gt;
ed.WriteMessage(ex.ToString)&lt;BR /&gt;
Finally&lt;BR /&gt;
tr.Dispose()&lt;BR /&gt;
End Try&lt;BR /&gt;
End Sub</description>
      <pubDate>Sat, 16 Jul 2005 11:37:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380648#M84349</guid>
      <dc:creator>Mikko</dc:creator>
      <dc:date>2005-07-16T11:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380649#M84350</link>
      <description>After looking at this a bit more, I find I get an fatal error when trying to reopen the drawing.&lt;BR /&gt;
Realised I didn't write the block definition so I revised the code somewhat but I'm still getting&lt;BR /&gt;
a fatal error.&lt;BR /&gt;
&lt;BR /&gt;
    &amp;lt;CommandMethod("blkInblk")&amp;gt; _&lt;BR /&gt;
    Public Sub blkInblk()&lt;BR /&gt;
        Dim bibId As ObjectId&lt;BR /&gt;
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument&lt;BR /&gt;
        Dim ed As Editor = doc.Editor&lt;BR /&gt;
        Dim tr As Transaction = doc.TransactionManager.StartTransaction&lt;BR /&gt;
        Try&lt;BR /&gt;
            Dim dwgName As String = HostApplicationServices.Current.FindFile("C:\temp\myTest.dwg", Application.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default)&lt;BR /&gt;
            Dim db As Database = New Database(False, False)&lt;BR /&gt;
            db.ReadDwgFile(dwgName, IO.FileShare.Read, True, "")&lt;BR /&gt;
            Dim takeBlkId As ObjectId&lt;BR /&gt;
            Dim tbt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)&lt;BR /&gt;
            If (tbt.Has("Circles")) Then&lt;BR /&gt;
                takeBlkId = tbt("Circles")&lt;BR /&gt;
            Else&lt;BR /&gt;
                ed.WriteMessage("This aint happening")&lt;BR /&gt;
                Exit Sub&lt;BR /&gt;
            End If&lt;BR /&gt;
            Dim bt As BlockTable = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite, True)&lt;BR /&gt;
            Dim newBTR As BlockTableRecord = New BlockTableRecord&lt;BR /&gt;
            newBTR.Name = "Circles"&lt;BR /&gt;
            bibId = bt.Add(newBTR)&lt;BR /&gt;
            tr.AddNewlyCreatedDBObject(newBTR, True)&lt;BR /&gt;
            Dim bref As BlockReference = New BlockReference(New Point3d(0.0, 0.0, 0.0), takeBlkId)&lt;BR /&gt;
            newBTR.AppendEntity(bref)&lt;BR /&gt;
            tr.AddNewlyCreatedDBObject(bref, True)&lt;BR /&gt;
            tr.Commit()&lt;BR /&gt;
        Catch ex As Exception&lt;BR /&gt;
            ed.WriteMessage(ex.ToString)&lt;BR /&gt;
        Finally&lt;BR /&gt;
            tr.Dispose()&lt;BR /&gt;
        End Try&lt;BR /&gt;
    End Sub&lt;BR /&gt;
&lt;BR /&gt;
FATAL ERROR: Unhandled Access Violation Reading 0x0019 Exception at 648fefd 1h&lt;BR /&gt;
&lt;BR /&gt;
Anybody see my error?</description>
      <pubDate>Sat, 16 Jul 2005 12:36:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380649#M84350</guid>
      <dc:creator>Mikko</dc:creator>
      <dc:date>2005-07-16T12:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380650#M84351</link>
      <description>my friend .i have the same problem with you .have you firgured oot the solution.&lt;BR /&gt;
please tell me if you have got it .</description>
      <pubDate>Fri, 23 May 2008 08:38:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380650#M84351</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2008-05-23T08:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to copy block from an unopened  docment?</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380651#M84352</link>
      <description>Attached is a snippet that I have had no issues with.&lt;BR /&gt;
&lt;BR /&gt;
What I did was use wblock to map a clone of the blocktable's entities from an external drawing database into the active database. If the external drawing didn't have a matching blocktable, the modelspace table is used instead as a default.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
ps: I wrote this function two years ago. it may not be correctly disposing the AcDb objects it uses, so be aware of that if you plan to copy/paste it into your code. Also, it's in VB.NET so if you want to use it, you will have to translate it into C#.</description>
      <pubDate>Fri, 23 May 2008 18:10:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-copy-block-from-an-unopened-docment/m-p/1380651#M84352</guid>
      <dc:creator>jbooth</dc:creator>
      <dc:date>2008-05-23T18:10:26Z</dc:date>
    </item>
  </channel>
</rss>

