gpt4 book ai didi

c# - 是否可以使用 Autodesk.AutoCAD.Interop 在 AutoCAD 中编辑 block 属性?

转载 作者:行者123 更新时间:2023-11-30 14:31:55 31 4
gpt4 key购买 nike

我开发了一个外部 WPF 应用程序来用 C# 生成绘图。我已经能够使用 Autodesk.AutoCAD.Interop 绘制、标注尺寸、添加 block 以及应用程序所需的所有其他内容,但是我似乎无法填充标题栏或生成部件列表。

我见过的所有示例都基于要求应用程序作为插件在 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;
}
}

最佳答案

是的,您完全可以将这两种方法结合起来。

  1. 编写在 AutoCAD 中执行工作的进程内 DLL。通过使用 [CommandMethod("MethodName")] 标记您的公共(public)方法,使您希望调用的命令可用于命令行。

  2. 通过互操作调用启动或连接 AutoCAD。

  3. 使用互操作 AcadApplication,网络加载您的 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();
}
}

然后从互操作 AcadApplication 中,网络加载 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 Interop 库。 YMMV.

关于c# - 是否可以使用 Autodesk.AutoCAD.Interop 在 AutoCAD 中编辑 block 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19290030/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com