September 2014

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        








« Creating an AutoCAD table using .NET | Main | Embedding fields in an AutoCAD table using .NET »

June 08, 2007

Creating an AutoCAD table containing block images using .NET

Further to the previous post showing the creation of a simple table, this shows how to add a column that contains a preview image of a particular block definition.

I had to modify the code somewhat to open the block table sooner than we did before (I tend not to leave it open for longer than I have to, but in this case we want to check it for appropriate block definitions earlier on, while we're creating the table). Then I added a simple check, to see whether a block definition corresponding to our "name" field exists for a particular row, and if so, we add a reference to the block table record in the 4th column of the table.

Here's the updated C# code:

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)

      {

        Transaction tr =

          doc.TransactionManager.StartTransaction();

        using (tr)

        {

          BlockTable bt =

            (BlockTable)tr.GetObject(

              doc.Database.BlockTableId,

              OpenMode.ForRead

            );


          Table tb = new Table();

          tb.TableStyle = db.Tablestyle;

          tb.NumRows = 5;

          // Added an additional column for the block image

          tb.NumColumns = 4;

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

            }

            // If a block definition exists for a block of our

            // "name" field, then let's set it in the 4th column

            if (bt.Has(str[i, 1]))

            {

              tb.SetBlockTableRecordId(i, 3, bt[str[i, 1]], true);

            }

          }

          tb.GenerateLayout();


          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 if you run the CRT command with some blocks in the current drawing called "FLANGE", "TILE", "BOLT" and "KEAN":

Table_with_block

TrackBack

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

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

blog comments powered by Disqus

Feed/Share

10 Random Posts