By Daniel Du
当前地图在显示管理器中默认的名字是“Default”,如果你想通过程序更改地图的名字,可以用下面的代码来实现。你需要使用Display Manager API来做。首先获取当前地图的Map ID,进而获得map对象,然后就可以为他的Name属性赋值了。注意这个方法仅适用于map 3D 2012及以前版本,在Map 3D 2013中会抛出eCreateInvalidName错误。
下面是完整实现代码:
// (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;
}
}
}