I'm trying to chase down an eInvalidContext error. Unfortunately I can't replicate it on my development machine and I only have error reports to work with. My suspicion is that it's tied to closing a transaction
based on the limited results of my web searching. The error appears to be caused from my code just after committing the transaction(line 39 below). Using an OpenCloseTransaction may solve the problem, but I"m not sure if it will, and I'd like to understand what's happening better. I have to admit, I'm not even understanding what the
context is within this code.
Can anyone here shed light on why eInvalidError might be occurring here?
The stacktrace in my error reporting is
moduleAutodesk.AutoCAD.DatabaseServices.Transaction
context-lineVoid DeleteUnmanagedObject()
moduleAutodesk.AutoCAD.Runtime.DisposableWrapper
context-lineVoid !DisposableWrapper()
moduleAutodesk.AutoCAD.Runtime.DisposableWrapper
context-lineVoid Dispose(Boolean)
moduleAutodesk.AutoCAD.Runtime.DisposableWrapper
context-lineVoid Dispose()
moduleAID.CAD_Utility.Layers
context-lineBoolean MakeLayer(System.String, Int16, Boolean)
For context the code is called from a command call, which is where I catch the error and report it.
[CommandMethod("IR_AssignDripFlows")]
public static void AssignDripFlowsToBlocks()
{
try
{
DripFactory.AssignDripFlows();
}
catch (Exception e)
{
AID_Application.HandleError(e);
}
}
The code appears to be throwing the error is:
/// <summary>
/// Makes the layer, makes sure it's thawed and on
/// </summary>
/// <param name="layerName">Name of the layer.</param>
/// <param name="color">The color.</param>
/// <param name="print">whether or not the layer prints</param>
/// <returns>True if the layername is valid, false if it's not</returns>
public static bool MakeLayer(string layerName, short color=7, bool print = true)
{
if (Utililty.IsValidSymbolName(layerName))
{
using (Active.Document.LockDocument())
{
using (Transaction tr = Active.Document.TransactionManager.StartTransaction())
{
LayerTable layerTable = (LayerTable)tr.GetObject(Active.Database.LayerTableId, OpenMode.ForWrite);
if (!layerTable.Has(layerName))
{
LayerTableRecord newLayer
= new LayerTableRecord
{
Name = layerName,
Color = Color.FromColorIndex(ColorMethod.ByAci, color),
IsPlottable = print
};
layerTable.Add(newLayer);
tr.AddNewlyCreatedDBObject(newLayer, true);
}
else
{
// make sure it's thawed and on. (maybe we should unlock it?)
LayerTableRecord ltr = tr.GetObject(layerTable[layerName], OpenMode.ForWrite) as LayerTableRecord;
if (ltr != null)
{
if (ltr.IsFrozen) ltr.IsFrozen = false;
ltr.IsOff = false;
}
}
tr.Commit();
}//<-eInValidContext throws here
}
}
else
{
Active.WriteMessage("\nInvalid Layername: " + layerName);
}
return Utililty.IsValidSymbolName(layerName);
}
Active.Document is just a helper class as follows:
...
using _CadAPP = Autodesk.AutoCAD.ApplicationServices.Application;
...
public static class Active
{
public static Document Document
{
get { return _CadAPP.DocumentManager.MdiActiveDocument; }
}
}
...