I've got the problem reduced a bit.
Code:
public class Main
{
[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;
DataTable table = null;
using(OpenCloseTransaction myT = db.TransactionManager.StartOpenCloseTransaction()) {
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: column count is " + table.NumColumns;
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(statusMessage);
Debug.WriteLine(statusMessage);
if ((table.NumColumns < 4) || (table.NumRows == 0) || (table.GetRowAt(0).Count < 4)) {
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 {
table = new DataTable();
table.AppendColumn(CellType.CharPtr, "SourceFile");
table.AppendColumn(CellType.CharPtr, "Feature");
table.AppendColumn(CellType.CharPtr, "AttributeFields");
table.AppendColumn(CellType.CharPtr, "Layer");
DataCell[] initialVals = new DataCell[4];
DataCellCollection row = new DataCellCollection();
for (int n = 0, count = initialVals.Length; n < count; ++n) {
DataCell cell = new DataCell();
cell.SetString(string.Empty);
row.Add(cell);
}
table.AppendRow(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*/);
} // 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";
}
Produces the log file results
Code:
Main#DuctTapeReset() - 12/21/2011 8:20:43 AM
New table: first row has 0 cells.
End reset.
...Sorry for the wordy code, but I'm trying to document this as best I can. Why doesn't AppendRow work? The table gets added to the drawing okay, but it's got one row, and that row doesn't have any DataCell contents in it.
Any ideas?