Change the name of the current map in the Display Manager

By Daniel Du

If you are trying to change the name of current map in display manager from “Default”? Here is the solution. To change the map name in the Display Manager, you need to get the map Id and use this map ID to get the map object, then assign a new value to the "Name" property of the map object. You will be using the Display Manager API for this.

// (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;

    }

  }

}

 

Please note that this method does not work for Map 3D 2013, it throws an exception with error message “eCreateInvalidName”, a change request have been logged, I will update this blog if there are some new information about this.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading