Delete Block Attributes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Guys,
I've been try to write a code that simple remove all attributes of a block, and then i'll put new ones.
To do that, i tried to clean the AttDefinitions of BlockTableRecord, and then synchronize the attributes like that:
[CommandMethod("CleanAttsDef")]
public static void CleanAttsDef()
{
Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor edt = doc.Editor;
PromptEntityOptions promptEntity = new PromptEntityOptions("Selecione o bloco para limpar os atributos");
PromptEntityResult result = edt.GetEntity(promptEntity);
if (result.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(result.ObjectId, OpenMode.ForWrite) as Entity;
edt.WriteMessage(ent.GetType().ToString());
if (ent is BlockReference br)
{
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId objID in btr)
{
Entity internalEnty = tr.GetObject(objID, OpenMode.ForWrite) as Entity;
if (internalEnty is AttributeDefinition attDef)
{
attDef.Erase();
}
}
tr.Commit();
}
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockReference br = tr.GetObject(result.ObjectId, OpenMode.ForWrite) as BlockReference;
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
btr.SynchronizeAttributes();
tr.Commit();
}
}
}
Note that i used the extension method of that post to do the Synchronize.
The code works perfect, except for one detail: after i run it, the block stay in a state that always i try to do something with they (for example change the scale factors) i face that message bellow:
Actually, i was ignoring that problem... but i started to face other problems related with that...
Mysteriously, when i close and open the drawing the message desapear... and also if i deleted the AttDefinition and use the AutoCAD command "ATTSYNC" i also can "solve" the problem...
Even so, I'm looking for a more "solid" solution using .NET itself...
Thanks in advance.