[OpenSCAD] Sharing: Dump echo output to a file

runsun runsun at gmail.com
Fri May 1 12:58:01 EDT 2015


Occasionally there might be a need to dump the echo output to a file. For
example, "automatically generate BOMs and debug measurements and information
to files." as described by carlosgs  here
<https://github.com/openscad/openscad/pull/845>   [1], or in the application
of my doctest lib (  here <https://github.com/runsun/openscad_doctest>   ). 

Unfortunately OpenScad can't do that, and there seems to be no plan
(according to [1]) for that feature. 

But there are ways to get around it:

(1) You can dump echo output to a file under command line (Linux):

>>> openscad <scad_file> -o dump.stl -D "<commands_to_execute>" 2>
>>> <output_file> 

For example, one of my uses looks like this:

>>> time openscad mylib.scad -o dump.stl -D "FORHTML=true;runtest()" 2>
>>> mylib_api.htm

(2) The above approach works, but every echo( ) inserts "ECHO: " to the
beginning of output and a trailing " in the end. It is inconvenient to have
multiple execution-unrelated "ECHO: " in an output file, especially if the
main purpose of dumping to a file is for further automatic processing. 

*The easiest way for OpenSCAD to solve this is to have an option to suppress
the display of "ECHO: "*. 

But, before that being considered a feature to add, we can use the following
simple python script. Note that: (1) It is coded on Linux and hasn't been
tested on Win or Mac; (2) It assumes that /all echo output are strings/. It
can be used this way:

>>> time python runscad.py mylib.scad "FORHTML=true;runtest()" mylib_api.htm



> ############################## ## runscad.py## Run an openscad file and
> dump the echo output to a file## >>> python runscad.py <scad_file>
> "<commands_to_execute>" <output_file>## Like:## >>> python
> runscad.py mylib.scad "FORHTML=true;runtest()" mylib_api.htm## by Runsun
> Pan (2015.5.1)############################## 

> import sys, os, reif len(sys.argv)<4:   print 

> '''# runscad.py## Not enough arguments. Required 3 arguments:##    python
> runscad.py <scad_file> "<command(s)>" <output_file>## For
> example:##    python runscad.py myprog.scad "FORHTML=true;MODE=10;run()"
> tests.html'''

> else:   cmd = 

> 'openscad %s -o dump.stl -D "%s" 2> %s'

> % tuple(sys.argv[1:])   os.system( cmd )   output_file= sys.argv[3]  
> lines = open( output_file, 'r').readlines()   

> # Remove the starting 'ECHO: "' and trailing " on each line

>    ptn = re.compile(r'ECHO: \"|"$')   for i,line in enumerate(lines):       
> lines[i] = re.sub( ptn,'', line)   

> # Save to file. Exclude the last line, which reads:   # "Current top level
> object is empty."

>    open( output_file, 'w').write( '\n'.join( lines[:-1] )
> )##############################

 



-----

$  Runsun Pan, PhD 

$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  ) 

$ -- hash parameter model: here , here 

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly 




--
View this message in context: http://forum.openscad.org/Sharing-Dump-echo-output-to-a-file-tp12529.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150501/3632eda2/attachment-0002.html>


More information about the Discuss mailing list