<?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: Enter dwg into current document in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6349511#M35990</link>
    <description>&lt;P&gt;Thank you for your help. Finally I combined your code and this&amp;nbsp;&lt;A href="http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-72029044-0840-4187-9A58-F2A4518E3A23&amp;nbsp;" target="_blank"&gt;http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-72029044-0840-4187-9A58-F2A4518E3A23&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Firstly I read input file&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);

            var ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("Select a dwg file to insert", documents,
                            "dwg; *",
                            "DialogName",
                            OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
                            OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder // .AllowMultiple
                          );
            System.Windows.Forms.DialogResult sdResult = ofd.ShowDialog();

            if (sdResult != System.Windows.Forms.DialogResult.OK) return;

            filePath = ofd.Filename;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;Then I insert it as blockreference&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;// Get the current database and start a transaction
            Database acCurDb;
            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;


            var id = ObjectId.Null;
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(filePath, System.IO.FileShare.Read, false, null);
                id = acCurDb.Insert("Frame", sourceDb, true);
            }
            using (var tr = acCurDb.TransactionManager.StartTransaction())
            {
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = tr.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                BlockReference br = new BlockReference(Point3d.Origin, id);
                acBlkTblRec.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                tr.Commit();
            }&lt;/PRE&gt;&lt;/LI-SPOILER&gt;</description>
    <pubDate>Wed, 25 May 2016 22:30:41 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-05-25T22:30:41Z</dc:date>
    <item>
      <title>Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6339194#M35984</link>
      <description>&lt;P&gt;I try to enter existing dwg in current document. Then I want to draw some entities. But there is exception&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/241070iD05BA20BE38CA13C/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="1234.jpg" title="1234.jpg" /&gt;&lt;/P&gt;&lt;P&gt;at&lt;/P&gt;&lt;PRE&gt; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                            OpenMode.ForWrite) as BlockTableRecord;&lt;/PRE&gt;&lt;P&gt;Full listing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;[CommandMethod("DrawScheme")]
        public void DrawScheme()
        {
            List&amp;lt;Entity&amp;gt; entitiesToDraw = new List&amp;lt;Entity&amp;gt;();
            

            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Database FrameDb = new Database(false, true);

            FrameDb.ReadDwgFile("Большая Рамка.dwg",
                            System.IO.FileShare.Read,
                            true,
                            "");
            // Create a variable to store the list of block identifiers
            ObjectIdCollection blockIds = new ObjectIdCollection();

            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
          FrameDb.TransactionManager;
            //insert frame from file
            using (Transaction myT = tm.StartTransaction())
            {
                // Open the block table
                BlockTable bt =
                    (BlockTable)tm.GetObject(FrameDb.BlockTableId,
                                            OpenMode.ForRead,
                                            false);

                // Check each block in the block table
                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr =
                      (BlockTableRecord)tm.GetObject(btrId,
                                                    OpenMode.ForRead,
                                                    false);
                    blockIds.Add(btrId);
                    btr.Dispose();
                }
                myT.Commit();
            }
            // Copy blocks from source to destination database
            IdMapping mapping = new IdMapping();
            FrameDb.WblockCloneObjects(blockIds,
                                        acCurDb.BlockTableId,
                                        mapping,
                                        DuplicateRecordCloning.Replace,
                                        false);
            //FrameDb.Dispose();

            // Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                                         OpenMode.ForRead) as BlockTable;

                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                            OpenMode.ForWrite) as BlockTableRecord;

                entitiesToDraw = SchemeDraw.getSchemeEntities(acBlkTblRec,acTrans);

                // Add the new object to the block table record and the transaction
                foreach (Entity currEntity in entitiesToDraw)
                {
                    //Because polyline3d is **** and i add it in SchemeDraw.getSchemeEntities method
                    if (currEntity is Polyline3d)
                        continue;
                    acBlkTblRec.AppendEntity(currEntity);
                    acTrans.AddNewlyCreatedDBObject(currEntity, true);
                }

                // Save the new line to the database
                acTrans.Commit();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 May 2016 00:05:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6339194#M35984</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-05-20T00:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6339532#M35985</link>
      <description>&lt;P&gt;you copy all the blocks from FrameDb into the current Database, including Modelspace and Layouts.&lt;/P&gt;&lt;P&gt;Filter on block.IsLayout&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2016 06:15:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6339532#M35985</guid>
      <dc:creator>SENL1362</dc:creator>
      <dc:date>2016-05-20T06:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6342950#M35986</link>
      <description>&lt;P&gt;Ok, this helped with exception. But nothing is drawn. And don't know if to dispose database at the end.&lt;/P&gt;&lt;PRE&gt;[CommandMethod("DrawFrame")]
        public void DrawFrame()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Database FrameDb = new Database(false, true);

            FrameDb.ReadDwgFile("BigFrame.dwg",
                            System.IO.FileShare.Read,
                            true,
                            "");
            // Create a variable to store the list of block identifiers
            ObjectIdCollection blockIds = new ObjectIdCollection();

            Autodesk.AutoCAD.DatabaseServices.TransactionManager tm =
          FrameDb.TransactionManager;
            //insert frame from file
            using (Transaction myT = tm.StartTransaction())
            {
                // Open the block table
                BlockTable bt =
                    (BlockTable)tm.GetObject(FrameDb.BlockTableId,
                                            OpenMode.ForRead,
                                            false);

                // Check each block in the block table
                foreach (ObjectId btrId in bt)
                {
                    BlockTableRecord btr =
                      (BlockTableRecord)tm.GetObject(btrId,
                                                    OpenMode.ForRead,
                                                    false);
                    if (!btr.IsLayout)
                        blockIds.Add(btrId);

                    //btr.Dispose();
                }
                myT.Commit();
            }
            // Copy blocks from source to destination database
            IdMapping mapping = new IdMapping();
            FrameDb.WblockCloneObjects(blockIds,
                                        acCurDb.BlockTableId,
                                        mapping,
                                        DuplicateRecordCloning.Replace,
                                        false);
            FrameDb.Dispose();
        }&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 May 2016 23:51:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6342950#M35986</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-05-22T23:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343315#M35987</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The last code you posted just clone each block definition (BlockTableRecord) from FrameDb to the current database block table.&lt;/P&gt;
&lt;P&gt;As BlockTableRecord is a non-graphical object, nothing is drawn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you try to clearly explain what you attempt to do when you say: "&lt;STRONG&gt;enter&lt;/STRONG&gt; existing dwg in current document".&lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 08:18:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343315#M35987</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-05-23T08:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343669#M35988</link>
      <description>&lt;P&gt;I have ready drawing in dwg file. I want to insert it into clear document and then to draw additional entities. So firstly will be this code with inserting drawing from outer dwg file and then will be code with drawing additional entities. To adjust this 2 drawings I also need to scale one of them. I need to insert frame and my code will be drawing lines inside this frame. So I need to scale frame or lines. To scale it I hope to use this&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/how-to-scale-drawing/m-p/5496608" target="_blank"&gt;https://forums.autodesk.com/t5/net/how-to-scale-drawing/m-p/5496608&lt;/A&gt; &amp;nbsp;though it's in visual basic&lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 12:18:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343669#M35988</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-05-23T12:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343684#M35989</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to copy/clone all entities from the source dwg model space into the current drawing model space, you can use something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        private void CloneModelSpaceEntities(string fileName)
        {
            var curDb = HostApplicationServices.WorkingDatabase;
            var ids = new ObjectIdCollection();
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(fileName, System.IO.FileShare.Read, false, null);
                using (var tr = sourceDb.TransactionManager.StartOpenCloseTransaction())
                {
                    var ms = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(sourceDb),
                        OpenMode.ForRead);
                    foreach (ObjectId id in ms) ids.Add(id);
                }
                var idMap = new IdMapping();
                sourceDb.WblockCloneObjects(
                    ids, 
                    SymbolUtilityServices.GetBlockModelSpaceId(curDb), 
                    idMap, 
                    DuplicateRecordCloning.Ignore, 
                    false);
            }
        }&lt;/PRE&gt;
&lt;P&gt;If you want to insert the source dwg model space as a block reference into the current drawing model space (which may be easier to scale), you can use something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        private void InsertModelSpace(string fileName)
        {
            var curDb = HostApplicationServices.WorkingDatabase;
            var id = ObjectId.Null;
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(fileName, System.IO.FileShare.Read, false, null);
                id = curDb.Insert("Test", sourceDb, true);
            }
            using (var tr = curDb.TransactionManager.StartTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(curDb), 
                    OpenMode.ForWrite);
                var br = new BlockReference(Point3d.Origin, id);
                ms.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                tr.Commit();
            }
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 May 2016 12:43:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6343684#M35989</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2016-05-23T12:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Enter dwg into current document</title>
      <link>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6349511#M35990</link>
      <description>&lt;P&gt;Thank you for your help. Finally I combined your code and this&amp;nbsp;&lt;A href="http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-72029044-0840-4187-9A58-F2A4518E3A23&amp;nbsp;" target="_blank"&gt;http://help.autodesk.com/view/ACD/2016/ENU/?guid=GUID-72029044-0840-4187-9A58-F2A4518E3A23&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Firstly I read input file&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);

            var ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("Select a dwg file to insert", documents,
                            "dwg; *",
                            "DialogName",
                            OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder |
                            OpenFileDialog.OpenFileDialogFlags.ForceDefaultFolder // .AllowMultiple
                          );
            System.Windows.Forms.DialogResult sdResult = ofd.ShowDialog();

            if (sdResult != System.Windows.Forms.DialogResult.OK) return;

            filePath = ofd.Filename;&lt;/PRE&gt;&lt;/LI-SPOILER&gt;&lt;P&gt;Then I insert it as blockreference&lt;/P&gt;&lt;LI-SPOILER&gt;&lt;PRE&gt;// Get the current database and start a transaction
            Database acCurDb;
            acCurDb = Application.DocumentManager.MdiActiveDocument.Database;


            var id = ObjectId.Null;
            using (var sourceDb = new Database(false, true))
            {
                sourceDb.ReadDwgFile(filePath, System.IO.FileShare.Read, false, null);
                id = acCurDb.Insert("Frame", sourceDb, true);
            }
            using (var tr = acCurDb.TransactionManager.StartTransaction())
            {
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = tr.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                BlockReference br = new BlockReference(Point3d.Origin, id);
                acBlkTblRec.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                tr.Commit();
            }&lt;/PRE&gt;&lt;/LI-SPOILER&gt;</description>
      <pubDate>Wed, 25 May 2016 22:30:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/enter-dwg-into-current-document/m-p/6349511#M35990</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-05-25T22:30:41Z</dc:date>
    </item>
  </channel>
</rss>

