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
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Try change this:
newDBText.TextStyleID = newTextStyleTable.GetField("ROMANS");
to
newDBText.TextStyleID = newTextStyleTable[ROMANS"];
Norman Yuan
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
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
Sir good day. how about if we want to create a new DIM Style and assign it?
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.