AutoCAD Map 3D Developer
Welcome to Autodeskā€™s AutoCAD Map 3D Developer Forums. Share your knowledge, ask questions, and explore popular AutoCAD Map 3D Developer topics.
cancel
Showing results forĀ 
ShowĀ Ā onlyĀ  | Search instead forĀ 
Did you mean:Ā 

Coordinate Conversion using the .NET API ???

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
1442 Views, 5 Replies

Coordinate Conversion using the .NET API ???

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.

5 REPLIES 5
Message 2 of 6
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 3 of 6
Anonymous
in reply to: 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.

Message 4 of 6
norman.yuan
in reply to: Anonymous

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

Drive CAD With Code

EESignature

Message 5 of 6
Anonymous
in reply to: norman.yuan

That worked, thanks again !!!

Message 6 of 6
TimNell
in reply to: Anonymous

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

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

ā€Boost