是否可以使用Autodesk.AutoCAD.Interop在AutoCAD中编辑块属性?

时间:2013-10-10 07:52:08

标签: c# .net autocad

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

我见过的所有示例都基于要求应用程序在AutoCAD中作为插件运行的机制。事实是,使用ModelSpace.InsertLine插入一行使用的是一行或两行代码,现在,它至少有8行代码!

有没有办法使用Autodesk.AutoCAD.Interop实现此功能?或者有没有办法将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 个答案:

答案 0 :(得分:6)

是的,你绝对可以将这两种方法结合起来。

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

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

  3. 使用互操作AcadApplication,netload你的DLL,然后从命令行调用你的工作函数。

  4. * Bonus *您也可以通过这种方式更轻松地将互操作参数传递给内部命令。

    以下是如何在进程中构建命令方法然后通过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中,netload dll并以这种格式从命令行调用该方法:

    (Command "EditBlockAtt" "BlockName" "AttributeName" "NewValue")
    

    但是如果你想要纯粹的Interop,那么在运行时你有一个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。