"""Grouper: plot gr_section.py's JSON: one panel per cut, objects in colours, a 10 cm grid. python build/gr_section_plot.py [x0 x1 z0 z1]""" import sys, json from PIL import Image, ImageDraw d = json.load(open(sys.argv[1])) box = [float(v) for v in sys.argv[3:7]] if len(sys.argv) >= 7 else None COL = {"SM_Grouper_Hull": (230, 230, 230), "SM_Grouper_Glass": (90, 200, 255), "SM_Grouper_Cabin": (255, 140, 60), "SM_Grouper_Hatch": (120, 255, 120), "SM_Grouper_Door_L": (255, 90, 200), "SM_Grouper_Door_R": (255, 90, 200), "SM_Grouper_Rotor": (255, 255, 90), "SM_Grouper_Antenna": (255, 255, 90)} panels = list(d.items()) W, H = 900, 900 img = Image.new("RGB", (W * len(panels), H), (18, 20, 26)) for n, (name, objs) in enumerate(panels): pim = Image.new("RGB", (W, H), (18, 20, 26)) dr = ImageDraw.Draw(pim) xs = [v for segs in objs.values() for s in segs for v in (s[0], s[2])] zs = [v for segs in objs.values() for s in segs for v in (s[1], s[3])] if not xs: continue x0, x1, z0, z1 = box if box else (min(xs), max(xs), min(zs), max(zs)) sc = min((W - 40) / (x1 - x0), (H - 60) / (z1 - z0)) ox = 20 tx = lambda x: ox + (x - x0) * sc tz = lambda z: H - 20 - (z - z0) * sc g = round(x0, 1) while g <= x1: dr.line([(tx(g), 20), (tx(g), H - 20)], fill=(40, 44, 54)) g += 0.1 g = round(z0, 1) while g <= z1: dr.line([(ox, tz(g)), (ox + W - 40, tz(g))], fill=(40, 44, 54)) g += 0.1 for o, segs in objs.items(): c = COL.get(o, (160, 160, 160)) for s in segs: dr.line([(tx(s[0]), tz(s[1])), (tx(s[2]), tz(s[3]))], fill=c, width=1) dr.text((ox, 4), name + " (grid 10 cm)", fill=(255, 220, 90)) img.paste(pim, (n * W, 0)) img.save(sys.argv[2]) print("PLOT OK")