By Daniel Du
Civil 3D provides .NET API and COM API, you are recommended to use .NET API if it is available, although this post will demo how to import DEM with COM API.
To use COM API, you need to add some references with interop, it is convenient to use AutoCAD .net wizard, it supports Civil 3D as well as other AutoCAD vertical products. Please check following references with wizard, and please note that you may add other references if some are missing.
The code snippet goes as below:
// (C) Copyright 2013 by Autodesk
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using Autodesk.AECC.Interop.UiLand;
using Autodesk.AutoCAD.Interop;
using Autodesk.Civil.ApplicationServices;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(LoadDEM_demo.MyCommands))]
namespace LoadDEM_demo
{
public class MyCommands
{
// COM objects:
private Autodesk.AutoCAD.Interop.IAcadApplication m_oAcadApp = null;
private Autodesk.AECC.Interop.UiLand.IAeccApplication m_oAeccApp = null;
private Autodesk.AECC.Interop.UiLand.IAeccDocument m_oAeccDoc = null;
private Autodesk.AECC.Interop.Land.IAeccDatabase m_oAeccDb = null;
string m_sAcadProdID = "AutoCAD.Application";
//Civil 3D 2013
string m_sAeccAppProgId = "AeccXUiLand.AeccApplication.10.0";
//Civil 3D 2012
//string m_sAeccAppProgId = "AeccXUiLand.AeccApplication.9.0";
private string m_sMessage = "";
[CommandMethod("LoadDemSurfaceop")]
public void LoadDemSurface()
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "USGS DEM( *.DEM) |*.dem";
ofd.FilterIndex = 0;
if (ofd.ShowDialog() == DialogResult.OK)
{
//use COM
m_oAcadApp = (IAcadApplication)System.Runtime.InteropServices
.Marshal.GetActiveObject(m_sAcadProdID);
if (m_oAcadApp != null)
{
m_oAeccApp = (IAeccApplication)m_oAcadApp
.GetInterfaceObject(m_sAeccAppProgId);
m_oAeccDoc = (IAeccDocument)m_oAeccApp.ActiveDocument;
m_oAeccDoc.Surfaces.ImportDEM(ofd.FileName);
}
m_oAeccDoc.Regen(Autodesk.AutoCAD.Interop.Common
.AcRegenType.acActiveViewport);
}
else
{
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
}
}
}
}