Translating between drawing point and geographic location using ObjectARX

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

Here is a code snippet to convert drawing point to a geo-location and vice-versa in a geo-located drawing. If you are looking for a .Net code sample, please refer to this blog post.

 Acad::ErrorStatus es;
 
 AcDbDatabase *pDb = 
 acdbHostApplicationServices()->workingDatabase();
 
 AcDbObjectId geodataId = AcDbObjectId::kNull;
 acdbGetGeoDataObjId(pDb, geodataId);
 
 <span>if</span><span> (geodataId.isNull() == Adesk::kFalse)</span>
 <span>{</span>
     AcDbTransactionManager *pTM 
                     = pDb->transactionManager();
 
 	AcTransaction *pTransaction 
                         = pTM->startTransaction(); 
 		
 	AcDbObject *pObj = NULL;
 	pTransaction->getObject(pObj, 
                             geodataId, 
                             AcDb::kForRead);
 
 	AcDbGeoData *pGeoData = AcDbGeoData::cast(pObj);
 	<span>if</span><span> (pGeoData != NULL)</span>
 	<span>{</span>
 	    pGeoData->upgradeOpen();
 
         <span>// convert from drawing point to Geolocation</span><span> </span>
         AcGePoint3d geoPoint(dblLongitude, 
                              dblLatitude, 
                              dblAltitude);
 
         AcGePoint3d drawingPt = AcGePoint3d::kOrigin; 
         es = pGeoData->transformFromLonLatAlt(
                             geoPoint, drawingPt);
 
         <span>// convert from Geolocation to drawing point</span><span> </span>
         es = pGeoData->transformToLonLatAlt(
                 dblDwgX, dblDwgY, dblDwgZ, 
                 dblLongitude, dblLatitude, dblAltitude);
 
 		pTM->endTransaction();
 	<span>}</span>
 	<span>else</span><span> </span>
 		pTM->abortTransaction();
 <span>}</span>
 


Comments

2 responses to “Translating between drawing point and geographic location using ObjectARX”

  1. Thanks for posting this code. I was just looking for some training material like this.

  2. Hi,
    I’m a Civil 3D user and I’m trying to implement this code to convert local coordinates (x,y) to UTM grid coordinates (north, east):
    Dim gd As GeoLocationData = DB.GeoDataObject.GetObject(OpenMode.ForRead)
    Dim m As Matrix3d = Matrix3d.Displacement(gd.DesignPoint.GetVectorTo(gd.ReferencePoint))
    m = m * Matrix3d.Rotation(gd.NorthDirection, gd.UpDirection, gd.DesignPoint)
    m = m * Matrix3d.Scaling(gd.ScaleFactor, gd.DesignPoint)
    Dim ptUTM as Point3d = ptLOCAL.TransformBy(m)
    But, gd.ScaleFactor always return 1, even if gd.ScaleEstimationMethod = ScaleEstimationMethod.ScaleEstMethodReferencePoint
    Note: Civil 3D return correct value:
    Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument.Settings.DrawingSettings.TransformationSettings.GridScaleFactor
    What is wrong? Did I miss something?

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading