ni3dprint Posted June 8, 2020 Posted June 8, 2020 Hi folks, Thank you so much in advance for your help! I've been using AUTOIT for manipulating gcode. So far I've just worked through the excellent help examples and although I'm sure the resulting code is clumsy it has functioned However now I'm trying to improve and advance things and I've stumbled across REGEX.. and I'm a bit stuck. What I would like to be able to do is to 'move'/'transform' the gcode in a file and re-write it to a new file. I only need to move it in one direction(X). At the heart of this I need a script to extract all the X values and then ADD or SUBTRACT an adjustment factor to transform and rewrite the code accordingly. So far using an example script and an example input - Func Test2() Local $iMove = -4 Local $sInput = '"G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925"' Local $sOutput = StringRegExpReplace($sInput, '(?<=[X])\d+.\d+', '\0') Display($sInput, $sOutput) EndFunc ;==>Test2 This identifies the correct values i.e 45.036 and 48.036 but is there a way to dyamically adjust them before they are replaced, by for example a factor of -4 ($iMove above). So far I can't seem to do math on the '\0' value i.e '\0'+ -4 ? Many thanks for your time and expertise!
FrancescoDiMuro Posted June 8, 2020 Posted June 8, 2020 @ni3dprint You could use Execute() to make your calculations, that can be used with strings as well. Give it a try and let us know Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
mikell Posted June 8, 2020 Posted June 8, 2020 Local $iMove = -4 $old = "G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925" $new = Execute("'" & StringRegExpReplace($old, "(?<=X)\d+.\d+", "' & Execute('$0+' & $iMove) & '") & "'") Msgbox(0,"", $old & @crlf & $new) Exit and dmob 2
ni3dprint Posted June 8, 2020 Author Posted June 8, 2020 Thank you both for your help.. Wow it works, although I don't really understand the execute line and all the brackets etc.. I had seen Execute() referred to in some of the scripts but there's almost nothing on it in the Help of Autoit so I'll have to get to know it. Now I just need to build it into the rest of my setup. Best wishes to you both
mikell Posted June 9, 2020 Posted June 9, 2020 22 hours ago, ni3dprint said: although I don't really understand the execute line and all the brackets etc.. Sorry for the lack of explanations You can't use a function (in this case, Execute an operation) directly inside the "replace" part of a SRER So the concept is to build a new string from the subject string by replacing each match by a string including the function, and then apply Execute on this new string Local $iMove = -4 $old = "G1 X45.036 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925" $string = "'" & StringRegExpReplace($old, "(?<=X)\d+.\d+", "' & Execute('$0+' & $iMove) & '") & "'" Msgbox(0,"new string", $string) $new = Execute($string) Msgbox(0,"result", $old & @crlf & $new) Hmm was it clear ?
Moderators Melba23 Posted June 10, 2020 Moderators Posted June 10, 2020 mikell, Quote Hmm was it clear ? Perfectly - and a nice piece of lateral thinking to get there. Bravo! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
ni3dprint Posted September 8, 2020 Author Posted September 8, 2020 Thanks Mikell for your help a while back with this... all pretty mind blowing stuff :) Now I have another slight issue - because the values have 3 or so decimal places when I apply the calculation I'm getting 10+ decimal places in the output. How can I reduce the result to 3 decimal places please? I've tried inserting Round into the script above in several different ways but no joy so far. I wonder is there a way to set a variable for the entire script that keeps the precision of all calculations to 3 decimal places? Thanks! Julian
ni3dprint Posted September 8, 2020 Author Posted September 8, 2020 Hi Folks, I may have solved it myself at least in a clumsy way - it looks like it may just be the old 'floating point arithmetic' issue? Local $new = Execute("'" & StringRegExpReplace($original, "(?<=X)-?\d+\.\d+", "' & Execute('$0' & $or & $xmin) & '") & "'") $Final = Execute("'" & StringRegExpReplace($new, "(?<=Y)-?\d+\.\d+", "' & Execute('$0' & $or & $ymin) & '") & "'") $Finally = Execute("'" & StringRegExpReplace($Final, "(?<=[X,Y])-?\d+\.\d+", "' & Execute(Round('$0',3)) & '") & "'")
mikell Posted September 9, 2020 Posted September 9, 2020 Do you mean something like this ? Local $iMove = -4 $old = "G1 X45.03678 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925" $new = Execute("'" & StringRegExpReplace($old, "(?<=X)\d+.\d+", "' & StringFormat('%.3f', Execute('$0+' & $iMove)) & '") & "'") Msgbox(0,"", $old & @crlf & $new)
ni3dprint Posted September 9, 2020 Author Posted September 9, 2020 Thanks Mikell, thats a bit tidier than mine although it did the job incidentally in the regex above - (?<=X)-?\d+\.\d+ this will pick up any digits after an X and with a decimal, however if I want it to include digits with no decimal can this be done? i.e it will pick up X0.0 but it won't find X0? I did try but gave up and ended up just adding .0 to all single digits
ni3dprint Posted September 9, 2020 Author Posted September 9, 2020 Actually this seems to work - (?<=X)\d.?\d?\d?\d?
mikell Posted September 9, 2020 Posted September 9, 2020 ? Local $iMove = -4 $old = "G1 X45 Y6.934 F7800.000 G1 Z0.600 F7800.000 G1 F900 G1 X48.036 Y1.076 E0.58925" $new = Execute("'" & StringRegExpReplace($old, "(?<=X)\d+(.\d+)?", "' & StringFormat('%.3f', Execute('$0+' & $iMove)) & '") & "'") Msgbox(0,"", $old & @crlf & $new)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now