📜 ⬆️ ⬇️

Work with tables in MultiCAD.NET. Part 2. Creating and editing



In the previous article, we introduced you to an example of using the table functionality of the MultiCAD.NET API to automatically generate a report on selected objects. We intentionally violate the chronology and today's publication will begin with the creation and formatting of the simplest table. Consider filling the table with data in text and numeric format, as well as the use of formulas. Then, we proceed to adding blocks and sub-tables as the contents of cells and conclude the article with a description of using properties of drawing objects as dynamically changeable data in a table.


Creating and formatting tables

In the MultiCAD.NET API, tables are represented by the McTable class from the Multicad.Symbols.Tables namespace. The following code fragment creates an empty table, and then adds 2 rows and 3 columns to it, starting from the zero position:
')
 McTable Table1 = new McTable(); int rowCount = 4; int colCount = 5; Table1.Rows.AddRange(0, rowCount + 1); Table1.Columns.AddRange(0, colCount); 


In the same way, any range of rows and columns can be added to the specified positions.

Add the content for the newly created table and define the format of the cells. For example, the table will store data on the list of parts: the sequence number in the table, name, batch number, material and quantity. For cells containing the number of units, we define the number format, the remaining cells will contain text.



 // Sets color and text height that will be used for all table cells by default: Table1.DefaultCell.TextHeight = 2.5; Table1.DefaultCell.TextColor = System.Drawing.Color.Aqua; // The first row is the title, let's merge all its cells and set text alignment and height System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, colCount - 1, 0); Table1.Merge(rect); Table1[0, 0].HorizontalTextAlign = HorizTextAlign.Center; Table1[0, 0].VerticalTextAlign = VertTextAlign.Center; Table1[0, 0].TextHeight = 5; // Fill up the table foreach (var cell in Table1.Rows[2].Cells) { cell.VerticalTextAlign = VertTextAlign.Center; cell.HorizontalTextAlign = HorizTextAlign.Center; } Table1.Columns[0].Width = 10; Table1[0, 0].Value = " "; Table1[1, 0].Value = "."; Table1[1, 1].Value = ""; Table1[1, 2].Value = ", ."; Table1[1, 3].Value = ""; Table1[1, 4].Value = " "; Table1[2, 0].Value = "1."; Table1[2, 1].Value = ""; Table1[2, 2].Type = CellFormatEnum.Number; Table1[2, 2].Value = "1"; Table1[2, 3].Value = " "; Table1[2, 4].Value = "1938-1"; Table1[3, 0].Value = "2."; Table1[3, 1].Value = ""; Table1[3, 2].Type = CellFormatEnum.Number; Table1[3, 2].Value = "1"; Table1[3, 3].Value = " "; Table1[3, 4].Value = "0132-2"; Table1[4, 0].Value = "3."; Table1[4, 1].Value = ""; Table1[4, 2].Type = CellFormatEnum.Number; Table1[4, 2].Value = "1"; Table1[4, 3].Value = ""; Table1[4, 4].Value = "0715-7"; 


One of the distinguishing features of tables in MultiCAD.NET is the ability to specify the placement of text in a cell if the width or height of the text is larger than the cell size when the table is filled. Using the HorzFits property HorzFits you can set the following horizontal HorzFits modes:



The VertFits property sets vertical text entry:



Of course, after the table is formed, you can edit its structure. For example, add, delete, or copy rows and columns. Add another “Bolt” part to the list, move the “Quantity” column to the last position and delete the column with part numbers:

 Table1.Rows.CopyRange(4, 5, 1, true); Table1.Columns.Move(2,5); Table1.Columns.Delete(0); 




Then add the ability to take into account the total number of parts in the table, using the built-in table formula "Amount":

 Table1.Rows.AddRange(Table1.Rows.Count, 1); System.Drawing.Rectangle rect2 = new System.Drawing.Rectangle(0, Table1.Rows.Count - 1, Table1.Columns.Count - 2, 0); Table1.Merge(rect2); Table1[Table1.Rows.Count - 1, 0].TextHeight = 5; Table1[Table1.Rows.Count - 1, 0].Value = ":"; String SummStartCell = Table1[2, Table1.Columns.Count - 1].AddressOfCell(); String SummEndCell = Table1[Table1.Rows.Count - 2, Table1.Columns.Count - 1].AddressOfCell(); Table1[Table1.Rows.Count - 1, Table1.Columns.Count - 1].ValueFormula = "=summ(" + SummStartCell + ":" + SummEndCell + ")"; 


As a result, our table will look like this:



To summarize data from a range of cells, a string representation of the formula was added to the result cell. In the table editor, the contents of this cell will look like this:



The table editor itself can be called by double-clicking on the table in the drawing or programmatically by calling the OnEdit() method OnEdit() table object.

Note that in the example for the contents of the cells with the number of parts, the number format was previously set. The default format is Auto , which allows you to automatically determine the type of data.

Adding a table to a drawing

As a normal primitive, a table can be added to a drawing using one of two methods:

Table.DbEntity.AddToCurrentDocument(); - adds a table primitive to the drawing, the coordinates of the upper left corner of the table coincide with the origin.
Table.PlaceObject(); - interactive object insertion. The user will be prompted to define the table insertion point. Depending on the values ​​of the arguments, insertion can be performed with a preliminary call to the table editor. Calling the method without arguments, as well as with the value McEntity.PlaceFlags.Normal will call the table editor.
Add a table to the drawing using the interactive selection of the insertion point, without calling the table editor:

 Table1.PlaceObject(McEntity.PlaceFlags.Silent); 


Using blocks and sub-tables as cell contents

In tables MultiCAD.NET, in addition to the usual text and numeric data, as the contents of the cells, you can use blocks and other tables. Consider this possibility on the example of creating a table that will describe the elements of a metal profile. The first column will indicate the type of the profile, the second - its specification, the third - the profile image:



Suppose a profile type specification is stored in a table of the following form:



And the general view of various profiles is in the external file as separate blocks:



Add a profile image and its specification to the main table.

Insert subtables

To insert subtables, use the method

McTable.InsertSubtable(ref McTable inTable, int row, int col, InsertionModeEnum mode);
inTable - the table to be inserted
row, col is the row and column number of the cell to insert into,
mode - insert table mode.
The insert modes can be as follows:

InSingleCell - the table will be inserted into a separate cell. The structure of the resulting table will be changed in accordance with the structure of the inserted table.
CellByCell - the table will be inserted “cell into cell”, starting with the top left. The resulting table will contain those cells that are common to the structures of both tables.
Over - the table will be inserted over. The cell sizes of both tables do not change.

Insert the profile specification table into a separate cell of the main table:

 Table1.InsertSubtable(ProfileTable1, 1, 1, InsertionModeEnum.InSingleCell); 




Blocks as cell contents

To insert a block, use the EmbedBlock() method of the Cell class, which allows you to embed a block in a separate cell, specifying the block ID, its name or the block name and the name of the file that contains it:

bool EmbedBlock(int row, int col, McObjectId Id);
bool EmbedBlock(int row, int col, ref String name);
bool EmbedBlock(int row, int col, ref String name, ref String fileName);
Insert the image of the guide profile, which is stored in an external file as a block:

 Table1[1, 0].Value = " "; Table1[1, 2].EmbedBlock("Profile_03", "C:\\Profiles.dwg"); 




Retrieving object properties to a table

Another useful function of the table API in MultiCAD.NET is the binding of primitives to the table. After an object is attached to a table, you can use its properties as the contents of a cell using formulas. The following code fragment appends a closed polyline to the table and receives the value of the area bounded by the polyline as the cell contents (0, 0):

 String object = Table1.AttachObject(polyline.ID); Table1[0, 0].ValueFormula = object + ".\"Geometry.Area\""; 


Binding objects to a table allows you to dynamically track changes: when the properties of an attached object change, the contents of the corresponding cell will also change.

Discussion of the article is also available on our forum: forum.nanocad.ru/index.php?showtopic=6511 .

Translation of the article into English: Tables in MultiCAD.NET. Part 2: Creating and modifying .

See also:

Work with tables in MultiCAD.NET. Part 1. Creating a report based on a template
Work with tables in MultiCAD.NET. Part 3. External table files and data exchange with Microsoft Excel

Source: https://habr.com/ru/post/198774/


All Articles