Updating an Excel spreadsheet from a linked AutoCAD table using .NET

Updating an Excel spreadsheet from a linked AutoCAD table using .NET

jayhar
Advisor Advisor
661 Views
1 Reply
Message 1 of 2

Updating an Excel spreadsheet from a linked AutoCAD table using .NET

jayhar
Advisor
Advisor

Hi,

How can insert this code in AutoCAD?

 

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows;


namespace LinkToExcel

{

  public class Commands

  {

    [CommandMethod("T2S")]

    static public void UpdateSpreadsheetFromTable()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      PromptEntityOptions opt =

        new PromptEntityOptions(

          "\nSelect table with spreadsheet to update: "

        );

      opt.SetRejectMessage(

        "\nEntity is not a table."

      );

      opt.AddAllowedClass(typeof(Table), false);

      PromptEntityResult per =

        ed.GetEntity(opt);

      if (per.Status != PromptStatus.OK)

        return;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        try

        {

          DBObject obj =

            tr.GetObject(

              per.ObjectId,

              OpenMode.ForRead

            );

          Table tb = (Table)obj;


          // It should always be a table

          // but we'll check, just in case


          if (tb != null)

          {

            // The table must be open for write


            tb.UpgradeOpen();


            // Update the data link from the table


            tb.UpdateDataLink(

              UpdateDirection.DataToSource,

              UpdateOption.ForceFullSourceUpdate

            );


            // And the spreadsheet from the data link


            ObjectId dlId = tb.GetDataLink(0, 0);

            DataLink dl =

              (DataLink)tr.GetObject(

                dlId,

                OpenMode.ForWrite

              );

            dl.Update(

              UpdateDirection.DataToSource,

              UpdateOption.ForceFullSourceUpdate

            );

          }

          tr.Commit();

          ed.WriteMessage(

            "\nUpdated the spreadsheet from the table."

          );

        }

        catch (Exception ex)

        {

          ed.WriteMessage(

            "\nException: {0}",

            ex.Message

          );

        }

      }

    }

  }

}

 

 

Jayhar.M.J

Please Subscribe YouTube Channel
https://www.youtube.com/channel/UCclj8v9vHQiFa8_DriuAk3w

Please Mark the Post or Posts as Solution(s) to help others find the answer quickly.
0 Likes
662 Views
1 Reply
Reply (1)
Message 2 of 2

moogalm
Autodesk Support
Autodesk Support

How can insert this code in AutoCAD?

I suggest you get comfortable with AutoCAD API programming, please go through AutoCAD Getting Started Tutorials

 

MyFirstAutoCADPlugin

And, self paced AutoCAD .NET Training Labs -