Jump to content

Simple? Editing a .txt file with gcode, making first and last line the same


Recommended Posts

Hi All,

I'm not too code savvy but I know this is a simple one! Here's my problem, I have a folder with text files containing gcode. they are appended .nc, .ngc, and .gcode randomly. I can rename them all .ngc which is my preferred file type. In these files there's lines of gcode, carrying commands for a CNC. What I need to do is remove some comments from the beginning and format the text a certain way. I'll show an example of the original file and what I need it to look like. The files can be 20 lines or 10,000 long depending on the part.

Old text:

( Header 1 text                      )
( Header 2 text                      )
G90   (set absolute distance mode)
G90.1 (set absolute distance mode for arc centers)
G17   (set active plane to XY)
G21   (set units to mm)
#<z_safe> =  0.250 
#<plunge_feed> =     5 

(---------------------------------------------------------)
G0 X 17.2644 Y 1.6127
M03 
G1 X 2.2842 Y 8.0674 F 0.01 
G1 X 15.3642 Y 17.8133
G1 X 17.2644 Y 1.6127
M05 
G0 X 0.0000 Y 0.0000
M05 

M02 

 

 

What it needs to look like:

G90
G21
G0 X17.2644 Y1.6127

M03
G1  X2.2842 Y8.0674  <--- copy and paste above the next m05
G1  X15.3642 Y17.8133
G1  X17.2644 Y1.6127

G1  X2.2842 Y8.0674  <--- pasted here
M05

G0 X0.000 Y0.000
M05
M02

Some things to note: g90 sets absolute coordinate mode, g21 tets units to mm, g0 is first coordinate to move the tool to. 

This is an important step: m03 is what turns on a laser. I need the first coordinate after every m03 to be copied to the line above m05 for each chunk of gcode. the very last m05 before the m02 needs to be ignored. There are multiple chunks with m03 x,ys then m05. each chunk is going to have different coordinates after the m03.

 

I have no idea where to start other than find the line with the wanted string, m03. dont know how to handle m05 though... any help is very much appreciated!

Edited by genbadger
Link to comment
Share on other sites


G90
G21
G0 X17.2644 Y1.6127

M03
G1  X2.2842 Y8.0674
G1  X15.3642 Y17.8133
G1  X17.2644 Y1.6127
G1  X2.2842 Y8.0674
M05

M03
G1  X3.14159 Y1.1111
G1  X15.3642 Y17.8133
G1  X17.2644 Y1.6127
G1  X3.14159 Y1.1111
M05

M03
G1  X4.5678 Y8.6753
G1  X15.3642 Y17.8133
G1  X17.2644 Y1.6127
G1  X4.5678 Y8.6753
M05

M03
G1  X7.7777 Y8.0674
G1  X15.3642 Y17.8133
G1  X17.2644 Y1.6127
G1  X7.7777 Y8.0674
M05

G0 X0.000 Y0.000
M05
M02

 

 

another example of a very small gcode file, notice how each m03 has a unique line following it and pasted above the next m05, not all the m05's, just the very next one

 

Link to comment
Share on other sites

Here :)

$sFilePath=@ScriptDir & "\test.txt"
$aFileContents=FileReadToArray($sFilePath) ; read all file lines to an array
$NumOfLines=@extended
$i=0
$sNewFileContents=""
$LineToCopy=""

While $i<$NumOfLines
    ConsoleWrite("line " & $i & ":" & $aFileContents[$i] & @CRLF)
    If $aFileContents[$i]="M03" Then
        ConsoleWrite("Found M03" & @CRLF)
        $LineToCopy=$aFileContents[$i+1] ; extract the line that comes after the M03 line
    ElseIf $aFileContents[$i]="M05" And $sNewFileContents<>"" Then
        ConsoleWrite("Found M05" & @CRLF)
        $sNewFileContents=$sNewFileContents & $LineToCopy & @CRLF ;append last saved line to contents
        $LineToCopy=""
    EndIf
    $sNewFileContents=$sNewFileContents & $aFileContents[$i] & @CRLF
    $i=$i+1

WEnd

ClipPut($sNewFileContents)

Change ClipPut with FileWrite().

Edited by ahmet
Link to comment
Share on other sites

Here :)

$sFilePath=@ScriptDir & "\test.txt"
$aFileContents=FileReadToArray($sFilePath) ; read all file lines to an array
$NumOfLines=@extended
$i=0
$sNewFileContents=""
$LineToCopy=""

While $i<$NumOfLines
    ConsoleWrite("line " & $i & ":" & $aFileContents[$i] & @CRLF)
    If $aFileContents[$i]="M03" Then
        ConsoleWrite("Found M03" & @CRLF)
        $LineToCopy=$aFileContents[$i+1] ; extract the line that comes after the M03 line
    ElseIf $aFileContents[$i]="M05" And $sNewFileContents<>"" Then
        ConsoleWrite("Found M05" & @CRLF)
        $sNewFileContents=$sNewFileContents & $LineToCopy & @CRLF ;append last saved line to contents
        $LineToCopy=""
    EndIf
    $sNewFileContents=$sNewFileContents & $aFileContents[$i] & @CRLF
    $i=$i+1

WEnd

ClipPut($sNewFileContents)

Change ClipPut with FileWrite().

wow so quick! thank you very very much! I'm off to try it now! 

EDIT: Could you advise me how to use the script? I replaced ClipPut with Filewrite() and added the path to the file but when i run it, no errors occur but no file change occurs either.. 

Edited by genbadger
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...