The previous post about tables in MultiCAD.NET described how to create, modify and format tables with different data types and mentioned using table templates. In this post we will talk about templates and look more closely at the API that allows you to save a table to an external file as a template and then load it to the drawing to create type tables. Also the post will describe the table data exchange process between a MultiCAD.NET application and Microsoft Excel.
Example table
Let’s assume that the example table describes different types of mounting profiles (C-studs, U-channels etc.) and contains the following data: name, code, thickness, function.
We showed how to create and format tables in the previous post. Here, we’ll discuss how to save this table to an external file so that we can use it later as a template.
Saving table as a template
To save a table to a separate file, the McTable.SaveToFile()
method is used. The table can be saved to a file of one of the following formats:
- .dat files,
- tabbed text (.txt),
- comma-separated text,
- XML,
- Microsoft Excel (.xls),
- OpenOffice.org (.ods).
The table to be saved can be picked from the drawing. The following command allows user to pick a table and save it to a file of the available formats:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[CommandMethod("smpl_SaveTable", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void smpl_SaveTable() { McObjectId idSelected = McObjectManager.SelectObject("Select table:"); if (idSelected.IsNull) { return; } McTable outObj = McObjectManager.GetObject(idSelected); if (outObj == null) { return; } // SaveToFile() without arguments opens the Save table to file dialog. outObj.SaveToFile(); } |
Loading table from a file
To load a table from an external file, use the McTable.LoadFromFile()
method. As well as for SaveToFile()
, the file name can be specified explicitly or chosen from the dialog. The following file formats are supported for loading:
- table .tbl, .dat files,
- text (.txt, .csv, .xml),
- Microsoft Access(.mdb, .accdb),
- Microsoft Excel (.xls, .xlsx),
- OpenOffice.org (.ods),
- StarOffice (.sxc)
The following command adds a table from the user selected file to the drawing:
1 2 3 4 5 6 7 8 9 10 11 12 |
[CommandMethod("smpl_LoadTable", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void smpl_LoadTable() { McTable Table2 = new McTable(); // LoadFromFile() without arguments opens the Load table from file dialog. if (Table2.LoadFromFile()) { Table2.PlaceObject(McEntity.PlaceFlags.Silent); } } |
Now the saved table can be used as a template for adding type tables to the drawing:
Exchanging data with Excel
In addition to saving tables to files, MultiCAD.NET has the ability to quickly exchange data with Microsoft Excel sheets.
When the ExportToExcel()
method is called, the table data is copied to a newly created and opened Excel book, keeping the cells format the same.
The following command lets the user select a table in the drawing and export it to an Excel sheet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[CommandMethod("smpl_ExportTableToExcel", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void smpl_ExportTableToExcel() { McObjectId idSelected = McObjectManager.SelectObject("Select table to export:"); if (idSelected.IsNull) { return; } McTable outObj = McObjectManager.GetObject(idSelected); if (outObj == null) { return; } outObj.ExportToExcel(); } |
Also MultiCAD.NET supports importing table data from Excel. To import a selected cell range from the opened Excel sheet, use the ImportFromExcel()
method:
1 2 3 4 5 6 7 8 9 10 11 |
[CommandMethod("smpl_ImportTableFromExcel", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void smpl_ImportTableFromExcel() { McTable Table2 = new McTable(); if (Table2.ImportFromExcel()) { Table2.PlaceObject(McEntity.PlaceFlags.Silent); } } |
This post concludes our review of the basic features of the tables API in MultiCAD.NET. This was definitely not the entire list of the abilities provided by the API, so if you have any questions or suggestions regarding this functionality please feel free to write comments. The tables feature list is currently being enhanced and we’ll be happy to get suggestions for more subjects to discuss in future posts.