马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
下面的命令显示了将扩展字典条目从一个实体复制到另一个实体的过程。 最后放置一个特殊的逻辑,将与源对象相同的字典名称设置为“DeepCloneObjects”,将具有不同名称的对象复制到目标实体中。
[C#] 纯文本查看 复制代码 [CommandMethod("copyExtDic")]
public void copyExtDic()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult surRes =
ed.GetEntity("Select source entity");
if (surRes.Status != PromptStatus.OK)
return;
PromptEntityResult tarRes =
ed.GetEntity("Select target entity");
if (tarRes.Status != PromptStatus.OK)
return;
ObjectIdCollection ids = new ObjectIdCollection();
ObjectId tarId = ObjectId.Null;
ObjectId surId = ObjectId.Null;
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
DBObject dbObj = tr.GetObject(surRes.ObjectId,
OpenMode.ForRead);
surId = dbObj.ExtensionDictionary;
if (surId != ObjectId.Null)
{
DBDictionary dbExt =
(DBDictionary)tr.GetObject(surId,
OpenMode.ForRead);
foreach (DBDictionaryEntry entry in dbExt)
{
ids.Add(entry.Value);
}
}
else
{
ed.WriteMessage("No dictionary to copy");
return;
}
//find if entiy has
DBObject target = tr.GetObject(tarRes.ObjectId,
OpenMode.ForRead);
tarId = target.ExtensionDictionary;
if (tarId == ObjectId.Null)
{
target.UpgradeOpen();
target.CreateExtensionDictionary();
tarId = target.ExtensionDictionary;
}
tr.Commit();
}
IdMapping mapping = new IdMapping();
db.DeepCloneObjects(ids, tarId, mapping, false);
//
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
DBDictionary dbExt =
(DBDictionary)tr.GetObject(surId, OpenMode.ForRead);
DBDictionary dbTarg =
(DBDictionary)tr.GetObject(tarId, OpenMode.ForWrite);
foreach (IdPair pair in mapping)
{
DBObject target = tr.GetObject(pair.Value,
OpenMode.ForRead);
dbTarg.SetName(
dbTarg.NameAt(pair.Value),
dbExt.NameAt(pair.Key));
}
tr.Commit();
}
} |