Nick Gilbert
Nick Gilbert

Reputation: 4240

AutoCAD eNotOpenForWrite

I'm programming an AutoCAD plugin in C#.NET. I need a way to set Table.IsReadEnabled and Table.IsWriteEnabled to true. I have a method called addRow() shown here:

public void addRow(String[] data)
        {
            OpenCloseTransaction tr = doc.TransactionManager.StartOpenCloseTransaction();
            DocumentLock docLock = doc.LockDocument();
            using (tr)
            using (docLock)
            {
                bool isRead = IsReadEnabled;
                bool isWrite = IsWriteEnabled;

                BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                selectedRow++; //Sets the lowest empty row as the one to be modified

                //Adding data to each cell
                for (int i = 0; i < data.Length; i++)
                {
                    Cells[selectedRow, i].SetValue(data[i], ParseOption.SetDefaultFormat);
                }
                GenerateLayout();

                //Attempting to update database with new data

                btr.AppendEntity(this);
                tr.AddNewlyCreatedDBObject(this, true);

                tr.Commit();

            }
        }

The first time it adds data to my table it works fine but the tr.Commit call sets the table's IsReadEnabled and IsWriteEnabled to false even with an OpenClose transaction. That causes AutoCAD to crash when it tries to add a new row of data to the table. I need to either re-enable writing and reading after the tr.commit call or set it up so that writing and reading are never disabled.

Upvotes: 3

Views: 2490

Answers (4)

Raghulan Gowthaman
Raghulan Gowthaman

Reputation: 468

Use try catch to get exact exception. Also transaction is missing.

Upvotes: 0

ialiashkevich
ialiashkevich

Reputation: 715

Most probably this error happens when previous transaction is not properly disposed. The "addRow" function from the question is example of how not to code. Always use the C# "using" statement to start transaction. It makes sure that transaction is disposed properly. Review and fix all places in your code where you start transaction.

using(OpenCloseTransaction tr = doc.TransactionManager.StartOpenCloseTransaction())
{
   // your logic here
}

Upvotes: 0

Nick Gilbert
Nick Gilbert

Reputation: 4240

if (!IsWriteEnabled || !IsReadEnabled) //Committing transactions closes everything for reading and writing so it must be reopened
{
    tr.GetObject(this.ObjectId, OpenMode.ForRead);
    tr.GetObject(this.ObjectId, OpenMode.ForWrite);
}

I just needed to add an if statement!

Upvotes: 1

Alain
Alain

Reputation: 404

I think you should use StartTransaction instead of StartOpenCloseTransaction. See attached blog.

Cheers, Alain

http://spiderinnet1.typepad.com/blog/2012/08/autocad-net-openclosetransaction-can-it-be-nested.html

Upvotes: 0

Related Questions