【问题标题】:Autodesk Design Automation API extract Text from DWG fileAutodesk Design Automation API 从 DWG 文件中提取文本
【发布时间】:2018-11-13 15:15:06
【问题描述】:

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

任何例子都会有所帮助。

谢谢

【问题讨论】:

    标签: text export autodesk dwg autodesk-designautomation


    【解决方案1】:

    “标题”信息是什么意思?可以举个例子吗?

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

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

    【讨论】:

    • 我认为它是一个 HYPERSTEELPAGEHEADER。这里描述的东西。 knowledge.autodesk.com/support/advance-steel/learn-explore/caas/… 我必须为此创建自定义活动吗?我还不熟悉.net API。我有点懂 Javascript...
    • 好的。我对 HYPERSTEELPAGEHEADER 有了一些了解。它显然只是具有属性的纸空间中的一个块。如果你让你的文本提取足够通用,以至于它也提取块属性,那么你会没事的。
    • 谢谢,目前我不知道提取块属性的 API 命令是什么样子的。我需要一个示例来了解如何构建 Skript。我听说我需要编写自己的 C++ 脚本才能使其正常工作。对吗?
    【解决方案2】:

    @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-10-10
      • 2017-06-30
      • 2020-05-18
      • 2017-08-14
      • 2020-11-19
      • 2018-01-29
      • 2019-03-14
      • 2021-12-03
      • 2018-11-13
      相关资源
      最近更新 更多