<?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: Edit attributes within a block. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3201572#M58759</link>
    <description>&lt;P&gt;Think easier yet to use Dictionary or SortedList to update attributes, e.g. :&lt;/P&gt;&lt;PRE&gt;         [CommandMethod("attupd")]
        public static void AttributeUpdate()
        {
            bool success = true;

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            try
            {
                // create options object

                PromptSelectionOptions pso = new PromptSelectionOptions();

                // add messages to options object

                pso.MessageForRemoval = "\n\tSelect a block references only!";

                pso.MessageForAdding = "\n\tSelect a block references &amp;gt;&amp;gt; ";

                // create filter object for attributed block references:

                SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "insert"), new TypedValue(66, 1) });

                // do selection
                PromptSelectionResult psr = ed.GetSelection(pso, filter);

                //check if selection is succeed
                if (psr.Status != PromptStatus.OK) return;

                using (DocumentLock docloc = doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        // get an array of ObjectIds
                        ObjectId[] ids = psr.Value.GetObjectIds();

                        //tags
                        string[] tags = { "TAG1", "TAG2", "TAG3", "TAG4" };

                        //to your settings (check yourself):
                        // string[] tags = { "CREV1", "TREV1", "DREV1", "VREV1", "AREV1", "DATAREV1", ",", "SREV1" };

                        //values
                        string[] values = { "FirstValue", "SecondValue", "ThirdValue", "ForthValue" };

                        //populate a dictionary with data:
                        Dictionary&amp;lt;string, string&amp;gt; dict = FillDictionary(tags, values);

                        //execute update command
                        AttUpdFromDict(tr, ids, dict);    

                        // commit transaction, will be disposed automatically because if it's inside the "Using{}" code block
                        tr.Commit();
                    }
                }
                //  you might be want to dispaly a message here in case if the program was ended up succesfully
                //  ed.WriteMessage("Done");
            }


            catch (System.Exception ex)
            {
                success = false;
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                //do nothing or display your message here
                ed.WriteMessage("\nCommand ended up with result of \"{0}\"",success==true? "Success": "Error");

            }
        }

        public static Dictionary&amp;lt;string, string&amp;gt; FillDictionary(string[] tags, string[] values)
        {
            Dictionary&amp;lt;string, string&amp;gt; dict = new Dictionary&amp;lt;string, string&amp;gt;();

            if (tags.Length != values.Length)
            {
                throw new System.Exception("Arrays have a different number of elements. Error...");

            }
            for (int i = 0; i &amp;lt; tags.Length; i++)
            {
                dict.Add(tags[i], values[i]);
            }
            return dict;
        }

        public static void AttUpdFromDict(Transaction tr, ObjectId[] objectIdList, Dictionary&amp;lt;string, string&amp;gt; dict)
        {
            for (int i = 0; i &amp;lt; objectIdList.Length; i++)
            {
                ObjectId objectId = objectIdList[i];

                BlockReference blockReference = (BlockReference)tr.GetObject(objectId, OpenMode.ForWrite);
                
                foreach (ObjectId item in blockReference.AttributeCollection)
                {
                    Entity ent = tr.GetObject(item, OpenMode.ForWrite, false) as Entity;

                    if (ent.GetType() == typeof(AttributeReference))
                    {
                        AttributeReference attRef = ent as AttributeReference;

                        if (dict.ContainsKey(attRef.Tag))
                            attRef.TextString = dict[attRef.Tag];
                    }
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 23 Oct 2011 09:24:13 GMT</pubDate>
    <dc:creator>Hallex</dc:creator>
    <dc:date>2011-10-23T09:24:13Z</dc:date>
    <item>
      <title>Edit attributes within a block.</title>
      <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3196728#M58757</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;I'm trying to&lt;/SPAN&gt; &lt;SPAN&gt;edit attributes of&lt;/SPAN&gt; &lt;SPAN&gt;a&lt;/SPAN&gt; &lt;SPAN&gt;block but&lt;/SPAN&gt; &lt;SPAN&gt;is giving&lt;/SPAN&gt; &lt;SPAN&gt;error.&amp;nbsp;&lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://forums.autodesk.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; private void ChangingAttibutes1(ObjectId[] objectIdList)
        {
            for (int i = 0; i &amp;lt; objectIdList.Length; i++)
            {
                ObjectId objectId = objectIdList[i];
                BlockReference blockReference = (BlockReference)objectId.GetObject(OpenMode.ForWrite);
                BlockTableRecord blockTableRecord = (BlockTableRecord)blockReference.BlockTableRecord.GetObject(OpenMode.ForWrite);
                
                Entity Entity = null;

                foreach (ObjectId item in blockTableRecord)
                {
                    Entity = transaction.GetObject(item, OpenMode.ForWrite) as Entity;
                   /* if (Entity.GetType() == typeof(AttributeDefinition))
                    {*/
                        AttributeDefinition attributeDefinition = Entity as AttributeDefinition;
                        AttributeReference attributeReference = new AttributeReference();
                        attributeReference.SetAttributeFromBlock(attributeDefinition, blockReference.BlockTransform);
                        if (attributeReference.Tag == "CREV1")
                        {
                            attributeReference.TextString = myList[0];
                        }
                        else if (attributeReference.Tag == "TREV1")
                        {
                            attributeReference.TextString = myList[1];
                        }
                        else if (attributeReference.Tag == "DREV1")
                        {
                            attributeReference.TextString = myList[2];
                        }
                        else if (attributeReference.Tag == "VREV1")
                        {
                            attributeReference.TextString = myList[3];
                        }
                        else if (attributeReference.Tag == "AREV1")
                        {
                            attributeReference.TextString = myList[4];
                        }
                        else if (attributeReference.Tag == "DATAREV1")
                        {
                            attributeReference.TextString = myList[5];
                        }
                        else if (attributeReference.Tag == "SREV1")
                        {
                            attributeReference.TextString = myList[6];
                        }
                    //}
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2011 12:57:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3196728#M58757</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-19T12:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes within a block.</title>
      <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3196794#M58758</link>
      <description>&lt;PRE&gt;rivate void ChangingAttibutes1(ObjectId[] objectIdList)
        {
            for (int i = 0; i &amp;lt; objectIdList.Length; i++)
            {
                ObjectId objectId = objectIdList[i];
                BlockReference blockReference = (BlockReference)objectId.GetObject(OpenMode.ForWrite);
                BlockTableRecord blockTableRecord = (BlockTableRecord)blockReference.BlockTableRecord.GetObject(OpenMode.ForWrite);
                
                Entity Entity = null;

                foreach (ObjectId item in blockReference.AttributeCollection)
                {
                    Entity = transaction.GetObject(item, OpenMode.ForWrite) as Entity;
                    if (Entity.GetType() == typeof(AttributeReference))
                    {
                        AttributeReference attributeReference = Entity as AttributeReference;

                        if (attributeReference.Tag == "CREV1")
                        {
                            attributeReference.TextString = myList[0];
                        }
                        else if (attributeReference.Tag == "TREV1")
                        {
                            attributeReference.TextString = myList[1];
                        }
                        else if (attributeReference.Tag == "DREV1")
                        {
                            attributeReference.TextString = myList[2];
                        }
                        else if (attributeReference.Tag == "VREV1")
                        {
                            attributeReference.TextString = myList[3];
                        }
                        else if (attributeReference.Tag == "AREV1")
                        {
                            attributeReference.TextString = myList[4];
                        }
                        else if (attributeReference.Tag == "DATAREV1")
                        {
                            attributeReference.TextString = myList[5];
                        }
                        else if (attributeReference.Tag == "SREV1")
                        {
                            attributeReference.TextString = myList[6];
                        }
                    }
                }
            }
        }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;SPAN&gt;&lt;SPAN&gt;It worked!&lt;/SPAN&gt; &lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;SPAN&gt;!&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2011 13:25:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3196794#M58758</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-19T13:25:08Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes within a block.</title>
      <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3201572#M58759</link>
      <description>&lt;P&gt;Think easier yet to use Dictionary or SortedList to update attributes, e.g. :&lt;/P&gt;&lt;PRE&gt;         [CommandMethod("attupd")]
        public static void AttributeUpdate()
        {
            bool success = true;

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            try
            {
                // create options object

                PromptSelectionOptions pso = new PromptSelectionOptions();

                // add messages to options object

                pso.MessageForRemoval = "\n\tSelect a block references only!";

                pso.MessageForAdding = "\n\tSelect a block references &amp;gt;&amp;gt; ";

                // create filter object for attributed block references:

                SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "insert"), new TypedValue(66, 1) });

                // do selection
                PromptSelectionResult psr = ed.GetSelection(pso, filter);

                //check if selection is succeed
                if (psr.Status != PromptStatus.OK) return;

                using (DocumentLock docloc = doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        // get an array of ObjectIds
                        ObjectId[] ids = psr.Value.GetObjectIds();

                        //tags
                        string[] tags = { "TAG1", "TAG2", "TAG3", "TAG4" };

                        //to your settings (check yourself):
                        // string[] tags = { "CREV1", "TREV1", "DREV1", "VREV1", "AREV1", "DATAREV1", ",", "SREV1" };

                        //values
                        string[] values = { "FirstValue", "SecondValue", "ThirdValue", "ForthValue" };

                        //populate a dictionary with data:
                        Dictionary&amp;lt;string, string&amp;gt; dict = FillDictionary(tags, values);

                        //execute update command
                        AttUpdFromDict(tr, ids, dict);    

                        // commit transaction, will be disposed automatically because if it's inside the "Using{}" code block
                        tr.Commit();
                    }
                }
                //  you might be want to dispaly a message here in case if the program was ended up succesfully
                //  ed.WriteMessage("Done");
            }


            catch (System.Exception ex)
            {
                success = false;
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                //do nothing or display your message here
                ed.WriteMessage("\nCommand ended up with result of \"{0}\"",success==true? "Success": "Error");

            }
        }

        public static Dictionary&amp;lt;string, string&amp;gt; FillDictionary(string[] tags, string[] values)
        {
            Dictionary&amp;lt;string, string&amp;gt; dict = new Dictionary&amp;lt;string, string&amp;gt;();

            if (tags.Length != values.Length)
            {
                throw new System.Exception("Arrays have a different number of elements. Error...");

            }
            for (int i = 0; i &amp;lt; tags.Length; i++)
            {
                dict.Add(tags[i], values[i]);
            }
            return dict;
        }

        public static void AttUpdFromDict(Transaction tr, ObjectId[] objectIdList, Dictionary&amp;lt;string, string&amp;gt; dict)
        {
            for (int i = 0; i &amp;lt; objectIdList.Length; i++)
            {
                ObjectId objectId = objectIdList[i];

                BlockReference blockReference = (BlockReference)tr.GetObject(objectId, OpenMode.ForWrite);
                
                foreach (ObjectId item in blockReference.AttributeCollection)
                {
                    Entity ent = tr.GetObject(item, OpenMode.ForWrite, false) as Entity;

                    if (ent.GetType() == typeof(AttributeReference))
                    {
                        AttributeReference attRef = ent as AttributeReference;

                        if (dict.ContainsKey(attRef.Tag))
                            attRef.TextString = dict[attRef.Tag];
                    }
                }
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Oct 2011 09:24:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3201572#M58759</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2011-10-23T09:24:13Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes within a block.</title>
      <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3202092#M58760</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Thank you.&lt;/SPAN&gt; &lt;SPAN&gt;I will&lt;/SPAN&gt; &lt;SPAN&gt;adopt!&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2011 09:57:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3202092#M58760</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-10-24T09:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Edit attributes within a block.</title>
      <link>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3202112#M58761</link>
      <description>&lt;P&gt;You're welcome&lt;/P&gt;&lt;P&gt;Cheers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;~'J'~&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2011 10:32:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/edit-attributes-within-a-block/m-p/3202112#M58761</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2011-10-24T10:32:13Z</dc:date>
    </item>
  </channel>
</rss>

