I have done it before, and I have even checked my older code, as I seem to be doing it the same way, but for this code it won't work. I haven't coded in C# in awhile, so it was kind of fun to do this project. It will allow you to select a number of drawings, and update attributes for any blocks within all files selected. Here is the part that I'm having issues with. If you need to see more, let me know.
The code works as expected except for the saving aspect.
void UpdBtnClick(object sender, EventArgs e)
{
string Dir = ( DwgListview.Tag as string ) + "\\";
Dictionary<string, Dictionary<string, Dictionary<string, string>>> UpdDict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
Dictionary<string, Dictionary<string, string>> BlkDict;
Dictionary<string, string> AttDict;
foreach ( ListViewItem lvi in UpdListview.Items ) {
string DwgPath = Dir + lvi.SubItems[ 0 ].Text;
string BlkName = lvi.SubItems[ 1 ].Text;
string Tag = lvi.SubItems[ 2 ].Text;
string Value = lvi.SubItems[ 3 ].Text;
if ( UpdDict.TryGetValue( DwgPath, out BlkDict ) ) UpdDict.Remove( DwgPath );
if ( BlkDict == null )
BlkDict = new Dictionary<string, Dictionary<string, string>>();
if ( BlkDict.TryGetValue( BlkName, out AttDict ) ) BlkDict.Remove( BlkName );
if ( AttDict == null ) AttDict = new Dictionary<string, string>();
if ( AttDict.ContainsKey( Tag ) ) AttDict.Remove( Tag );
AttDict.Add( Tag, Value );
BlkDict.Add( BlkName, AttDict );
UpdDict.Add( DwgPath, BlkDict );
}
Document Doc;
DocumentLock DocLock = null;
string nValue;
Database cDb = HostApplicationServices.WorkingDatabase;
foreach ( KeyValuePair<string, Dictionary<string, Dictionary<string, string>>> kvp in UpdDict ) {
Database Db = MyUtility.GetDatabaseAtPath( kvp.Key, cDb, FileOpenMode.OpenForReadAndWriteNoShare, FindFileHint.Default, out Doc );
if ( Db == null ) {
MessageBox.Show( "Could not open drawing: " + System.Environment.NewLine + kvp.Key );
continue;
}
if ( Doc == null ) HostApplicationServices.WorkingDatabase = Db;
else DocLock = Doc.LockDocument();
using ( Transaction Trans = Db.TransactionManager.StartTransaction() ) {
ObjectId BlkTblId = Db.BlockTableId;
BlkDict = kvp.Value;
foreach ( KeyValuePair<string, Dictionary<string, string>> kvp2 in BlkDict ) {
AttDict = kvp2.Value;
ObjectId BlkRecId = MyUtility.GetNonErasedTableRecordId( BlkTblId, kvp2.Key );
BlockTableRecord BlkRec = Trans.GetObject( BlkRecId, OpenMode.ForRead ) as BlockTableRecord;
foreach ( ObjectId id in BlkRec.GetBlockReferenceIds( true, true ) ) {
BlockReference BlkRef = Trans.GetObject( id, OpenMode.ForRead ) as BlockReference;
if ( BlkRef.IsErased ) continue;
foreach ( ObjectId attId in BlkRef.AttributeCollection ) {
AttributeReference AttRef = Trans.GetObject( attId, OpenMode.ForRead ) as AttributeReference;
if ( AttDict.TryGetValue( AttRef.Tag, out nValue ) ) {
AttRef.UpgradeOpen();
AttRef.TextString = nValue;
AttRef.DowngradeOpen();
}
}
}
}
Trans.Commit();
}
HostApplicationServices.WorkingDatabase = cDb;
if ( Doc == null ) {
Db.RetainOriginalThumbnailBitmap = true;
Db.SaveAs( kvp.Key, DwgVersion.Current );
Db.Dispose();
}
else DocLock.Dispose();
}
this.Close();
}