Author Topic: Setting the Global Coordinate System Programatically  (Read 2531 times)

0 Members and 1 Guest are viewing this topic.

autogis

  • Guest
Setting the Global Coordinate System Programatically
« on: October 05, 2016, 02:33:57 PM »
The command to assign a Global Coordinate System in Autocad Map3D 2015 is MAPCSASSIGN.   My question is how do I set this programatically using ObjectArx?  Here is my failed attempt at doing so:

Code: [Select]
// Start a transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    // Open the UCS table for read
    UcsTable ut = tr.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;

    // Open the UCS table record Model space for write (Get Existing EPSG 4269)
    UcsTableRecord utr = tr.GetObject(ut["4269"], OpenMode.ForWrite) as UcsTableRecord;

    // Open the active viewport
    ViewportTableRecord acVportTblRec = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;

    // Set the UCS current
    acVportTblRec.SetUcs(utr.ObjectId);
    doc.Editor.UpdateTiledViewportsFromDatabase();

    // Display the name of the current UCS
    UcsTableRecord acUCSTblRecActive = tr.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord;

    acApp.ShowAlertDialog("The current UCS is: " + acUCSTblRecActive.Name);

    // Save the new object(s) to the database
    tr.Commit();

}

But I am getting a key not found error.  Any help would be appreciated.  Thanks




autogis

  • Guest
Re: Setting the Global Coordinate System Programatically
« Reply #1 on: October 07, 2016, 11:33:30 AM »
Problem solved.  I was using the wrong code to set the Global Coordinate System Programatically.

Note: In order to use the below code, besides adding a reference to AcCoreMgd,AcDbMdg and AcMdg, you also need to add a reference to ManagedMapApi (..Program Files\Autodesk\AutoCAD 2015\Map\ManagedMapApi.dll, set Copy Local to False).

Here is the working code:
Code: [Select]
// Lock Document
using (DocumentLock docLock = doc.LockDocument())
{

// There is no API to set a coordinate system using AcMapMap
// The only way you can set the coordinate system is via ProjectModel
Autodesk.Gis.Map.MapApplication mapApp = Autodesk.Gis.Map.HostMapApplicationServices.Application;
Autodesk.Gis.Map.Project.ProjectModel projModel = mapApp.ActiveProject;
ed.WriteMessage("\n BEFORE Current Projection is : " + projModel.Projection + "\n");

// Set projection to EPSG 4269 (LL83)
projModel.Projection = "LL83";

ed.WriteMessage("\n AFTER Current Projection is : " + projModel.Projection + "\n");
}