blob: 049c394551f0c9e7346b679f5217c2c78d968a86 [file] [log] [blame]
#!/usr/bin/env python3
import os
import re
import shutil
import subprocess
import sys
import time
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 + ".tmp1.svg\n")
ofile.write("quit -noprompt\n")
output = run_magic(destdir, tcl_path, input_techfile, " XR")
tmp1_svg = os.path.join(destdir, "{}.tmp1.svg".format(basename))
tmp2_svg = os.path.join(destdir, "{}.tmp2.svg".format(basename))
assert os.path.exists(tmp1_svg), tmp1_svg + " doesn't exist!?"
os.unlink(tcl_path)
for i in range(0, 3):
# Remove the background
with open(tmp1_svg) as f:
data = f.read()
data = re.sub('<rect[^>]* style="[^"]*fill-opacity:1;[^"]*"/>', '', data)
with open(tmp2_svg, 'w') as f:
f.write(data)
# Use inkscape to crop
retcode = run_inkscape([
"--verb=FitCanvasToDrawing",
"--verb=FileSave",
"--verb=FileClose",
"--verb=FileQuit",
tmp2_svg])
if retcode == 0:
break
for i in range(0, 3):
# Convert back to plain SVG
retcode = run_inkscape([
"--export-plain-svg=%s" % (tmp2_svg),
"--export-background-opacity=1.0",
tmp2_svg])
if retcode == 0:
break
os.unlink(tmp1_svg)
# Move into the correct location
os.rename(tmp2_svg, output_svg)
print("Created", output_svg)
def run_inkscape(args):
p = subprocess.Popen(["inkscape"] + args)
try:
p.wait(timeout=60)
except subprocess.TimeoutExpired as e:
print("ERROR: Inkscape timed out! Sending SIGTERM")
p.terminate()
try:
p.wait(timeout=60)
except subprocess.TimeoutExpired as e:
print("ERROR: Inkscape timed out! Sending SIGKILL")
p.kill()
p.wait()
return p.returncode
convert_to_svg(*sys.argv[1:])