diff --git a/extensions/th2_output.py b/extensions/th2_output.py index cf25d0b..45f0804 100755 --- a/extensions/th2_output.py +++ b/extensions/th2_output.py @@ -74,6 +74,16 @@ def fstr_trim_zeros(s: str) -> str: return "0.0" if s == "-0.0" else s +def format_options_leading_space(options): + """ + Like format_options() but with a leading space if non-empty. + """ + formatted = format_options(options) + if formatted: + return " " + formatted + return "" + + class Th2Line: def __init__(self, type = 'wall'): self.type = type @@ -92,7 +102,8 @@ def close(self): self._last.split()[-2:] != self.points[0].split()): self.points.append(self.points[0]) def output(self): - print_utf8("line %s %s" % (self.type, format_options(self.options))) + formatted_options = format_options_leading_space(self.options) + print_utf8(f"line {self.type}{formatted_options}") print(" " + "\n ".join(self.points)) print("endline\n") @@ -126,7 +137,8 @@ def output(self, prefix): line.output() # output area - print_utf8("area %s %s" % (self.type, format_options(self.options))) + formatted_options = format_options_leading_space(self.options) + print_utf8(f"area {self.type}{formatted_options}") for lineid in ids: print_utf8(" " + lineid) print("endarea\n") @@ -172,7 +184,7 @@ def print_scrap_begin(self, id, test, options = {}): def print_scrap_end(self, test): if test: - print("endscrap\n") + print("endscrap\n\n") def output(self): root = self.document.getroot() @@ -241,6 +253,8 @@ def output(self): print('##XTHERION## xth_me_image_insert {%s 1 1.0} {%s %s} "%s" 0 {}' % \ (fstr2(paramsTrans[0]), fstr2(paramsTrans[1]), XVIroot, href)) + print('\n') + self.print_scrap_begin('scrap1', not self.options.lay2scr) if self.options.layers == 'current': @@ -514,7 +528,8 @@ def output_point(self, node): options['orientation'] = orient # output in therion format - print_utf8("point %s %s %s %s" % (fstr(params[0]), fstr(params[1]), type, format_options(options))) + formatted_options = format_options_leading_space(options) + print_utf8("point %s %s %s%s\n" % (fstr(params[0]), fstr(params[1]), type, formatted_options)) def output_area(self, node): mat = self.i2d_affine(node) diff --git a/extensions/th2ex.py b/extensions/th2ex.py index b259762..ff3c595 100644 --- a/extensions/th2ex.py +++ b/extensions/th2ex.py @@ -294,6 +294,16 @@ def parse_options_new(a): parse_options = parse_options_new + +def key_options_item(item: tuple) -> tuple: + """ + Sort key for options. Alphabetic with some exceptions. + """ + if item[0] == 'id': + return '0' + item[0], item[1] + return item + + # TODO this fails for -text "[foo bar]" (will be -text [foo bar], no quotes) def format_options(options): ''' @@ -334,7 +344,7 @@ def format_option(key, value): return ret ret = [] - for key,value in sorted(options.items()): + for key,value in sorted(options.items(), key=key_options_item): if key in repeatable_options and isinstance(value, list): for v in value: ret.append(format_option(key, v))