0

I would like to use the Autodesk Design Automation API to extract all Text and Header information from a .dwg file into a json object. Is this possible with the Design Automation API?

Any example would help.

Thankyou

Kaliph
  • 81
  • 10

2 Answers2

1

@Kaliph, yes, without a plugin in .NET/C++/Lisp code, it is impossible to extract block attributes by script only. I'd recommend .NET. It would be easier for you to get started with if you are not familiar with C++.

Firstly, I'd suggest you take a look at the training labs of AutoCAD .NET API:

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

pick the latest version if you installed a latest version of AutoCAD. The main workflow of API is same across different versions, though. you can also pick C++ (ObjectARX) if you like.

In the tutorials above, it demos how to work with block. And the blog below talks about how to get attributes:

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

I copied here for convenience:

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();

      }

    }

  }

}

I have a sample on making signs on a drawing. It covers getting attributes and modifying attributes:

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

And I also have a sample on getting Table cells of a drawing:

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

Hope these could help you to make the plugin for your requirements.

Xiaodong Liang
  • 2,051
  • 2
  • 9
  • 14
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

Albert Szilvasy
  • 461
  • 3
  • 5
  • I think its a HYPERSTEELPAGEHEADER. Something that is described here. https://knowledge.autodesk.com/support/advance-steel/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/AdvSteel-FAQ-List/files/GUID-DB070CF8-741D-4C62-B116-EFCF8BE7F6A1-htm.html Do I have to create a custom activity for that? I am not familiar with the .net API yet. I understand Javascript a bit... – Kaliph Jun 04 '18 at 21:09
  • Ok. I've educated myself a little about HYPERSTEELPAGEHEADER. It is apparently just a block in paperspace with attributes. If you make you text extraction generic enough such that it also extracts block attributes then you'll be fine. – Albert Szilvasy Jun 07 '18 at 00:54
  • Thanks, at the moment I don´t know how an API command to extract the block attributes look like. I need an example to understand how to build the Skript. I have heard that I need to write my own C++ script to get this working. Is that right? – Kaliph Jun 07 '18 at 13:40