Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?

后端 未结 1 1647
轮回少年
轮回少年 2021-01-06 13:15

I have developed an external WPF application to generate drawings in c#. I have been able to draw, dimension, add blocks and every thing else required by the application usi

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 13:33

    Yes, you can absolutely combine the two approaches.

    1. Write an in-process DLL that does the work in a AutoCAD. Make the commands you wish to call available to the command line by flagging your public methods with [CommandMethod("MethodName")].

    2. Get AutoCAD started or connected via interop.

    3. Using the interop AcadApplication, netload your DLL, and then call your work functions from the command line.

    *Bonus * You can pass interop parameters to internal commands much easier this way too.

    Here's an example of how you could build a commandmethod in-process and then call it via 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();
        }
    }
    

    Then from an interop AcadApplication, netload the dll and call the method from the commandline in this format:

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

    However if you want to go pure Interop, this may get you what you need given you have an AcadDocument object at runtime:

    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()
                .Where(attribute => attribute.TagString.Equals("AttributeName", 
                                    StringComparison.CurrentCultureIgnoreCase)))
            {
                attribute.TextString = "New Value";
            }
        }
    }
    

    Also note this is using the AutoCAD 2012 Interop libraries. YMMV.

    0 讨论(0)
提交回复
热议问题