Setting coordinate system for map using .net API

By Daniel Du

Issue

Using GetMapSRS() API we can get the coordinate system of map, but I am not finding any API to assign a coordinate system to the AcMapMap object. Am I missing something or is it not allowed to assign a CS to AcMapMap ?

Solution

There is no API to set a coordinate system using AcMapMap.  The only way you can set the coordinate system is via ProjectModel. The GetSRS method on AcMapMap only reflects what’s set on the map project.

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.Platform;

using OSGeo.MapGuide;

 

// This line is not mandatory, but improves loading performances

[assembly: CommandClass(typeof(SetMapSRS.MyCommands))]

 

namespace SetMapSRS

{

 

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

  {

    [CommandMethod("SetMapCoordinateSystem")]

    public void SetMapCoordinateS
ystem()

    {

      //the parameter is coordinate system code

      SetMapSRS("CA-I");

 

      //checking

      string wkt = AcMapMap.GetCurrentMap().GetMapSRS();

      Editor ed = Autodesk.AutoCAD.ApplicationServices.Application

        .DocumentManager.MdiActiveDocument.Editor;

      MgCoordinateSystemFactory coordSysFac = new MgCoordinateSystemFactory();

      string code = coordSysFac.ConvertWktToCoordinateSystemCode(wkt);

      ed.WriteMessage("\n MapSRS is Changed to: " + code + "\n");

 

 

    }

 

    public void SetMapSRS(string srsCode)

    {

      Autodesk.Gis.Map.MapApplication mapApp = Autodesk.Gis.Map

        .HostMapApplicationServices.Application;

      Autodesk.Gis.Map.Project.ProjectModel projModel = mapApp.ActiveProject;

 

      projModel.Projection = srsCode;

    }

 

  }

 

}

 

Hope this helps.


Comments

One response to “Setting coordinate system for map using .net API”

  1. Autodesk.Gis.Map.Project.ProjectModel required ManagedMapApi.dll as reference in VB.Net 2010 but it is producing following error.
    Attempt was made to load an assembly with an incorrect format: C:\Program Files\Autodesk\AutoCAD Civil 3D 2013\ManagedMapApi.dll.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading