Active Member
Posts: 8
Registered: ‎08-15-2007
Message 1 of 2 (247 Views)

Block Attribute Text Color

247 Views, 1 Replies
10-31-2012 04:48 AM

Good Day,


I am looking to go through a whole drawing and change the text color of any block attribute to say 0 as the drawings I am recieving have all the text at a weird color. I was unable to find anything on the forum. I already have a routine that changes text of certain blocks based on layer, but I want this to change the color. Any help would be great! Below is code I used for the layer change I talked about earlier.


publicstaticvoid strFind(string sFind, stringsReplace)


string str = "";


Document document = Application.DocumentManager.MdiActiveDocument;


Editoreditor = document.Editor;


Transactiontransaction = document.Database.TransactionManager.StartTransaction();




TypedValue[] valueArray = newTypedValue[] { newTypedValue(0, "TEXT") };


SelectionFilter filter = newSelectionFilter(valueArray);


PromptSelectionResultresult = editor.SelectAll(filter);


ObjectIdCollection ids = newObjectIdCollection(result.Value.GetObjectIds());


foreach (ObjectId id inids)


DBText text = (DBText) transaction.GetObject(id, OpenMode.ForWrite);




str =

Regex.Replace(text.TextString, sFind, sReplace);


text.TextString = str;








Posts: 213
Registered: ‎09-15-2011
Message 2 of 2 (213 Views)

Re: Block Attribute Text Color

11-03-2012 08:36 PM in reply to: GreenMan415



The following code will change all attribute colors to color index 0 (ByBlock color) of all found block references in the current drawing. If you use a SelectionFilter for the editor to get the selection set based on DxfCodes of TypedValues, you should use DxfCode = INSERT for block references.


public static void ChangeBlockAttributeColors()
    Document document = Application.DocumentManager.MdiActiveDocument;
    const int replaceColorIndex = 0; // change color to ByBlock
    ChangeBlockAttributeColors(document, replaceColorIndex);

/// <summary>
/// Change all colors of attributes of all block references to a given color index
/// </summary>
/// <param name="document">Document to change</param>
/// <param name="replaceColorIndex">Color ByBlock: colorIndex = 0, Color ByLayer: colorIndex = 256</param>
public static void ChangeBlockAttributeColors(Document document, int replaceColorIndex = 256)
    using (Transaction trans = document.Database.TransactionManager.StartTransaction())
        Editor editor = document.Editor;
            var valueArray = new TypedValue[]
                    new TypedValue(0, "INSERT")    // Filter block references
            var filter = new SelectionFilter(valueArray);
            PromptSelectionResult result = editor.SelectAll(filter);
            if (result.Value == null)
            var ids = new ObjectIdCollection(result.Value.GetObjectIds());
            foreach (ObjectId id in ids)
                var blockRef = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
                if (blockRef == null)
                AttributeCollection attributes = blockRef.AttributeCollection;
                foreach (ObjectId attributeId in attributes)
                    var attribute = (AttributeReference)trans.GetObject(attributeId, OpenMode.ForRead);
                    if (attribute.ColorIndex != replaceColorIndex)
                        attribute.ColorIndex = replaceColorIndex;
        catch (Runtime.Exception ex)
            editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);





Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.