mirror of https://github.com/vitalif/openscad
Allow other files than .scad as input
parent
4ef994ac3e
commit
48e1d6a5a9
|
@ -3,13 +3,14 @@
|
|||
# Export-import test
|
||||
#
|
||||
#
|
||||
# Usage: <script> file.scad --openscad=<executable-path> --format=<format> file.png
|
||||
# Usage: <script> <inputfile> --openscad=<executable-path> --format=<format> file.png
|
||||
#
|
||||
#
|
||||
# step 1. input a .scad file, output an export format (csg, stl, off, dxf, svg, amf)
|
||||
# step 2. For non-csg exports, create a temporary new .scad file with 'import()' of exported file
|
||||
# step 3. render temporary new .scad file to .png file
|
||||
# step 4. (done in CTest) - compare the generated .png file to expected output
|
||||
# step 1. If the input file is _not_ and .scad file, create a temporary .scad file importing the input file.
|
||||
# step 2. process the .scad file, output an export format (csg, stl, off, dxf, svg, amf)
|
||||
# step 3. If the export format is _not_ .csg, create a temporary new .scad file importing the exported file
|
||||
# step 4. render the .csg or .scad file to the given .png file
|
||||
# step 5. (done in CTest) - compare the generated .png file to expected output
|
||||
# of the original .scad file. they should be the same!
|
||||
#
|
||||
# This script should return 0 on success, not-0 on error.
|
||||
|
@ -24,7 +25,21 @@ def failquit(*args):
|
|||
print('exiting test_3d_export.py with failure')
|
||||
sys.exit(1)
|
||||
|
||||
def createImport(inputfile, scadfile):
|
||||
print ('createImport: ' + inputfile + " " + scadfile)
|
||||
outputdir = os.path.dirname(scadfile)
|
||||
try:
|
||||
if not os.path.exists(outputdir): os.mkdir(outputdir)
|
||||
f = open(scadfile,'w')
|
||||
f.write('import("'+inputfile+'");'+os.linesep)
|
||||
f.close()
|
||||
except:
|
||||
failquit('failure while opening/writing ' + scadfile)
|
||||
|
||||
|
||||
#
|
||||
# Parse arguments
|
||||
#
|
||||
formats = ['csg', 'stl','off', 'amf', 'dxf', 'svg']
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--openscad', required=True, help='Specify OpenSCAD executable')
|
||||
|
@ -32,26 +47,37 @@ parser.add_argument('--format', required=True, choices=[item for sublist in [(f,
|
|||
args,remaining_args = parser.parse_known_args()
|
||||
|
||||
args.format = args.format.lower()
|
||||
scadfile = remaining_args[0]
|
||||
inputfile = remaining_args[0] # Can be .scad file or a file to be imported
|
||||
pngfile = remaining_args[-1]
|
||||
remaining_args = remaining_args[1:-1]
|
||||
remaining_args = remaining_args[1:-1] # Passed on to the OpenSCAD executable
|
||||
|
||||
if not os.path.exists(scadfile):
|
||||
failquit('cant find .scad file named: ' + scadfile)
|
||||
if not os.path.exists(inputfile):
|
||||
failquit('cant find input file named: ' + inputfile)
|
||||
if not os.path.exists(args.openscad):
|
||||
failquit('cant find openscad executable named: ' + args.openscad)
|
||||
|
||||
outputdir = os.path.dirname(pngfile)
|
||||
scadbasename = os.path.basename(scadfile)
|
||||
inputpath, inputfilename = os.path.split(inputfile)
|
||||
inputbasename,inputsuffix = os.path.splitext(inputfilename)
|
||||
|
||||
if args.format == 'csg':
|
||||
threedfilename = re.sub(r"\.scad$", '.' + args.format, scadfile)
|
||||
# Must export to same folder for include/use/import to work
|
||||
exportfile = inputfile + '.' + args.format
|
||||
else:
|
||||
threedfilename = os.path.join(outputdir, scadbasename + '.' + args.format)
|
||||
exportfile = os.path.join(outputdir, inputfilename)
|
||||
if args.format != inputsuffix[1:]: exportfile += '.' + args.format
|
||||
|
||||
# If we're not reading an .scad or .csg file, we need to import it.
|
||||
if inputsuffix != '.scad' and inputsuffix != '.csg':
|
||||
# FIXME: Remove tempfile if created
|
||||
tempfile = os.path.join(outputdir, inputfilename + '.scad')
|
||||
createImport(inputfile, tempfile)
|
||||
inputfile = tempfile
|
||||
|
||||
#
|
||||
# First run: Just export the given filetype
|
||||
#
|
||||
export_cmd = [args.openscad, scadfile, '-o', threedfilename] + remaining_args
|
||||
export_cmd = [args.openscad, inputfile, '-o', exportfile] + remaining_args
|
||||
print('Running OpenSCAD #1:')
|
||||
print(' '.join(export_cmd))
|
||||
result = subprocess.call(export_cmd)
|
||||
|
@ -62,26 +88,21 @@ if result != 0:
|
|||
#
|
||||
# Second run: Import the exported file and render as png
|
||||
#
|
||||
newscadfilename = threedfilename
|
||||
newscadfile = exportfile
|
||||
# If we didn't export a .csg file, we need to import it
|
||||
if args.format != 'csg':
|
||||
newscadfilename += '.scad'
|
||||
try:
|
||||
if not os.path.exists(outputdir): os.mkdir(outputdir)
|
||||
f = open(newscadfilename,'w')
|
||||
f.write('import("'+threedfilename+'");'+os.linesep)
|
||||
f.close()
|
||||
except:
|
||||
failquit('failure while opening/writing '+newscadfilename)
|
||||
newscadfile += '.scad'
|
||||
createImport(exportfile, newscadfile)
|
||||
|
||||
create_png_cmd = [args.openscad, newscadfilename, '--render', '-o', pngfile] + remaining_args
|
||||
create_png_cmd = [args.openscad, newscadfile, '--render', '-o', pngfile] + remaining_args
|
||||
print('Running OpenSCAD #2:')
|
||||
print(' '.join(create_png_cmd))
|
||||
result = subprocess.call(create_png_cmd)
|
||||
if result != 0:
|
||||
failquit('OpenSCAD #2 failed with return code ' + str(result))
|
||||
|
||||
try: os.remove(threedfilename)
|
||||
except: failquit('failure at os.remove('+threedfilename+')')
|
||||
if newscadfilename != threedfilename:
|
||||
try: os.remove(newscadfilename)
|
||||
except: failquit('failure at os.remove('+newscadfilename+')')
|
||||
try: os.remove(exportfile)
|
||||
except: failquit('failure at os.remove('+exportfile+')')
|
||||
if newscadfile != exportfile:
|
||||
try: os.remove(newscadfile)
|
||||
except: failquit('failure at os.remove('+newscadfile+')')
|
||||
|
|
Loading…
Reference in New Issue