2010 64bit btw...
Thank you.
But "
acdb18.dll" reference to concrete AutoCAD version.
May be it better (extension method):
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:
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()));
}
}
}
}