textexportautodeskdwgautodesk-designautomation

Autodesk Design Automation API extract Text from DWG file


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


Solution

  • @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.