Does anyone have a clean way of adding a entry to the Extension Dictionary of all objects added to a drawing?
Before I get too deep in this I was seeing if you guys had any suggestions and ideas.
Problems I have found
--If you try to create a block using the UI the block will be empty------None of the entities are added.
--If you add a dimension it kills it and quits adding the dictionary to the newly added objects.
Added the second if statement because with a empty drawing when the first entity was added a 'RegAppTableRecord' object caused it to fail
and a 'DimStyleTableRecord' would cause it to fail in AddUserName() method when casting it to a DBobject.--(Now cast to a Entity)
void Database_ObjectAppended(object sender, ObjectEventArgs e)
{
if (!cmdFlag)
return;
if (e.DBObject.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Entity))))
appendedIds.Add(e.DBObject.ObjectId);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
namespace HpadStudentCad
{
public class Loader : IExtensionApplication
{
//// Collection to hold open documents
static Dictionary<Document, DocMgr> docs;
DocumentCollection documents;
public void Initialize()
{
documents = Application.DocumentManager;
docs = new Dictionary<Document,DocMgr>();
///// Add all open documents to collection and while adding
///// Create New DocMgr Instance
foreach (Document doc in documents)
{
docs.Add(doc, new DocMgr(doc));
}
//// Add Handlers for Drawings
documents.DocumentActivated += new DocumentCollectionEventHandler(documents_DocumentActivated);
documents.DocumentToBeDestroyed += new DocumentCollectionEventHandler(documents_DocumentToBeDestroyed);
}
//// If a document becomes active and has not been added to collection then add it.
void documents_DocumentActivated(object sender, DocumentCollectionEventArgs e)
{
if (!(docs.ContainsKey(e.Document)))
{
docs.Add(e.Document, new DocMgr(e.Document));
}
}
//// If a document is closed then remove it from the collection.
void documents_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
if (docs.ContainsKey(e.Document))
{
docs[e.Document].DocDestroy();
docs.Remove(e.Document);
}
}
public void Terminate()
{
documents.DocumentActivated -= new DocumentCollectionEventHandler(documents_DocumentActivated);
documents.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(documents_DocumentToBeDestroyed);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////HELPER CLASS FOR DOCUMENTS////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
public class DocMgr
{
private Document doc;
private Database db;
private static string userName;
private List<ObjectId> appendedIds;
private const string DICT_NAME = "HpadStudentDictionary";
private const string USER_NAME_ENTRY = "HpadStudentUserName";
private bool cmdFlag;
static DocMgr()
{
userName = Environment.UserName;
}
public DocMgr(Document document)
{
appendedIds = new List<ObjectId>();
cmdFlag = false;
doc = document;
db = doc.Database;
db.ObjectAppended += new ObjectEventHandler(Database_ObjectAppended);
db.ObjectModified += new ObjectEventHandler(Database_ObjectModified);
doc.CommandWillStart += new CommandEventHandler(doc_CommandWillStart);
doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
}
void doc_CommandWillStart(object sender, CommandEventArgs e)
{
cmdFlag = true;
}
void doc_CommandEnded(object sender, CommandEventArgs e)
{
if (!cmdFlag) return;
foreach (ObjectId id in appendedIds)
{
AddUserName(id);
}
cmdFlag = false;
appendedIds.Clear();
}
void Database_ObjectModified(object sender, ObjectEventArgs e)
{
}
void Database_ObjectAppended(object sender, ObjectEventArgs e)
{
if (!cmdFlag)
return;
if (e.DBObject.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Entity))))
appendedIds.Add(e.DBObject.ObjectId);
}
private void AddUserName(ObjectId id)
{
using (Transaction trx = db.TransactionManager.StartTransaction())
{
Entity ent = trx.GetObject(id, OpenMode.ForRead) as Entity;
if (ent == null) return;
try
{
if (ent.ExtensionDictionary == ObjectId.Null)
{
ent.UpgradeOpen();
ent.CreateExtensionDictionary();
}
DBDictionary studentDic;
DBDictionary extDic = (DBDictionary)trx.GetObject(ent.ExtensionDictionary, OpenMode.ForRead);
if (!extDic.Contains(DICT_NAME))
{
extDic.UpgradeOpen();
studentDic = new DBDictionary();
Xrecord xr = new Xrecord();
xr.Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, userName));
extDic.SetAt(DICT_NAME, studentDic);
studentDic.SetAt(USER_NAME_ENTRY, xr);
}
}
catch (System.Exception)
{
}
trx.Commit();
}
}
public void DocDestroy()
{
doc.Database.ObjectAppended -= new ObjectEventHandler(Database_ObjectAppended);
doc.Database.ObjectModified -= new ObjectEventHandler(Database_ObjectModified);
doc.CommandEnded -= new CommandEventHandler(doc_CommandEnded);
doc.CommandWillStart -= new CommandEventHandler(doc_CommandWillStart);
}
}
}