在Map 3D显示管理器中更改当前地图的名字
By?Daniel Du
當(dāng)前地圖在顯示管理器中默認(rèn)的名字是“Default”,如果你想通過(guò)程序更改地圖的名字,可以用下面的代碼來(lái)實(shí)現(xiàn)。你需要使用Display Manager API來(lái)做。首先獲取當(dāng)前地圖的Map ID,進(jìn)而獲得map對(duì)象,然后就可以為他的Name屬性賦值了。注意這個(gè)方法僅適用于map 3D 2012及以前版本,在Map 3D 2013中會(huì)拋出eCreateInvalidName錯(cuò)誤。
?
下面是完整實(shí)現(xiàn)代碼:
// (C) Copyright 2012 by Autodesk
//
using?System;
using?Autodesk.AutoCAD.Runtime;
using?Autodesk.AutoCAD.ApplicationServices;
using?Autodesk.AutoCAD.DatabaseServices;
using?Autodesk.AutoCAD.Geometry;
using?Autodesk.AutoCAD.EditorInput;
using?Autodesk.Gis.Map;
using?Autodesk.Gis.Map.Project;
using?Autodesk.Gis.Map.DisplayManagement;
?
// This line is not mandatory, but improves loading performances
[assembly:?CommandClass(typeof(ChangeMapNameInDM.MyCommands))]
?
namespace?ChangeMapNameInDM
{
?
??// This class is instantiated by AutoCAD for each document when
??// a command is called by the user the first time in the context
??// of a given document. In other words, non static data in this class
??// is implicitly per-document!
??public?class?MyCommands
? {
?
????Editor?ed =?Application.DocumentManager.MdiActiveDocument.Editor;
????MapApplication?app =?HostMapApplicationServices.Application;
?
??? [Autodesk.AutoCAD.Runtime.CommandMethodAttribute("CHGMAPNAME")]
????public?void?ChangeMapName()
??? {
??????try
????? {
????????string?mapName =?null;
????????PromptResult?stringPromptResult =?null;
????????bool?succeeded =?false;
??????? stringPromptResult = ed.GetString("\nEnter the new Map name:");
?
??????? mapName = stringPromptResult.StringResult.Trim();
????????if?(stringPromptResult.Status ==?PromptStatus.OK
????????????????????????? && mapName.Length > 0)
??????? {
????????? succeeded = changeMapName(mapName);
??????? }
????????else
??????? {
????????? ed.WriteMessage("\nERROR: Invalid Map name");
??????? }
?
????????if?(!succeeded)
??????? {
????????? ed.WriteMessage("\nERROR: Name change failure");
??????? }
????? }
??????catch?(System.Exception?err)
????? {
??????? ed.WriteMessage(err.Message);
????? }
??? }
?
?
????private?bool?FindCurrentMapId(ref?ObjectId?currentMapId)
??? {
??????bool?isFound =?false;
??????// Get the project associated with the current AutoCAD document
??????ProjectModel?project =?null;
????? Autodesk.AutoCAD.DatabaseServices.TransactionManager?TM
??????? = app.ActiveProject.Database.TransactionManager;
????? project = app.ActiveProject;
?
??????try
????? {
????????using?(Transaction?trans = TM.StartTransaction())
??????? {
??????????ObjectId?managerId =?DisplayManager.Create(project)
????????????????????????????? .MapManagerId(project,?true);
??????????MapManager?manager = trans.GetObject(managerId,?OpenMode.ForRead)
??????????????????????????????as?MapManager;
??????????if?(null?!= manager)
????????? {
??????????? currentMapId = manager.CurrentMapId;
??????????? isFound =?true;
????????? }
????????? trans.Commit();
??????? }
????? }
??????catch?(Autodesk.AutoCAD.Runtime.Exception)
????? {
??????? ed.WriteMessage("\nUnable to get the current Map's Object ID.");
????? }
?
??????return?isFound;
??? }
?
????public?bool?changeMapName(string?name)
??? {
??????// Get the Object Id for the current Map
??????ObjectId?currentMapId =?new?ObjectId();
??????string?message =?"";
????? Autodesk.AutoCAD.DatabaseServices.TransactionManager?TM
??????? = app.ActiveProject.Database.TransactionManager;
?
??????if?(!FindCurrentMapId(ref?currentMapId))
????? {
????????return?false;
????? }
?
?
??????try
????? {
????????using?(Transaction?trans = TM.StartTransaction())
??????? {
??????????Map?currentMap = (Map)trans.GetObject(currentMapId,?OpenMode.ForWrite);
??????????// Change the name
????????? currentMap.Name = name;
?
????????? trans.Commit();
??????? }
?
????? }
??????catch?(Autodesk.AutoCAD.Runtime.Exception?ex)
????? {
??????? message =?string.Format("\nUnable to change name, msg:{0}",
??????????????????????????????? ex.Message);
??????? ed.WriteMessage(message);
????? }
?
??????return?true;
??? }
? }
}
?
作者:峻祁連郵箱:junqilian@163.com?
出處:http://junqilian.cnblogs.com?
轉(zhuǎn)載請(qǐng)保留此信息。
本文轉(zhuǎn)自峻祁連. Moving to Cloud/Mobile博客園博客,原文鏈接:http://www.cnblogs.com/junqilian/archive/2012/08/13/2636165.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的在Map 3D显示管理器中更改当前地图的名字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java多线程 -- ReadWrite
- 下一篇: keepalived高可用集群配置