omdl  v0.5
OpenSCAD Mechanical Design Library
Datatable

Data table encoding and lookup. More...

+ Collaboration diagram for Datatable:

Files

file  table.scad
 Data table encoding and lookup.
 

Functions

function table_get_row_idx (rows, row_id)
 Get the index for a table row identifier. More...
 
function table_get_row (rows, row_id)
 Get the row for a table row identifier. More...
 
function table_get_col_idx (cols, col_id)
 Get the index for a table column identifier. More...
 
function table_get_col (cols, col_id)
 Get the column for a table column identifier. More...
 
function table_get (rows, cols, row_id, col_id)
 Get the value for a table row and column identifier. More...
 
function table_get_row_cols (rows, cols, col_id)
 Form a vector from the specified column of each table row. More...
 
function table_get_row_ids (rows)
 Form a vector of each table row identifier. More...
 
function table_exists (rows, cols, row_id, col_id)
 Test the existence of a table row and column identifier. More...
 
function table_size (rows, cols)
 Get the size of a table. More...
 
module table_check (rows, cols, verbose=false)
 Perform some basic validation/checks on a table. More...
 
module table_dump (rows, cols, rows_sel, cols_sel, number=true)
 Dump a table to the console. More...
 
function table_copy (rows, cols, rows_sel, cols_sel)
 Create a copy of select rows and columns of a table. More...
 
function table_sum (rows, cols, rows_sel, cols_sel)
 Sum select rows and columns of a table. More...
 

Detailed Description

Data table encoding and lookup.

Example

use <table.scad>;
table_cols =
[ // id, description
["id", "row identifier"],
["ht", "head type [r|h|s]"],
["td", "thread diameter"],
["tl", "thread length"],
["hd", "head diameter"],
["hl", "head length"],
["nd", "hex nut flat-to-flat width"],
["nl", "hex nut length"]
];
table_rows =
[ // id, ht, td, tl, hd, hl, nd, nl
["m3r08r", "r", 3.000, 8.00, 5.50, 3.000, 5.50, convert_length(1.00, "in")],
["m3r14r", "r", 3.000, 14.00, 5.50, 3.000, 5.50, convert_length(1.25, "in")],
["m3r16r", "r", 3.000, 16.00, 5.50, 3.000, 5.50, convert_length(1.50, "in")],
["m3r20r", "r", 3.000, 20.00, 5.50, 3.000, 5.50, convert_length(1.75, "in")]
];
table_check( table_rows, table_cols, true );
table_dump( table_rows, table_cols );
m3r16r_tl = table_get( table_rows, table_cols, "m3r16r", "tl" );
if ( table_exists( cols=table_cols, col_id="nl" ) )
echo ( "metric 'nl' available" );
table_ids = table_get_row_ids( table_rows );
table_cols_tl = table_get_row_cols( table_rows, table_cols, "tl" );
echo ( table_ids=table_ids );
echo ( table_cols_tl=table_cols_tl );
tnew = table_copy( table_rows, table_cols, cols_sel=["tl", "nl"] );
tsum = table_sum( table_rows, table_cols, cols_sel=["tl", "nl"] );
echo ( m3r16r_tl=m3r16r_tl );
echo ( tnew=tnew );
echo ( tsum=tsum );

Result

ECHO: "[ INFO ] table_check(); begin table check"
ECHO: "[ INFO ] table_check(); row identifier found at column zero."
ECHO: "[ INFO ] table_check(); checking row column counts."
ECHO: "[ INFO ] table_check(); checking for repeat column identifiers."
ECHO: "[ INFO ] table_check(); checking for repeat row identifiers."
ECHO: "[ INFO ] table_check(); table size: 4 rows by 8 columns."
ECHO: "[ INFO ] table_check(); end table check"
ECHO: ""
ECHO: "row: 0"
ECHO: "[m3r08r] [id] (row identifier) = [m3r08r]"
ECHO: "[m3r08r] [ht] (head type [r|h|s]) = [r]"
ECHO: "[m3r08r] [td] (thread diameter) = [3]"
ECHO: "[m3r08r] [tl] (thread length) = [8]"
ECHO: "[m3r08r] [hd] (head diameter) = [5.5]"
ECHO: "[m3r08r] [hl] (head length) = [3]"
ECHO: "[m3r08r] [nd] (hex nut flat-to-flat width) = [5.5]"
ECHO: "[m3r08r] [nl] (hex nut length) = [25.4]"
ECHO: ""
ECHO: "row: 1"
ECHO: "[m3r14r] [id] (row identifier) = [m3r14r]"
ECHO: "[m3r14r] [ht] (head type [r|h|s]) = [r]"
ECHO: "[m3r14r] [td] (thread diameter) = [3]"
ECHO: "[m3r14r] [tl] (thread length) = [14]"
ECHO: "[m3r14r] [hd] (head diameter) = [5.5]"
ECHO: "[m3r14r] [hl] (head length) = [3]"
ECHO: "[m3r14r] [nd] (hex nut flat-to-flat width) = [5.5]"
ECHO: "[m3r14r] [nl] (hex nut length) = [31.75]"
ECHO: ""
ECHO: "row: 2"
ECHO: "[m3r16r] [id] (row identifier) = [m3r16r]"
ECHO: "[m3r16r] [ht] (head type [r|h|s]) = [r]"
ECHO: "[m3r16r] [td] (thread diameter) = [3]"
ECHO: "[m3r16r] [tl] (thread length) = [16]"
ECHO: "[m3r16r] [hd] (head diameter) = [5.5]"
ECHO: "[m3r16r] [hl] (head length) = [3]"
ECHO: "[m3r16r] [nd] (hex nut flat-to-flat width) = [5.5]"
ECHO: "[m3r16r] [nl] (hex nut length) = [38.1]"
ECHO: ""
ECHO: "row: 3"
ECHO: "[m3r20r] [id] (row identifier) = [m3r20r]"
ECHO: "[m3r20r] [ht] (head type [r|h|s]) = [r]"
ECHO: "[m3r20r] [td] (thread diameter) = [3]"
ECHO: "[m3r20r] [tl] (thread length) = [20]"
ECHO: "[m3r20r] [hd] (head diameter) = [5.5]"
ECHO: "[m3r20r] [hl] (head length) = [3]"
ECHO: "[m3r20r] [nd] (hex nut flat-to-flat width) = [5.5]"
ECHO: "[m3r20r] [nl] (hex nut length) = [44.45]"
ECHO: ""
ECHO: "table size: 4 rows by 8 columns."
ECHO: "metric 'nl' available"
ECHO: table_ids = ["m3r08r", "m3r14r", "m3r16r", "m3r20r"]
ECHO: table_cols_tl = [8, 14, 16, 20]
ECHO: m3r16r_tl = 16
ECHO: tnew = [[8, 25.4], [14, 31.75], [16, 38.1], [20, 44.45]]
ECHO: tsum = [58, 139.7]

Function Documentation

module table_check ( rows  ,
cols  ,
verbose  = false 
)

Perform some basic validation/checks on a table.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
verbose<boolean> Be verbose during check.

Check that: (1) the first table column identifier is 'id'. (2) Make sure that each row has the same number of columns as defined in the columns vector. (3) Make sure that there are no repeating column identifiers. (4) Make sure that there are no repeating row identifiers.

Definition at line 254 of file table.scad.

function table_copy ( rows  ,
cols  ,
rows_sel  ,
cols_sel   
)

Create a copy of select rows and columns of a table.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
rows_sel<1d-vector> A n-tuple vector of row identifier to select.
cols_sel<1d-vector> A n-tuple vector of column identifier to select.
Returns
<2d-vector> The selected rows and columns of the table.
module table_dump ( rows  ,
cols  ,
rows_sel  ,
cols_sel  ,
number  = true 
)

Dump a table to the console.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
rows_sel<1d-vector> A n-tuple vector of row identifier to select.
cols_sel<1d-vector> A n-tuple vector of column identifier to select.
number<boolean> Number the table rows.

Output each table row to the console. To output only select rows and columns, assign the desired identifiers to rows_sel and cols_sel. For example to output only the column identifiers 'c1' and 'c2', assign cols_sel = ["c1", "c2"].

Definition at line 337 of file table.scad.

function table_exists ( rows  ,
cols  ,
row_id  ,
col_id   
)

Test the existence of a table row and column identifier.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
row_id<string> The row identifier string to locate.
col_id<string> The column identifier string to locate.
Returns
true if the row and column identifier exists, otherwise returns false.
function table_get ( rows  ,
cols  ,
row_id  ,
col_id   
)

Get the value for a table row and column identifier.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
row_id<string> The row identifier string to locate.
col_id<string> The column identifier string to locate.
Returns
<decimal|string> The value at the located row_id and col_id. If it does not exists, returns undef.
function table_get_col ( cols  ,
col_id   
)

Get the column for a table column identifier.

Parameters
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
col_id<string> The column identifier string to locate.
Returns
<vector> The column where the row identifier is located. If the identifier does not exists, returns undef.
function table_get_col_idx ( cols  ,
col_id   
)

Get the index for a table column identifier.

Parameters
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
col_id<string> The column identifier string to locate.
Returns
<decimal> The column index where the identifier is located. If the identifier does not exists, returns empty_v.
function table_get_row ( rows  ,
row_id   
)

Get the row for a table row identifier.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
row_id<string> The row identifier string to locate.
Returns
<vector> The row where the row identifier is located. If the identifier does not exists, returns undef.
function table_get_row_cols ( rows  ,
cols  ,
col_id   
)

Form a vector from the specified column of each table row.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
col_id<string> The column identifier string.
Returns
<vector> The vector formed by selecting the col_id for each row in the table. If column does not exists, returns undef.
function table_get_row_ids ( rows  )

Form a vector of each table row identifier.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
Returns
<vector> The vector of table row identifiers. If column "id" does not exists, returns undef.
Note
This functions assumes the first element of each table row to be the row identifier, as enforced by the table_check(). As an alternative, the function table_get_row_cols(), of the form table_get_row_cols(rows, cols, "id"), may be used without this assumption.
function table_get_row_idx ( rows  ,
row_id   
)

Get the index for a table row identifier.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
row_id<string> The row identifier string to locate.
Returns
<decimal> The row index where the identifier is located. If the identifier does not exists, returns empty_v.
function table_size ( rows  ,
cols   
)

Get the size of a table.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
Returns
<decimal> The table size.

The size is reported as: (1) The number of rows when only the rows parameter is specified. (2) The number of columns when only the cols parameter is specified. (3) The (rows * columns) when both parameters are specified.

function table_sum ( rows  ,
cols  ,
rows_sel  ,
cols_sel   
)

Sum select rows and columns of a table.

Parameters
rows<2d-vector> A two dimensional vector (r-tuple x c-tuple) containing the table rows.
cols<2d-vector> A two dimensional vector (c-tuple x 1-tuple) containing the table columns.
rows_sel<1d-vector> A vector n-tuple of row identifier to select.
cols_sel<1d-vector> A vector n-tuple of column identifier to select.
Returns
<1d-vector> The sum of the selected rows and columns of the table.