0

我想使用 Autodesk Design Automation API 将 .dwg 文件中的所有文本和标题信息提取到 json 对象中。设计自动化 API 可以做到这一点吗?

任何例子都会有所帮助。

谢谢

4

2 回答 2

1

@Kaliph,是的,如果没有 .NET/C++/Lisp 代码中的插件,仅通过脚本提取块属性是不可能的。我推荐.NET。如果您不熟悉 C++,上手会更容易。

首先,我建议你看看 AutoCAD .NET API 的培训实验室:

https://www.autodesk.com/developer-network/platform-technologies/autocad

如果您安装了最新版本的 AutoCAD,请选择最新版本。不过,不同版本的 API 的主要工作流程是相同的。如果你愿意,你也可以选择 C++ (ObjectARX)。

在上面的教程中,它演示了如何使用块。下面的博客讨论了如何获取属性:

http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html

为了方便,我在这里复制:

using Autodesk.AutoCAD;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;


namespace MyApplication

{

  public class DumpAttributes

  {

    [CommandMethod("LISTATT")]

    public void ListAttributes()

    {

      Editor ed =

        Application.DocumentManager.MdiActiveDocument.Editor;

      Database db =

        HostApplicationServices.WorkingDatabase;

      Transaction tr =

        db.TransactionManager.StartTransaction();


      // Start the transaction

      try

      {

        // Build a filter list so that only

        // block references are selected

        TypedValue[] filList = new TypedValue[1] {

          new TypedValue((int)DxfCode.Start, "INSERT")

        };

        SelectionFilter filter =

          new SelectionFilter(filList);

        PromptSelectionOptions opts =

          new PromptSelectionOptions();

        opts.MessageForAdding = "Select block references: ";

        PromptSelectionResult res =

          ed.GetSelection(opts, filter);


        // Do nothing if selection is unsuccessful

        if (res.Status != PromptStatus.OK)

          return;


        SelectionSet selSet = res.Value;

        ObjectId[] idArray = selSet.GetObjectIds();

        foreach (ObjectId blkId in idArray)

        {

          BlockReference blkRef =

            (BlockReference)tr.GetObject(blkId,

              OpenMode.ForRead);

          BlockTableRecord btr =

            (BlockTableRecord)tr.GetObject(

              blkRef.BlockTableRecord,

              OpenMode.ForRead

            );

          ed.WriteMessage(

            "\nBlock: " + btr.Name

          );

          btr.Dispose();


          AttributeCollection attCol =

            blkRef.AttributeCollection;

          foreach (ObjectId attId in attCol)

          {

            AttributeReference attRef =

              (AttributeReference)tr.GetObject(attId,

                OpenMode.ForRead);


            string str =

              ("\n  Attribute Tag: "

                + attRef.Tag

                + "\n    Attribute String: "

                + attRef.TextString

              );

            ed.WriteMessage(str);

          }

        }

        tr.Commit();

      }

      catch (Autodesk.AutoCAD.Runtime.Exception ex)

      {

        ed.WriteMessage(("Exception: " + ex.Message));

      }

      finally

      {

        tr.Dispose();

      }

    }

  }

}

我有一个在图纸上制作标志的样本。它涵盖了获取属性和修改属性:

https://forge.autodesk.com/cloud_and_mobile/2016/02/sign-title-block-of-dwg-file-with-autocad-io-view-data-api.html

我还有一个关于获取绘图表格单元格的示例:

https://forge.autodesk.com/blog/get-cell-data-autocad-table-design-automation-api

希望这些可以帮助您根据您的要求制作插件。

于 2018-06-11T02:41:22.993 回答
0

“标题”信息是什么意思?能给我举个例子吗?

如果您熟悉 AutoCAD .NET API(或 C++ 或 Lisp),找到提取所有文本对象的方法相对容易。

这是一个提取块和层名称的示例: https ://github.com/Autodesk-Forge/design.automation-.net-custom.activity.sample

于 2018-06-04T16:42:54.067 回答