| #!/usr/bin/env python3 |
| |
| import os |
| import re |
| import subprocess |
| import sys |
| |
| from gds_to_lef_gds_mag import _magic_tcl_header, run_magic |
| |
| |
| def convert_to_svg(input_gds, input_techfile): |
| input_gds = os.path.abspath(input_gds) |
| input_techfile = os.path.abspath(input_techfile) |
| |
| destdir, gdsfile = os.path.split(input_gds) |
| |
| basename, ext = os.path.splitext(gdsfile) |
| |
| output_svg = os.path.join(destdir, "{}.svg".format(basename)) |
| assert not os.path.exists(output_svg), output_svg + " already exists!?" |
| |
| tcl_path = os.path.join(destdir, "{}.gds2svg.tcl".format(basename)) |
| with open(tcl_path, 'w') as ofile: |
| _magic_tcl_header(ofile, input_gds) |
| |
| ofile.write("load " + basename + "\n") |
| ofile.write("box 0 0 0 0\n") |
| ofile.write("select top cell\n") |
| ofile.write("expand\n") |
| ofile.write("view\n") |
| ofile.write("select clear\n") |
| ofile.write("box position -1000 -1000\n") |
| ofile.write("plot svg " + basename + ".tmp.svg\n") |
| ofile.write("quit -noprompt\n") |
| |
| output = run_magic(destdir, tcl_path, input_techfile, " XR") |
| tmp_svg = os.path.join(destdir, "{}.tmp.svg".format(basename)) |
| assert os.path.exists(tmp_svg), tmp_svg + " doesn't exist!?" |
| os.unlink(tcl_path) |
| |
| # Remove the background |
| with open(tmp_svg) as f: |
| data = f.read() |
| data = re.sub('<rect[^>]* style="[^"]*fill-opacity:1;[^"]*"/>', '', data) |
| with open(tmp_svg, 'w') as f: |
| f.write(data) |
| |
| # Crop the image using inkscape |
| subprocess.call(["inkscape", "--verb=FitCanvasToDrawing", "--verb=FileSave", "--verb=FileClose", "--verb=FileQuit", tmp_svg]) |
| |
| # Move into the correct location |
| os.rename(tmp_svg, output_svg) |
| print("Created", output_svg) |
| |
| |
| convert_to_svg(*sys.argv[1:]) |