Author Topic: How to specify a Font for the text in a table?  (Read 2332 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
How to specify a Font for the text in a table?
« on: August 07, 2013, 05:10:07 AM »
Hi.
With Kean's blog, I have learned how to create an AutoCAD table. But I have a further step to go. I want the font for all the texts to be a specified font(for example, Times New Roman) under any circumstance. How to do this?

These are my codes at the moment.
Code - C#: [Select]
  1. [CommandMethod("InsertTable")]
  2.         public void InsertNow()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
  9.             if (pr.Status != PromptStatus.OK) return;
  10.            
  11.             using (Transaction trans = db.TransactionManager.StartTransaction())
  12.             {
  13.                 CreateTable( pr.Value);
  14.                 trans.Commit();
  15.             }                      
  16.         }
  17.         public void CreateTable(Point3d ptInsert)
  18.         {                      
  19.             Table tb = new Table();
  20.             tb.NumRows = 5;
  21.             tb.NumColumns = 3;
  22.             tb.SetRowHeight(100);
  23.             tb.SetColumnWidth(500);
  24.             tb.Position = ptInsert;
  25.  
  26.             // 定义一个数组,表示要插入到表格中的内容
  27.             string[,] str = new string[5, 3];
  28.             str[0, 0] = "Part No.";            str[0, 1] = "Name ";
  29.             str[0, 2] = "Material ";            str[1, 0] = "1876-1";
  30.             str[1, 1] = "Flange";            str[1, 2] = "Perspex";
  31.             str[2, 0] = "0985-4";            str[2, 1] = "Bolt";
  32.             str[2, 2] = "Steel";            str[3, 0] = "3476-K";
  33.             str[3, 1] = "Tile";            str[3, 2] = "Ceramic";
  34.             str[4, 0] = "8734-3";            str[4, 1] = "Kean";            str[4, 2] = "Mostly water";
  35.  
  36.             //通过循环把内容插入到表格中
  37.             for (int i = 0; i < 5; i++)
  38.             {
  39.                 for (int j = 0; j < 3; j++)
  40.                 {
  41.                     tb.SetTextHeight(i, j, 50);
  42.                     tb.SetTextString(i, j, str[i, j]);
  43.                     tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
  44.                 }
  45.             }
  46.  
  47.             AppendEntity(tb);
  48.         }
  49.         public static ObjectId AppendEntity(Entity ent)
  50.         {
  51.             Database db = HostApplicationServices.WorkingDatabase;
  52.             ObjectId entId;
  53.             using (Transaction trans = db.TransactionManager.StartTransaction())
  54.             {
  55.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  56.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  57.                 entId = btr.AppendEntity(ent);
  58.                 trans.AddNewlyCreatedDBObject(ent, true);
  59.                 trans.Commit();
  60.             }
  61.             return entId;
  62.         }
  63.  

« Last Edit: August 07, 2013, 05:21:29 AM by Kerry »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to specify a Font for the text in a table?
« Reply #1 on: August 07, 2013, 05:25:14 AM »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

waterharbin

  • Guest
Re: How to specify a Font for the text in a table?
« Reply #2 on: August 07, 2013, 08:11:58 AM »
Hi, Kerry.
In your post, you suggested to re-write the  AssertOrMakeTextStyle( )   to return the ID of the Record. So, this is what I do:
Code: [Select]
     public ObjectId AssertOrMakeTextStyleUpdated(string styleName, string styleFileName)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            ObjectId recordId;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tsTable = tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
                TextStyleTableRecord tsTableRecord = new TextStyleTableRecord();
                if (!tsTable.Has(styleName))
                {
                    tsTableRecord.Name = styleName;
                    tsTableRecord.FileName = styleFileName;
                    tsTableRecord.IsVertical = false;
                    tsTableRecord.TextSize = 0.0;

                    tsTable.Add(tsTableRecord);
                    tr.AddNewlyCreatedDBObject(tsTableRecord, true);
                }
                else
                    tsTableRecord = tr.GetObject(tsTable[styleName], OpenMode.ForRead) as TextStyleTableRecord;
                recordId = tsTableRecord.ObjectId;
                tr.Commit();               
            }

            return recordId;
        }

Then I use it to define the table:
Code: [Select]
string txtStyleName = "kbubsComicTextStyle";
            string txtFileName = "Comic.ttf";

            Table tb = new Table();
            tb.NumRows = 5;
            tb.NumColumns = 3;
            tb.SetRowHeight(100);
            tb.SetColumnWidth(500);
            tb.Position = ptInsert;
            [color=red]tb.SetTextStyle(AssertOrMakeTextStyleUpdated(txtStyleName, txtFileName), 0);[/color]

But it seems that it does not work? Where is the problem?
« Last Edit: August 07, 2013, 08:38:47 AM by 闻仲 »

Micaletti

  • Guest
Re: How to specify a Font for the text in a table?
« Reply #3 on: August 07, 2013, 09:50:55 AM »
You didn't set a font.

tsTableRecord.Font = new FontDescriptor("Romans", false, false, null, null)
« Last Edit: August 07, 2013, 11:42:00 AM by Micaletti »

fixo

  • Guest
Re: How to specify a Font for the text in a table?
« Reply #4 on: August 07, 2013, 03:27:38 PM »
This should work
Code: [Select]
TextStyleTable ttb = (TextStyleTable)trx.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                ObjectId txsId = ObjectId.Null;
                if (ttb.Has("MyCoolTextStyle"))
                {
                    txsId = ttb["MyCoolTextStyle"];
                    tbl.Cells[0, 0].TextStyleId = txsId;// <-- A2010
                }
Another way is to set mtext formatted string like
these:

Code: [Select]
\fCalibri|c0|b0|i1|p34;\C1;Table Title
\fComic Sans MS|c0|b0|i1|p34;\C1;Table Title