genbadger

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

5 posts in this topic

#1 ·  Posted (edited)

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

Share this post


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


Link to post
Share on other sites

#3 ·  Posted (edited)

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

#4 ·  Posted (edited)

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

Share this post


Link to post
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

    • ur
      By ur
      I am trying to identify the window based on the window title and text.
      The title will be the "erwin DM - filename"

      It is working till date, but some operating systems our application is displaying window as "erwin DM - [filename]"
       
      I tried  "erwin DM - *filename*" But this regular expression is not working.
      Any suggestion?
       
      $sModelFile = "C:\Users\Administrator\Documents\My Models\eMovies.erwin" $wdModel = _WinWaitActivate1("erwin DM - "&FileNameOnly($sModelFile),"") Func _WinWaitActivate1($title,$text,$timeout=0);Will Return the window Handler Logging("Waiting for "&$title&":"&$text) $dHandle = WinWait($title,$text,$timeout) if not ($dHandle = 0) then If Not WinActive($title,$text) Then WinActivate($title,$text) return WinWaitActive($title,$text,$timeout) Else Logging("Timeout occured while waiting for the window...") Exit EndIf EndFunc Func FileNameOnly($sFilePath) Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = "" Local $aPathSplit = _PathSplit($sFilePath, $sDrive, $sDir, $sFileName, $sExtension) ;_ArrayDisplay($aPathSplit, "_PathSplit of " & @ScriptFullPath) return $sFileName EndFunc  
    • natedog102
      By natedog102
      This small UDF helps you save any text to speech to a .wav file. 
      UDF:
      #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #include-once ; #INDEX# ======================================================================================================================= ; Title .........: _TTStoWav() ; AutoIt Version : 3.3.14.2 ; Language ......: English ; Author(s) .....: natedog102 ; Modifiers .....: ; Forum link ....: https://www.autoitscript.com/forum/topic/191573-tts-text-to-speech-to-wav-file-using-sapi/ ; Description ...: TTS to .wav file using SAPI ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _TTStoWav ; Description ...: TTS to .wav file using SAPI ; Syntax.........: _TTStoWav($sText, $sLocation[, $iRate = 1[, $iVolume = 100]]) ; Parameters ....: $sText - String you want converted to a .wav ; $sLocation - Save location + file name, example: "C:\folder\sample.wav" ; $iRate - TTS speak rate between -10 and 10 ; $iVolume - Volume of the TTS between 0 and 100 ; Return values .: Success - Returns one ; Failure - Returns zero ; Author ........: natedog102 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _TTStoWav($sText, $sLocation, $iRate = 1, $iVolume = 100) $oFileStream = ObjCreate("SAPI.SpFileStream") If @error Then Return 0 EndIf $oFileStream.Format.Type = 39 $oFileStream.Open($sLocation, 3) $oSpeech = ObjCreate('SAPI.SpVoice') If @error Then Return 0 EndIf $oSpeech.AudioOutputStream = $oFileStream $oSpeech.Rate = $iRate $oSpeech.Volume = $iVolume $oSpeech.Speak($sText, 3) $oSpeech.WaitUntilDone(10000) $oFileStream.Close() Return 1 EndFunc ;==>_TTStoWav And here's example usage:
      _TTStoWav("This is a test", "C:\folder-must-exist\sample2.wav") I have never made a UDF before so I don't know if I coded it correctly or if I followed all the recommended guidelines. Apologies if I haven't.
      DOWNLOAD:
       _TTStoWav.au3
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      in need the path/text present int above shown input box to be copied or retrieved into a variable .. how can i do that?
       
      Thanks
       
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Friends i am new to autoit and i am stuck in middle of automation of flashing tool..
      my requirement is i have a window and i have to get the value present in that window (below is the picture)

      in the above window i need to get the value of the highlighted field.the value of this filed keeps on changing...ii need to get the current value present in the field..below are the details of the window and field..

       
      CAN it be done?
       
      thanks