Civil 3D提供的基于.net的API和基于COM的API,现在推荐大家使用.net API,但有时也需要使用COM API。 这个例子演示如何使用COM API来导入DEM数据生成Civil 3D曲面。 Civil 3D开发中使用COM API需要添加的引用比较啰嗦,建议使用向导创建项目简化操作,向导能帮助你添加大部分引用,如果还缺什么,自己再添加就简单多了。注意使用COM API,需要勾选AutoCAD 和Civil 3D的interOp相关引用。
下面是使用COM API导入DEM数据的代码:
// (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";
string m_sAeccAppProgId = "AeccXUiLand.AeccApplication.10.0"; //Civil 3D 2013
//string m_sAeccAppProgId = "AeccXUiLand.AeccApplication.9.0"; //Civil 3D 2012
private string m_sMessage = "";
[CommandMethod("LoadDemSurfaceop")]
public void LoadDemSurface()
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "USGS DEM( *.DEM) |*.dem|GEOTIFF(*.tif)|*.tif|ESRI ASCII 栅格(*.asc)|*.asc|ESRI ASCII 栅格(*.txt)|*.txt|ESRI 二进制栅格(*.adf)|*.adf";
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
{
}
}
}
}
本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2013/01/29/2881124.html
,如需转载请自行联系原作者