Browse Source

Clean up helper.py SVG parser. Allow SVG ellipses to be used as circles.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
3d796032c0
1 changed files with 29 additions and 26 deletions
  1. +29
    -26
      helper.py

+ 29
- 26
helper.py View File

@@ -267,18 +267,14 @@ def panel_to_components(tree):

# Get components layer
root = tree.getroot()
groups = root.findall(".//svg:g[@inkscape:label='components']", ns)
group = root.find(".//svg:g[@inkscape:label='components']", ns)
# Illustrator uses `id` for the group name.
if len(groups) < 1:
groups = root.findall(".//svg:g[@id='components']", ns)
if len(groups) < 1:
# Don't test with `not group` since Elements with no subelements are falsy.
if group is None:
group = root.find(".//svg:g[@id='components']", ns)
if group is None:
raise UserException("Could not find \"components\" layer on panel")

# Get circles and rects
components_group = groups[0]
circles = components_group.findall(".//svg:circle", ns)
rects = components_group.findall(".//svg:rect", ns)

components = {}
components['params'] = []
components['inputs'] = []
@@ -286,29 +282,20 @@ def panel_to_components(tree):
components['lights'] = []
components['widgets'] = []

for el in circles + rects:
for el in group:
c = {}

# Get name
name = el.get('{http://www.inkscape.org/namespaces/inkscape}label')
if name is None:
name = el.get('{' + ns['inkscape'] + '}label')
if not name:
name = el.get('id')
if not name:
name = ""
name = str_to_identifier(name).upper()
c['name'] = name

# Get color
fill = el.get('fill')
style = el.get('style')
if fill:
color_match = re.search(r'#(.{6})', fill)
color = color_match.group(1).lower()
elif style:
color_match = re.search(r'fill:\S*#(.{6});', style)
color = color_match.group(1).lower()
else:
raise UserException("Cannot get color of component")

# Get position
if el.tag == "{http://www.w3.org/2000/svg}rect":
if el.tag == '{' + ns['svg'] + '}rect':
x = float(el.get('x'))
y = float(el.get('y'))
width = float(el.get('width'))
@@ -319,11 +306,27 @@ def panel_to_components(tree):
c['height'] = round(height, 3)
c['cx'] = round(x + width / 2, 3)
c['cy'] = round(y + height / 2, 3)
elif el.tag == "{http://www.w3.org/2000/svg}circle":
elif el.tag == '{' + ns['svg'] + '}circle' or el.tag == '{' + ns['svg'] + '}ellipse':
cx = float(el.get('cx'))
cy = float(el.get('cy'))
c['cx'] = round(cx, 3)
c['cy'] = round(cy, 3)
else:
print(f"Element in components layer is not rect, circle, or ellipse: {el}")
continue

# Get color
fill = el.get('fill')
style = el.get('style')
if fill:
color_match = re.search(r'#(.{6})', fill)
color = color_match.group(1).lower()
elif style:
color_match = re.search(r'fill:\S*#(.{6});', style)
color = color_match.group(1).lower()
else:
print(f"Cannot get color of component: {el}")
continue

if color == 'ff0000':
components['params'].append(c)


Loading…
Cancel
Save