winder / Universal-G-Code-Sender

A cross-platform G-Code sender for GRBL, Smoothieware, TinyG and G2core.
http://winder.github.io/ugs_website/
GNU General Public License v3.0
1.86k stars 757 forks source link

Enhancement: Advanced macros #2404

Closed andrewmurraydavid closed 6 months ago

andrewmurraydavid commented 6 months ago

I updated the macros to support multi-line macros, variables and basic math operations (+, -, /, *). With this in place, more advanced feature could be added, like basic functions (ROUND, FIX, etc.) in the future.

Should be backwards compatible with existing macros (single-line macros).

I need this for more advanced macros including probing, slot/hole drilling for fixtures (things that need to be repeatable exactly the same without copy-pasting code).

⚠️ MAKE A BACKUP OF YOUR MACROS BEFORE TESTING THIS LOCALLY, I'm still working on a fix for this.

andrewmurraydavid commented 6 months ago

@winder @breiler Java isn't the language I have experience most in, so let me know what needs fixing or improving!

breiler commented 6 months ago

Thanks for the PR, I am currently travelling so I can't review this at the moment.

breiler commented 6 months ago

Could you split the PR into two - one for the multiline support and then one for the scripting?

I have been keeping an eye on @asmr-hex branch for adding scripting to UGS which I think is the way forward. The implementation is based on the javascript engine: https://github.com/winder/Universal-G-Code-Sender/compare/master...asmr-hex:Universal-G-Code-Sender:expression-support

We already us it for some simple scripting in the DRO here: https://github.com/winder/Universal-G-Code-Sender/blob/master/ugs-core/src/com/willwinder/universalgcodesender/model/GUIBackend.java#L725

Your method could be a start, but will probably be replaced with the JavaScript engine later on.

You are using [] as a placeholder for scripts, which conforms with cncjs. But we will probably not be 100% compatible if we go the JavaScript route. @asmr-hex is using {} which I also like as it resembles the javascript scopes. Both have its benefits, so if anyone have any input, please share.

andrewmurraydavid commented 6 months ago

Thanks for your initial review, @breiler !

Replacing with the JS engine is no problem, I wasn't aware that there's already work on using expressions. Happy to replace [] with {} for future-proofing. I used LinuxCNC and it uses [] for expressions, but other interpreters use {}, so it's more of a "business" decision.

I'll focus this PR on multiline and create a new PR for advanced macros, if that works for you, or I can close this and create separate PRs.