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

时间:2018-06-04 10:01:29

标签: text export autodesk dwg autodesk-designautomation

我想使用Autodesk Design Automation API将.dwg文件中的所有Text和Header信息提取到json对象中。这可以通过Design Automation API实现吗?

任何例子都会有所帮助。

三江源

2 个答案:

答案 0 :(得分:0)

What do you mean by "Header" information? Can you give an example?

Finding an extracting all text objects is relatively easy if you are familiar with the AutoCAD .NET API (or C++ or Lisp).

Here's an example that extracts blocks and layer names: https://github.com/Autodesk-Forge/design.automation-.net-custom.activity.sample

答案 1 :(得分:0)

@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

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