Author Topic: Cell styles in TableStyle...  (Read 21342 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Cell styles in TableStyle...
« Reply #60 on: July 26, 2010, 10:28:36 AM »
2010 64bit btw...
Thank you.
But "acdb18.dll" reference to concrete AutoCAD version.

May be it better (extension method):
Code: [Select]
   public static class AcadTableStyle
    {
        /// <summary>
        /// Extended method for TableStyle class.
        /// </summary>
        /// <param name="tableStyle">TableStyle object (source)</param>
        /// <param name="cellStyleName">Cell Style name</param>        
        public static void AddCellStyle(this TableStyle tableStyle, string cellStyleName)
        {
            Document dwg = acad.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;            
            try
            {
                if (tableStyle.CellStyles.Contains(cellStyleName)) throw new System.Exception("TableStyle has CellStyle with it name already!");
                TableStyle ts = tableStyle;
                IAcadTableStyle2 ts2 = (IAcadTableStyle2)((AcadApplication)acad.AcadApplication).ActiveDocument.ObjectIdToObject((int)ts.ObjectId.OldIdPtr);
                using (Transaction t = dwg.TransactionManager.StartTransaction())
                {                    
                    lock (dwg.LockDocument())
                    {
                        ts.UpgradeOpen();
                        ts2.CreateCellStyle(cellStyleName);
                        t.Commit();
                    }                    
                }                
            }
            catch (System.Exception e)
            {
                ed.WriteMessage(string.Format("{0}{1}{0}", "\n", e.Message));
            }          
        }
    }
? I do not confirm, but I ask.

Testing:

Code: [Select]
using System;
using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;


using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(CellStyles.MyCommands))]

namespace CellStyles
{
    public class MyCommands
    {
        [CommandMethod("q1", CommandFlags.Modal)]
        public void TestMethod()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                DBDictionary tsd = (DBDictionary)tr.GetObject(db.TableStyleDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry item in tsd)
                {
                    TableStyle ts = (TableStyle)tr.GetObject(item.Value, OpenMode.ForRead);
                    ed.WriteMessage(string.Format("{0}Table style name: '{1}'", Environment.NewLine, ts.Name));
                    ed.WriteMessage(string.Format("{0}(TableStyle.CellStyles.IsReadOnly ='{1}')",
                        Environment.NewLine, ts.CellStyles.IsReadOnly));

                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                    string stName = "MyStyle";// name for my new cell style

                    ed.WriteMessage(string.Format("{0}Before modify: Cell Styles Count: {1}",
                        Environment.NewLine, ts.CellStyles.Count));

                    ts.UpgradeOpen();

                    [color=red]ts.AddCellStyle(stName);[/color][color=green]//Extension method[/color]

                    //AcDbTable.createCellStyle(ts.UnmanagedObject, stName);

                    ed.WriteMessage(string.Format("{0}After modify: Cell Styles Count: {1}{0}",
                                    Environment.NewLine, ts.CellStyles.Count));

                    ts.SetCellClass(CellClass.Label, stName);

                    ed.WriteMessage(string.Format("{0}{1}{0}", "\nAfter modify:", new string('*', 30)));
                    //Print all cell style names of table style
                    PrintCellStylesInfo(ts);
                }
                tr.Commit();
            }
        }

        private void PrintCellStylesInfo(TableStyle ts)
        {
            foreach (var cs in ts.CellStyles)
            {
                Document doc = acadApp.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                ed.WriteMessage(string.Format("{0}CellStyle Type: '{1}'; CellStyle.ToString: {2}",
                                Environment.NewLine, cs.GetType(), cs.ToString()));
            }
        }
    }
}
« Last Edit: July 26, 2010, 10:38:16 AM by Hwd »

Glenn R

  • Guest
Re: Cell styles in TableStyle...
« Reply #61 on: July 26, 2010, 10:37:47 AM »
Maybe, but you're still using COM and that's version dependent as well. Personally, I would use the DLLImport option over COM. Alexander Rivilis had a good implementation for multiple versions shown here.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Cell styles in TableStyle...
« Reply #62 on: July 26, 2010, 10:40:24 AM »
Maybe, but you're still using COM and that's version dependent as well. Personally, I would use the DLLImport option over COM. Alexander Rivilis had a good implementation for multiple versions shown here.
Thank you!