3

我开发了一个外部 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;
    }
}
4

1 回答 1

6

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

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

  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 互操作库。YMMV。

于 2013-10-10T09:57:14.320 回答