フォントは各文字セットを構成する文字の形を定義します。1 つのフォントは、複数のスタイルで使用できます。文字スタイルにフォント ファイルを設定するには、FileName プロパティを使用します。TrueType フォントまたは AutoCAD でコンパイル済みの SHX フォントを文字スタイルに割り当てることができます。
次の例は、アクティブな文字スタイルの Font プロパティを使用して現在のフォントの値を取得し、フォントの書体を「PlayBill」に変更します。書体の変更の効果を確認するには、現在の図面にマルチ テキストか 1 行文字をいくつか記入してから、この例を実行してください。フォント PlayBill がシステム上に存在しない場合、この例を実行するには、代わりのフォントを指定する必要があることに注意してください。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("UpdateTextFont")]
public static void UpdateTextFont()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the current text style for write
TextStyleTableRecord acTextStyleTblRec;
acTextStyleTblRec = acTrans.GetObject(acCurDb.Textstyle,
OpenMode.ForWrite) as TextStyleTableRecord;
// Get the current font settings
Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acFont;
acFont = acTextStyleTblRec.Font;
// Update the text style's typeface with "PlayBill"
Autodesk.AutoCAD.GraphicsInterface.FontDescriptor acNewFont;
acNewFont = new
Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("PlayBill",
acFont.Bold,
acFont.Italic,
acFont.CharacterSet,
acFont.PitchAndFamily);
acTextStyleTblRec.Font = acNewFont;
acDoc.Editor.Regen();
// Save the changes and dispose of the transaction
acTrans.Commit();
}
}