Transform object Between Coordinate System

Transform object Between Coordinate System

Sgear
Advocate Advocate
3,395 Views
9 Replies
Message 1 of 10

Transform object Between Coordinate System

Sgear
Advocate
Advocate

Hi

this is how I can transform drawing Between Coordinate System see Attachment
https://goo.gl/NqAAKt

 

I ask this first in theswamp.org
https://goo.gl/hX5Fr4

 

is it poseble to loop over all object in open drawing and fransform Between Coordinate System

 

 

0 Likes
Accepted solutions (2)
3,396 Views
9 Replies
Replies (9)
Message 2 of 10

fieldguy
Advisor
Advisor
Accepted solution

this requires a vertical product like map 3d. check the geospatial platform dev guide

 

 

http://docs.autodesk.com/MAP/2014/ENU/Developer_Guides/index.html

0 Likes
Message 3 of 10

norman.yuan
Mentor
Mentor
Accepted solution

It looks like you are talking about transformation between geographic coordinate system, be it between geodedic and projected, or between projected ones.

 

The simplest way is to use AutoCAD Map/Civil3D built-in approach. Assume you have a drawing with coorinates of one projection, you can do it in different ways (but since you are asking in programming forum, I assume you need to do it programmatically):

 

1. do it with Map's drawing set query, as your unfinished (not working?) project did;

2. use Map's platform API to calculate the tramsform matrix and move all the entities (this would be bad/tedius to do). 

3. You can export all entityes in one drawing to shape/SDF, and import he shape/SDF, and then import back into a drawing with the other projected coordinate system;

4. If you not only know the 2 projections' code, but also have the knowledge of how the 2 projections are defined in AutoCD Map/Civil (there origins and the origin's difference intermed of Northing/Easting, the difference of the angle of the Nortings...), you can easily move all entities in one cordinate system to the other.

 

If you use AutoCAD Map/Civil, I'd suggest you use Map's built-in approach, eiterh attaching drawing to query in the entities in other projection, or export/import. Both can be done with AutoCAD Map ObjectARC .NET API.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 10

Sgear
Advocate
Advocate

Thanks

0 Likes
Message 5 of 10

Sgear
Advocate
Advocate

 

 

Maybe to quick to close this

 

1. the attachment project works I can transformation between coordinate system no problem there

 

 

I tray to move point from one coordinate system to another it is a little difference

I can use attachment project but I just need to know 🙂

 

in the Picture attachment you can see the difference

 

1. Blue point I use attachment project

2. Read Point I use this code

 

 

         Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
            Dim trans As Transaction = db.TransactionManager.StartTransaction()

            Dim factory As MgCoordinateSystemFactory = Nothing
            Dim catalog As MgCoordinateSystemCatalog = Nothing
            Dim coordDic As MgCoordinateSystemDictionary = Nothing

            factory = New MgCoordinateSystemFactory()

            catalog = factory.GetCatalog()
            coordDic = catalog.GetCoordinateSystemDictionary()

            Dim ISN93 As MgCoordinateSystem = coordDic.GetCoordinateSystem("ISN93")
            Dim ISN1900 As MgCoordinateSystem = coordDic.GetCoordinateSystem("Reykjavik.IcelandGrid")

            Dim coordSysFactory As New MgCoordinateSystemFactory()

            Try
                Dim selOpts As New PromptSelectionOptions()
                Dim res As PromptSelectionResult

                selOpts.SingleOnly = True
                selOpts.MessageForAdding = "Select an entity"
                res = editor.GetSelection(selOpts)
                If (res.Status <> PromptStatus.OK) Then
                    Return
                End If

                Dim id As ObjectId = res.Value.GetObjectIds(0)

                Dim ent As Entity = trans.GetObject(id, OpenMode.ForWrite, False)
                Dim mat As Matrix3d = ent.Ecs

                Dim ISN93ToISN1900 As MgCoordinateSystemTransform = factory.GetTransform(ISN93, ISN1900)


                If TypeOf ent Is DBPoint Then
                    Dim opt As DBPoint = TryCast(ent, DBPoint)
                    Dim ISN93ToISN1900Coord As MgCoordinate = ISN93ToISN1900.Transform(opt.Position.X, opt.Position.Y, opt.Position.Z)

                    editor.WriteMessage(opt.Position.X & "   " & opt.Position.Y & "   " & opt.Position.Z)

                    Dim moveVec As New Vector3d(ISN93ToISN1900Coord.X - opt.Position.X, ISN93ToISN1900Coord.Y - opt.Position.Y, ISN93ToISN1900Coord.Z - opt.Position.Z)
                    Dim moveMat As Matrix3d = Matrix3d.Displacement(moveVec)
                    ent.TransformBy(ent.Ecs.PostMultiplyBy(moveMat))

                    trans.Commit()

                End If

            Catch ex As System.Exception
                trans.Abort()
                MsgBox(ex.Message)
            Finally
                trans.Dispose()
            End Try
0 Likes
Message 6 of 10

fieldguy
Advisor
Advisor

Is your data 3D (is your Z not zero)? That's just a guess... my transformations all use Point2D.  What is the distance between the 2 points? I don't believe "UNITS" should be an issue - @norman.yuan might provide more info...

0 Likes
Message 7 of 10

Sgear
Advocate
Advocate

The data is 2D, Z is zero

the distance between the 2 point 0.024  not much but should not be

 

UNITS = Millimeters

0 Likes
Message 8 of 10

fieldguy
Advisor
Advisor

try creating a new DBPoint instead of "moving" - just to rule that part out of the problem.

 

I am not good with vb syntax anymore.

Dim ISN93ToISN1900Coord As MgCoordinate = ISN93ToISN1900.Transform(opt.Position.X, opt.Position.Y, opt.Position.Z)

create a new point2d with ISN93ToISN1900Coord.X and ISN93ToISN1900Coord.Y.

place a new DBPoint at the new point2d

 

<edit> create a new Point3D with mgcoordinate.X, Y, Z.

create a new DBPoint at new Point3D 

0 Likes
Message 9 of 10

norman.yuan
Mentor
Mentor

I am not sure where the difference bwtween using AutoCAD Map built-in command/operation and using Platform API's calculation was from, in your case.

 

I just did a quick verification, like this:

 

1. I have a shape file created in projected coordinate system "NAD83.BC/Abers", in which a point's coordinate is X=1233743.880, Y=1196519.261.

2 I want to convert this point into projected coordinate system "UTM83-10".

 

So, here is what I did in different ways and the results are all the same, the converted UTM83-10 coordinate is X=545837.500, Y=6173925.001. (well, both of the coordinate systems have unit in "METER", thus the converted number may have tiny difference in the 5th, or 6th decimal, which is ignorable):

 

1. I start a blank drawing, assign UTM83-10 coordinate system to the blank drawing. Then I use "MapImport" command to import the shape into the drawing. Then I checked the point's coordinate.

 

2. I start a blank drawing, assign it UTM83-10 coordinate system, drag the shape into the drawing, which is equivalent to use MaFDO connection to load FDO data into AutoCAD MAP. This also correctly convert the NAD83.BC/Abers coordinate into UTM83-10 coordinate, the same as 1.

 

3. I wrote code to programmatically convert the coordinate with Platform API, just as you did (with actually moving the DBPoint). the result is the same. here is the code anyway:

 

using System;
using Autodesk.AutoCAD.Geometry;
using OSGeo.MapGuide;

namespace MapPlatformCsTranform
{
    public class CoordConverter
    {
        public static void Convert(string fromCs, string toCs, Point2d fromPoint, out Point2d toPoint)
        {
            toPoint = Point2d.Origin;

            var factory = new MgCoordinateSystemFactory();
            var catalog = factory.GetCatalog();
            var coordDic = catalog.GetCoordinateSystemDictionary();

            MgCoordinateSystem  fromCoord = null;
            if (coordDic.Has(fromCs)) fromCoord = coordDic.GetCoordinateSystem(fromCs);
            if (fromCoord==null)
            {
                throw new InvalidOperationException(
                    $"Coordinate system {fromCs} is not available in AutoCAD Map.");
            }

            MgCoordinateSystem toCoord = null;
            if (coordDic.Has(toCs)) toCoord = coordDic.GetCoordinateSystem(toCs);
            if (toCoord == null)
            {
                throw new InvalidOperationException(
                    $"Coordinate system {toCs} is not available in AutoCAD Map.");
            }

            var coordTransform = factory.GetTransform(fromCoord, toCoord);
            var mgCoord = coordTransform.Transform(fromPoint.X, fromPoint.Y);

            toPoint = new Point2d(mgCoord.X, mgCoord.Y);
        }
    }
}
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

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

namespace MapPlatformCsTranform
{
    public class MyCommands 
    {
        [CommandMethod("CalcCoord")]
        public static void RunDocCommand()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            try
            {
                string sourceCs = "NAD83.BC/Abers";
                string destiCs = "UTM83-10";
                var sourcePoint = new Point2d(1233743.880, 1196519.261);
                CoordConvertion(sourceCs, destiCs, sourcePoint);

                sourceCs = "UTM83-10";
                destiCs = "NAD83.BC/Abers";
                sourcePoint = new Point2d(545837.50, 6173925.001);
                CoordConvertion(sourceCs, destiCs, sourcePoint);

            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        private static void CoordConvertion(string sourceCs, string destiCs, Point2d sourcePoint)
        {
            CoordConverter.Convert(sourceCs, destiCs, sourcePoint, out Point2d destiPoint);

            var msg = $"Output point: x={destiPoint.X}, y={destiPoint.Y}";
            CadApp.ShowAlertDialog(msg);
        }
    }
}

I did not try drawing set/attaching approach, but expect the result would be the same. So, whether you use AutoCAD built-in approaches, as I suggested in previous reply, or use AutoCAD platform API to calculate the coordinate, the result should be the same with negligible difference (because the numbers are float point numbers).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 10 of 10

Sgear
Advocate
Advocate

Thanks

 

 

How do you convert out to VB.NET

 

CoordConverter.Convert(sourceCs, destiCs, sourcePoint, out Point2d destiPoint);

 

0 Likes