Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want to know if I’m doing this right:
- In order to retrieve some data placed in a Result Buffer, I have to remember the order of the data placed in the result buffer?
- Secondly, how does one store a vector3d in a result buffer which is to be placed in an entity’s extended dictionary?
Given Information:
Let’s suppose I have the following information:
- Contact’s name (string)
- Contact’s phone: (int)
- Vector
What I want to do:
I want to add the above information to an entity’s Extension dictionary.
Am I doing this right?
Here’s the calling code:
[CommandMethod("AddAndReadXRecord")] public void AddXAndReadRecord() { RecordAdderAndReader addAndRead = new RecordAdderAndReader(Application.DocumentManager.CurrentDocument); addAndRead.ReadEntityXRecord(); }
And here are the supporting code/classes:
public class RecordAdderAndReader { private Document doc; private Database db; private Editor ed; private ObjectId entityId; // the name of the Xrecord which will be added private const string markNo = "MarkNo"; public RecordAdderAndReader(Document doc) { this.doc = doc; this.db = doc.Database; this.ed = doc.Editor; PromptEntityResult entityResult = ed.GetEntity("Select entity to add the " + markNo + " Xrecord to it's extension dictionary. The Default No will be it's Handle."); if (entityResult.Status != PromptStatus.OK) { return; } else { // set the panel name - to it's handle. using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject entity = tr.GetObject(entityResult.ObjectId, OpenMode.ForRead) as DBObject; this.entityId = entity.Id; string defaultPanelName = entity.Handle.ToString(); XRecordAdderHelper xRecordAdder = new XRecordAdderHelper(entity.Id); xRecordAdder.PersistDataToObject("MarkNo", entity.Handle.ToString()); tr.Commit(); } } } public void ReadEntityXRecord() { XRecordReader reader = new XRecordReader(markNo); reader.readValue(entityId); } } internal class XRecordAdderHelper { private readonly ObjectId id; private ObjectId extensionDictionaryId; public XRecordAdderHelper(ObjectId id) { this.id = id; addExtensionDictionaryIfItDoesNotExist(id); } private void addExtensionDictionaryIfItDoesNotExist(ObjectId id) { Database db = id.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { DBObject dbObj = tr.GetObject(id, OpenMode.ForRead); this.extensionDictionaryId = dbObj.ExtensionDictionary; if (extensionDictionaryId == ObjectId.Null) { dbObj.UpgradeOpen(); dbObj.CreateExtensionDictionary(); extensionDictionaryId = dbObj.ExtensionDictionary; } tr.Commit(); } } public void PersistDataToObject(string xRecordName, string dataValue) { Database db = id.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { // get the extension dictionary DBDictionary extensionDictionary = tr.GetObject(this.extensionDictionaryId, OpenMode.ForWrite) as DBDictionary; // set up the data. List<TypedValue> typedValues = new List<TypedValue> { new TypedValue((int)DxfCode.ExtendedDataAsciiString, "Freddie Mercury") , new TypedValue((int)DxfCode.Int32, 123456) , new TypedValue((int)DxfCode.Real, 1) , new TypedValue((int)DxfCode.Real, 0) , new TypedValue((int)DxfCode.Real, 0) }; // If I want to add a Vector, am I doing this right? ResultBuffer rb = new ResultBuffer(typedValues.ToArray()); // add the data to the XRecord Xrecord xrecord = new Xrecord(); xrecord.Data = rb; // add the Xrecord to the ExtensionDictionary // what if the record already exists? Add error checking for this. extensionDictionary.SetAt(xRecordName, xrecord); tr.AddNewlyCreatedDBObject(xrecord, true); tr.Commit(); } } } public class XRecordReader { private readonly string xRecordEntryNane; private Editor ed; public XRecordReader(string xRecordEntryNane) { this.xRecordEntryNane = xRecordEntryNane; this.ed = Application.DocumentManager.CurrentDocument.Editor; } public void readValue(ObjectId objectId) { string name = ""; int phoneNumber = 0; double vectorX = 0; double vectorY = 0; double vectorZ = 0; using (Transaction tr = objectId.Database.TransactionManager.StartTransaction()) { DBObject dBObject = tr.GetObject(objectId, OpenMode.ForRead) as DBObject; DBDictionary extensionDictionary = tr.GetObject(dBObject.ExtensionDictionary, OpenMode.ForRead) as DBDictionary; if (extensionDictionary.Contains(xRecordEntryNane)) { Xrecord xrecord = tr.GetObject(extensionDictionary.GetAt(xRecordEntryNane), OpenMode.ForRead, false) as Xrecord; if (xrecord != null) { ResultBuffer rb = xrecord.Data; if (rb != null) { TypedValue[] tvArray = rb.AsArray(); for (int i = 0; i < tvArray.Length; i++) { if (i == 0) { name = (string)tvArray[i].Value; } else if (i == 1) { phoneNumber = (int)tvArray[i].Value; } else if (i == 2) { vectorX = (double)tvArray[i].Value; } else if (i == 3) { vectorY = (double)tvArray[i].Value; } else if (i == 4) { vectorZ = (double)tvArray[i].Value; } } rb.Dispose(); } } } // print all the results ed.WriteMessage("\nName: " + name); ed.WriteMessage("\nPhone: " + phoneNumber.ToString()); ed.WriteMessage("\nVector Values: X:" + vectorX.ToString() + ", V: " + vectorY.ToString() + " Z: " + vectorZ.ToString()); tr.Commit(); } } }
Your ideas/assistance is much appreciated.
Solved! Go to Solution.