TheSwamp

Code Red => .NET => Topic started by: IDabble on August 18, 2011, 10:17:23 AM

Title: C# create TextStyle
Post by: IDabble on August 18, 2011, 10:17:23 AM
I'm finding examples everywhere that seem to work for others, but the following code is not working for me.  It completes with no errors, but when I check with the STYLE command, there is no TextStyle added.

Code: [Select]
/// <source> http://through-the-interface.typepad.com/through_the_interface/2011/07/minesweeper-in-autocad-using-net.html </source>
        /// <summary> adds a text style to a drawing if it doesn't already exist </summary>
        public static void AddTextStyle()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TextStyleTable tab = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite, false);
                if (!tab.Has("MinesweeperStyle"))
                {
                    TextStyleTableRecord rec = new TextStyleTableRecord();
                    rec.Name = "MinesweeperStyle";
                    rec.FileName = "txt.shx";
                    //rec.Font = new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("txt", false, false, 0, 0);
                    rec.IsShapeFile = true;
                    rec.TextSize = 1.0;
                    rec.XScale = 1.0;
                    /* MessageBox.Show(string.Format("Name: {0}\nBigFontFileName: {1}\nFileName: {2}\nFont: {3}\nIsShapeFile: {4}\nIsVertical: {5}\n" +
                        "ObliquingAngle: {6}\nTextSize: {7}\nXScale: {8}", rec.Name, rec.BigFontFileName, rec.FileName, rec.Font.ToString(),
                        rec.IsShapeFile.ToString(), rec.IsVertical.ToString(), rec.ObliquingAngle.ToString(), rec.TextSize.ToString(), rec.XScale.ToString()),
                        "Create TextStyle"); */
                    tab.Add(rec);
                    trans.AddNewlyCreatedDBObject(rec, true);
                }
                trans.Commit();
            }
        }

Anybody know what I'm missing?
Title: Re: C# create TextStyle
Post by: IDabble on August 18, 2011, 01:14:05 PM
IsShapeFile = false;   :oops:
Movin' on now.
Title: Re: C# create TextStyle
Post by: Andrey Bushman on August 25, 2011, 09:53:11 AM
Code: [Select]
...
        public ObjectId CreateNew(TextStyleInfo styleSettings) {
            if (Exists(styleSettings.Name)) throw new System
    .Exception(string.Format(TextStyleMngRes.StyleAlredyExists, styleSettings));

            TextStyleTableRecord ts = new TextStyleTableRecord();

            //По умолчанию выставляю следующие настройки
            ts.Name = styleSettings.Name;
            ts.FileName = styleSettings.FontName;
            ts.Annotative = styleSettings.Annotative ? AnnotativeStates.True : AnnotativeStates.False;
            ts.ObliquingAngle = styleSettings.ObliqueAngle * Math.PI / 180;
            ts.SetPaperOrientation(styleSettings.OrientationToLayout);
            ts.BigFontFileName = styleSettings.BigFontName;
            ts.FlagBits = (byte) 0;
            ts.FlagBits += styleSettings.UpsideDown ? (byte) 2 : (byte) 0;
            ts.FlagBits += styleSettings.Backwards ? (byte) 4 : (byte) 0;
            ts.TextSize = styleSettings.TextHeight;
            ts.XScale = styleSettings.WidthFactor;
            ts.IsVertical = styleSettings.Vertical;

            //Запускаю транзакцию
            using (Transaction trs = TargetDb.TransactionManager.StartTransaction()) {
                try {
                    //Добавляю созданный стиль в базу данных
                    TextStyleTable tStyles = (TextStyleTable) trs.GetObject(TargetDb.TextStyleTableId,
                        OpenMode.ForRead);
                    tStyles.UpgradeOpen();
                    tStyles.Add(ts);
                    trs.AddNewlyCreatedDBObject(ts, true);

                    //А теперь задаю те настройки, которые указаны в параметре
                    SetSettings(ts.Name, styleSettings);

                    //Завершаю транзакцию, сохраняя все изменения
                    trs.Commit();
                }
                catch { }
            }
            return ts.ObjectId;
        }
...

TextStyleInfo - my class, but it is not difficult to understand, as well as that works.

Title: Re: C# create TextStyle
Post by: IDabble on August 25, 2011, 11:21:40 AM
Very informative & nice code.  Thank you for posting it.