plottertools / vpype-gcode

gcode extension for vpype
MIT License
35 stars 7 forks source link

Permit vpype-gcode expression substitution into patterns. #29

Open tatarize opened 1 year ago

tatarize commented 1 year ago

If, we, for example needed gcode that set some value 20mm over per layer_id currently there is no set of methods that would allow this.

So the template would have something like %layer_id*20*mm% and that would be substituted by the correct value.

Breaking changes

Using expression substitution from vpype proper requires that any direct use of % must be %% to avoid parsing confusion.

tatarize commented 1 year ago

*Also, it's not clear if it could be integrated the other way since you clearly have {} replacements for those values in places too if that could be done rather than the format thing it might be more useful.

tyehle commented 1 year ago

Could also eval an fstring instead of using format_map to allow full python expressions within the curly braces.

>>> template = "{layer_id*20}mm"
>>> vars = {"layer_id": 2}
>>> eval("f'{}'".format(template), vars)
'40mm'

This would mean that you couldn't put a single quote in the template string, but that could be mitigated somewhat by changing to f'''stuff'''. In that case only a ''' would cause problems in the template string

tyehle commented 1 year ago

This would also let you do awesome stuff like

layer_join = "{'G01 X0 Y0\nM0 Change color' if layer_index == 2 else ''}"
tatarize commented 1 year ago

I think @abey79 struck a balance here with the combination of safety and power. layer_join = "{__import__('os').system('rm -rf /a-path-you-really-care-about')}"

I believe the vpype evaluation actually would permit some similar code. I'm just not well versed enough on it. He spent a while coming up with the formatting and my hopes for vpype-gcode is integration though that's seeming a bit far fetched, but I still try to adhere as best as I can to the regular API of vpype to facilitate that.

abey79 commented 1 year ago

It randomly occurred to me that using the jinja templating engine might be a good avenue as well. In retrospect, possibly for vpype expression as well. Amongst the advantages I can think of: