马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- [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();
- }
- }
|