Bug when successively launching a function I've created

Bug when successively launching a function I've created

m.chavet7MJRB
Enthusiast Enthusiast
422 Views
3 Replies
Message 1 of 4

Bug when successively launching a function I've created

m.chavet7MJRB
Enthusiast
Enthusiast

I've created a function in the following C# code that lets me import a shapefile into Civil 3D with object data.

When I run this command for the first time, it works without a hitch. When I run it a second time, the launching is interrupted.

 

The exception is as follows:

 

System.NullReferenceException: 'Object reference not set to an instance of an object.'


And occurs on the following line:

 

importer.Init("SHP", shpFiles);

 

 

 

 

 

[CommandMethod("AEPImportShape")]

public void AEPImportShape()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    MapApplication mapApp = HostMapApplicationServices.Application;
    using (Importer importer = mapApp.Importer)
    {

        Autodesk.AutoCAD.Windows.OpenFileDialog ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog("Select Shapefile", null,
            "shp", "Select shp", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);

        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            try
            {
                //Array.Sort(shpFiles);
                string shpFiles = ofd.GetFilenames()[0];
                importer.Init("SHP", shpFiles);      //!!!!!!!!!!!!!!THE PROBLEM OCCURS HERE!!!!!!!!!!!!!!!!!!!

                string fileName = Path.GetFileNameWithoutExtension(shpFiles).Replace(" ", "");

                foreach (InputLayer inputLayer in importer)
                {
                    inputLayer.SetLayerName(LayerNameType.LayerNameDirect, fileName);
                    inputLayer.SetDataMapping(ImportDataMapping.NewObjectDataOnly, fileName);
                    //inputLayer.Dispose();
                }

                importer.ImportPolygonsAsClosedPolylines = true;
                ImportResults result = importer.Import(true);

                sw.Stop();
                importer.Dispose();

                dynamic acadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
                acadApp.ZoomExtents();
            }

            catch
            {
                sw.Stop();
                importer.Dispose();

            }


        }

    }

}

 

 

0 Likes
Accepted solutions (1)
423 Views
3 Replies
Replies (3)
Message 2 of 4

Anton_Huizinga
Advocate
Advocate

If you debug the code, what is shpFiles before you use it? Null? 

0 Likes
Message 3 of 4

ActivistInvestor
Mentor
Mentor

Did You try stepping through the code to find out what variable is null?

0 Likes
Message 4 of 4

fieldguy
Advisor
Advisor
Accepted solution

I decided to try this because I already had your code from your post in the map developer forum (which is where this post belongs as well IMO).

I believe the problem is "ofd" but it is definitely not null (System.IO.File.Exists(ofd.Filename) is true).

For some reason, the managedmapapi does not like it.

Try separating your "ofd" out of the "import" method - something like this:

[CommandMethod("SHP2")]
static public void SHP2()
{
      Autodesk.AutoCAD.Windows.OpenFileDialog ofd = new Autodesk.AutoCAD.Windows.OpenFileDialog(
          "Select SHP File", null, "shp", "Select shp", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension);
      if (ofd.ShowDialog() == DialogResult.OK)
      {
          string shpname = ofd.Filename;
          Import2(shpname);
      }// 
}//acad command - SHP2

private static void Import2(string shpname)
{
    string fname = Path.GetFileNameWithoutExtension(shpname).Replace(" ", "");
    MapApplication mapApp = HostMapApplicationServices.Application;
    Importer importer = mapApp.Importer;
    try
    {
        importer.Init("SHP", shpname);
        foreach (InputLayer il in importer)
        {
            il.SetLayerName(LayerNameType.LayerNameDirect, fname);
            il.SetDataMapping(ImportDataMapping.NewObjectDataOnly, fname);
        }// foreach shp layer
        importer.ImportPolygonsAsClosedPolylines = true;
        ImportResults ir = importer.Import(true);
    }
    catch { }
    finally { };
}// Import2
0 Likes