Autodesk Design Automation API 从 DWG 文件中提取文本

Autodesk Design Automation API extract Text from DWG file

我想使用 Autodesk Design Automation API 将 .dwg 文件中的所有文本和页眉信息提取到 json 对象中。这可以通过设计自动化实现吗 API?

任何例子都会有所帮助。

谢谢

"Header" 信息是什么意思?能举个例子吗?

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

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

@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

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

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

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