本地需要安装RealDWG,将安装目录路径加入到环境变量Path中
项目引用:acdbmgd.dll
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace CAD_DataExtract
{
public class Program
{
static void Main(string[] args)
{
string fileName = @"AutoCAD_RealDwgExtract.dwg";
HostAppServices dwg = new HostAppServices();
RuntimeSystem.Initialize(dwg, 1033);
Database db = new Database(false, true);
var cancellationTokenSource = new CancellationTokenSource(120000);
try
{
var dbCreationTask = Task.Factory.StartNew(() => LoadFile(db, fileName), cancellationTokenSource.Token);
dbCreationTask.Wait();
}
catch (AggregateException e) when ((e.InnerException as TaskCanceledException) != null)
{
Console.WriteLine("Database初始化超时。");
}
catch (AggregateException e) when (e.InnerException != null)
{
throw e.InnerException;
}
catch
{
throw;
}
}
/// <summary>
/// 载入文件
/// </summary>
/// <param name="db">数据库对象</param>
/// <param name="dwgPath">文件地址</param>
private static void LoadFile(Database db, string dwgPath)
{
db.ReadDwgFile(dwgPath, FileShare.ReadWrite, true, "");
db.CloseInput(true);
}
}
public class HostAppServices : HostApplicationServices
{
public override string FindFile(string fileName, Database database, FindFileHint hint)
{
throw new NotImplementedException();
}
}
}