0

Good Day,

I'm creating an Autocad csharp program for to calculate a few levels and insert a block at a point the user clicks on, and doing this on a while loop so the user can keep on selecting new points to calculate and insert. Only it works perfectly at the first point the user specifies, but the moment they click on another point the error message pops up "eInvalidOpenState." and the program cancels. Anyone familiar with this and why it is happening?

I've added a small part of the program where the problem is so you can see what it is doing.

*Edit: I've added the lock and removed the dispose, yet that still leaves the original error of the while loop not working. The entire program works correctly the first time around, on second loop of the while it requests the user to specify the point and when the user does it crashes and gives the following error message:

************** Exception Text ************** Autodesk.AutoCAD.Runtime.Exception: eInvalidOpenState at Autodesk.AutoCAD.DatabaseServices.DBObject.UpgradeOpen() at Level_Arrow.Program.InsertBLock(String[] args) in c:\Users+\Documents\Visual Studio 2013\Projects\Level Arrow\Level Arrow\myCommands.cs:line 94 at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction) at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke()

if (drgUnits == "mm")
{
    while (drgUnits == "mm")
    {
        using (tr)
        {
            PromptPointOptions ptIns = new PromptPointOptions("");
            ptIns.Message = "\nSpecify insertion point: ";
            PromptPointResult ptInsert = ed.GetPoint(ptIns);

            if (ptInsert.Status == PromptStatus.Cancel) return;

            double yInsert = ptInsert.Value.Y;

            Scale3d blkScale = new Scale3d(drgScalemm, drgScalemm, drgScalemm);
            ObjectId bdId = bt[blkName];
            Point3d pt = ptInsert.Value;

            BlockReference insblkref = new BlockReference(pt, bdId);
            insblkref.ScaleFactors = blkScale;
            insblkref.Rotation = 0;

            btr.UpgradeOpen();
            btr.AppendEntity(insblkref);
            btr.DowngradeOpen();
            tr.AddNewlyCreatedDBObject(insblkref, true);

            if (OrgBtr.HasAttributeDefinitions)
            {
                foreach (ObjectId id in OrgBtr)
                {
                    DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                    AttributeDefinition ad = obj as AttributeDefinition;

                    if (ad != null && !ad.Constant)
                    {
                        AttributeReference ar = new AttributeReference();
                        ar.SetAttributeFromBlock(ad, insblkref.BlockTransform);
                        ar.Position =ad.Position.TransformBy(insblkref.BlockTransform);
                        if (ad.Justify != AttachmentPoint.BaseLeft)
                        {
                            ar.AlignmentPoint.TransformBy(insblkref.BlockTransform);
                        }
                        if (ar.IsMTextAttribute)
                        {
                            ar.UpdateMTextAttribute();
                        }
                        ar.TextString = ptDisp.ToString();
                        ObjectId arId =  insblkref.AttributeCollection.AppendAttribute(ar);                         
                        tr.AddNewlyCreatedDBObject(ar, true);
                        break;
                    }
                }
            }

            tr.Commit();
            tr.Dispose(); 
        }
    }
}
Cald
  • 201
  • 2
  • 6

2 Answers2

1

Its hard to understand from the code (it seems to be incomplete) and you didn't mention the source of the exception, but from a fast glance it looks like there is a possibility that the tr object is being accessed after you disposed of him.

In any case when you are using using() you don't need to dispose or close.

Amorphis
  • 398
  • 2
  • 19
  • `t looks like there is a possibility that the tr object is being accessed after you disposed of him.` tr is accessed in the using block, try to match the braces. – Niranjan Singh Oct 14 '14 at 08:06
  • 1
    He is also disposing of it inside the using block – Amorphis Oct 14 '14 at 08:07
  • You Should Lock the active document when updating it. Use a using statement to make sure it disposes. most AutoCAD functions work like that. – Alain Oct 14 '14 at 08:14
0

You should Lock the active document when updating it. Use a using statement to make sure it disposes. most AutoCAD functions work like that. In VB syntax:

    Using lock = Acdoc.lockdocument
      Using trans = acdoc.TransactionManager.starttransaction
          <<do your stuff>>
      end using
    end using
Alain
  • 304
  • 3
  • 9