<?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: Insert blockreference using block name in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703180#M52454</link>
    <description>&lt;P&gt;Try this sample code,&lt;/P&gt;&lt;P&gt;add to namespace declarations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="verdana,geneva"&gt;using &lt;/FONT&gt;&lt;FONT face="verdana,geneva"&gt;Autodesk.AutoCAD.Internal;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;      public void testInsertBlock()
        {
            string blockName = "BlockName";//&amp;lt;-- block name to be inserted

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            DocumentLock doclock = doc.LockDocument();

            using (doclock)
            {

                Transaction tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    try
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        if (!bt.Has(blockName))
                        {
                            ed.WriteMessage("\nBlock \"{0}\" does not exist", blockName);
                            return;
                        }

                        ObjectId blkID = bt[blockName];

                        ed.WriteMessage("\nBlock \"{0}\" does exist, ObjectId is: {1}", blockName, blkID);

                        PromptPointOptions ptOpt = new PromptPointOptions("\nSpecify a block position : ");

                        PromptPointResult res = ed.GetPoint(ptOpt);

                        if (res.Status != PromptStatus.OK)
                        {
                            if (res.Status == PromptStatus.Cancel)
                            {
                                ed.WriteMessage("\nInterrupted by user. Exit.."); return;
                            }
                            else
                            {
                                ed.WriteMessage("\nError on specifying a point.Exit..."); return;
                            }
                        }
                        Point3d pt = res.Value;

                        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        BlockTableRecord btrec = tr.GetObject(blkID, OpenMode.ForRead) as BlockTableRecord;

                        BlockReference bref = new BlockReference(pt, blkID);

                        bref.SetDatabaseDefaults();

                        if (btrec.Annotative == AnnotativeStates.True)
                        {

                            ObjectContextManager ocm = db.ObjectContextManager;

                            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

                            ObjectContexts.AddContext(bref, occ.CurrentContext);
                        }

                        Matrix3d mat = Matrix3d.Identity;

                        bref.TransformBy(mat);

                        btr.AppendEntity(bref);

                        tr.AddNewlyCreatedDBObject(bref, true);

                        if (btrec.HasAttributeDefinitions)
                        {
                            Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;

                            foreach (ObjectId subid in btrec)
                            {
                                Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);

                                AttributeDefinition attDef = ent as AttributeDefinition;

                                if (attDef != null)
                                {
                                    //ed.WriteMessage("\nValue: " + attDef.TextString);// for debug only

                                    AttributeReference attRef = new AttributeReference();

                                    attRef.SetDatabaseDefaults();

                                    attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);

                                    attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);

                                    attRef.Tag = attDef.Tag;

                                    attRef.AdjustAlignment(db);

                                    atcoll.AppendAttribute(attRef);

                                    tr.AddNewlyCreatedDBObject(attRef, true);

                                }

                            }

                        }
                        tr.Commit();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        ed.WriteMessage(("AutoCAD Exception: " + ex.Message + "\n" + ex.StackTrace));

                    }
                    catch (System.Exception ex)
                    {
                        ed.WriteMessage(("Sytem Exception: " + ex.Message + "\n" + ex.StackTrace));

                    }
                    finally
                    {
                        // optional message
                    }
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 18 Nov 2012 06:29:20 GMT</pubDate>
    <dc:creator>Hallex</dc:creator>
    <dc:date>2012-11-18T06:29:20Z</dc:date>
    <item>
      <title>Insert blockreference using block name</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703178#M52453</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Drawing contains a block with "BlkName". I want to insert blockreferences of this existing block using .Net. I gone through the help and some other threads. All shows how to insert a new block and then use the BlockID returned by the insert function. In my case I only have the block name and there is no Block id. How to get it or how to create a block reference of an existing block?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Shijith&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Nov 2012 05:50:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703178#M52453</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-11-18T05:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Insert blockreference using block name</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703180#M52454</link>
      <description>&lt;P&gt;Try this sample code,&lt;/P&gt;&lt;P&gt;add to namespace declarations:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="verdana,geneva"&gt;using &lt;/FONT&gt;&lt;FONT face="verdana,geneva"&gt;Autodesk.AutoCAD.Internal;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;      public void testInsertBlock()
        {
            string blockName = "BlockName";//&amp;lt;-- block name to be inserted

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            DocumentLock doclock = doc.LockDocument();

            using (doclock)
            {

                Transaction tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    try
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        if (!bt.Has(blockName))
                        {
                            ed.WriteMessage("\nBlock \"{0}\" does not exist", blockName);
                            return;
                        }

                        ObjectId blkID = bt[blockName];

                        ed.WriteMessage("\nBlock \"{0}\" does exist, ObjectId is: {1}", blockName, blkID);

                        PromptPointOptions ptOpt = new PromptPointOptions("\nSpecify a block position : ");

                        PromptPointResult res = ed.GetPoint(ptOpt);

                        if (res.Status != PromptStatus.OK)
                        {
                            if (res.Status == PromptStatus.Cancel)
                            {
                                ed.WriteMessage("\nInterrupted by user. Exit.."); return;
                            }
                            else
                            {
                                ed.WriteMessage("\nError on specifying a point.Exit..."); return;
                            }
                        }
                        Point3d pt = res.Value;

                        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        BlockTableRecord btrec = tr.GetObject(blkID, OpenMode.ForRead) as BlockTableRecord;

                        BlockReference bref = new BlockReference(pt, blkID);

                        bref.SetDatabaseDefaults();

                        if (btrec.Annotative == AnnotativeStates.True)
                        {

                            ObjectContextManager ocm = db.ObjectContextManager;

                            ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");

                            ObjectContexts.AddContext(bref, occ.CurrentContext);
                        }

                        Matrix3d mat = Matrix3d.Identity;

                        bref.TransformBy(mat);

                        btr.AppendEntity(bref);

                        tr.AddNewlyCreatedDBObject(bref, true);

                        if (btrec.HasAttributeDefinitions)
                        {
                            Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;

                            foreach (ObjectId subid in btrec)
                            {
                                Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);

                                AttributeDefinition attDef = ent as AttributeDefinition;

                                if (attDef != null)
                                {
                                    //ed.WriteMessage("\nValue: " + attDef.TextString);// for debug only

                                    AttributeReference attRef = new AttributeReference();

                                    attRef.SetDatabaseDefaults();

                                    attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);

                                    attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);

                                    attRef.Tag = attDef.Tag;

                                    attRef.AdjustAlignment(db);

                                    atcoll.AppendAttribute(attRef);

                                    tr.AddNewlyCreatedDBObject(attRef, true);

                                }

                            }

                        }
                        tr.Commit();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        ed.WriteMessage(("AutoCAD Exception: " + ex.Message + "\n" + ex.StackTrace));

                    }
                    catch (System.Exception ex)
                    {
                        ed.WriteMessage(("Sytem Exception: " + ex.Message + "\n" + ex.StackTrace));

                    }
                    finally
                    {
                        // optional message
                    }
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Nov 2012 06:29:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703180#M52454</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-11-18T06:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Insert blockreference using block name</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703184#M52455</link>
      <description>&lt;P&gt;Hi Hallex,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks man it worked. I am actualy looking for this portion of the code "blkId = bt[bname];". I got the "has" but didn't find the item collection. I never thought about bt[]. Thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it required to have the code for attributes? is it nesessory or a better idea?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With Thanks&lt;/P&gt;&lt;P&gt;Shijith&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Nov 2012 07:02:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703184#M52455</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2012-11-18T07:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: Insert blockreference using block name</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703228#M52456</link>
      <description>&lt;P&gt;This line of code is answer:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if (btrec.HasAttributeDefinitions)&lt;BR /&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;// add attribute here&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//otherwise this code&amp;nbsp;will be ignored and you&lt;/P&gt;&lt;P&gt;go further usual way&lt;/P&gt;&lt;P&gt;Glad you got it to work btw&lt;/P&gt;</description>
      <pubDate>Sun, 18 Nov 2012 13:31:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-blockreference-using-block-name/m-p/3703228#M52456</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2012-11-18T13:31:21Z</dc:date>
    </item>
  </channel>
</rss>

