是否可以使用 Autodesk.AutoCAD.Interop 在 AutoCAD 中编辑块属性
本文关键字:AutoCAD 编辑 属性 Interop 可以使 Autodesk 是否 | 更新日期: 2023-09-27 18:34:38
我开发了一个外部 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;
}
}
是的,您绝对可以结合使用这两种方法。
-
编写在AutoCAD中执行工作的进程内DLL。 通过使用 [CommandMethod("MethodName"(] 标记公共方法,使要调用的命令可用于命令行。
-
通过互操作调用启动或连接 AutoCAD。
-
使用互操作 Acad应用程序,对 DLL 进行网络加载,然后从命令行调用工作函数。
*奖金 * 您也可以通过这种方式更轻松地将互操作参数传递给内部命令。
下面是如何在进程中生成命令方法,然后通过 COM 调用它的示例:
[CommandMethod("EditBlockAtt")]
public void EditBlockAtt()
{
var acDb = HostApplicationServices.WorkingDatabase;
var acEd = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
var blockNamePrompt = acEd.GetString(Environment.NewLine + "Enter block name: ");
if (blockNamePrompt.Status != PromptStatus.OK) return;
var blockName = blockNamePrompt.StringResult;
var attNamePrompt = acEd.GetString(Environment.NewLine + "Enter attribute name: ");
if (attNamePrompt.Status != PromptStatus.OK) return;
var attName = attNamePrompt.StringResult;
var acPo = new PromptStringOptions(Environment.NewLine + "Enter new attribute value: "){ AllowSpaces = true };
var newValuePrompt = acEd.GetString(acPo);
if (newValuePrompt.Status != PromptStatus.OK) return;
var newValue = newValuePrompt.StringResult;
using (var acTrans = acDb.TransactionManager.StartTransaction())
{
var acBlockTable = acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (acBlockTable == null) return;
var acBlockTableRecord = acTrans.GetObject(acBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
if (acBlockTableRecord == null) return;
foreach (var blkId in acBlockTableRecord)
{
var acBlock = acTrans.GetObject(blkId, OpenMode.ForRead) as BlockReference;
if (acBlock == null) continue;
if (!acBlock.Name.Equals(blockName, StringComparison.CurrentCultureIgnoreCase)) continue;
foreach (ObjectId attId in acBlock.AttributeCollection)
{
var acAtt = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
if (acAtt == null) continue;
if (!acAtt.Tag.Equals(attName, StringComparison.CurrentCultureIgnoreCase)) continue;
acAtt.UpgradeOpen();
acAtt.TextString = newValue;
}
}
acTrans.Commit();
}
}
然后,从互操作 Acad应用程序中,网络加载 dll 并按以下格式从命令行调用该方法:
(Command "EditBlockAtt" "BlockName" "AttributeName" "NewValue")
但是,如果您想采用纯互操作,这可能会为您提供所需的内容,因为您在运行时有一个 AcadDocument 对象:
foreach (AcadEntity ent in acadDoc.ModelSpace)
{
var block = ent as AcadBlockReference;
if (block == null) continue;
{
if (!block.Name.Equals("BlockName", StringComparison.CurrentCultureIgnoreCase)) continue;
var atts = block.GetAttributes() as object[];
if (atts == null) continue;
foreach (var attribute in atts.OfType<AcadAttributeReference>()
.Where(attribute => attribute.TagString.Equals("AttributeName",
StringComparison.CurrentCultureIgnoreCase)))
{
attribute.TextString = "New Value";
}
}
}
另请注意,这是使用 AutoCAD 2012 互操作库。 扬子晚报.