A quicky for 2010:
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();
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()));
}
}
}
public static class AcDbTable
{
// Acad::ErrorStatus createCellStyle(const ACHAR* pszCellStyle);
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Unicode,
EntryPoint = "?createCellStyle@AcDbTableStyle@@QEAA?AW4ErrorStatus@Acad@@PEB_W@Z")]
public static extern ErrorStatus createCellStyle(IntPtr tableStyle, string name);
}
}