I'm trying to update the values of some attributes in the paperspace. This block with its attributes is found on multiple layouts. I have tried the code posted below but I'm getting an NullReferenceException from the AttributeCollection. The block is inserted in the drawing layout. Can someone point me in the right direction how to fix this or what I'm doing wrong? Thanks in advance!
namespace InvullenKleinTitelhoek
{
public class Class2
{
[CommandMethod("KleineTitelhoekbis")]
public void UpdateBlockAttributes()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor edt = doc.Editor;
//To change
//blockname TBM_DATA04_S_N
//att AT_DATA_DESC3_S_N
//att AT_DATA_DESC4_S_N"
//-----------------------------------------------------------------//
PromptStringOptions lijnDrie = new PromptStringOptions("Lijn 3");
lijnDrie.AllowSpaces = true;
PromptResult textStringLijnDrie = edt.GetString(lijnDrie);
string stLijnDrie = textStringLijnDrie.StringResult;
PromptStringOptions lijnVier = new PromptStringOptions("Lijn 4");
lijnVier.AllowSpaces = true;
PromptResult textStringLijnVier = edt.GetString(lijnVier);
string stLijnVier = textStringLijnVier.StringResult;
//-----------------------------------------------------------------//
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
DBDictionary layoutDict = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach(DBDictionaryEntry entry in layoutDict)
{
Layout ltr = (Layout)trans.GetObject((ObjectId)entry.Value, OpenMode.ForWrite);
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in btr)
{
DBObject obj = trans.GetObject(id, OpenMode.ForRead);
BlockReference br = obj as BlockReference;
AttributeCollection attCol = br.AttributeCollection;
foreach (ObjectId objId in attCol)
{
DBObject dbobj = trans.GetObject(objId, OpenMode.ForRead) as DBObject;
AttributeReference attRef = dbobj as AttributeReference;
if (attRef.Tag == "AT_DATA_DESC3_S_N")
{
attRef.UpgradeOpen();
attRef.TextString = stLijnDrie;
}
if (attRef.Tag == "AT_DATA_DESC4_S_N")
{
attRef.UpgradeOpen();
attRef.TextString = stLijnVier;
}
}
}
trans.Commit();
}
}
catch (System.Exception ex)
{
edt.WriteMessage("error: " + ex.Message);
}
}
}
}
}