.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
[C#] Creating a new Text Style and how to use it or assign it
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.TextStyle TableId, 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.FontDescripto r myNewTextStyle = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor( "ROMANS", false, false, 0, 0); //newTextStyleTableRecord.Font = myNewTextStyle; newTextStyleTable.Add(newTextStyleTableRecord); newTransaction.AddNewlyCreatedDBObject(newTextStyl eTableRecord, 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 ![]()
<3 you all
but I love my gf more
DDDDDDDDDD XD
Solved! Go to Solution.
Re: [C#] Creating a new Text Style and how to use it or assign it
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Try change this:
newDBText.TextStyleID = newTextStyleTable.GetField("ROMANS");
to
newDBText.TextStyleID = newTextStyleTable[ROMANS"];
Re: [C#] Creating a new Text Style and how to use it or assign it
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
Final code
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.BlockTabl eId, OpenMode.ForRead) as BlockTable; BlockTableRecord newBlockTableRecord; newBlockTableRecord = (BlockTableRecord)newTransaction.GetObject(newBloc kTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite); TextStyleTable newTextStyleTable = newTransaction.GetObject(newDoc.Database.TextStyle TableId, 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.FontDescripto r myNewTextStyle = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor( "ROMANS", false, false, 0, 0); //newTextStyleTableRecord.Font = myNewTextStyle; newTextStyleTable.Add(newTextStyleTableRecord); newTransaction.AddNewlyCreatedDBObject(newTextStyl eTableRecord, 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...
DDDDDD
