- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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!!!
Solved! Go to Solution.