Is there a way to use the .NET API to do coordinate conversions similar to the Lisp based ade_projsetsrc, ade_projsetdest, and ade_projptforward functions. Ideally a function would wrap the Select System dialog also.
Yes. You use AutoCAD Map platform API, which is available since AutoCAD Map 2009 (Map 2008 came with preview version). Please refer to the documentation in AutoCAD Map ObjectARX SDK. be warned though, the .NET assemblies you need to set references to are named differently if you use Acad Map 2009 or use Acad Map 2010/11/12.
You also have to make sure to have Autodesk's geospatial coordinate systems set up correctly (if you use Win7, it is in folder "C:\ProgramData\Autodesk\Geospatial Coordinate Systems". Note, there is some changes in AutoCAD Map on how to set up coordinate system library between Nap 2009 and Map 2010/11/12 (I do not have Map 2013, not know if there is change on this). This setup has the same effect on the said list function and the platform .NET API on coordinate system.
Here is some code I use to convert coordinate:
private static void Convert( string sourceCoordCode, string destCoordCode, double xIn, double yIn, out double xOut, out double yOut) { xOut = 0.00; yOut = 0.00; MgCoordinateSystemFactory factory = null; MgCoordinateSystemCatalog catalog = null; MgCoordinateSystemDictionary coordDic = null; MgCoordinateSystem coordIn = null; MgCoordinateSystem coordOut = null; MgCoordinateSystemTransform coordTransform = null; MgCoordinate destCoord = null; try { factory = new MgCoordinateSystemFactory(); catalog = factory.GetCatalog(); coordDic = catalog.GetCoordinateSystemDictionary(); coordIn = coordDic.GetCoordinateSystem(sourceCoordCode); coordOut = coordDic.GetCoordinateSystem(destCoordCode); coordTransform = factory.GetTransform(coordIn, coordOut); destCoord = coordTransform.Transform(xIn, yIn); yOut = destCoord.GetY(); xOut = destCoord.GetX(); } catch { throw; } finally { if (factory != null) factory.Dispose(); if (catalog != null) catalog.Dispose(); if (coordDic != null) coordDic.Dispose(); if (coordIn != null) coordIn.Dispose(); if (coordOut != null) coordOut.Dispose(); if (coordTransform != null) coordTransform.Dispose(); if (destCoord != null) destCoord.Dispose(); } }
To use this method, something like this:
double x1=123456.00, y1=1234567.00;
double x2,y2;
Convert("UTM27-10","UTM83-10",x1, y1, out x2, out y2);
The output of converted coordinate is identical to the output of the said list functions.
HTH.
Norman Yuan
Thanks for the sample. What DLL files are referenced to define the MgCoordinate functions (2010). I can't find a mention of this anywhere and have tried loading all the managed dlls.
I am not sure with Acad Map 2010. I use Acad Map 2012. When doing Map Platform API programming, you need to set references to these assemblies at least (make sure you set "Copy Local" to "False"):
Autodesk.Map.Platform.Core.dll
OSGeo.MapGuide.Foundation.dll
OSGeo.mapGuide.Geometry.dll
OSGeo.MapGuide.PlatformBase.dll.
For the code in my sample, the MgCoordinatexxxxx classes are included in OSGeo.MapGuide.Geometry.dll assembly.
HTH
Norman Yuan
Thanks guys Just for VB .net guys out there. This is completely untested so please post any corrections but I know not everyone can convert to VB the only change I have made it to use point2d just to make it a little easier. I noted it would not be hard to make it point3d as mgcoordinate and the transform do have a .z but you will need to be careful in the conversion as the third dimension can have a large effect on the result.
(Make sure you reference the the OSgeo per post in thread)
Imports OSGeo.MapGuide Imports Autodesk.AutoCAD.Geometry Public Class MAPcoordinates Function ConvertCoord(sourceCoordCode As String, destCoordCode As String, Ipoint As Point2d) As Point2d Dim Factory As New MgCoordinateSystemFactory Dim Catalog As MgCoordinateSystemCatalog = Factory.GetCatalog Dim coordDic As MgCoordinateSystemDictionary = Catalog.GetCoordinateSystemDictionary Dim coordsource As MgCoordinateSystem = coordDic.GetCoordinateSystem(sourceCoordCode) Dim coorddest As MgCoordinateSystem = coordDic.GetCoordinateSystem(destCoordCode) Dim coordtrans As MgCoordinateSystemTransform = Factory.GetTransform(coordsource, coorddest) Dim destcoord As MgCoordinate = coordtrans.Transform(Ipoint.X, Ipoint.Y) Dim Tcoord As New Point2d(destcoord.X, destcoord.Y) End Function End Class