Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

SHP import

fieldguy
Advisor

SHP import

fieldguy
Advisor
Advisor

I am looking for some help with SHP import using mapapplication.importer.  I have the dotnet importexport example from map objectarx but the import only supports MAPINFO and MIF formats.

 

I can import the entities (closed polygons) but having trouble with creating object data table and od records parts. 

0 Likes
Reply
Accepted solutions (1)
2,919 Views
5 Replies
Replies (5)

norman.yuan
Mentor
Mentor

@fieldguy wrote:

... map objectarx but the import only supports MAPINFO and MIF formats.

 


It does support importing shape (how could it not?). You simply pass "SHP" in the importer's Init() method. As for OD data, you add an ImportDataMapping with ImportDataMapping.NewObjectDataOnly/ExistingObjectDataOnly and supply a OD table name when set the datamapping to input layer.

 

I thought the code sample from MAP objectARX SDK shows all the tricks. But I can put together a simple SHP sample code later, if you wish.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

norman.yuan
Mentor
Mentor
Accepted solution

Below is the code sample of importing SHAPE:

 

using System.IO;

using Autodesk.AutoCAD.ApplicationServices;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.Gis.Map;
using Autodesk.Gis.Map.Project;
using Autodesk.Gis.Map.ObjectData;
using Autodesk.Gis.Map.ImportExport;

namespace ImportShp
{
    public class ShpImportUtil
    {
        private Importer _mapImporter = null;
        private Document _dwg = null;
        private MapApplication _mapApp = null;

        public ShpImportUtil(Document dwg)
        {
            _dwg = dwg;
            _mapApp = HostMapApplicationServices.Application;
        }

        public void ImportShape(string shpFileName, bool attributeAsOdData)
        {
            if (!File.Exists(shpFileName))
            {
                throw new FileNotFoundException(
                    $"Cannot find shape file: \n{shpFileName}");
            }

            _mapImporter = _mapApp.Importer;

            DoImport(shpFileName, attributeAsOdData);
        }

        #region private methods

        private void DoImport(string shpFile, bool addOdData)
        {
            _mapImporter.Init("SHP", shpFile);

            var fileName = Path.GetFileNameWithoutExtension(shpFile);

            foreach (InputLayer inputLayer in _mapImporter)
            {
                // use file name as layer name
                // assume it is qualified to be AutoCAD layer name
                var layerName = fileName;
                inputLayer.SetLayerName(LayerNameType.LayerNameDirect, layerName);

                if (addOdData)
                {
                    // use file name as OD table name
                    // assume the drawing does not have an OD table with the same name
                    var odTableName = fileName;
                    inputLayer.SetDataMapping(ImportDataMapping.NewObjectDataOnly, odTableName);
                }
            }

            _mapImporter.ImportPolygonsAsClosedPolylines = true;

            var result = _mapImporter.Import(true);
            var count = result.EntitiesImported;
            var msg = $"{count} entit{(count > 1 ? "ies" : "y")} imported!";
            CadApp.ShowAlertDialog(msg);
        }

        /// <summary>
        /// Make sure a new OD Table name
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private string GetValidObjectDataTableName(string tableName)
        {
            ProjectModel proj = _mapApp.Projects.GetProject(_dwg);
            Tables tables = proj.ODTables;
            int i = 0;
            string tName = tableName;
            while (tables.IsTableDefined(tName))
            {
                i++;
                tName = tableName + "_" + i;
            }

            return tName;
        }

        #endregion
    }
}

As you can see, to let Importer to create OD data from SHAPE's attribute data, you only need to set DataMapping to the InputLayer (for shape, only one layer per shape file).

 

To use it in a CommandClass:

 

using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;

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

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

            var shpFile = GetShapeFileName();
            try
            {
                var mapUtil = new ShpImportUtil(dwg);
                mapUtil.ImportShape(shpFile, true);
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError:\n{0}.", ex.Message);
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        private static string GetShapeFileName()
        {
            var shpFile = "";
            using (var dlg = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.Title = "Select Shape File for Lmport";
                dlg.Multiselect = false;
                dlg.Filter = "Shape File *.shp|*.shp";
                if (dlg.ShowDialog()== System.Windows.Forms.DialogResult.OK)
                {
                    shpFile = dlg.FileName;
                }
            }
            return shpFile;
        }
    }
}

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

fieldguy
Advisor
Advisor

Thanks again @norman.yuan. Kudos!

Your code is easy to read.

  

0 Likes

subash.nalla
Enthusiast
Enthusiast
Hi can you share me the dotnet example from map objectarx but the import only supports MAPINFO and MIF formats code. I tried numerous ways but was not able to import the Object Data inside the MIF file. Thanks in advance.
0 Likes

norman.yuan
Mentor
Mentor

According to your post in the .NET forum, it seems you have already know how to use Autodesk.Gis.Map.ImportExport.Importer xlass to import *.MIF file. You only need to configure the Importer a bit more details: setting up each InputLayer.

 

By default, if you do nothing about the InputLayer, AutoCAD Map will use the map layer name as AutoCAD layer name and use the Coordinate System of the map, and ignore the attribute data (only import geometries). You can setting up the InputLayer as you needed: using different Coordinate system (Map will automatically does the CS conversion); using desired AutoCAD layer name; and importing attribute data as Object Data. In your case, you need to call InputLayer.SetDataMapping(). The code would be like:

 

foreach (InputLayer layer in _importer)
{

     // If needed, change the CS
     layer.TargetCoordinateSystem = layer.OriginalCoordinateSys; // or whatever CS

 

     // If needed, use desired CAD layer name, other than the default (Map layer name)

     layer.SetLayerName(LayerNameType.LayerNameDirect, "MyDesiredLayerName");


     if ([I need import OD])
     {
        layer.SetDataMapping(ImportDataMapping.NewObjectDataOnly, "[OD Table Name]")
     }
}

 

Obviously, the the import source has multiple layers, you want to make sure the layer name/OD table name do not duplicate. Usually, you would simply use the map layer name for both CAD layer name and OD Table name.

 

Hope this helps.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes