.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
DouceDeux
9252 Views, 6 Replies

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

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 😄

❤️ you all

but I love my gf more :DDDDDDDDDDD XD

6 REPLIES 6
Message 2 of 7
norman.yuan
in reply to: DouceDeux

Try change this:

 

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

 

to

 

newDBText.TextStyleID = newTextStyleTable[ROMANS"];

Message 3 of 7
DouceDeux
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

Message 4 of 7
DouceDeux
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... :DDDDDDD

Message 5 of 7
john-malulan
in reply to: DouceDeux

Sir good day. how about if we want to create a new DIM Style and assign it?

Message 6 of 7
_gile
in reply to: john-malulan

Hi,


@john-malulan  a écrit :

Sir good day. how about if we want to create a new DIM Style and assign it?


You should have a look at this section of the documentation and the related topics.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 7
john-malulan
in reply to: _gile

Thanks Sir. have a good day

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost