Skip to content

XRecords Objects

Overview

  • Code to Store Data

Store XRecord in Main Directory

 [CommandMethod("Test", CommandFlags.UsePickSet)]
        public static void Test()
        {
            try 
            {
                using (var transaction = ActiveUtil.TransactionManager.StartTransaction())
                {
                    DBDictionary namedDictionary = ActiveUtil.Database.NamedObjectsDictionaryId.GetObject<DBDictionary>();
                    var resultBuf = new ResultBuffer(new TypedValue((int)DxfCode.Text, "Hey this is string data"));
                    var myXrecord = new Xrecord
                    {
                        Data = resultBuf
                    };
                    namedDictionary.UpgradeOpen();
                    namedDictionary.SetAt("MyCustomData", myXrecord);
                    transaction.AddNewlyCreatedDBObject(myXrecord, true);
                    transaction.Commit();
                }

                using (var transaction = ActiveUtil.TransactionManager.StartTransaction())
                {
                    DBDictionary namedDictionary = ActiveUtil.Database.NamedObjectsDictionaryId.GetObject<DBDictionary>();
                    try
                    {
                        Xrecord xRec = namedDictionary.GetAt("MyCustomData").GetObject<Xrecord>();
                        TypedValue[] xRecData = xRec.Data.AsArray();
                        foreach (TypedValue tv in xRecData)
                        { 
                            ActiveUtil.Editor.WriteLine(tv.Value.ToString()); 
                        }
                    }
                    catch (System.Exception)
                    {

                    }
                }
            }
            catch (System.Exception ex)
            {
                Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");
            }
        }

Set XRecord in Object

private static void SetXRecordInObject(ObjectId id, string key, ResultBuffer rb)
        {
            try
            {
                using (DocumentLock acLckDoc =
                Application.DocumentManager.MdiActiveDocument.LockDocument())
                {
                    Database db = Application.DocumentManager.MdiActiveDocument.Database;
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        using (Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity)
                        {
                            if (ent != null)
                            {
                                if (ent.ExtensionDictionary == ObjectId.Null)
                                {
                                    ent.CreateExtensionDictionary();
                                }
                                using (DBDictionary xDict =
                                (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite))
                                {
                                    using (Xrecord xRec = new Xrecord())
                                    {
                                        xRec.Data = rb;
                                        try
                                        {
                                            xDict.Remove(key);
                                        }
                                        catch (System.Exception) { }
                                        xDict.SetAt(key, xRec);
                                        tr.AddNewlyCreatedDBObject(xRec, true);
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }

Get XRecords from Object

        private static List<object> GetXRecordFromObject(ObjectId id, string key)
        {
            List<object> returnValue = new List<object>();
            try
            {
                using (DocumentLock acLckDoc =
                Application.DocumentManager.MdiActiveDocument.LockDocument())
                {
                    Database db = Application.DocumentManager.MdiActiveDocument.Database;
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        using (Entity ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity)
                        {
                            if (ent.ExtensionDictionary == ObjectId.Null) { return returnValue; }
                            try
                            {
                                using (DBDictionary xDict =
                                (DBDictionary)tr.GetObject(ent.ExtensionDictionary, OpenMode.ForRead, false))
                                {
                                    using (Xrecord xRec = (Xrecord)tr.GetObject(xDict.GetAt(key),
                                    OpenMode.ForRead, false))
                                    {
                                        TypedValue[] xRecData = xRec.Data.AsArray();
                                        foreach (TypedValue tv in xRecData) { returnValue.Add(tv.Value); }
                                    }
                                }
                            }
                            catch (System.Exception) { }
                        }
                        tr.Commit();
                    }
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            return returnValue;
        }

Sample Code to Store Text in object

/// <summary>
        /// Save a textstring in the ExtensionDictionary of an object
        /// </summary>
        public static void SetObjXRecordText(ObjectId id, string key, string value)
        {
            using (ResultBuffer resBuf = new ResultBuffer(new TypedValue((int)DxfCode.Text, value)))
            {
                SetXRecordInObject(id, key, resBuf);
            }
        }
 /// <summary>
        /// Read a string from the ExtensionDictionary of an object
        /// </summary>
        public static string GetObjXRecordText(ObjectId id, string key)
        {
            string returnValue = string.Empty;
            List<object> records = GetXRecordFromObject(id, key);
            if (records.Count > 0) { returnValue = Convert.ToString(records[records.Count - 1]); }
            return returnValue;
        }

Code to Store Interger in Object

 /// <summary>
        /// Save an integer in the ExtensionDictionary of an object
        /// </summary>
        public static void SetObjXRecordInt32(ObjectId id, string key, int value)
        {
            using (ResultBuffer resBuf = new ResultBuffer(new TypedValue((int)DxfCode.Int32, value)))
            {
                SetXRecordInObject(id, key, resBuf);
            }
        }
        /// <summary>
        /// Read an integer from the ExtensionDictionary of an object
        /// </summary>
        public static int GetObjXRecordInt32(ObjectId id, string key)
        {
            int returnValue = -1;
            List<object> records = GetXRecordFromObject(id, key);
            if (records.Count > 0) { returnValue = Convert.ToInt32(records[records.Count - 1]); }
            return returnValue;
        }