<?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 Trying to insert a block with C# in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981107#M26075</link>
    <description>&lt;P&gt;I am trying to insert a block that will not initially exist in the current drawing, but once inserted may be inserted multiple times.&amp;nbsp; I have code that determines the name of the block and the insertion point I need to insert from a directory and returns the block name to the routine that will insert the block.&amp;nbsp; It appears to work, except that the block never shows up.&amp;nbsp; I don't know what I am missing and could some suggestions.&lt;/P&gt;&lt;PRE&gt; class BlockInsert
    {
        public void InsertBlock(AcGeo.Point3d InsPt, string BlkName)
        {
            AppSvc.Document cDoc = App.DocumentManager.MdiActiveDocument;
            DBSvc.Database cDB = cDoc.Database;
            AcEdIn.Editor cEd = cDoc.Editor;

            using (DBSvc.Transaction Trans1 = cDB.TransactionManager.StartTransaction())
            {
                DBSvc.BlockTable blkTable;
                blkTable = Trans1.GetObject(cDB.BlockTableId, DBSvc.OpenMode.ForWrite) as DBSvc.BlockTable;
                DBSvc.BlockTableRecord blkTableRec = new DBSvc.BlockTableRecord();
                if (!blkTable.Has(BlkName))
                {
                    blkTable.Add(blkTableRec);
                    Trans1.AddNewlyCreatedDBObject(blkTableRec, true);
                    blkTableRec.Name = BlkName;
                }
                // Insert support block at insertion point InsPt
                DBSvc.BlockTableRecord space = (DBSvc.BlockTableRecord)Trans1.GetObject(cDB.CurrentSpaceId, DBSvc.OpenMode.ForWrite);
                DBSvc.BlockReference rfml = new DBSvc.BlockReference(InsPt, blkTableRec.ObjectId);
                space.AppendEntity(rfml);
                Trans1.AddNewlyCreatedDBObject(rfml, true);
                Trans1.Commit();
            }
            MessageBox.Show("\nThe block name is: " + BlkName, "BlockName", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }&lt;/PRE&gt;</description>
    <pubDate>Fri, 04 May 2018 18:37:38 GMT</pubDate>
    <dc:creator>sonny3g</dc:creator>
    <dc:date>2018-05-04T18:37:38Z</dc:date>
    <item>
      <title>Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981107#M26075</link>
      <description>&lt;P&gt;I am trying to insert a block that will not initially exist in the current drawing, but once inserted may be inserted multiple times.&amp;nbsp; I have code that determines the name of the block and the insertion point I need to insert from a directory and returns the block name to the routine that will insert the block.&amp;nbsp; It appears to work, except that the block never shows up.&amp;nbsp; I don't know what I am missing and could some suggestions.&lt;/P&gt;&lt;PRE&gt; class BlockInsert
    {
        public void InsertBlock(AcGeo.Point3d InsPt, string BlkName)
        {
            AppSvc.Document cDoc = App.DocumentManager.MdiActiveDocument;
            DBSvc.Database cDB = cDoc.Database;
            AcEdIn.Editor cEd = cDoc.Editor;

            using (DBSvc.Transaction Trans1 = cDB.TransactionManager.StartTransaction())
            {
                DBSvc.BlockTable blkTable;
                blkTable = Trans1.GetObject(cDB.BlockTableId, DBSvc.OpenMode.ForWrite) as DBSvc.BlockTable;
                DBSvc.BlockTableRecord blkTableRec = new DBSvc.BlockTableRecord();
                if (!blkTable.Has(BlkName))
                {
                    blkTable.Add(blkTableRec);
                    Trans1.AddNewlyCreatedDBObject(blkTableRec, true);
                    blkTableRec.Name = BlkName;
                }
                // Insert support block at insertion point InsPt
                DBSvc.BlockTableRecord space = (DBSvc.BlockTableRecord)Trans1.GetObject(cDB.CurrentSpaceId, DBSvc.OpenMode.ForWrite);
                DBSvc.BlockReference rfml = new DBSvc.BlockReference(InsPt, blkTableRec.ObjectId);
                space.AppendEntity(rfml);
                Trans1.AddNewlyCreatedDBObject(rfml, true);
                Trans1.Commit();
            }
            MessageBox.Show("\nThe block name is: " + BlkName, "BlockName", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 May 2018 18:37:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981107#M26075</guid>
      <dc:creator>sonny3g</dc:creator>
      <dc:date>2018-05-04T18:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981137#M26076</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code you show should work, but it creates an empty block, this is why you can't see it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use &lt;A href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=OREFNET-Autodesk_AutoCAD_DatabaseServices_Database_Insert_string_Database__MarshalAsUnmanagedType_U1__bool" target="_blank"&gt;this overload of the Database.Insert() method&lt;/A&gt; to add a new BlockTableRecord in the BlockTable containing the entities of the source database model space.&lt;/P&gt;</description>
      <pubDate>Fri, 04 May 2018 18:54:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981137#M26076</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-05-04T18:54:58Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981287#M26077</link>
      <description>&lt;P&gt;Thanks for the reply and help Giles. Unfortunately, I am not a good enough programmer to understand how to implement your suggestion in my program.&amp;nbsp; I am really just a dabbler being tasked with improving over our current crop of LISP programs.&lt;/P&gt;</description>
      <pubDate>Fri, 04 May 2018 19:50:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981287#M26077</guid>
      <dc:creator>sonny3g</dc:creator>
      <dc:date>2018-05-04T19:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981983#M26078</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public void InsertBlock(Point3d insPt, string blockName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                // check if the block table already has the 'blockName'" block
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(blockName))
                {
                    try
                    {
                        // search for a dwg file named 'blockName' in AutoCAD search paths
                        var filename = HostApplicationServices.Current.FindFile(blockName + ".dwg", db, FindFileHint.Default);
                        // add the dwg model space as 'blockName' block definition in the current database block table
                        using (var sourceDb = new Database(false, true))
                        {
                            sourceDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, "");
                            db.Insert(blockName, sourceDb, true);
                        }
                    }
                    catch
                    {
                        ed.WriteMessage($"\nBlock '{blockName}' not found.");
                        return;
                    }
                }

                // create a new block reference
                using (var br = new BlockReference(insPt, bt[blockName]))
                {
                    var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    space.AppendEntity(br);
                    tr.AddNewlyCreatedDBObject(br, true);
                }
                tr.Commit();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 May 2018 09:14:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7981983#M26078</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-05-05T09:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7987274#M26079</link>
      <description>&lt;P&gt;Thank you Gilles, that did the trick.&amp;nbsp; I am going to dig into your code to make sure I understand what you did.&amp;nbsp; Seeing working code and trying to adapt it to my needs seems to be the best way for me to learn how to write better code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again!!&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 13:36:56 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/7987274#M26079</guid>
      <dc:creator>sonny3g</dc:creator>
      <dc:date>2018-05-08T13:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/8278801#M26080</link>
      <description>&lt;P&gt;Giles,&lt;/P&gt;&lt;P&gt;The code you sent me for inserting blocks has been working flawlessly and I am very grateful for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, though, I need to modify your code so that if the block already exists in the block table, it will deepclone the existing block instead of inserting a new one or copying the existing one.&amp;nbsp; I figured by extending your&lt;/P&gt;&lt;P&gt;"if (!blkTable.Has(BlkName))" statement with an else statement I would be able to excute the deepclone command instead of inserting the block.&lt;/P&gt;&lt;P&gt;So far, I replaced your original code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create a new block reference&lt;/P&gt;&lt;P&gt;&amp;nbsp; using (var br = new DBSvc.BlockReference(InsPt, blkTable[BlkName])){&lt;/P&gt;&lt;P&gt;&amp;nbsp; var space = (DBSvc.BlockTableRecord)Trans1.GetObject(cDB.CurrentSpaceId, DBSvc.OpenMode.ForWrite);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; space.AppendEntity(br);&lt;/P&gt;&lt;P&gt;&amp;nbsp; Trans1.AddNewlyCreatedDBObject(br, true);}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;with this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; DBSvc.IdMapping dupMap = new DBSvc.IdMapping();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; DBSvc.BlockTableRecord dupBTR = blkTableRec.DeepClone(blkTable, dupMap, true);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; dupBTR.Name = "duplicate";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blkTable.Add(dupBTR);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Trans1.AddNewlyCreatedDBObject(dupBTR, true);&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately it does not work.&amp;nbsp; Any help is appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 16:03:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/8278801#M26080</guid>
      <dc:creator>sonny3g</dc:creator>
      <dc:date>2018-09-19T16:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to insert a block with C#</title>
      <link>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/8278964#M26081</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like you're confusing BlockTableRecord (i.e. a block definition which belongs to the BlockTable) and BlockReference (i.e. one or more inserted references in some space or other BlockTableRecord).&lt;/P&gt;
&lt;P&gt;You can read &lt;A href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-5AE8AD29-14EE-4636-A17D-EC86ED7047AF" target="_blank"&gt;this topic&lt;/A&gt; and the related ones.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Sep 2018 17:03:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/trying-to-insert-a-block-with-c/m-p/8278964#M26081</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-09-19T17:03:48Z</dc:date>
    </item>
  </channel>
</rss>

