Tables in MultiCAD.NET. Part 1: Generating reports based on table templates

Tables_20_en

With this post, we’re beginning a series of articles about features and details of the MultiCAD.NET API for creating and modifying table entities.

For many drawings, tables are indispensable: they’re used to present descriptions, specifications, and instructions for creating construction elements represented in a drawing. It’s common to create tables based on properties extracted from drawing objects. Automating this process can save time and reduce errors.

As an example, let’s consider generating a report that contains a list of socket properties extracted from an electric map (a drawing that illustrates socket placement for a number of rooms.) Each socket is marked by a note that contains the following text data:

  • socket type,
  • code number/manufacturer,
  • room number.

Tables_14

The algorithm to create the table can be divided into three steps:

  • creating the custom report template,
  • loading the template and filling the table with data,
  • breaking the entire table into a number of pages and inserting the pages.

Creating the table template

Usually table reports are based on standard templates that are created with the tables UI. For demonstration purposes we are going to create a custom template using the MultiCAD.NET API, so that the whole table will look as follows:

Tables_15_en

Each cell value represents the number of sockets of a specific code in a specific room. Also, the table contains a page header that could be added for each page and a header for the first page with the table’s title.

Here is an example of the method that creates the template and saves it to a file with a specified name:

Users will be able to change the suggested table’s structure and cell formatting in the table editor.

This implementation of the table contains cells with special string identificators: #code and #room. These strings help to find the correct place for the data when the table is being filled. This ensures the table will be filled properly even if the numbers of rows or columns are changed.

Constructing a report

An algorithm for report building can be divided into the following steps:

  1. adding the command for creating the report
  2. selecting all the notes for multilayered constructions that contain description for sockets,
  3. loading the template and open the table editor to provide the ability to make changes in the template manually,
  4. structuring the data from the selected objects,
  5. filling in the table headers (the table’s title and column names),
  6. filling in the rows.

Here is the entire code for the example, with a discussion of key points:

Adding the command for creating the report

We will add the key method that is the handler for the smpl_CreateTableReport command and implements the algorithm:

Selecting objects

Selecting all notes for multilayered constructions can be performed using object filter:

As a result we will get an array of Ids for all notes in the drawing. Using these Ids we can obtain a text string of each note:

To simplify filling up the table, all text data should be arranged in an single structure that supports searching and sorting operations. Here the following structure of nested dictionaries is used: <product_code<room_number, count=”">>.

Loading the template

Let’s assume that the template is already created and saved to an external file (for example, C:\template.dat file) using the CreateTemplate() method. Now the saved table can be loaded using the McTable.LoadFromFile() method. Let’s create a table object, read the saved table into it and launch the table editor. It’s important to note that the table must be saved in a format that can keep the table’s structure and formatting, as well as its contents.

Tables_16_en

After that we can switch to filling up the table. The process includes sorting data received from the notes strings and adding them in appropriate cells. There is a quite wide list of possible implementations, one of which you can find in the example’s source code.

Page division

MultiCAD.NET contains an effective ability to divide a table into multiple fragments without losing its integrity. It’s used to split a large table in order to place it on a smaller sheet of paper, while still being able to edit it as a whole. To insert a page breaks by maximum page height the McTable.PagesTable.SetPageHeight() is used.

The following code fragment divides the table into separable pages so that the total height of the filled rows of each page should be less than 50 mm:

Tables_19_en

Another option is to insert a page break by a row index using the McTable.PagesTable.SetPageBreak() method. For example, to insert a page break after the row with index 7:

The result table will contain two pages:

Tables_18_en

After the table is divided, insertion points for each page need to be set. The points are set relative to the table’s insertion point using the McTable.PagesTable.SetOriginPage() method. The first parameter of the method is a page index, and the second parameter is the page insertion point. For example, to insert pages with the interval of 5:

Loading the example application to AutoCAD

Like any MultiCAD application, the example can simply run in nanoCAD after loading the example’s assembly and calling the registered smpl_CreateTableReport command. To run the MultiCAD application in AutoCAD, it’s required to use the special Object Enabler application. It’s important to note that the standard MultiCAD Enabler for AutoCAD (which is available in the nanoCAD Developers’ Club) doesn’t support objects from the Multicad.Symbols namespace. For running applications that deal with such objects, SPDS GraphiCS or MechaniCS (that contain full set of primitives including table and note entities) should be launched first.

One thought on “Tables in MultiCAD.NET. Part 1: Generating reports based on table templates

  1. Pingback: Tables in MultiCAD.NET. Part 2: Creating and modifying | nanoCAD API Blog

Leave a Reply