LDTrimanoved.net logo

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.

You can print a brief overview of LDTrim's command-line syntax, including options not described here, with ldtrim -help.

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.

For the time being, an instance of this interface is available at anoved.net/cgi-bin/ldtrim.

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 10, 2008 at 12:21 PM

0 Comment

Post a Comment