.NET

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

Block Attribute Text Color

242 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: 164
Registered: ‎09-15-2011
Message 2 of 2 (208 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

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
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 to get help installing your software.

Ask the Community