how to find standard text style font from one file

how to find standard text style font from one file

VallimanalanT
Enthusiast Enthusiast
942 Views
2 Replies
Message 1 of 3

how to find standard text style font from one file

VallimanalanT
Enthusiast
Enthusiast

hi,

 

how to find standard text style font from one file, i found some error in the red color code line.

 

        [CommandMethod("AttachFontCommand")]
        public static void AttachFont() // This method can have any name
        {

            Document acadDoc = Application.DocumentManager.MdiActiveDocument;

            ///get the current drawing database
            Database acadCurDb = acadDoc.Database;

            /// Get the current font settings
            FontDescriptor acFont;

            TextStyleTableRecord openTextStyleTbl;

            int errorIndex;

            /// prompt receive the Block file path
            PromptStringOptions pBlkFile = new PromptStringOptions("\nEnter Block file path: ");
            pBlkFile.AllowSpaces = true;
            PromptResult pBlkFileRes = acadDoc.Editor.GetString(pBlkFile);
            string sBlockfile = pBlkFileRes.StringResult;

                using (Database OpenDb = new Database(false, true))
                {

                    OpenDb.ReadDwgFile(sBlockfile, System.IO.FileShare.ReadWrite, true, "");

                    ObjectIdCollection ObjectIds = new ObjectIdCollection();
                
                    using (Transaction acadTrans = OpenDb.TransactionManager.StartTransaction())
                    {

                        /// Open the exiting text style for read
                        openTextStyleTbl = acadTrans.GetObject(OpenDb.Textstyle, OpenMode.ForRead) as TextStyleTableRecord;

                    //TODO : remodify the code
                    if (openTextStyleTbl.Name.ToString() == "STANDARD")
                    {
                        acFont = openTextStyleTbl.Font;
                    }
                    else
                    {
                        acFont = openTextStyleTbl.Font;
                    }

                    acadTrans.Commit();

                    }
                }
                if (acFont.ToString() != null)
                {
                    /// Start a transaction for text style
                    using (Transaction acTrans = acadCurDb.TransactionManager.StartTransaction())
                    {
                        TextStyleTableRecord currentTextStyleTbl;

                        currentTextStyleTbl = acTrans.GetObject(acadCurDb.Textstyle, OpenMode.ForWrite) as TextStyleTableRecord;

                        currentTextStyleTbl.Font = acFont;

                      //  Application.SetSystemVariable("USERS3", acFont);

                        acadDoc.Editor.Regen();

                        acTrans.Commit();

                        errorIndex = 0;
                    }
                }
                else
                {
                    errorIndex = 0;
                }

            Application.SetSystemVariable("USERS2", errorIndex.ToString());
        }
0 Likes
Accepted solutions (1)
943 Views
2 Replies
Replies (2)
Message 2 of 3

SENL1362
Advisor
Advisor
Accepted solution

Not all TextStyles have Names, for example Shape Files.

Slightly modified code sample, iterating all TextStyles

 

...
                var fontForStandard = default(Autodesk.AutoCAD.GraphicsInterface.FontDescriptor);
                using (var tr=db.TransactionManager.StartTransaction())
                {
                    var tsTbl = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                    foreach (ObjectId tsId in tsTbl)
                    {
                        var textStyle = (TextStyleTableRecord)tr.GetObject(tsId, OpenMode.ForRead);
                        //Some TextStyles do NOT have names, for example ltshapes.shx
                        //  textStyle.IsShapeFile==TRUE
                        if (string.IsNullOrWhiteSpace(textStyle.Name))
                        {
                            //ShapeFile
                        }
                        else if (textStyle.Name.Equals("STANDARD", StringComparison.OrdinalIgnoreCase))
                        {
                            fontForStandard = textStyle.Font;
                        }

                        if (tsId== db.Textstyle)
                        {
                            //this is the Current TextStyle
                        }
                     }
                    tr.Commit();
                }
...
0 Likes
Message 3 of 3

VallimanalanT
Enthusiast
Enthusiast

Thank you

0 Likes