AutoCAD eNotOpenForWrite

I am programming an AutoCAD plugin in C # .NET. I need to install Table.IsReadEnabled

and Table.IsWriteEnabled

on 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 call tr.Commit

sets the table IsReadEnabled

and IsWriteEnabled

to false even on a transaction OpenClose

. This causes AutoCAD to crash when trying to add a new data table to the table. I need to either re-enable writing and reading after the call tr.Commit

, or configure it so that writing and reading are never disabled.

+3


source to share


4 answers


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!

+1


source


I think you should use StartTransaction

instead StartOpenCloseTransaction

. See the attached blog.

Cheers, Alain



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

0


source


Most likely, this error occurs when the previous transaction is not configured correctly. The "addRow" function from the question is an example of how not to code. Always use the C # "using" statement to start a transaction. It ensures that the transaction works properly. Review and fix all the places in the code where you start a transaction.

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

      

0


source


Use try catch to get the exact exception. There is also no transaction.

0


source







All Articles