-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathldraw2scad
executable file
·88 lines (80 loc) · 3.52 KB
/
ldraw2scad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
""" Translate LDraw library or file to OpenSCAD library or file. """
import os
import argparse
from ldraw_to_scad import LDrawConverter
def translate_dir(converter, src, dest, self_contained=False):
""" translate a whole model directory """
types = ['.mpd', '.ldr', '.dat']
lst = {}
for fdir, _, files in os.walk(src, followlinks=True):
rel = os.path.relpath(fdir, src)
for file in sorted(files):
base, ext = os.path.splitext(file)
key = os.path.join(rel, base)
if ext in types:
# We will now override the old extension with the new
# one. Therefore let's warn the user that the one with
# the old extension will get skipped.
if key in lst:
print(f'Skipping {os.path.join(src, key+lst[key])}')
lst[key] = ext
for key, value in lst.items():
print(f'Translating {os.path.join(src, key+value)}'
f' to {os.path.join(dest, key+".scad")}...')
converter.convert_file(os.path.join(src, key+value),
os.path.join(dest, key+".scad"),
self_contained)
def main():
""" Main function """
parser = argparse.ArgumentParser(
description='Convert an LDraw part to OpenSCAD')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-t', '--translib', action='store_true',
help='translate the library')
parser.add_argument(
'-s', '--selfcontained', action='store_true',
help='create self-contained files')
parser.add_argument(
'-u', '--uncommented', action='store_true',
help='create uncommented files')
group.add_argument('ldraw_file', nargs='?', metavar='FILENAME',
help='source file to translate')
parser.add_argument('output_file', nargs='?', metavar='OUTPUT_FILENAME',
help='name of the translated file')
parser.add_argument(
'-l', '--lib', default=os.path.join('lib', 'ldraw'), metavar='LIB_DIR',
help='location of the LDraw parts library')
parser.add_argument(
'-o', '--openscadlibs', default='.', metavar='OPENSCAD_LIB_DIR',
help='location of the OpenSCAD libraries')
parser.add_argument(
'-n', '--libname', default='LDraw', metavar='LIB_NAME',
help='name of the OpenSCAD library')
parser.add_argument(
'--line', default=0.2, type=float, metavar='LINE_WIDTH',
help='width of lines, 0 for no lines')
args = parser.parse_args()
converter = LDrawConverter(libdir=args.lib)
converter.set('scadlibs', args.openscadlibs)
converter.set('scadlibname', args.libname)
converter.set('line', args.line)
converter.set('commented', not args.uncommented)
if args.translib:
print("Translating library...")
converter.convert_lib(args.selfcontained)
else:
if os.path.isdir(args.ldraw_file):
translate_dir(
converter, args.ldraw_file,
args.output_file if args.output_file else args.ldraw_file,
args.selfcontained)
else:
scadfile = args.output_file if args.output_file else \
os.path.splitext(args.ldraw_file)[0] + '.scad'
print(f"Translating {args.ldraw_file} to {scadfile}...")
converter.convert_file(args.ldraw_file, scadfile,
args.selfcontained)
if __name__ == '__main__':
main()