.NET

.NET

Reply
Active Contributor
47 Posts
4 Kudos
Registered: ‎09-04-2012
Post 1 of 4
Accepted Solution

[C#] Creating a new Text Style and how to use it or assign it

1648 Views, 3 Replies
11-26-2012 02:15 PM

Hello all ^^

I want to programatically create a text style in the current database based on a font located in the AutoCAD /fonts folder. After that, I want to assign that text style to a newly created DBText.

 

I am close but can't quite get it. Let's see what I have:

(I have 2 commented lines that I stumbled upon but I'm not sure they helped because I still got errors and left it as comment.

 

using (Transaction newTransaction = newDoc.Database.TransactionManager.StartTransaction())
{
  TextStyleTable newTextStyleTable = newTransaction.GetObject(newDoc.Database.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
  
  if (!newTextStyleTable.Has("ROMANS"))  //The TextStyle is currently not in the database
  {
    newTextStyleTable.UpgradeOpen();
    newTextStyleTableRecord = new TextStyleTableRecord();
    newTextStyleTableRecord.FileName = "romans.shx";
    newTextStyleTableRecord.Name = "ROMANS";
    //Autodesk.AutoCAD.GraphicsInterface.FontDescriptor myNewTextStyle = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("ROMANS", false, false, 0, 0);
    //newTextStyleTableRecord.Font = myNewTextStyle;
    newTextStyleTable.Add(newTextStyleTableRecord);
    newTransaction.AddNewlyCreatedDBObject(newTextStyleTableRecord, true);
  }
  DBText newDBText = new DBText();
  newDBText.SetDatabaseDefaults();
textEspecificacion.Position = new Point3d(0,0,0); newDBText.Height = 7.0; newDBText.TextString = "HELLO"; newDBText.TextStyleID = newTextStyleTable.GetField("ROMANS"); newBlockTableRecord.AppendEntity(newDBText); newTransaction.AddNewlyCreatedDBObject(newDBText, true); newTransaction.Commit(); }

 Please tell me what I'm doing wrong.

I get key not found error.

Do i have to lock the document?

Thanks in advance :smileyvery-happy:

<3 you all

but I love my gf more :smileyvery-happy:DDDDDDDDDD XD

Try change this:

 

newDBText.TextStyleID = newTextStyleTable.GetField("ROMANS");

 

to

 

newDBText.TextStyleID = newTextStyleTable[ROMANS"];

*Expert Elite*
1,084 Posts
215 Kudos
Registered: ‎04-27-2009
Post 2 of 4

Re: [C#] Creating a new Text Style and how to use it or assign it

11-27-2012 06:44 AM in reply to: DouceDeux

Try change this:

 

newDBText.TextStyleID = newTextStyleTable.GetField("ROMANS");

 

to

 

newDBText.TextStyleID = newTextStyleTable[ROMANS"];

Active Contributor
47 Posts
4 Kudos
Registered: ‎09-04-2012
Post 3 of 4

Re: [C#] Creating a new Text Style and how to use it or assign it

11-27-2012 10:59 AM in reply to: DouceDeux

That worked!

Do you know why one line didn't work .GetField(...) (eKeyNotFound) and the other did?

Is the TextStyleTable an enumerable itself where the text styles are saved?

 

P.S. I'll try to edit my first post for forum knowledgebase purposes

Active Contributor
47 Posts
4 Kudos
Registered: ‎09-04-2012
Post 4 of 4

Final code

11-27-2012 11:13 AM in reply to: DouceDeux

Correcting my first code and adding the help from the solution we have:

 

using (Transaction newTransaction = newDoc.Database.TransactionManager.StartTransaction())
{
  BlockTable newBlockTable;
  newBlockTable = newTransaction.GetObject(newDoc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
  BlockTableRecord newBlockTableRecord;
  newBlockTableRecord = (BlockTableRecord)newTransaction.GetObject(newBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  TextStyleTable newTextStyleTable = newTransaction.GetObject(newDoc.Database.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
  
  if (!newTextStyleTable.Has("ROMANS"))  //The TextStyle is currently not in the database
  {
    newTextStyleTable.UpgradeOpen();
    newTextStyleTableRecord = new TextStyleTableRecord();
    newTextStyleTableRecord.FileName = "romans.shx";
    newTextStyleTableRecord.Name = "ROMANS";
    //Autodesk.AutoCAD.GraphicsInterface.FontDescriptor myNewTextStyle = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("ROMANS", false, false, 0, 0);
    //newTextStyleTableRecord.Font = myNewTextStyle;
    newTextStyleTable.Add(newTextStyleTableRecord);
    newTransaction.AddNewlyCreatedDBObject(newTextStyleTableRecord, true);
  }
  DBText newDBText = new DBText();
  newDBText.SetDatabaseDefaults();
  textEspecificacion.Position = new Point3d(0,0,0);
  newDBText.Height = 7.0;
  newDBText.TextString = "HELLO";
  newDBText.TextStyleID = newTextStyleTable["ROMANS"];
  newBlockTableRecord.AppendEntity(newDBText);
  newTransaction.AddNewlyCreatedDBObject(newDBText, true);
  newTransaction.Commit();
}

 I hope this helps other new people like me.

Thanks for the help.

Next new topic... String keywords... :smileyvery-happy:DDDDDD

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post