【求助】如何遍历字典
如何遍历字典,搞不懂Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
DBDictionary NOD = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
foreach (DBDictionary d in NOD) //这里错了
{
。。。
}
错误 1 无法将类型“Autodesk.AutoCAD.DatabaseServices.DBDictionaryEntry”转换为“Autodesk.AutoCAD.DatabaseServices.DBDictionary” C:\Documents and Settings\888888\My Documents\Visual Studio 2008\Projects\Lab1\Lab1\Class1.cs 379 17 Lab1
都定义为DBDictionary 了,怎变成了DBDictionaryEntry?
保存在字典的是DBDictionaryEntry
DBDictionaryEntry是键值对,即key-objectid
但是我的 d和NOD都定义为DBDictionary啊,foreach不能用了……
我遍历字典的目的是
1.找到键值为“AAAA”的项
2.返回它的 父节点(ID和键值)和兄弟节点(ID和键值) 本帖最后由 lzh741206 于 2010-12-24 14:35 编辑
虽然字典的组织像一个Tree,但它确实不是以Tree的形式储存的
下面的代码是的TlsBasal里关于字典的部分操作,你可以看看
另外,父节点可以通过打开Id后获取实体的OwnerId属性获取
/// <summary>
/// 获取子字典
/// </summary>
/// <param name="dict">根字典</param>
/// <param name="createSubDictionary">是否创建子字典</param>
/// <param name="dictNames">键值列表</param>
/// <returns></returns>
public DBDictionary GetSubDictionary(DBDictionary dict, bool createSubDictionary, params string[] dictNames)
{
if (createSubDictionary)
{
using (dict.UpgradeOpenAndRun())
{
dict.TreatElementsAsHard = true;
}
foreach (string name in dictNames)
{
if (dict.Contains(name))
{
dict = GetObject<DBDictionary>(dict.GetAt(name));
}
else
{
DBDictionary subDict = new DBDictionary();
dict.SetAt(name, subDict);
Transaction.AddNewlyCreatedDBObject(subDict, true);
dict = subDict;
dict.TreatElementsAsHard = true;
}
}
}
else
{
foreach (string name in dictNames)
{
if (dict.Contains(name))
{
dict = GetObject<DBDictionary>(dict.GetAt(name));
}
else
{
return null;
}
}
}
return dict;
} //保存和获取数据
#region Value
/// <summary>
/// 保存数据到字典
/// </summary>
/// <param name="value">数据</param>
/// <param name="dict">字典</param>
/// <param name="key">键值</param>
public void SetToDictionary(DBObject value, DBDictionary dict, string key)
{
if (dict.Contains(key))
dict.Remove(key);
dict.SetAt(Transaction, key, value);
}
/// <summary>
/// 从字典中获取数据
/// </summary>
/// <param name="dict">字典</param>
/// <param name="key">键值</param>
/// <returns>数据</returns>
public DBObject GetFromDictionary(DBDictionary dict, string key)
{
if (dict != null)
{
if (dict.Contains(key))
{
DBObject obj = GetObject(dict.GetAt(key));
return obj;
}
}
return null;
}
/// <summary>
/// 保存数据到实体字典
/// </summary>
/// <param name="value">数据</param>
/// <param name="obj">图元实体</param>
/// <param name="keys">键值列表</param>
public void SetToDictionary(DBObject value, DBObject obj, params string[] keys)
{
string key;
var dictnames = GetDictNames(keys, out key);
ObjectId id = obj.ExtensionDictionary;
if (id == ObjectId.Null)
{
using (obj.UpgradeOpenAndRun())
{
obj.CreateExtensionDictionary();
}
id = obj.ExtensionDictionary;
}
DBDictionary dict = GetSubDictionary(id, true, dictnames);
SetToDictionary(value, dict, key);
}
/// <summary>
/// 保存数据到命名字典
/// </summary>
/// <param name="value">数据</param>
/// <param name="keys">键值列表</param>
public void SetToDictionary(DBObject value, params string[] keys)
{
string key;
var dictnames = GetDictNames(keys, out key);
DBDictionary dict = GetSubDictionary(Database.NamedObjectsDictionaryId, true, dictnames);
SetToDictionary(value, dict, key);
}
/// <summary>
/// 从对象字典中获取数据
/// </summary>
/// <param name="obj">图元实体</param>
/// <param name="keys">键值列表</param>
/// <returns>数据</returns>
public DBObject GetFromDictionary(DBObject obj, params string[] keys)
{
string key;
var dictnames = GetDictNames(keys, out key);
ObjectId id = obj.ExtensionDictionary;
DBDictionary dict = GetSubDictionary(id, false, dictnames);
return GetFromDictionary(dict, key);
}
/// <summary>
/// 从命名字典中获取数据
/// </summary>
/// <param name="keys">键值列表</param>
/// <returns>数据</returns>
public DBObject GetFromDictionary(params string[] keys)
{
string key;
var dictnames = GetDictNames(keys, out key);
DBDictionary dict = GetSubDictionary(Database.NamedObjectsDictionaryId, false, dictnames);
return GetFromDictionary(dict, key);
}
#endregion 飞狐兄威武
页:
[1]