Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Showing a splash-screen from your AutoCAD .NET application | Main | Creating an AutoCAD table containing block images using .NET »

    June 06, 2007

    Creating an AutoCAD table using .NET

    This suggestion came in a few weeks ago from Kélcyo Pereira, and I've borrowed some code from Sreekar Devatha, from DevTech India, to help implement the suggestion.

    The following C# code creates a very simple table and inserts it at the position selected by the user. The table is really very simply - a 5 (row) x 3 (column) table created from string values, no other data-types. It picks up the current style and aligns each cell as "middle, center". That's really all there is to it.

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.DatabaseServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Geometry;

    using Autodesk.AutoCAD.Runtime;


    namespace TableCreation

    {

      public class Commands

      {

        [CommandMethod("CRT")]

        static public void CreateTable()

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;


          PromptPointResult pr =

            ed.GetPoint("\nEnter table insertion point: ");

          if (pr.Status == PromptStatus.OK)

          {

            Table tb = new Table();

            tb.TableStyle = db.Tablestyle;

            tb.NumRows = 5;

            tb.NumColumns = 3;

            tb.SetRowHeight(3);

            tb.SetColumnWidth(15);

            tb.Position = pr.Value;


            // Create a 2-dimensional array

            // of our table contents

            string[,] str = new string[5, 3];

            str[0, 0] = "Part No.";

            str[0, 1] = "Name ";

            str[0, 2] = "Material ";

            str[1, 0] = "1876-1";

            str[1, 1] = "Flange";

            str[1, 2] = "Perspex";

            str[2, 0] = "0985-4";

            str[2, 1] = "Bolt";

            str[2, 2] = "Steel";

            str[3, 0] = "3476-K";

            str[3, 1] = "Tile";

            str[3, 2] = "Ceramic";

            str[4, 0] = "8734-3";

            str[4, 1] = "Kean";

            str[4, 2] = "Mostly water";


            // Use a nested loop to add and format each cell

            for (int i = 0; i < 5; i++)

            {

              for (int j = 0; j < 3; j++)

              {

                tb.SetTextHeight(i, j, 1);

                tb.SetTextString(i, j, str[i, j]);

                tb.SetAlignment(i, j, CellAlignment.MiddleCenter);

              }

            }

            tb.GenerateLayout();


            Transaction tr =

              doc.TransactionManager.StartTransaction();

            using (tr)

            {

              BlockTable bt =

                (BlockTable)tr.GetObject(

                  doc.Database.BlockTableId,

                  OpenMode.ForRead

                );

              BlockTableRecord btr =

                (BlockTableRecord)tr.GetObject(

                  bt[BlockTableRecord.ModelSpace],

                  OpenMode.ForWrite

                );

              btr.AppendEntity(tb);

              tr.AddNewlyCreatedDBObject(tb, true);

              tr.Commit();

            }

          }

        }

      }

    }

    And here's what you see when you run the CRT command and select a point:

    Simple_table

    I'd like to take this further by showing more advanced concepts around tables - please post a comment if you have a particular suggestion or request.

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200df351f56708833

    Listed below are links to weblogs that reference Creating an AutoCAD table using .NET:

    Comments

    Obrigado por ter postado este exemplo, será de grande importância para aqueles que estão iniciando.
    Em especial, obrigado por me atender, valeu mesmo.
    Espero no futuro ter um exemplo aprimorado deste post.

    "Debtor for having postado this example, will be of great importance for that they are initiating.
    In special, obliged for taking care of to me, he was valid exactly.
    I wait in the future to have an example improved of this post."

    Hello Kean,

    Is it possible to create a new tablestyle with the .net API?

    regards
    Patrick

    Hello Patrick,

    Yes, it is. I'll add it to my list of future topics to cover.

    Regards,

    Kean

    Hello Kean,
    Is it possible to add a row dynamically (using .net code) to an existing table?

    I receive an eexception "eNonApplicable" when I try to do this in my c# code:

    myInfoTable.NumRows++

    If it is possible, what are the steps needed to perform this operation?

    Thank you.

    Hi David,

    You should be able to, but not just by incrementing the count.

    Have you tried using myInfoTable.InsertRows()?

    Regards,

    Kean

    It's strange, because NumRows++ works for the first time. It redimension the table from 1 to 2 rows without loosing data.
    The InsertRows() method works fine but makes the table loosing its previows content.
    Any Idea?
    I was also trying DataTable object wich is a wrapper for acDbDataTable to store information but there is not information about this object.

    Thank you very much.

    I'm really no expert - I don't use Tables day in and out - and this isn't really a forum to get support... if you're an ADN member, please submit your question here, otherwise I suggest posting to the AutoCAD .NET Discussion Group.

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search