That’s bizarre. Could it be there is some interaction with my use of beginRecord
and endRecord
??? Because even if I do multiple pushMatrix()
operations before, it still complains with the same failure on the final popMatrix()
The full code below.
add_library('svg')
# xml.etree module
from xml.etree import ElementTree
import math
import utils
import life
import random as ran
DPMM = 96/25.4 # dots per mm
PAPER_WIDTH_PX = int(210 * DPMM) #A4
PAPER_HEIGHT_PX = int(297 * DPMM) #A4
BORDER_MARGIN_PX = 15 * DPMM
TEXT_BLOCK_HEIGHT_PX = 30 * DPMM
TEXT_BLOCK_WIDTH_PX = PAPER_WIDTH_PX - 2*BORDER_MARGIN_PX
DISH_BLOCK_HEIGHT_PX = PAPER_HEIGHT_PX - 2*BORDER_MARGIN_PX - TEXT_BLOCK_HEIGHT_PX
DISH_BLOCK_WIDTH_PX = PAPER_WIDTH_PX - 2*BORDER_MARGIN_PX
ROWS=3
COLS=2
CELL_HEIGHT = DISH_BLOCK_HEIGHT_PX / ROWS
CELL_WIDTH = DISH_BLOCK_WIDTH_PX / COLS
PETRI_DIAMETER = 0.85*min(CELL_WIDTH, CELL_HEIGHT)
LOWER_CIRCLE_MARGIN_MM = 10#*DPMM
TEXT_SIZE_PX = 0.8*LOWER_CIRCLE_MARGIN_MM * DPMM
print("CELL_HEIGHT {0} CELL_WIDTH {1}".format(CELL_HEIGHT, CELL_WIDTH))
OUTPUT_FN = '.\\graphics\\combined.svg'
def settings():
size(PAPER_WIDTH_PX, PAPER_HEIGHT_PX)
def setup():
noLoop()
svgs = [ '.\\graphics\\99_dishes.svg', ]
#initial alignment for Dish Block
translate(0,0)
pushMatrix() #store the default reference frame
#even pushing multiple uncessary times results in same failure.
#translate(0,0)
#pushMatrix()
translate(BORDER_MARGIN_PX, BORDER_MARGIN_PX)
beginRecord(SVG, svgs[0])
noFill()
for col in range(COLS):
for row in range(ROWS):
pushMatrix() #store the default reference frame
#translate(CELL_WIDTH*col, CELL_HEIGHT*row)
#circle(0.5*CELL_WIDTH,CELL_HEIGHT-0.5*PETRI_DIAMETER-LOWER_CIRCLE_MARGIN_MM*DPMM,PETRI_DIAMETER)
translate(CELL_WIDTH*col+0.5*CELL_WIDTH, CELL_HEIGHT*row+(0.5*(CELL_HEIGHT-LOWER_CIRCLE_MARGIN_MM*DPMM)))
circle(0,0,PETRI_DIAMETER)
popMatrix() #lose this shifted reference frame
endRecord()
#speciesFactory = life.GermSpeciesFactory(PETRI_DIAMETER)
speciesFactory = life.BezierGermSpeciesFactory(PETRI_DIAMETER)
names = []
for col in range(COLS):
for row in range(ROWS):
#factory = ran.choice([life.BezierGermSpeciesFactory(PETRI_DIAMETER),life.GermSpeciesFactory(PETRI_DIAMETER)])
factory = life.BezierGermSpeciesFactory(PETRI_DIAMETER)
singleSpecies = factory.new_species()
print(singleSpecies)
names.append(singleSpecies.name)
bugs = []
svgs.append(".\\graphics\\{0}_cell.svg".format((col,row)))
beginRecord(SVG, svgs[-1])
pushMatrix() #store the default reference frame
#xtrans, ytrans = (CELL_WIDTH*col+0.5*CELL_WIDTH, CELL_HEIGHT*row+0.5*CELL_HEIGHT)
#print("translating to {0}, {1}".format(xtrans, ytrans))
#translate(xtrans, ytrans)
translate(CELL_WIDTH*col+0.5*CELL_WIDTH, CELL_HEIGHT*row+(0.5*(CELL_HEIGHT-LOWER_CIRCLE_MARGIN_MM*DPMM)))
count = 0
for _ in range(ran.randint(0,20)):
#print("col {0} row {1} bug {2}".format(col, row, _))
bugs.append(singleSpecies.germ())
bugs[-1].plot(show_control=False)
count+=1
print("placed {0} bugs in petri {1}".format(count,(col,row)))
#choose specie and quantity
# get list of n-germs, name from a new species
popMatrix() #lose this shifted reference frame
endRecord()
FONT_SIZE=10
svgs.append(".\\graphics\\88_text.svg")
beginRecord(SVG, svgs[-1])
for col in range(COLS):
for row in range(ROWS):
pushMatrix() #store the default reference frame
translate(CELL_WIDTH*col, CELL_HEIGHT*row)
fill(0)
name_field = names.pop(0)
text(name_field,
0.5*CELL_WIDTH,
CELL_HEIGHT-(0.5*LOWER_CIRCLE_MARGIN_MM*DPMM)-0.5*FONT_SIZE,
CELL_WIDTH,
LOWER_CIRCLE_MARGIN_MM*DPMM
)
popMatrix() #lose this shifted reference frame
endRecord()
svgs.append(".\\graphics\\77_title.svg")
beginRecord(SVG, svgs[-1])
#translate to TEXT BLOCK
print("dies here...")
popMatrix() #goes back to original(0,0) ###FAILS HERE
translate(BORDER_MARGIN_PX, BORDER_MARGIN_PX+TEXT_BLOCK_HEIGHT_PX)
rect(0,0,TEXT_BLOCK_WIDTH_PX, TEXT_BLOCK_HEIGHT_PX)
endRecord()
# create an empty combined.svg file
beginRecord(SVG, OUTPUT_FN); endRecord()
ElementTree.register_namespace('', 'http://www.w3.org/2000/svg')
tree = ElementTree.parse(OUTPUT_FN)
combined = tree.getroot()
# add the (circles/squares) svgs to the combined.svg file
for svg in svgs:
file = ElementTree.parse(svg).getroot()
group = ElementTree.SubElement(combined, 'g')
group.set('id', svg)
group.set('inkscape:groupmode', 'layer')
group.set('inkscape:label', svg)
for child in file.getchildren():
group.append(child)
# save the changes to the combined.svg file
tree.write(OUTPUT_FN)
if True:
with open(OUTPUT_FN) as f:
s = f.read()
with open(OUTPUT_FN, "w") as f:
old_string = "text style=\"stroke:none;\""
new_string = "text style='stroke:none;text-anchor:middle;dominant-baseline:center'"
s = s.replace(old_string, new_string)
f.write(s)
print("Done!")
def draw():
pass