# 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:

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!

##### 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

##### 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 #### Share this post ##### Link to post ##### 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..

##### Share on other sites

If you want to write to the file you read from use the following code

$hFile=FileOpen($sFilePath,$FO_OVERWRITE) ; you have to include <FileConstants.au3> or use 2 instead of$FO_OVERWRITE
FileWrite($sFilePath,$sNewFileContents)
FileClose(hFile) #### Share this post ##### Link to post ##### 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 • ### Similar Content • Good morning, I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part. Can someone tell me what I do wrong ? Here is my code:btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window. I tried to follow the help, made a lot of different parameter combinations (here it's $BS_BOTTOM, but I tried $BS_VCENTER and $BS_TOP), still get the same result : vertical alignement is a no go for me.
Can someone help ?
TIA
Gilles

• So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.

First File:
jumps

Or:
Text1 = jumps
Text2 = fence
Second File:
My Dog XXXXX over the YYYYY.

After Script:
My Dog jumps over the fence

I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.

Any help is appreciated.

• By misioooo
Hi!

I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
Part with button looks like this:
$ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1,$hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
• By vin1
i have a script that selects  a text file and deletes a line (text input required) in the text file selected
i have to make it remove all lines found on a file i name, toRemoveLines.txt
it has to remove lines from all text files found in a folder
this is the script that has to be modified
where it says "select file" it has to be "select folder"
where it says "line text input" it has to be all lines from a text file
#Include <File.au3> Global $success = False$file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4) $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text")$file_count_lines = _FileCountLines($file_name) for$i = 0 to $file_count_lines$Lines_text_output = FileReadLine($file_name,$i) if StringInStr($Lines_text_output,$line_text_input) then _FileWriteToLine($file_name,$i, "", 1) $success = True ExitLoop EndIf Next if$success = True Then MsgBox(0, "Success", "Line has been deleted") Else MsgBox(0, "Failure", "Line wasn't found") EndIf
• By Skeletor
Hi All,
It has been bugging me for a while now.
Can you view the AutoIt code from within a compiled exe. Like how you can with most .Net exe's by using a certain program.
If this is possible, how then can I secure my program (if this method exists).

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...