Got it, give a try
Code:
public class Main
{
[CommandMethod ("READTABLE")]
public static void ReadTest()
{
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.DataTable table = null;
using (Transaction myT = db.TransactionManager.StartTransaction())
{
try
{
DBDictionary NamedObjsDict = (DBDictionary)myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
if (NamedObjsDict.Contains(DuctTapeEntry))
{
table = (DataTable)myT.GetObject(NamedObjsDict.GetAt(DuctTapeEntry), OpenMode.ForRead);
for (int n = 0; n < table.NumRows; n++)
{
DataCellCollection row = table.GetRowAt(n);
for (int c = 0; c < table.NumColumns; c++)
{
DataCell cell = table.GetCellAt(n, c); ;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nCell Value: " + cell.Value.ToString());
}
}
}
}
catch (System.Exception err)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Exception:\n" + err.StackTrace);
}
}
}
[CommandMethod("DUCTTAPERESET")]
public static void DuctTapeReset()
{
Debug.Listeners.Add(new TextWriterTraceListener("DebugOutput02.txt"));
string statusMessage = "Main#DuctTapeReset() - " + DateTime.Now.ToLocalTime();
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.DataTable table = null;
//using (OpenCloseTransaction myT = db.TransactionManager.StartOpenCloseTransaction())
//{
using (Transaction myT = db.TransactionManager.StartTransaction())
{
try
{
DBDictionary NamedObjsDict = (DBDictionary)myT.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
if (NamedObjsDict.Contains(DuctTapeEntry))
{
table = (DataTable)myT.GetObject(NamedObjsDict.GetAt(DuctTapeEntry), OpenMode.ForWrite);
statusMessage = "Found table: row count is " + table.NumRows;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
if ((table.NumColumns != 4) || (table.NumRows == 0))
{
NamedObjsDict.Remove(DuctTapeEntry);
statusMessage = "Table corrupted: removed it from Named Objects Dictionary. Call this command again to try re-inserting.";
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
}
else
{
DataCellCollection row = new DataCellCollection();
for (int n = 0; n < table.NumColumns; n++)
{
DataCell cell = new DataCell();
cell.SetString("blah"+ ((n+1)*(n+1)).ToString());
row.Add(cell);
}
table.InsertRowAt(table.NumRows-1, row, false);
}
}
else
{
table = new DataTable();
table.AppendColumn(CellType.CharPtr, "SourceFile");
table.AppendColumn(CellType.CharPtr, "Feature");
table.AppendColumn(CellType.CharPtr, "AttributeFields");
table.AppendColumn(CellType.CharPtr, "Layer");
DataCellCollection row = new DataCellCollection();
for (int n = 0; n < table.NumColumns; n++)
{
DataCell cell = new DataCell();
cell.SetString(string.Empty);
row.Add(cell);
}
if (table.NumRows == 0)
{
table.InsertRowAt(0, row, true);
}
else
{
//table.AppendRow(row,true);
table.InsertRowAt(table.NumRows - 1, row, true);
}
statusMessage = "New table: first row has " + table.GetRowAt(0).Count + " cells.";
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
NamedObjsDict.SetAt(DuctTapeEntry, table);
myT.AddNewlyCreatedDBObject(table, true/*add*/);
table.DowngradeOpen();//<-- important?
} // as now written, new table has initial blank row
}
catch (System.Exception err)
{
statusMessage = "Exception: " + err.Message;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
}
finally
{
myT.Commit();
if (table != null) table.Dispose();
statusMessage = "End reset.";
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage); Debug.Flush();
}
} // end using myT
}
static readonly string DuctTapeEntry = "DuctTapeConfig";
}
}