LDTrim

LDTrim is a simple program that condenses or formats LDraw code by trimming extraneous characters like spaces – or inserting them to align values in columns.

LDTrim was developed to help “pretty-print” examples for documentation or discussion, but it could also be used to reduce file size or to standardize the format of many models.

Much of what LDTrim does is already possible with LDraw Design Pad. For example, see “Trim Lines” and “Auto Round Selection” under the “Tools” menu.

Usage

Here is the output of ldtrim -help:

LDTrim 1.1
Usage: ldtrim                         Default:
    [-in FILE]  | [-in FILE FILE...]    stdin (use -ing to glob wildcards)
    [-out FILE] | [-out DIRECTORY]      stdout
    [-precision DIGITS]                 all significant digits preserved
    [-trim zeros|none]                  zeros (trims redundant trailing zeros)
    [-justify left|right|none]          none (alignment of values in field)
    [-invalid omit|keep]                omit (keep retains unrecognized lines)
    [-width CHARACTERS|max]             variable (minimum width of all fields)
    [-1..15 CHARACTERS]                 variable (override width for one field)
    [-h|--h|-help|--help]               display this usage summary

If multiple input files are specified without -out, they are modified in place.
If -out is given with multiple input files, output is saved to that directory.

The -precision option causes values to be rounded down and may append zeros to
reach the specified precision unless -trim zeros is explicitly specified.

Whitespace between fields is condensed to a single space.

The -width option pads values with spaces to reach the specified field width.
If -width max is given, the widest value is used as the width for all fields.
(Sub-part names are ignored for this comparison.) If a field value exists that
is wider than the requested width, that width is used as the field width.

Right justification is used if -width or -1..15 is specified without -justify.
If none of -width, -1..15, or -justify is specified, fields are unaligned.

If environment variable GATEWAY_INTERFACE is defined, LDTrim is a CGI script.

Example

Suppose you have saved this LDraw code as sample.ldr:

1 0 20.000000 0.000000 20.000000 0.000000 -1.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 4085c.dat 1 1 28.000000 7.000000 27.000000 0.000000 -1.000000 0.000000 0.500000 0.000000 -0.866025 0.866025 0.000000 0.500000 3023.dat 1 25 36.000000 -5.000000 11.000000 0.000000 -1.000000 0.000000 0.707106 0.000000 -0.707106 0.707106 0.000000 0.707106 2412b.dat 

Running ldtrim -in sample.ldr -out trimmed.ldr will save the following as trimmed.ldr:

1 0 20 0 20 0 -1 0 1 0 0 0 0 1 4085c.dat 1 1 28 7 27 0 -1 0 0.5 0 -0.866025 0.866025 0 0.5 3023.dat 1 25 36 -5 11 0 -1 0 0.707106 0 -0.707106 0.707106 0 0.707106 2412b.dat 

The default behavior is to condense the code as much as possible without modifying the model. Alternatively, you can use LDTrim to reformat code. For instance, ldtrim -precision 2 -width 5 -1 1 -2 3 -in trimmed.ldr -out aligned.ldr outputs the following as aligned.ldr:

1 0 20.00 0.00 20.00 0.00 -1.00 0.00 1.00 0.00 0.00 0.00 0.00 1.00 4085c.dat 1 1 28.00 7.00 27.00 0.00 -1.00 0.00 0.50 0.00 -0.87 0.87 0.00 0.50 3023.dat 1 25 36.00 -5.00 11.00 0.00 -1.00 0.00 0.71 0.00 -0.71 0.71 0.00 0.71 2412b.dat 

Giving a minimum field -width argument implies -justify right. Similarly, decimal -precision implies -trim none, leaving trailing zeros intact. The -1 and -2 options override the minimum width for the first two columns.

Batch Mode

If LDTrim is given multiple input files, it will process them all, saving the results to files of the same name in the directory given by -out or overwriting the input files if no output folder is given.

ldtrim -in $LDRAWDIR/parts/*.dat -out /trimmed/parts/ 

Note that with a single -in file, -out is interpreted as a single output file, but with multiple -in files, -out is interpreted as a directory to contain the results.

Web Interface

LDTrim has a rudimentary web interface built in. If the GATEWAY_INTERFACE environment variable is detected, LDTrim will behave like a CGI script. Specifically, it will print an HTML form with controls that roughly correspond to its command-line options. LDraw code posted via this form (to itself, as the action ldtrim) is processed according to the selected options. The results are returned in the same form.

Download

LDTrim is also available as a platform-independent Starkit (50 KB) which requires an 8.5 Tclkit.

The current version of LDTrim is 1.1; it fixes a bug that caused malformed line endings to be output on Windows.

Posted on Saturday, May 10th, 2008. Tags: , , .