Inserted Table causing crash after saving,closing then reopening

nshupeFMPE3
Advocate
Advocate

Inserted Table causing crash after saving,closing then reopening

nshupeFMPE3
Advocate
Advocate

So I have a table that was generated using this code

public static Table BuildTable(Transaction Trans, String title, List<String> headers, Point3d insertPoint, LoopDataCollection tabledata)
{
    Table tb = new Table();

    try
    {
        tb.Position = insertPoint;

        tb.TableStyle = Active.Database.Tablestyle;

        tb.SetSize(tabledata.Count + 3, 1);

        tb.SetRowHeight(.5);
        tb.SetColumnWidth(1.5);
        tb.Cells[0, 0].SetValue(title, ParseOption.ParseOptionNone);
        tb.Cells[0, 0].TextHeight = .22;

        //Set the width of the columns
        tb.InsertColumns(0, 1.00, 1);
        tb.InsertColumns(0, 1.625, 1);
        tb.InsertColumns(0, 1.5, 1);

        //for each Header
        int irowcount = 1;
        int icolcount = 0;
        foreach (String header in headers)
        {
            tb.Cells[irowcount, icolcount].SetValue(header, ParseOption.ParseOptionNone);
            tb.Cells[irowcount, icolcount].TextHeight = 0.20;
            icolcount++;
        }

        //Set header row height
        tb.Rows[irowcount].Height = .50;

        //for each Data row
        irowcount = 2;
        foreach (LoopMarkerData loop in tabledata)
        {

            tb.Cells[irowcount, 0].SetValue(loop.ZoneId, ParseOption.ParseOptionNone);
            tb.Cells[irowcount, 0].TextHeight = 0.20;

            tb.Cells[irowcount, 1].SetValue(loop.Manifold, ParseOption.ParseOptionNone);
            tb.Cells[irowcount, 1].TextHeight = 0.20;

            tb.Cells[irowcount, 2].SetValue(loop.Loop, ParseOption.ParseOptionNone);
            tb.Cells[irowcount, 2].TextHeight = 0.20;

            tb.Cells[irowcount, 3].TextString = loop.Length;
            tb.Cells[irowcount, 3].TextHeight = 0.20;

            // Set row height for data
            tb.Rows[irowcount].Height = .375;

            //next row
            irowcount++;
        }

        //Add Totals
        tb.Cells[tb.Rows.Count - 1, 1].SetValue("Total", ParseOption.ParseOptionNone);
        tb.Cells[tb.Rows.Count - 1, 1].TextHeight = 0.25;

        tb.Cells[tb.Rows.Count - 1, 3].Contents.Add();
        tb.Cells[tb.Rows.Count - 1, 3].Contents[0].Formula = "=Sum(D3:D" + (tb.Rows.Count - 1) + ")";
        //Generate the layout
        tb.GenerateLayout();
    }
    catch (Autodesk.AutoCAD.Runtime.Exception ex)
    {
        Active.WriteMessage("\nError in UtilitiesHNC.BuildTable: " + ex.Message);
        _fail = true;
        Trans.Abort();
    }
    catch (System.Exception ex)
    {
        Active.WriteMessage("\nError in UtilitiesHNC.BuildTable: " + ex.Message);
        _fail = true;
        Trans.Abort();
    }
    return tb;
}


The file attached with the title "test_made_in_old_tools.dwg" has a working table, which is not causing any problems.

nshupeFMPE3_0-1709838446256.png

 



But if I use this code, I get a table that "works". But if I save and close the file, then reopen the file, I get a table where the formula cell is blank, and will crash my cad if clicked on

public static Table BuildTable(string title, List<string> headers, Point3d insertPoint, LoopDataCollection tableData)
{
    /*
         *Table gets created with default 1 row and 1 column
         * so when inserting default rows and columns, remove old row and column
         * which get pushed to the last index
         */

    Table tb = new Table();

    try
    {
        tb.Position = insertPoint;
        tb.Layer = LayerName;
        tb.TableStyle = Active.Database.Tablestyle;

        int zoneCol = 0, manifoldCol = 1, loopCol = 2, lengthCol = 3;

        int titleRow = 0, headersRow = 1, totalRow = 2;


        tb.InsertColumns(0, 1.5, 4);
        tb.DeleteColumns(tb.Columns.Count - 1, 1);//deletes the default column
        tb.SetColumnWidth(1.5);
        tb.Columns[manifoldCol].Width = 1.63;
        tb.Columns[loopCol].Width = 1;


        tb.InsertRows(0, .5, 3);
        tb.DeleteRows(tb.Rows.Count - 1, 1);
        tb.SetRowHeight(.5);
        tb.Cells.TextHeight = .19;//main text height

        //Title row
        CellRange titleRange = CellRange.Create(tb, titleRow, zoneCol, titleRow, lengthCol);
        tb.MergeCells(titleRange);
        tb.Rows[titleRow].TextHeight = .25;//bigger title
        tb.Cells[titleRow, 0].TextString = title;

        tb.Rows[titleRow].Alignment = CellAlignment.MiddleCenter;

        ////headers row
        tb.Cells[headersRow, zoneCol].TextString = "ZONE";
        tb.Cells[headersRow, manifoldCol].TextString = "MANIFOLD";
        tb.Cells[headersRow, loopCol].TextString = "LOOP";
        tb.Cells[headersRow, lengthCol].TextString = "LENGTH";

        tb.Rows[headersRow].Alignment = CellAlignment.MiddleCenter;


        foreach (LoopMarkerData data in tableData)
        {
            int row = tb.Rows.Count - 1;
            tb.InsertRows(row, .38, 1);

            tb.Cells[row, zoneCol].SetValue(data.ZoneId, ParseOption.SetDefaultFormat);

            tb.Cells[row, manifoldCol].SetValue(data.Manifold, ParseOption.SetDefaultFormat);

            tb.Cells[row, loopCol].SetValue(data.Loop, ParseOption.SetDefaultFormat);

            tb.Cells[row, lengthCol].TextString = data.LengthTotal;

        }

        ////total row
        int bottomRow = tb.Rows.Count - 1;

        tb.Cells[bottomRow, manifoldCol].TextString = "TOTAL";

        tb.Cells[tb.Rows.Count - 1, 3].Contents.Add();
        tb.Cells[tb.Rows.Count - 1, 3].Contents[0].Formula = "=Sum(D3:D" + (tb.Rows.Count - 1) + ")";


        tb.GenerateLayout();
    }
    catch (Exception ex)
    {
        Active.WriteMessage($"\nError in {nameof(BuildTable)}: {ex.Message}");
        return null;
    }
    return tb;
}



The file labeled "broken_test_in_old_tools.dwg" has the working table, and the "broken" table. I'm really at a loss for what could be causing the problem, and when I click the cell that is suppose to have the formula but doesn't, AutoCAD crashes without any prompt for a stack trace or anything.

nshupeFMPE3_1-1709838593527.png

 

Thanks for any help.





0 Likes
Reply
755 Views
11 Replies
Replies (11)

ActivistInvestor
Advisor
Advisor

Have you tried adding the Table to a database earlier, before setting most properties?

0 Likes

nshupeFMPE3
Advocate
Advocate

I had not tried that, so I made this test method and got the same sort of result. 

 

[CommandMethod(nameof(TestTest))]
public void TestTest()
{
    ObjectId tbId = ObjectId.Null;

    Point3d insertPoint = Active.Editor.GetUserPoint("\nInsert Point: ");
    if(insertPoint == Point3d.Origin) return;

    using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
    using(BlockTableRecord btr = tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite, false, true) as BlockTableRecord)
    {
        Table tb = new Table();

        tb.Position = insertPoint;
        tb.Layer = "0";
        tb.TableStyle = Active.Database.Tablestyle;

        tbId = btr.AppendEntity(tb);

        tr.AddNewlyCreatedDBObject(tb, true);
        tr.Commit();
    }

    LoopDataCollection loopData = LoopDataCollection.Get();

    using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
    using (Table tb = tr.GetObject(tbId, OpenMode.ForWrite, false, true) as Table)
    {
        try
        {
            int zoneCol = 0, manifoldCol = 1, loopCol = 2, lengthCol = 3;

            int titleRow = 0, headersRow = 1, totalRow = 2;


            tb.InsertColumns(0, 1.5, 4);
            tb.DeleteColumns(tb.Columns.Count - 1, 1);//deletes the default column
            tb.SetColumnWidth(1.5);
            tb.Columns[manifoldCol].Width = 1.63;
            tb.Columns[loopCol].Width = 1;


            tb.InsertRows(0, .5, 3);
            tb.DeleteRows(tb.Rows.Count - 1, 1);
            tb.SetRowHeight(.5);
            tb.Cells.TextHeight = .19;//main text height

            //Title row
            CellRange titleRange = CellRange.Create(tb, titleRow, zoneCol, titleRow, lengthCol);
            tb.MergeCells(titleRange);
            tb.Rows[titleRow].TextHeight = .25;//bigger title
            tb.Cells[titleRow, 0].TextString = "LOOP SCHEDULE";

            tb.Rows[titleRow].Alignment = CellAlignment.MiddleCenter;

            ////headers row
            tb.Cells[headersRow, zoneCol].TextString = "ZONE";
            tb.Cells[headersRow, manifoldCol].TextString = "MANIFOLD";
            tb.Cells[headersRow, loopCol].TextString = "LOOP";
            tb.Cells[headersRow, lengthCol].TextString = "LENGTH";

            tb.Rows[headersRow].Alignment = CellAlignment.MiddleCenter;


            foreach (LoopMarkerData data in loopData)
            {
                int row = tb.Rows.Count - 1;
                tb.InsertRows(row, .38, 1);

                tb.Cells[row, zoneCol].SetValue(data.ZoneId, ParseOption.SetDefaultFormat);

                tb.Cells[row, manifoldCol].SetValue(data.Manifold, ParseOption.SetDefaultFormat);

                tb.Cells[row, loopCol].SetValue(data.Loop, ParseOption.SetDefaultFormat);

                tb.Cells[row, lengthCol].TextString = data.LengthTotal;

            }

            ////total row
            int bottomRow = tb.Rows.Count - 1;

            tb.Cells[bottomRow, manifoldCol].TextString = "TOTAL";

            tb.Cells[tb.Rows.Count - 1, 3].Contents.Add();
            tb.Cells[tb.Rows.Count - 1, 3].Contents[0].Formula = "=Sum(D3:D" + (tb.Rows.Count - 1) + ")";


            tb.GenerateLayout();
            tb.Commit();
        }
        catch
        {

        }
    }

}

 

0 Likes

ActivistInvestor
Advisor
Advisor

Are you calling SetDatabaseDefaults() on the new Table somewhere?

 

If that doesn't change anything, try running the AUDIT command.

 

0 Likes

nshupeFMPE3
Advocate
Advocate
Thanks for the suggestions. Unfortunately neither worked. I added SetDatabaseDefaults() to the table creation before appending it to the block table record of the paperspace layout. I still got the same buggy result.
At which point I ran the AUDIT command, first time it asked if I wanted to fix errors and I said no and it said there was only 1 problem. Next time I had it fix the problem and it reported success. But I had like a dozen bugged tables from testing and it didnt fix any of them or the newest bugged one.
0 Likes

daniel_cadext
Advisor
Advisor

GenerateLayout should be just after set size.
SetDatabaseDefaults should be before.

My bet is on SetValue, what is data.ZoneId?

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes

ActivistInvestor
Advisor
Advisor

Are you running AUDIT right after your code creates the Table or after you save and reopen the drawing?

 

I'm not going any further on this because I just noticed that your code has an empty catch{} block😬

 

If there's an exception thrown, you won't see it unless you're running in the debugger.

Put a line of code in the catch block that displays the exception on the console.

 

nshupeFMPE3
Advocate
Advocate

@daniel_cadext 
I made some of the changes you suggested, but I'm still getting the same result. Also ZoneId is a string that in this case is a number ie 1, 2, 3, ...

Here is the code that I changed, in this transaction I try creating and setting basic parameters. Then I open the table again and edit the other things that need doing.

using (Transaction tr = Active.Database.TransactionManager.StartTransaction())
using(BlockTableRecord btr = tr.GetObject(Active.Database.CurrentSpaceId, OpenMode.ForWrite, false, true) as BlockTableRecord)
{
    Table tb = new Table();

    tb.Position = insertPoint;
    tb.Layer = "0";
    tb.TableStyle = Active.Database.Tablestyle;
    tb.SetDatabaseDefaults(Active.Database);

    tb.SetSize(loopData.Count + 3, 1);

    tb.GenerateLayout();

    tbId = btr.AppendEntity(tb);

    tr.AddNewlyCreatedDBObject(tb, true);

    tr.Commit();
}


@ActivistInvestor 

I rewrote the code to use the standard API instead of a bunch of extension methods that I normally use in order to post it here for easier reading by the community. The "original" and this shared version should be functionally the same, but the "original" version has a try/catch that would alert me if there was an error. I've also watched the running of both using the debugger with a break point and haven't had an exceptions thrown. 

I did just try running audit after creating the table and got a new result. I have a file right now that I keep opening and saving in for testing. Its full of tables that are "broken". I placed a table using the command in question, and then before saving or closing I ran Audit with Yes to fix problems and got this

Fix any errors detected? [Yes/No] <N>: y
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 1000    objects audited
Auditing Entities Pass 2
Pass 2 1000    objects audited
Auditing Blocks
 20      Blocks auditedReference to a deleted block 45
Blocks are self referencing
Block "*T" references block "*U"
Block "*U" references block "*U"
The reference to block "*U" from block "*U" will be deleted
 29      Blocks audited
Auditing AcDsRecords
Total errors found 1 fixed 1
Erased 0 objects


And had this printed above one of the tables. Its not on the table that was made immediatly before running Audit, but the table that was made during the last "test" where I made a table, saved, closed, reopened. 

nshupeFMPE3_0-1710165990409.png

 




0 Likes

ActivistInvestor
Advisor
Advisor

run your code in an empty drawing or a drawing that contains only what your code needs to run and then run audit after your code runs. 

0 Likes

nshupeFMPE3
Advocate
Advocate

So I used the file MinimumNeeded.dwg, which as the name implies is the minimum items, layers, etc to run the code. I run the code and generate the table, then run AUDIT and get this output.

Command: AUDIT
Fix any errors detected? [Yes/No] <N>: y
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 300     objects audited
Auditing Entities Pass 2
Pass 2 300     objects audited
Auditing Blocks
 4       Blocks audited
Auditing AcDsRecords
Total errors found 0 fixed 0
Erased 0 objects


Save, close, reopen. Problem still occurs, which is in the file test-1

0 Likes

ActivistInvestor
Advisor
Advisor

And after you save and reopen the drawing and run Audit?

0 Likes

nshupeFMPE3
Advocate
Advocate

@ActivistInvestor 

So right after placing the table and running audit (haven't saved yet.)

Fix any errors detected? [Yes/No] <N>:
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 300     objects audited
Auditing Entities Pass 2
Pass 2 300     objects audited
Auditing Blocks
 4       Blocks audited
Auditing AcDsRecords
Total errors found 0 fixed 0
Erased 0 objects


After saving (not closing)

Command: _qsave
Command: Specify opposite corner or [Fence/WPolygon/CPolygon]: audit
Fix any errors detected? [Yes/No] <N>:
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 300     objects audited
Auditing Entities Pass 2
Pass 2 300     objects audited
Auditing Blocks
 4       Blocks audited
Auditing AcDsRecords
Total errors found 0 fixed 0
Erased 0 objects


After closing and opening again

Fix any errors detected? [Yes/No] <N>:
Auditing Header
Auditing Tables
Auditing Entities Pass 1
Pass 1 300     objects audited
Auditing Entities Pass 2
Pass 2 300     objects audited
Auditing Blocks
Reference to a deleted block 4
Blocks are self referencing
Block "*T" references block "*U"
Block "*U" references block "*U"
 4       Blocks audited
Auditing AcDsRecords
Total errors found 1 fixed 0
Erased 0 objects
0 Likes