.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Block Attribute Text Color

1 REPLY 1
Reply
Message 1 of 2
GreenMan415
504 Views, 1 Reply

Block Attribute Text Color

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

 

}

1 REPLY 1
Message 2 of 2
khoa.ho
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

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost