using System;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof (TextStyleTableRecordProblem
.MyCommands))]
namespace TextStyleTableRecordProblem
{
public class MyCommands
{
[CommandMethod("Test")]
public static void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
string styleName = "MySlopeyTextStyle";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTable tsTbl =
(TextStyleTable)
tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
if (!tsTbl.Has(styleName))
{
Make_TextStyle(styleName, "romans", 3.5,
1.0, 15 * Math.PI / 180);
}
TextStyleTableRecord tsTblRec =
(TextStyleTableRecord)
tr.GetObject(tsTbl[styleName], OpenMode.ForRead);
BlockTableRecord model =
(BlockTableRecord)
tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
OpenMode.ForWrite);
AcadApp.SetSystemVariable("TEXTSTYLE", styleName);
using (DBText txt
= new DBText
()) {
//txt.SetDatabaseDefaults();
txt.TextStyleId = tsTbl[styleName];
txt.TextString = "Test-DBTEXT";
//txt.WidthFactor = 1.0;
//txt.Height =
// Convert.ToInt32(Application.GetSystemVariable("DIMSCALE"))
// *3.5;
//txt.o
model.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
}
tr.Commit();
}
}
//=============================================
public static void Make_TextStyle(string styleName, string font, double fontSize,
double widthFactor, double obliqueAngle)
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
ObjectId textStyleID = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTable tsTbl =
(TextStyleTable)
tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite, false);
if (!tsTbl.Has(styleName)) // make new
{
TextStyleTableRecord tsTblRec
= new TextStyleTableRecord
();
tsTblRec.Name = styleName;
// Font, Not Bold, Not Italic, Characters, Pitch&Family
tsTblRec
.Font = new FontDescriptor
(font,
false,
false,
0,
0);
tsTblRec.XScale = widthFactor;
tsTblRec.Annotative = AnnotativeStates.False;
tsTblRec.TextSize =
Convert.ToInt32(Application.GetSystemVariable("DIMSCALE"))*
fontSize;
tsTblRec.ObliquingAngle = obliqueAngle;
tsTbl.Add(tsTblRec);
tr.AddNewlyCreatedDBObject(tsTblRec, true);
}
tr.Commit();
tr.Dispose();
}
}
}
}