【发布时间】:2013-10-17 20:59:21
【问题描述】:
我开发了一个外部 WPF 应用程序来在 c# 中生成绘图。我已经能够使用 Autodesk.AutoCAD.Interop 绘制、标注、添加块以及应用程序所需的所有其他内容,但是我似乎无法填充标题栏或生成零件列表。
我看到的所有示例都基于要求应用程序在 AutoCAD 中作为插件运行的机制。事实是,使用ModelSpace.InsertLine插入一行是一两行代码,现在,至少是8行代码!
有没有办法使用 Autodesk.AutoCAD.Interop 实现此功能?或者有没有办法将互操作与可以从外部 exe 调用的插件结合使用?
任何关于此的指针将不胜感激。
谢谢。
编辑 举例说明:
// before - Draw Line with Autodesk.AutoCAD.Interop
private static AcadLine DrawLine(double[] startPoint, double[] endPoint)
{
AcadLine line = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint);
return line;
}
// Now - Draw line with Autodesk.AutoCAD.Runtime
[CommandMethod("DrawLine")]
public static Line DrawLine(Coordinate start, Coordinate end)
{
// Get the current document and database
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create a line that starts at 5,5 and ends at 12,3
Line acLine = new Line(start.Point3d, end.Point3d);
acLine.SetDatabaseDefaults();
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
// Save the new object to the database
acTrans.Commit();
return acLine;
}
}
【问题讨论】:
-
值得注意的是,尽管使用 AcMdg.dll 和 AcDbMdg.dll 的 AutoCAD 内部开发通常涉及更多,但与 Interop 对应物相比,它的效率很高。
-
@reckface - 请告诉我,您如何在不显式打开 AutoCad 的情况下向其发送命令。可以通过纯.Net代码来完成吗?
-
我有一个适当地称为 CAD 的包装器类。我调用 CAD.Setup(),它会创建一个新的 AutoCAD.Application(如果尚未创建),然后调用 app.Documents.Add 或创建,然后 CAD 类会从那里包装我对文档的所有调用。这是一个带有 ClickOnce 部署的纯 .Net exe 安装。