Friday, November 30, 2018

Exporting Shape From AutoCAD Civil/Map, 2D or 3D?

It is very common practice when using AutoCAD Civil/Map that user needs to export AutoCAD geometries as Shape file (*.shp). AutoCAD Map provides built-in command "MapExport" for doing this. AutoCAD Map also comes with ObjectARX Map APIs that includes Autodesk.Gis.Map.ImportExport namespace, which allows the shape exporting to be done programmatically. I have a few custom applications written and used from many years since AutoCAD Map 2006, exporting 2D shape files. These applications had been worked as expected, until our recent upgrade from Civil2015 to Civil2018: suddenly these custom applications exported shape file in 3D, which is not we want (for regulatory data submission).

When using "MapExport" command to export shape file, user is given a chance to decide exporting shape as 2D or 3D, as picture below shows:


In our practice at work, when manually exporting shape, we never needed to click "Driver Options.." button to decide exporting to 2D or 3D. AutoCAD Map always defaults to 2D exporting. This is also the case for my Map API code, where I had never needed to have code to set exporting driver option for the object Autodesk.Gis.Map.ImportExport.Exporter, well, until we moved to Civil/Map 2018. However, the built-in command still always defaults to 2D export, even you re-run the command after the previous command where you chose to export to 3D.

So, I had to modify my code of more than 10 years of old to force 2D export, using the methods GetDriveOptions()/SetDriverOptions() of Autodesk.Gis.Map.ImportExport.Exporter class.

Due to the poor AutoCAD Map API documentation, I ran into a obstacle: what is the value for the method SetDriveOptions()'s argument of Autodesk.Gis.Map.ImportExport.NameValueCollection type, in order to export in 2D or 3D? I searched all over the Internet and came back empty-handed. But I eventually figured it out by saving an exporting profile (*.epf file), as shown in picture below:


*.epf file is actually an XML file. The "Driver Options" for 2D/3D exporting can be easily spotted by opening this *.epf file in NotePad, as the picture shows below:



With this information finally available, I went ahead modifying my shape exporting code like this (in red):

public class  MyShapeExporter
{
    private Exporter _exporter = null;
 
    public MyShapeExporter()
    {
 
    }
 
    public void ExportClosedPolylines(IEnumerable<ObjectId> entIds, string shapeFileName)
    {
        try
        {
            _exporter = HostMapApplicationServices.Application.Exporter;
            _exporter.Init("SHP", shapeFileName);
            _exporter.SetStorageOptions(StorageType.FileOneEntityType, GeometryType.Polygon, null);
            _exporter.ClosedPolylinesAsPolygons = true;
            _exporter.SetSelectionSet(new ObjectIdCollection(entIds.ToArray()));
            var options = _exporter.GetDriverOptions();
            var opt = new Autodesk.Gis.Map.Utilities.StringPair("FDO_SHAPE_DIMENSION", "2D");
            if (!options.Contains(opt))
            {
                options.Add(opt);
            }
            _exporter.SetDriverOptions(options);
            _exporter.Export(true);
 
        }
        finally
        {
            _exporter = null;
        }
    }
}


In summary, the code I showed here in red was never needed until we moved from AutoCAD Civil/Map2015 to AutoCAD Civil/Map2018 (it could be since 2016 or 2017, which I never actually used/tested), which indicates some AutoCAD Map API behaviour change, although the manual process with built-in command "MapExport" remains the same from early version to AutoCAD Civil/Map 2018.

Obviously, with this added a few lines of code, we can now explicitly decide to export shape as 2D or 3D.




Followers

About Me

My photo
After graduating from university, I worked as civil engineer for more than 10 years. It was AutoCAD use that led me to the path of computer programming. Although I now do more generic business software development, such as enterprise system, timesheet, billing, web services..., AutoCAD related programming is always interesting me and I still get AutoCAD programming tasks assigned to me from time to time. So, AutoCAD goes, I go.