是否有可能使用Autodesk.AutoCAD.Interop在AutoCAD中编辑块属性? [英] Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?

查看:763
本文介绍了是否有可能使用Autodesk.AutoCAD.Interop在AutoCAD中编辑块属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个外部的WPF应用程序生成的C#图纸。我已经能够绘制,尺寸,加块和其他需要使用Autodesk.AutoCAD.Interop应用​​程序的每一件事情,但是我似乎无法填充的标题栏,或生成零件清单。

所有我见过的例子是基于需要应用程序作为一个插件在AutoCAD运行机制。事实是,插入用的线是code。使用ModelSpace.InsertLine,现在,它至少8行code!一行或两行

有没有办法实现使用Autodesk.AutoCAD.Interop这个功能?或者是有没有办法使用互操作与可从外部EXE调用插件结合?

在这个任何指针将是AP preciated。

感谢。

修改 为了说明:

  //前 - 画线与Autodesk.AutoCAD.Interop
私有静态AcadLine的DrawLine(双[]的startPoint,双[]端点)
{
    AcadLine线= ThisDrawing.ModelSpace.AddLine(的startPoint,终点);
    回线;
}
//现在 - 与Autodesk.AutoCAD.Runtime抽奖线
[使用CommandMethod(DrawLine的)]
公共静态线的DrawLine(坐标开始,协调完)
{
    //获取当前文档和数据库
    //获取当前文档和数据库
    文档acDoc = Application.DocumentManager.MdiActiveDocument;
    数据库acCurDb = acDoc.Database;

    //开始交易
    使用(交易acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        //打开块表读
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead)为BlockTable;

        //打开用于写入的块表记录模式空间
        BlockTableRecord acBlkTblRec;
        acBlkTblRec = acTrans.GetObject(acBlkTbl [BlockTableRecord.ModelSpace],OpenMode.ForWrite)为BlockTableRecord;

        //创建,在5,5开始,结束于12,3线
        行acLine =新线(start.Point3d,end.Point3d);

        acLine.SetDatabaseDefaults();

        //新的对象添加到块表记录和交易
        acBlkTblRec.AppendEntity(acLine);
        acTrans.AddNewlyCreatedDBObject(acLine,真正的);

        //保存新对象到数据库
        acTrans.Commit();
        返回acLine;
    }
}
 

解决方案

是的,你完全可以将二者结合起来的方法。

  1. 写一个进程内DLL,它确实在AutoCAD中工作。让你想通过标记你的公开的方法[使用CommandMethod(方法名)来调用可用的命令行的命令。

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

  3. 使用互操作AcadApplication,NETLOAD您的DLL,然后在命令行中调用你的工作职能。

*奖励*您可以通过互操作参数,内部命令更容易这样了。

下面是一个如何可以构建过程中的一个使用CommandMethod,然后通过COM调用它的一个例子:

  [使用CommandMethod(EditBlockAtt)
    公共无效EditBlockAtt()
    {
        VAR ACDB = HostApplicationServices.WorkingDatabase;
        VAR杆进洞= AcadApplication.DocumentManager.MdiActiveDocument.Editor;

        VAR blockNamePrompt = acEd.GetString(Environment.NewLine +输入块名称:);
        如果(blockNamePrompt.Status = PromptStatus.OK!)回报;
        VAR块名称= blockNamePrompt.StringResult;

        VAR attNamePrompt = acEd.GetString(Environment.NewLine +输入属性名称:);
        如果(attNamePrompt.Status = PromptStatus.OK!)回报;
        VAR attName = attNamePrompt.StringResult;

        VAR ACPO =新PromptStringOptions(Environment.NewLine +输入新的属性值:){AllowSpaces = TRUE};
        VAR newValuePrompt = acEd.GetString(ACPO);
        如果(newValuePrompt.Status = PromptStatus.OK!)回报;
        VAR为newValue = newValuePrompt.StringResult;

        使用(VAR acTrans = acDb.TransactionManager.StartTransaction())
        {
            VAR acBlockTable = acTrans.GetObject(acDb.BlockTableId,OpenMode.ForRead)为BlockTable;
            如果(acBlockTable == NULL)回报;

            VAR acBlockTableRecord =
                acTrans.GetObject(acBlockTable [BlockTableRecord.ModelSpace],OpenMode.ForRead)为BlockTableRecord;
            如果(acBlockTableRecord == NULL)回报;

            的foreach(在acBlockTableRecord VAR BLKID)
            {
                VAR acBlock = acTrans.GetObject(BLKID,OpenMode.ForRead)为如BlockReference;
                如果(acBlock == NULL)继续;
                如果(!acBlock.Name.Equals(块名称,StringComparison.CurrentCultureIgnoreCase))继续;
                的foreach(的ObjectId attId在acBlock.AttributeCollection)
                {
                    VAR acAtt = acTrans.GetObject(attId,OpenMode.ForRead)为AttributeReference;
                    如果(acAtt == NULL)继续;

                    如果继续(acAtt.Tag.Equals(attName,StringComparison.CurrentCultureIgnoreCase)!);

                    acAtt.UpgradeOpen();
                    acAtt.TextString =为newValue;
                }
            }

            acTrans.Commit();
        }
    }
 

然后从互操作AcadApplication,NETLOAD DLL和调用从命令行这种格式的方法:

 (命令EditBlockAtt块名称AttributeName的的NewValue)
 

不过,如果你想要去的纯互操作,这可能会得到你所需要的赐给你必须在运行时AcadDocument对象:

 的foreach(AcadEntity耳鼻喉科在acadDoc.ModelSpace)
{
    VAR块=耳鼻喉科为AcadBlockReference;
    如果(块== NULL)继续;
    {
        如果继续(block.Name.Equals(块名称,StringComparison.CurrentCultureIgnoreCase)!);
        VAR的ATT = block.GetAttributes()为对象[];
        如果(ATT以== NULL)继续;

        的foreach(var属性在atts.OfType< AcadAttributeReference>()
            。凡(属性=> attribute.TagString.Equals(AttributeName的,
                                StringComparison.CurrentCultureIgnoreCase)))
        {
            attribute.TextString =新价值;
        }
    }
}
 

也使用AutoCAD 2012的Interop库注意这一点。情况因人而异。

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 using Autodesk.AutoCAD.Interop, however I can't seem to populate The title block, or generate a parts list.

All the examples I've seen are based on the mechanism that requires the application to run as a plugin inside AutoCAD. The truth is, inserting a line used is one or two lines of code using ModelSpace.InsertLine, now, it's at least 8 lines of code!

Is there a way to achieve this functionality using the Autodesk.AutoCAD.Interop? Or is there a way to combine using the interop with a plugin that can be called from the external exe?

Any pointers on this will be appreciated.

Thanks.

EDIT To illustrate:

// 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;
    }
}

解决方案

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

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

这篇关于是否有可能使用Autodesk.AutoCAD.Interop在AutoCAD中编辑块属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆