<?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 Open state of block reference in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/open-state-of-block-reference/m-p/9125800#M20889</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;The following method erases blocks from a drawing database accesses, adds new blocks--including a block that just has attribute definitions (adTBAtt)--and then uses those attribute definitions to add corresponding attribute references (arTBAtt) to to the block reference (brTBAtt2). When I try to UpgradeOpen, it throws an eNotOpenForWrite exception. When I leave out UpgradeOpen, it throws an eInvalidOpenState exception. BTW I have to define brTBAtt2 even though I already defines that exact same block reference earlier (brTBAtt), because&amp;nbsp;brTBAtt.BlockTransform has an exception (learned that from stepping through the code a checking the variables). What am I doing wrong? I have also attached&amp;nbsp;AttachTBAttributeDef. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;private static Boolean AddAttRef(Database db, string strLayout, string strSize, string strVDocNr, BlockReference brTB)
        {
            int intTBAtt = brTB.AttributeCollection.Count;
            string[] str_TBTag = new string[intTBAtt];
            string[] str_TBAttVal = new string[intTBAtt];
            string strFrame = "Z_" + strSize.ToUpper() + "_FRAME";
            (str_TBTag, str_TBAttVal) = EraseFrameAndTB(db, strSize, brTB, strLayout, strFrame);

            string strTemp = "Wdi" + strSize.ToLower() + "1";
            BlockReference brTemp = AttachFrameAndTB(db, strSize, strLayout, strTemp);

            String strTBName = "WDC-TBI1";
            BlockReference brTBAtt = AttachTBAttributeDef(db, strLayout, strTBName);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockReference brTBAtt2 = AcadHelper.GetBlockReferenceFromPrefix(strTBName,strLayout,db);
                BlockTableRecord btrTBAtt = (BlockTableRecord)tr.GetObject(brTBAtt2.BlockTableRecord, OpenMode.ForWrite);

                if (btrTBAtt.HasAttributeDefinitions)
                {
                    foreach (ObjectId oidTBAtt2 in btrTBAtt)
                    {
                        DBObject dboTBAtt = tr.GetObject(oidTBAtt2, OpenMode.ForRead) as DBObject;
                        if (dboTBAtt is AttributeDefinition)
                        {
                            AttributeDefinition adTBAtt = tr.GetObject(oidTBAtt2, OpenMode.ForRead) as AttributeDefinition;
                            if ((adTBAtt != null) &amp;amp;&amp;amp; !adTBAtt.Constant)
                            {
                                using (AttributeReference arTBAtt = new AttributeReference())
                                {
                                    arTBAtt.SetAttributeFromBlock(adTBAtt, brTBAtt2.BlockTransform);
                                    arTBAtt.Position = adTBAtt.Position.TransformBy(brTBAtt2.BlockTransform);
                                    arTBAtt.TextString = "I AM GROOT!";

                                    // Following  line throws eInvalidOpenState exception
                                    brTBAtt2.UpgradeOpen();
                                    // If last line is commented out, following line throws eNotOpenForWrite exception
                                    brTBAtt2.AttributeCollection.AppendAttribute(arTBAtt); 
                                    tr.AddNewlyCreatedDBObject(arTBAtt, true);
                                }
                            }
                        }
                        
                    }
                }
                tr.Commit();
            }

            //AddPartDataToBOM(db, strLayout);

            return true;
        }&lt;/PRE&gt;&lt;PRE&gt;private static BlockReference AttachTBAttributeDef(Database db, string strLayout, string strTBName)
        {
            BlockReference brTBAtt = null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Database dbTBAtt = new Database(false, true);
                string strPath = Path.GetDirectoryName(AcadHelper.FindFile(strTBName + ".DWG"));
                Point3d p3dIns = new Point3d(0, 0, 0);
                string strFileName = strPath + "\\" + strTBName + ".dwg";

                dbTBAtt.ReadDwgFile(strFileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                dbTBAtt.CloseInput(true);

                if (DefineBlockFromDisk2(db, strTBName, strPath))
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);

                    if (bt == null)
                        return null;

                    ObjectId oid = AcadHelper.GetBlockTableRecordByName(strTBName, db);

                    // Create and insert the new block reference
                    brTBAtt = new BlockReference(p3dIns, oid);

                    BlockTableRecord btrTBAtt = tr.GetObject(brTBAtt.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;

                    string strHandle = GetLayoutBlockTableRecordByName(db, strLayout);
                    ObjectId btrObjectId = GetObjectIDFromHandle(db, strHandle);
                    BlockTableRecord btrLayout = (BlockTableRecord)tr.GetObject(btrObjectId, OpenMode.ForWrite);

                    //Adding block reference to database
                    if (brTBAtt.IsNewObject)
                    {
                        using (brTBAtt)
                        {
                            btrLayout.AppendEntity(brTBAtt);
                            tr.AddNewlyCreatedDBObject(brTBAtt, true);
                        }
                    }
                    tr.Commit();
                }
            }
            return brTBAtt;
        }&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Nov 2019 15:04:26 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-11-04T15:04:26Z</dc:date>
    <item>
      <title>Open state of block reference</title>
      <link>https://forums.autodesk.com/t5/net-forum/open-state-of-block-reference/m-p/9125800#M20889</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;The following method erases blocks from a drawing database accesses, adds new blocks--including a block that just has attribute definitions (adTBAtt)--and then uses those attribute definitions to add corresponding attribute references (arTBAtt) to to the block reference (brTBAtt2). When I try to UpgradeOpen, it throws an eNotOpenForWrite exception. When I leave out UpgradeOpen, it throws an eInvalidOpenState exception. BTW I have to define brTBAtt2 even though I already defines that exact same block reference earlier (brTBAtt), because&amp;nbsp;brTBAtt.BlockTransform has an exception (learned that from stepping through the code a checking the variables). What am I doing wrong? I have also attached&amp;nbsp;AttachTBAttributeDef. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;private static Boolean AddAttRef(Database db, string strLayout, string strSize, string strVDocNr, BlockReference brTB)
        {
            int intTBAtt = brTB.AttributeCollection.Count;
            string[] str_TBTag = new string[intTBAtt];
            string[] str_TBAttVal = new string[intTBAtt];
            string strFrame = "Z_" + strSize.ToUpper() + "_FRAME";
            (str_TBTag, str_TBAttVal) = EraseFrameAndTB(db, strSize, brTB, strLayout, strFrame);

            string strTemp = "Wdi" + strSize.ToLower() + "1";
            BlockReference brTemp = AttachFrameAndTB(db, strSize, strLayout, strTemp);

            String strTBName = "WDC-TBI1";
            BlockReference brTBAtt = AttachTBAttributeDef(db, strLayout, strTBName);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockReference brTBAtt2 = AcadHelper.GetBlockReferenceFromPrefix(strTBName,strLayout,db);
                BlockTableRecord btrTBAtt = (BlockTableRecord)tr.GetObject(brTBAtt2.BlockTableRecord, OpenMode.ForWrite);

                if (btrTBAtt.HasAttributeDefinitions)
                {
                    foreach (ObjectId oidTBAtt2 in btrTBAtt)
                    {
                        DBObject dboTBAtt = tr.GetObject(oidTBAtt2, OpenMode.ForRead) as DBObject;
                        if (dboTBAtt is AttributeDefinition)
                        {
                            AttributeDefinition adTBAtt = tr.GetObject(oidTBAtt2, OpenMode.ForRead) as AttributeDefinition;
                            if ((adTBAtt != null) &amp;amp;&amp;amp; !adTBAtt.Constant)
                            {
                                using (AttributeReference arTBAtt = new AttributeReference())
                                {
                                    arTBAtt.SetAttributeFromBlock(adTBAtt, brTBAtt2.BlockTransform);
                                    arTBAtt.Position = adTBAtt.Position.TransformBy(brTBAtt2.BlockTransform);
                                    arTBAtt.TextString = "I AM GROOT!";

                                    // Following  line throws eInvalidOpenState exception
                                    brTBAtt2.UpgradeOpen();
                                    // If last line is commented out, following line throws eNotOpenForWrite exception
                                    brTBAtt2.AttributeCollection.AppendAttribute(arTBAtt); 
                                    tr.AddNewlyCreatedDBObject(arTBAtt, true);
                                }
                            }
                        }
                        
                    }
                }
                tr.Commit();
            }

            //AddPartDataToBOM(db, strLayout);

            return true;
        }&lt;/PRE&gt;&lt;PRE&gt;private static BlockReference AttachTBAttributeDef(Database db, string strLayout, string strTBName)
        {
            BlockReference brTBAtt = null;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Database dbTBAtt = new Database(false, true);
                string strPath = Path.GetDirectoryName(AcadHelper.FindFile(strTBName + ".DWG"));
                Point3d p3dIns = new Point3d(0, 0, 0);
                string strFileName = strPath + "\\" + strTBName + ".dwg";

                dbTBAtt.ReadDwgFile(strFileName, FileOpenMode.OpenForReadAndAllShare, false, null);
                dbTBAtt.CloseInput(true);

                if (DefineBlockFromDisk2(db, strTBName, strPath))
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);

                    if (bt == null)
                        return null;

                    ObjectId oid = AcadHelper.GetBlockTableRecordByName(strTBName, db);

                    // Create and insert the new block reference
                    brTBAtt = new BlockReference(p3dIns, oid);

                    BlockTableRecord btrTBAtt = tr.GetObject(brTBAtt.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;

                    string strHandle = GetLayoutBlockTableRecordByName(db, strLayout);
                    ObjectId btrObjectId = GetObjectIDFromHandle(db, strHandle);
                    BlockTableRecord btrLayout = (BlockTableRecord)tr.GetObject(btrObjectId, OpenMode.ForWrite);

                    //Adding block reference to database
                    if (brTBAtt.IsNewObject)
                    {
                        using (brTBAtt)
                        {
                            btrLayout.AppendEntity(brTBAtt);
                            tr.AddNewlyCreatedDBObject(brTBAtt, true);
                        }
                    }
                    tr.Commit();
                }
            }
            return brTBAtt;
        }&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Nov 2019 15:04:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/open-state-of-block-reference/m-p/9125800#M20889</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-11-04T15:04:26Z</dc:date>
    </item>
  </channel>
</rss>

