Change the Color of Attribute Definition using C#

Change the Color of Attribute Definition using C#

ilovejingle
Enthusiast Enthusiast
723 Views
3 Replies
Message 1 of 4

Change the Color of Attribute Definition using C#

ilovejingle
Enthusiast
Enthusiast

I have a block in the drawing which contains an attribute whoes tag is "24GENPOS", What I want to do is to change the layer and color of that specific attribute definition, I am starting with the color first, The program can correctly locate the block and execute the code, however the color is not changed.

 

        public void cmdATTLAY()
        {
            var attName = "24GENPOS";
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId blkTabId = db.BlockTableId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable blkTab = trans.GetObject(blkTabId, OpenMode.ForRead) as BlockTable;
                    foreach(ObjectId blkTabRecId in blkTab)
                    {
                        bgFunction.ChangeAttLayer(blkTabRecId, attName, "something");
                    }
                }
                catch(Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage("Error : " + ex.Message);
                }
            }
        }
        public static void ChangeAttLayer(ObjectId blockId,string attName, string layerName)
        {
            using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                BlockTableRecord blkTabRec = trans.GetObject(blockId, OpenMode.ForRead) as BlockTableRecord;
                foreach (ObjectId objId in blkTabRec)
                {
                    DBObject objDb = trans.GetObject(objId, OpenMode.ForWrite);
                    if (objDb is AttributeDefinition)
                    {
                        var attDef = (AttributeDefinition)objDb;
                        if (attDef.Tag == attName)
                        {
                            attDef.ColorIndex = 1;
                        }
                    }
                }
                trans.Commit();
            }

        }
0 Likes
Accepted solutions (1)
724 Views
3 Replies
Replies (3)
Message 2 of 4

Alexander.Rivilis
Mentor
Mentor
Accepted solution

@ilovejingle 

 

You have to add trans.Commit() in method cmdATTLAY()

 

P.S.: If drawing has block references (BlockReference) with this attribute, changing color of attribute definitions do not change color of attribute references

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 4

ilovejingle
Enthusiast
Enthusiast

opps, silly mistake of mine. Thank you very much. Do you have how to sync all the attribute references?

0 Likes
Message 4 of 4

Alexander.Rivilis
Mentor
Mentor

@ilovejingle wrote:

opps, silly mistake of mine. Thank you very much. Do you have how to sync all the attribute references?


There are several ways:

1. Start command _ATTSYNC with Editor.Command or Editor.CommandAsync

2. Iterate all block references (BlockReference) in Database (not only in Model Space, but in all BlockTableRecord's), find all attribute (AttributeReference) with your's Tag and change color.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes