.NET

Reply
Active Contributor
DouceDeux
Posts: 46
Registered: ‎09-04-2012
Message 1 of 4 (1,301 Views)
Accepted Solution

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

1301 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

*Expert Elite*
norman.yuan
Posts: 961
Registered: ‎04-27-2009
Message 2 of 4 (1,280 Views)

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
DouceDeux
Posts: 46
Registered: ‎09-04-2012
Message 3 of 4 (1,266 Views)

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
DouceDeux
Posts: 46
Registered: ‎09-04-2012
Message 4 of 4 (1,262 Views)

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community