Skip to content

Instantly share code, notes, and snippets.

@FrankBuss
Created July 21, 2018 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FrankBuss/e3d13eceff5565c41be62e9c48ad1473 to your computer and use it in GitHub Desktop.
Save FrankBuss/e3d13eceff5565c41be62e9c48ad1473 to your computer and use it in GitHub Desktop.
converts all text elements to paths in a SVG file, and extracts coordinates for circles
#!/usr/bin/python3
# In your SVG file you can add circles as placeholders for knobs and ports
# for each circle, add a property description text like this:
#
# name KNOB1
# ofs -4.75 -100.6
#
# This will create a header file entry like this:
#
# #define KNOB1_POS Vec(43.589677, 136.896189)
#
# Include the header file and then you can use as the coordinate for your GUI elements:
#
# addParam(ParamWidget::create<Davies1900hBlackKnob>(KNOB1_POS, module, MyModule::PITCH_PARAM, -3.0, 3.0, 0.0));
#
# When you move the placeholder, just call the script again to create a new header file.
# The placeholders are removed from the final SVG file by the script.
from lxml import etree
import subprocess, shutil, sys
import os
SVG_DPI = 96.0
MM_PER_IN = 25.4
def mm2px(millimeters):
return millimeters * (SVG_DPI / MM_PER_IN)
panel_height = 380
# Set input and output file name
input_file_name = "MyModule-org.svg"
output_file_name = "MyModule.svg"
# create initial output file
shutil.copy("res/" + input_file_name, "res/" + output_file_name)
# Get text and desc elements from the original document
input_file = open("res/" + input_file_name)
tree = etree.parse(input_file)
texts = tree.getroot().xpath("//*[local-name() = $name]", name="text")
descs = tree.getroot().xpath("//*[local-name() = $name]", name="desc")
input_file.close()
# Use Inkscape to convert each text element to a path and create header file
header_file = open("src/" + output_file_name + ".hpp", "w")
command = ["inkscape"]
for t in texts:
command += ["--verb=EditDeselect", "--select=" + t.get("id"), "--verb=ObjectToPath"]
for desc in descs:
lines = desc.text.split("\n")
define = ""
for line in lines:
items = line.split(" ")
name = items[0]
if name == "name":
define = items[1]
if name == "ofs":
ofsx = float(items[1])
ofsy = float(items[2])
if len(define) > 0:
parent = desc.getparent()
cx = float(parent.get("cx"))
cy = float(parent.get("cy"))
header_file.write("#define %s_POS Vec(%f, %f)\n" % (define, mm2px(cx + ofsx), mm2px(cy + ofsy)))
command += ["--verb=EditDeselect", "--select=" + parent.get("id"), "--verb=EditDelete"]
header_file.close()
command += ["--verb=FileSave", "--verb=FileClose", "--verb=FileQuit", "res/" + output_file_name]
subprocess.call(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment