.NET

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

Block Attribute Text Color

253 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();

 

try

{

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);

 

if(text.TextString.Contains(sFind))

{

str =

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

text.UpgradeOpen();

text.TextString = str;

text.UpgradeOpen();

}

}

transaction.Commit();

transaction.Dispose();

 

}

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 2 of 2 (219 Views)

Re: Block Attribute Text Color

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

Hi,

 

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.

 

[CommandMethod("ChangeBlockAttributeColors")]
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;
        try
        {
            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)
                return;
            var ids = new ObjectIdCollection(result.Value.GetObjectIds());
            foreach (ObjectId id in ids)
            {
                var blockRef = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
                if (blockRef == null)
                    continue;
                AttributeCollection attributes = blockRef.AttributeCollection;
                foreach (ObjectId attributeId in attributes)
                {
                    var attribute = (AttributeReference)trans.GetObject(attributeId, OpenMode.ForRead);
                    if (attribute.ColorIndex != replaceColorIndex)
                    {
                        attribute.UpgradeOpen();
                        attribute.ColorIndex = replaceColorIndex;
                        attribute.DowngradeOpen();
                    }
                }
            }
            trans.Commit();
        }
        catch (Runtime.Exception ex)
        {
            editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);
        }
    }
}

 

-Khoa

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.