Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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();
}
}
}
}
Solved! Go to Solution.