Code Red > .NET

Chasing down and understanding eInvalidContext..

(1/3) > >>

Atook:
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


--- Code: ---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)

--- End code ---

For context the code is called from a command call, which is where I catch the error and report it.

--- Code - C#: ---[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:

--- Code - C#: ---/// <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:

--- Code - C#: ---...using _CadAPP = Autodesk.AutoCAD.ApplicationServices.Application;...public static class Active{  public static Document Document  {    get { return _CadAPP.DocumentManager.MdiActiveDocument; }  }}...

huiz:
Do you get an ObjectId from LayerTable[layerName] or a LayerTableRecord? Then you don't need the GetObject function.

huiz:
And a tip, a deleted layer exist untill the drawing is closed. You don't check that.

huiz:
And you could have wrapped everything in a Try-Catch so you could read the System.Exception value. That makes sense in a lot of cases.


 :yes:

Jeff H:
Also as gile mentioned in your link(will be easier to just quote him than reword it)

--- Quote ---What i was asking is: in which context do you call this method? from a CommandMethod? a modal dialog? a modeless palette? ...
IOW, you should show the code which call this method.

--- End quote ---
You can have VS find all references to method but make you test everywhere it is called from and might be different contexts.

Navigation

[0] Message Index

[#] Next page

Go to full version