<?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 block with attribute in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/insert-block-with-attribute/m-p/11564204#M10891</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you create/insert a new block reference, you must explicitly Create the attribute references (AttributeReference) and add them to the block attribute collection (AttributeCollection). This is done by using the attribute definitions (AttributeDefinition) contained in the block definition (BlockTableRecord) as templates. See &lt;A href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568" target="_blank" rel="noopener"&gt;this topic&lt;/A&gt; of the docs.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// Add the block reference
BlockReference bref = new BlockReference(ptm, acBlkTbl["TestBlock"]);
acBlkTblRec.AppendEntity(bref);
acTrans.AddNewlyCreatedDBObject(bref, true);

// Add the attribute references
var blockDef = (BlockTableRecord)acTrans.GetObject(acBlkTbl["TestBlock"], OpenMode.ForRead);
foreach (ObjectId id in blockDef)
{
    if (id.ObjectClass == RXObject.GetClass(typeof(AttributeDefinition)))
    {
        var attDef = (AttributeDefinition)acTrans.GetObject(id, OpenMode.ForRead);
        if (!attDef.Constant)
        {
            var attRef = new AttributeReference();
            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
            bref.AttributeCollection.AppendAttribute(attRef);
            acTrans.AddNewlyCreatedDBObject(attRef, true);

            if (attRef.Tag.ToUpper() == "WTYPE")
            {
                attRef.TextString = "SR23";
            }
        }
    }
}
acTrans.Commit();&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 19 Nov 2022 17:18:01 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2022-11-19T17:18:01Z</dc:date>
    <item>
      <title>Insert block with attribute</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-with-attribute/m-p/11563801#M10890</link>
      <description>&lt;P&gt;hello everyone,&lt;/P&gt;&lt;P&gt;I wanted to insert a block which is exsiting on the drawing to a new point and updating attribitue defenetion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;BlockReference bref = new BlockReference(ptm, acBlkTbl["TestBlock"]);

                foreach (ObjectId attId in bref.AttributeCollection)
                {
                    AttributeReference ar =
                        acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;

                    if (ar != null &amp;amp;&amp;amp; ar.Tag.ToUpper() == "WTYPE")
                    {
                        ar.UpgradeOpen();
                        ar.TextString = "SR23";
                        ar.DowngradeOpen();
                    }
                }

                acBlkTblRec.AppendEntity(bref);
                acTrans.AddNewlyCreatedDBObject(bref, true);
                acTrans.Commit();&lt;/LI-CODE&gt;&lt;P&gt;I didn't get any warning during the build, but does not appear any block at the point place.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Nov 2022 10:40:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-with-attribute/m-p/11563801#M10890</guid>
      <dc:creator>bahman.jf.68</dc:creator>
      <dc:date>2022-11-19T10:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: Insert block with attribute</title>
      <link>https://forums.autodesk.com/t5/net-forum/insert-block-with-attribute/m-p/11564204#M10891</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you create/insert a new block reference, you must explicitly Create the attribute references (AttributeReference) and add them to the block attribute collection (AttributeCollection). This is done by using the attribute definitions (AttributeDefinition) contained in the block definition (BlockTableRecord) as templates. See &lt;A href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-2107599E-9405-4D8B-A6DD-83D603B41568" target="_blank" rel="noopener"&gt;this topic&lt;/A&gt; of the docs.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// Add the block reference
BlockReference bref = new BlockReference(ptm, acBlkTbl["TestBlock"]);
acBlkTblRec.AppendEntity(bref);
acTrans.AddNewlyCreatedDBObject(bref, true);

// Add the attribute references
var blockDef = (BlockTableRecord)acTrans.GetObject(acBlkTbl["TestBlock"], OpenMode.ForRead);
foreach (ObjectId id in blockDef)
{
    if (id.ObjectClass == RXObject.GetClass(typeof(AttributeDefinition)))
    {
        var attDef = (AttributeDefinition)acTrans.GetObject(id, OpenMode.ForRead);
        if (!attDef.Constant)
        {
            var attRef = new AttributeReference();
            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
            bref.AttributeCollection.AppendAttribute(attRef);
            acTrans.AddNewlyCreatedDBObject(attRef, true);

            if (attRef.Tag.ToUpper() == "WTYPE")
            {
                attRef.TextString = "SR23";
            }
        }
    }
}
acTrans.Commit();&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Nov 2022 17:18:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/insert-block-with-attribute/m-p/11564204#M10891</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-11-19T17:18:01Z</dc:date>
    </item>
  </channel>
</rss>

