Jump to content
genbadger

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

Recommended Posts

genbadger

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
genbadger


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
ahmet

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
genbadger

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
ahmet

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

    • reida
      By reida
      Hello. On page there is mail address. How to keep in a text file this address? 
      It is page and email
      https://alpari.com/
      info@alpari.com
    • caramen
      By caramen
      Hey.  
       
      I requested help about how to get a value from a text in a variable. 
      Now i know how to do that. But i learned with the command FileRead. Now i whould like to know how to replace the command :
      FileRead('Dossier.txt') The purpose is to read a webpage text. To find some value inside. 
       
      Btw i tryed to play with WindowsInfo.au3 but i dont got much thing.  
    • therks
      By therks
      I'm trying to create a simple clock widget that automatically scales the text to the size of the window. I came up with the following method, but it doesn't work as well as I'd like. It especially has trouble scaling to the width of the window for some reason (in the example, try resizing the window to be narrow and tall).
      Does anyone have a better method?
      #include <Misc.au3> #include <WinAPIConv.au3> #include <GUIConstants.au3> #include <GDIPlus.au3> Opt('MustDeclareVars', 1) Global $_FONT_FAMILY = 'Arial', $_LB_TEXT Main() Func Main() _GDIPlus_Startup() Local $hGUI GUIRegisterMsg($WM_SIZE, WM_SIZE) $hGUI = GUICreate('', 300, 100, Default, Default, $WS_OVERLAPPEDWINDOW, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST)) $_LB_TEXT = GUICtrlCreateLabel('This is a string', 0, 0, 300, 100, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont($_LB_TEXT, _MeasureString($hGUI, GUICtrlRead($_LB_TEXT), $_FONT_FAMILY), 0, 0, $_FONT_FAMILY, 5) GUISetState() Local $iGM While 1 $iGM = GUIGetMsg() Switch $iGM Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd _GDIPlus_Shutdown() EndFunc Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam) GUICtrlSetFont($_LB_TEXT, _MeasureString($hWnd, GUICtrlRead($_LB_TEXT), $_FONT_FAMILY), 0, 0, $_FONT_FAMILY, 5) EndFunc Func _MeasureString($hWnd, $sString, $sFont = 'Arial') Local $iError, $aSize, $hGraphic, $hFormat, $hFamily, $tLayout, $iFontSize, $hFont, $aInfo If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) EndIf $aSize = WinGetClientSize($hWnd) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd) $hFormat = _GDIPlus_StringFormatCreate() $hFamily = _GDIPlus_FontFamilyCreate($sFont) $tLayout = _GDIPlus_RectFCreate(0, 0, $aSize[0], $aSize[1]) $iFontSize = 0 Do If Not $hFamily Then $iError = 1 $iFontSize = 10 ExitLoop EndIf $iFontSize += 1 $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, 0) $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat) _GDIPlus_FontDispose($hFont) If $aInfo[1] = 0 Then ExitLoop Until DllStructGetData($aInfo[0], 3) >= $aSize[0] Or DllStructGetData($aInfo[0], 4) >= $aSize[1] $iFontSize -= 1 _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_GraphicsDispose($hGraphic) Return SetError($iError, 0, $iFontSize) EndFunc
    • dadalt95
      By dadalt95
      Hi!
      I want to get the signal in this website:
      https://binary-signal.com/pt/chart/eurusd
      I have tried using _IEBodyReadText  and some _StringBetween. What happens is that the text are being update every tick and _IEBodyReadText doesn't.
      To perform the update I used  _IEAction($oIE, "refresh") but it's not good because the website block me after some time due too many requests..
      Is there any other way to get this text every tick?
      PS: The text I want to get is WAIT, CALL or PUT.
       
      Here is the code:
      global $oIE = _IECreate ('https://binary-signal.com/pt/chart/eurusd', 0, 1 , 1 , 0) Local $sText = _IEBodyReadText($oIE) $result = _StringBetween ( $sText , 'PUTEUR/USD on Binary-signal.com', 'sinal está PRONTA') ;MsgBox ( 0, "asf", $result[0]) $espera=StringInStr($result[0], "WAIT") $compra=StringInStr($result[0], "CALL") $venda=StringInStr($result[0], "PUT") ;MsgBox($MB_SYSTEMMODAL, "", $espera) $n=0 $c=0 Captar() Func Captar() ;_IENavigate($oIE, "https://binary-signal.com/pt/chart/eurusd") ;MsgBox($MB_SYSTEMMODAL, "", $n) Local $sText = _IEBodyReadText($oIE) $result = _StringBetween ( $sText , 'PUTEUR/USD on Binary-signal.com', 'sinal está PRONTA') If (Not $compra=0) And $n=0 Then MsgBox($MB_SYSTEMMODAL, "", "COMPRE") $n=1 $c=$c+1 ;_IEAction($oIE, "refresh") Sleep(60000) Captar() ElseIf (Not $venda=0) And $n=0 Then MsgBox($MB_SYSTEMMODAL, "", "VENDA") $c=$c+1 $n=1 ;_IEAction($oIE, "refresh") ;MsgBox($MB_SYSTEMMODAL, "", $n) Sleep(60000) Captar() ElseIf (Not $venda=0) And $n=1 Then ;MsgBox($MB_SYSTEMMODAL, "", "Esperando próxima rodada") $n=1 ;MsgBox($MB_SYSTEMMODAL, "", $n) ;_IEAction($oIE, "refresh") Sleep(60000) Captar() ElseIf (Not $venda=0) And $n=1 Then ;MsgBox($MB_SYSTEMMODAL, "", "Esperando próxima rodada") $n=1 ;MsgBox($MB_SYSTEMMODAL, "", $n) ;_IEAction($oIE, "refresh") Sleep(60000) Captar() Else ;MsgBox("", "", "ESPERE") $n=0 Sleep(1000) ;_IEAction($oIE, "refresh") Local $sText = _IEBodyReadText($oIE) $result = _StringBetween ( $sText , 'PUTEUR/USD on Binary-signal.com', 'sinal está PRONTA') Captar() EndIf EndFunc  
    • vin1
      By vin1
      i need a script that can replace words in text with other words from a words list
      it has to have 2 edit boxes input/output and a button "run"
      from word_list.txt it choses which word to replace with. words are separated with commas for multiple choice
       

       
       

       
       
      ; AutoIt GUI Example ; Created: 17/01/2005 - CyberSlug ; Modifed: 05/12/2011 - guinness ; Modifed: 09/06/2014 - mLipok #Region INCLUDE #include <AVIConstants.au3> #include <GuiConstantsEx.au3> #include <TreeViewConstants.au3> #EndRegion INCLUDE #Region GUI GUICreate("Sample GUI", 400, 420) GUISetIcon(@SystemDir & "\mspaint.exe", 0) #EndRegion GUI #Region EDIT GUICtrlCreateEdit(@CRLF & " Sample Edit Control", 10, 10, 380, 175) GUICtrlSetTip(-1, '#Region EDIT') #EndRegion EDIT #Region EDIT2 GUICtrlCreateEdit(@CRLF & " Sample Edit Control", 10, 185, 380, 175) GUICtrlSetTip(-1, '#Region EDIT') #EndRegion EDIT2 #Region BUTTON GUICtrlCreateButton("Sample Button", 10, 360, 100, 30) GUICtrlSetTip(-1, '#Region BUTTON') #EndRegion BUTTON #Region GUI MESSAGE LOOP GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete() #EndRegion GUI MESSAGE LOOP  
×