Author Topic: Text Style  (Read 2812 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Text Style
« on: December 13, 2008, 01:43:50 AM »
Following on from this posted here in answer to a question ..
Code - C#: [Select]
  1. [CommandMethod("TestText")]
  2. static public void TestDbText()
  3. // CodeHimBelongaKbub@TheSwamp ©  Dec 2008
  4. {
  5.     Database db = HostApplicationServices.WorkingDatabase;
  6.     string txtStr = "This is a DbText string.";
  7.     string txtStyle = "kbubsComicTextStyle";
  8.  
  9.     using (Transaction tr = db.TransactionManager.StartTransaction())
  10.     {
  11.         BlockTableRecord CurrentSpaceBtr = tr.GetObject(db.CurrentSpaceId,
  12.                                             OpenMode.ForWrite) as BlockTableRecord ;
  13.         DBText textObj = new DBText();
  14.         textObj.TextString = txtStr;
  15.         textObj.Height = 500;
  16.         textObj.Position = new Point3d(0,0,0) ;
  17.        
  18.         textObj.HorizontalMode = TextHorizontalMode.TextMid;
  19.         textObj.AlignmentPoint = textObj.Position;
  20.  
  21.         TextStyleTable txtTbl = tr.GetObject(db.TextStyleTableId,
  22.                             OpenMode.ForRead) as TextStyleTable ;
  23.         if (txtTbl.Has(txtStyle))
  24.         {
  25.             TextStyleTableRecord txtTbr = tr.GetObject(txtTbl[txtStyle],
  26.                                         OpenMode.ForRead) as TextStyleTableRecord;
  27.             textObj.TextStyle = txtTbr.ObjectId;
  28.         }
  29.  
  30.         CurrentSpaceBtr.AppendEntity(textObj);
  31.         tr.AddNewlyCreatedDBObject(textObj, true);
  32.  
  33.         tr.Commit();
  34.     }
  35. }
  36.  


I wanted to check if the TextStyle existed ..
Code - C#: [Select]
  1.         [CommandMethod("AOMT")]
  2.         static public void AOMT()
  3.         // CodeHimBelongaKbub@TheSwamp ©  Dec 2008
  4.         {
  5.             AssertOrMakeTextStyle("kbubsISOTextStyle", "isocp.shx");
  6.             AssertOrMakeTextStyle("kbubsComicTextStyle", "Comic.ttf");
  7.         }
  8.  
  9.         static public void AssertOrMakeTextStyle(string styleName, string styleFileName)
  10.         {
  11.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  12.             Database db = doc.Database;
  13.             using (Transaction tr = db.TransactionManager.StartTransaction())
  14.             {
  15.                 TextStyleTable tsTable = tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
  16.                 if (!tsTable.Has(styleName))
  17.                 {
  18.                     TextStyleTableRecord tsTableRecord = new TextStyleTableRecord();
  19.                     tsTableRecord.Name = styleName;
  20.                     tsTableRecord.FileName = styleFileName;
  21.                     tsTableRecord.IsVertical = false;
  22.                     tsTableRecord.TextSize = 0.0;
  23.  
  24.                     tsTable.Add(tsTableRecord);
  25.                     tr.AddNewlyCreatedDBObject(tsTableRecord, true);
  26.                 }
  27.                 tr.Commit();
  28.             }
  29.         }


.. With these results



« Last Edit: August 07, 2013, 06:32:14 AM by Kerry »
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text Style
« Reply #1 on: December 13, 2008, 02:12:18 AM »
Then this sort of thing can be used

Code - C#: [Select]
  1. [CommandMethod("TestText")]
  2. static public void TestDbText()
  3. // CodeHimBelongaKbub@TheSwamp ©  Dec 2008
  4. {
  5.     Database db = HostApplicationServices.WorkingDatabase;
  6.     string txtStr = "This is a DbText string.";
  7.     string txtStyleName = "kbubsComicTextStyle";
  8.     string txtFileName = "Comic.ttf";
  9.  
  10.     using (Transaction tr = db.TransactionManager.StartTransaction())
  11.     {
  12.         BlockTableRecord currentSpaceBtr = tr.GetObject(db.CurrentSpaceId,
  13.                                             OpenMode.ForWrite) as BlockTableRecord;
  14.         DBText textObj = new DBText();
  15.         textObj.TextString = txtStr;
  16.         textObj.Height = 500;
  17.         textObj.Position = new Point3d(0, 0, 0);
  18.  
  19.         textObj.HorizontalMode = TextHorizontalMode.TextMid;
  20.         textObj.AlignmentPoint = textObj.Position;
  21.  
  22.         TextStyleTable txtTbl = tr.GetObject(db.TextStyleTableId,
  23.                                 OpenMode.ForRead) as TextStyleTable;
  24.         if (!txtTbl.Has(txtStyleName))
  25.         {
  26.             AssertOrMakeTextStyle(txtStyleName, txtFileName);
  27.         }
  28.         TextStyleTableRecord txtTbr = tr.GetObject(txtTbl[txtStyleName],
  29.                                           OpenMode.ForRead) as TextStyleTableRecord;
  30.         if (txtTbr != null)
  31.         {
  32.             textObj.TextStyle = txtTbr.ObjectId;
  33.         }
  34.         currentSpaceBtr.AppendEntity(textObj);
  35.         tr.AddNewlyCreatedDBObject(textObj, true);
  36.  
  37.         tr.Commit();
  38.     }
  39. }

Interestingly, If I define a nonexistant FontFile the AutoCAD Substitution takes over and the text will be displayed in the Named style but with a substituted Font.
« Last Edit: August 07, 2013, 06:31:23 AM by Kerry »
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Text Style
« Reply #2 on: December 13, 2008, 02:28:16 AM »
To be usefull in anything other than a learning sense, the AssertOrMakeTextStyle( )   should probably be re-written to return the ID of the Record :)
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.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Text Style
« Reply #3 on: December 14, 2008, 10:30:16 AM »
Quote
If I define a nonexistant FontFile the AutoCAD Substitution takes over and the text will be displayed in the Named style but with a substituted Font.
I have a bit of code to check for this. It parses the supportpath in preferences to find the fonts folder and looks in there. However, running your code with a nonexistant font, then adding that font back into the fonts folder and reopening the drawing, everything looks fine, so perhaps I'll get rid of it as unnecessary.





David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Text Style
« Reply #4 on: December 15, 2008, 09:44:54 AM »
very nice Kerry.  I was messing around with text last week and could have used the example.  Is anyone using Annotative text, as it can be programmed in 09 now.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)