Can't update Title Block attributes via .NET API

Can't update Title Block attributes via .NET API

bleite6S4UE
Explorer Explorer
875 Views
2 Replies
Message 1 of 3

Can't update Title Block attributes via .NET API

bleite6S4UE
Explorer
Explorer

Hi everyone!

I have a dwg file that contains a Viewport, a MTEXT and a Title Block and I'm trying to update the Title Block's attribute through the .NET API. According to AutoCAD Electrical the Title Block is of type Block Reference, but when I try to update via .NET API, it never finds it as a Block Reference, it finds it as a Block Table Record, and when I iterate through the Block  Table Record, it only finds AttributeDefinition objects, not AttributeReference objects. 

 

As I was reading about it, AttributeDefinition only modifies the default value for future insertions, not for current instances. To update the value of an attribute of an existing block reference I need to use AttributeReference. I can modify its AttributeDefinition objects, like change the tag, prompt or TextString, but I can't modify the existing instance of the Title Block. 

 

When I run the .NET application, it finds all 4 blocks, 2 are block references, the other 2 aren't. 

 

This is the piece of code that is trying to find and modify the title block:

 

 

 

Database db = new Database(false, true);
db.ReadDwgFile(inputFilePath, System.IO.FileShare.Read, true, "");

// Start a transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    try
    {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        // Get the block table
        BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        ed.WriteMessage($"\nBlock Table Type: {blockTable.GetType()}");

        // Iterate through each block table record
        foreach (ObjectId btrId in blockTable)
        {
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

            // Print the block name
            ed.WriteMessage($"\nBlock Record Name: {btr.Name}");
            ed.WriteMessage($"\nBlock Record Type: {btr.GetType()}");

            // Iterate through title block and modify desired lines
            foreach (ObjectId entId in btr)
            {
                Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);

                if (ent is BlockReference)
                {
                    ed.WriteMessage($"{ent.GetType()} is a Block Reference.");
                }

                if (ent is MText)
                {
                    MText? mtext = tr.GetObject(entId, OpenMode.ForWrite) as MText;
                }

                if (ent is AttributeDefinition)
                {
                    ed.WriteMessage($"\nEntity type: {ent.GetType()}");
                    AttributeDefinition attributeDefinition = tr.GetObject(entId, OpenMode.ForWrite) as AttributeDefinition;
                    if (attributeDefinition != null && attributeDefinition.Tag == "LOCATION")
                    {
                        ed.WriteMessage($"\nAttribution Tag: {attributeDefinition.Tag}");
                        attributeDefinition.TextString = "Kansas City";
                    }
                }
            }
        }

        // Commit the transaction
        tr.Commit();
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
        // Handle exceptions
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage($"Error: {ex.Message}\n");
    }
    finally
    {
        tr.Dispose();
    }
}

 

 

Any help would be much appreciate it! Thank you!!!

 

0 Likes
Accepted solutions (1)
876 Views
2 Replies
Replies (2)
Message 2 of 3

Ed__Jobe
Mentor
Mentor
Accepted solution

Your code is searching for block definitions, not block references (aka "INSERT"). Plus, you're working with modelspace and I would hope that your title block is in paperspace. This thread shows you how to iterate a paperspace layout's block contents and find a block by name.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 3 of 3

bleite6S4UE
Explorer
Explorer

Thanks Ed. After a lot of try and error and some code restructuring based on what you said, I was able to find the Title Block and it did find it as a Block Reference. 

0 Likes