Laser Cutter GCODE from Traditional Slicer

What’s up y’all.  As you hopefully know from my recent post I recently added an adjustable Z axis laser holder to my laser engrave making it a (weak) laser cutter.  I needed a way to turn my laser on and off at the times that I needed it to, but slicers like Cura and Simplify3D are meant for 3D printers not laser engravers like what I’m doing.  (I’m a weirdo, I know, nothing new…)  As I was looking at regular ol’ GCODE I noticed that (at least with S3D) you can have different speeds for printing speed than movement speed.  So I thought how could I find each instance of F200 (cutting speed), jump to the front of that line add M106 (to turn Laser on Full Blast) and then add a line break.  I would need to do this with all of the speeds for print (shells and infill) and then also with a (different) movement speed, F2000 for example.  I spent about 5 minutes researching how I could do this with python and thought it would be too cumbersome.  I’ve used VBA at work (long story) and I knew I could do some stuff with it.  I experimented with it a little bit by recording macros and researching online and I came up with a way do what I needed in the span of a couple of hours, you may think that’s slow and I’d stick my tongue out at you in response…  I’m an electrical engineer and wannabe programmer, not a real one.  Haha.

Here’s the code I used.  If you open the VBA editor in Word, you can copy and paste this code in as a new macro

~~~~~

Sub AdjustGCode()

 

Dim OnMove As String

Dim OffMove As String

Dim OnCount As Integer

Dim OffCount As Integer

 

OnMove = “F200”

If OnMove > “” Then

‘ Set the counter to zero for each loop

OnCount = 0

Application.ScreenUpdating = False

With Selection

.HomeKey Unit:=wdStory

With .Find

.ClearFormatting

.Text = OnMove

‘ Loop until Word can no longer

‘ find the search string and

‘ count each instance

Do While .Execute

OnCount = OnCount + 1

Selection.HomeKey Unit:=wdLine ‘ home button,

Selection.TypeText Text:=”M106 ”

Selection.TypeParagraph

Selection.MoveDown Unit:=wdLine, Count:=2

Selection.MoveRight

Loop

End With

‘ show the number of occurences

‘ MmsgBox sResponse & ” appears ” & iCount & ” times”

End With

Application.ScreenUpdating = True

End If

OffMove = “F2000”

If OnMove > “” Then

‘ Set the counter to zero for each loop

OffCount = 0

Application.ScreenUpdating = False

With Selection

.HomeKey Unit:=wdStory

With .Find

.ClearFormatting

.Text = OffMove

‘ Loop until Word can no longer

‘ find the search string and

‘ count each instance

Do While .Execute

OffCount = OffCount + 1

Selection.HomeKey Unit:=wdLine ‘ home button,

Selection.TypeText Text:=”M107 ”

Selection.TypeParagraph

Selection.MoveDown Unit:=wdLine, Count:=2

Selection.MoveRight

Loop

End With

‘ show the number of occurences

‘ MsgBox sResponse & ” appears ” & iCount & ” times”

End With

Application.ScreenUpdating = True

MsgBox OnMove + ” appears ” & OnCount & ” times ” & OffMove + ” appears ” & OffCount & ” times”

End If

End Sub

~~~~

I saved it in the Normal.dotm modules so it would be available each time I forced open a GCODE file in Word.  Here’s a screenshot so you can see what I mean:

Here’s a downloadable copy (in a TXT file) in case the formatting gets wonky: VBA GCODE Adjust for Laser Cutter

It basically does exactly what I intuitively thought, it finds the F200, presses the home button to move to the front of the line, types the code I need for the laser, adds a line break and moves down two lines (to bypass that iteration of F200).  It’s a very brute force way of doing what I needed and it’s pretty slow to work, but it’s worked every time so far.  A thing to keep in mind is that the speed you’re adjusting for needs to be different than other reference-able speeds.  In example, I tried a cutting speed of 1000 mm/min but 1000 mm/min was also the same speed as the z axis so each time the gcode adjusted the Z speed I got a flash of the laser.  I changed the Z speed to 1001 mm/min to avoid accidentally doing this again later on.

My only experience with GCODE has been with the laser engravers and 3d printing.  So there may be an obvious way to do what I finagled a way to do (like CAM software I suspect), but I got this to work in a short period of time, and I didn’t have to change any of the rest of my controller nor firmware.  That was a big plus to me.

Leave a Reply