- 积分
- 521
- 明经币
- 个
- 注册时间
- 2010-12-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2012-7-7 09:54:51
|
显示全部楼层
本帖最后由 sxpd 于 2012-7-7 09:56 编辑
用XDATA不是更简单
你的扩展数据用法有问题
贴个相关的程序你看看
- public static class DBDictionaryEx
- {
- public static IEnumerable<T> GetAllObjects<T>(this DBDictionary dict, Transaction tr) where T : DBObject
- {
- foreach (DBDictionaryEntry e in dict)
- {
- yield return
- tr.GetObject(e.Value, OpenMode.ForRead) as T;
- }
- }
-
- public static T GetAt<T>(this DBDictionary dict, Transaction tr, string key) where T : DBObject
- {
- if (dict.Contains(key))
- {
- ObjectId id = dict.GetAt(key);
- if (!id.IsNull)
- {
- return tr.GetObject(id, OpenMode.ForRead) as T;
- }
- }
- return null;
- }
- public static void SetAt<T>(this DBDictionary dict, Transaction tr, string key, T obj) where T : DBObject
- {
- using (dict.UpgradeOpenAndRun())
- {
- dict.SetAt(key, obj);
- tr.AddNewlyCreatedDBObject(obj, true);
- }
- }
- #region GetSubDictionary
- internal static List<string> GetDictNames(string[] keys, out string key)
- {
- List<string> dictNames = new List<string>(keys);
- if (dictNames.Count > 0)
- {
- int index = dictNames.Count - 1;
- key = dictNames[index];
- dictNames.RemoveAt(index);
- }
- else
- {
- key = "*";
- }
- return dictNames;
- }
- internal static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, IEnumerable<string> dictNames)
- {
- Database db = dict.Database;
- DBDictionary subdict;
- if (createSubDictionary)
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- subdict = dict.GetAt(name).Open<DBDictionary>();
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- else
- {
- using (dict.UpgradeOpenAndRun())
- {
- subdict = new DBDictionary();
- dict.SetAt(name, subdict);
- db.AddDBObject(subdict);
- }
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- }
- }
- else
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- subdict = dict.GetAt(name).Open<DBDictionary>();
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- else
- return null;
- }
- }
- return dict;
- }
- public static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, params string[] dictNames)
- {
- return
- GetSubDictionary(
- dict,
- createSubDictionary,
- (IEnumerable<string>)dictNames);
- }
- internal static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, IEnumerable<string> dictNames)
- {
- if (createSubDictionary)
- {
- using (dict.UpgradeOpenAndRun())
- dict.TreatElementsAsHard = true;
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- dict = dict.GetAt(name).GetObject<DBDictionary>(tr);
- }
- else
- {
- DBDictionary subDict = new DBDictionary();
- dict.SetAt(tr, name, subDict);
- dict = subDict;
- dict.TreatElementsAsHard = true;
- }
- }
- }
- else
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- dict = dict.GetAt(name).GetObject<DBDictionary>(tr);
- else
- return null;
- }
- }
- return dict;
- }
- internal static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, IEnumerable<string> dictNames)
- {
- ObjectId id = obj.ExtensionDictionary;
- if (id.IsNull)
- {
- using (obj.UpgradeOpenAndRun())
- {
- obj.CreateExtensionDictionary();
- }
- id = obj.ExtensionDictionary;
- }
- DBDictionary dict = id.GetObject<DBDictionary>(tr);
- return
- GetSubDictionary(
- dict,
- tr,
- createSubDictionary,
- dictNames);
- }
- /// <summary>
- /// 获取子字典
- /// </summary>
- /// <param name="dict">根字典</param>
- /// <param name="createSubDictionary">是否创建子字典</param>
- /// <param name="dictNames">键值列表</param>
- /// <returns></returns>
- public static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, params string[] dictNames)
- {
- return
- GetSubDictionary(
- dict,
- tr,
- createSubDictionary,
- (IEnumerable<string>)dictNames);
- }
- /// <summary>
- /// 获取实体字典的子字典
- /// </summary>
- /// <param name="obj">图元对象</param>
- /// <param name="createSubDictionary">是否创建子字典</param>
- /// <param name="dictNames">键值列表</param>
- /// <returns></returns>
- public static DBDictionary GetSubDictionary(this DBObject obj, Transaction tr, bool createSubDictionary, params string[] dictNames)
- {
- return
- GetSubDictionary(
- obj,
- tr,
- createSubDictionary,
- (IEnumerable<string>)dictNames);
- }
- #endregion
- }
|
|