在Map 3D显示管理器中更改当前地图的名字

简介:


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

[assemblyCommandClass(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 
转载请保留此信息。




本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2012/08/13/2636165.html ,如需转载请自行联系原作者
相关文章
|
5月前
|
小程序 API
小程序在获取当前位置信息在地图上显示
小程序在获取当前位置信息在地图上显示
103 0
|
8月前
|
Windows
更改某些软件的默认安装位置
更改某些软件的默认安装位置
82 0
X11/XWindow更改属性代码
X11/XWindow更改属性代码
75 0
|
小程序 开发者
小程序__02--navigationBarTitleText配置子页面标题,依旧显示默认标题,子页面配置不显示
navigationBarTitleText配置子页面标题,依旧显示默认标题,子页面配置不显示
|
JavaScript
使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态。点击列表的项,切换二级列表的显示或隐藏状态
使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态。点击列表的项,切换二级列表的显示或隐藏状态
388 0
使用jQuery 中的显示与隐藏动画效果实现折叠下拉菜单的收缩和展开,在页面的列表中有若干项,列表的每项中有一个二级列表,二级列表默认为隐藏状态。点击列表的项,切换二级列表的显示或隐藏状态
|
数据安全/隐私保护
标签打印软件中如何为标签文件设置密码
为了保护用标签打印软件制作的标签文件的安全性和隐私性,标签打印软件有一项对制作好的标签文件加密功能,类似文件加密一样,在打开标签的时候需要输入正确的标签密码才能打开标签。接下来就教大家在标签打印软件中如何设置标签密码。
1005 0