Inserting A Table into a Side Database

Inserting A Table into a Side Database

jschierenbeck
Enthusiast Enthusiast
1,111 Views
4 Replies
Message 1 of 5

Inserting A Table into a Side Database

jschierenbeck
Enthusiast
Enthusiast

My goal is to create a BOM table and insert it to a new drawing, without opening the new drawing.
My approach is to create a new database from a template drawing, then add the table to the new database.

I am able to get everything to work when I insert the table to the active drawing, but when I try and use a side database it does not insert the table.
Am I doing something wrong here?

 

Attached is my Code

0 Likes
Accepted solutions (1)
1,112 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

When you create a new Database for reading a dwg file (db.ReadDwgFile) you have to use false for the buildDefaultDrawing argument. And you do not need to do it twice.

 

Try like this (not tested):

            using (DocumentLock acLckDoc = acdoc.LockDocument())
            {
                using (var newDb = new Database(false, true))
                {
                    //Create a new .dwg using a template drawing.
                    newDb.ReadDwgFile(dwgtemplate, FileOpenMode.OpenForReadAndAllShare, false, null);
                
                    // Start a transaction in the new database
                    using (Transaction acTrans = newDb.TransactionManager.StartTransaction())
                    {
                        // Import the Excel BOM as a datatable.
                        System.Data.DataTable bomDT = Excel.LoadCSVFile(dwgBOMCSV);
                        int totalRows = bomDT.Rows.Count;
                        totalRows += 2;

                        string[,] bomArray = new string[totalRows, 4];
                        bomArray[1, 0] = "TAG";
                        bomArray[1, 1] = "DESCRIPTION";
                        bomArray[1, 2] = "CATALOG";
                        bomArray[1, 3] = "QTY";

                        int i = 2;
                        // Load the datatable into an array
                        foreach (DataRow item in bomDT.Rows)
                        {
                            var tag = item[0].ToString();
                            var description = item[1].ToString();
                            var catalog = item[2].ToString();
                            var qty = item[3].ToString();
                            ed.WriteMessage("\n" + item[2]);

                            bomArray[i, 0] = tag;
                            bomArray[i, 1] = description;
                            bomArray[i, 2] = catalog;
                            bomArray[i, 3] = qty;
                            i += 1;
                        }
                        var bt = acTrans.GetObject(newDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                        var tb = new Table();
                        
                        tb.TableStyle = newDb.Tablestyle;
                        // insert rows and columns
                        totalRows -= 1;
                        tb.InsertRows(1, .2533, totalRows);
                        tb.InsertColumns(0, 2, 1);
                        tb.InsertColumns(0, 7, 1);
                        tb.InsertColumns(0, 2, 1);

                        tb.Position = new Point3d(1.5, 22, 0);
                        totalRows += 1;
                        for (int a = 1; a < totalRows; a++)
                        {
                            for (int b = 0; b < 4; b++)
                            {
                                string itemToInsert = bomArray[a, b];
                                tb.Cells[a, b].TextHeight = .1033;
                                tb.Cells[a, b].TextString = itemToInsert;
                                tb.Cells[a, b].Alignment = CellAlignment.MiddleCenter;
                            }
                        }

                        tb.GenerateLayout();
                        var btr = acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                        btr.AppendEntity(tb);
                        acTrans.AddNewlyCreatedDBObject(tb, true);
                        acTrans.Commit();
                                                                                                      
                    }
                    newDb.SaveAs(bomDwg, DwgVersion.Current);                                          
                }
            }
            HostApplicationServices.WorkingDatabase = acDb;
        }   

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

jschierenbeck
Enthusiast
Enthusiast

@_gile  Thanks for the quick response!

I tried the code you gave me, but it still does the same thing.
It creates the new drawing, but no table is inserted. 

 

If I insert the table on the active drawing (using the active drawing database instead of "newDb") it works perfectly.

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

Try removing db.generateLayout(); or displacing it after the table is added to the transaction.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

jschierenbeck
Enthusiast
Enthusiast

@_gile  Removing the tb.GenerateLayout solved the issue! Seems to be working perfectly now.
Thanks for the help.

0 Likes