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

    • 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

    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
              </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
          </div>
      </div>
      Many thanks in advance
      cheers
      Roger
    • robcull
      By robcull
      Hello all! I have had some issues reading text from different types of windows, occasionally, specifically with controlgettext. 
      **Before I begin, I know there are better ways to do what I attempt in the example below. That's not the point of this post. The point is my issues with controlgettext. 
      I am about to cite an example with an application you may be familiar with called SpeedFan (v4.52). My problem is not specific to speedfan, it is simply the most recent and easily reproducible example I can think of. 
      So, the goal of the script below is to get a string of text containing the current fan RPMs from the highlighted control in the screenshot below (see "speedfan_control_details.png").

      Now, here's a simple script for grabbing the window handle and reading the text from that control: 
      $wintitle = "SpeedFan 4.52" $controlID = "197934" ;will be reformatted as "[ID:######]" $hwnd = wingethandle($wintitle) if @error<>0 then msgbox(0, "WinGetHandle", "FAILURE. @error="&@error) Exit EndIf $text = ControlGetText($hwnd, "", "[ID:"&$controlID&"]") if @error=1 then msgbox(0, "ControlGetText", "FAILURE. @error="&@error) ;failure returns "" and @error=1 Exit EndIf msgbox (0, "ControlGetText", "SUCCESS. @error="&@error &@CRLF& "$text="&$text) ;success returns string and @error=0 You'll see that the ControlGetText operation runs without error, however it does not capture any text from the control. If you explore the other controls in this one window, you'll find mixed results across the board. Neither the temps nor voltages can be read, while the log field and some other elements can be read. Even when you read the text from the whole window, those elements are not included in the visible nor hidden texts. 
       
      I have run into this issue many times in the past- inconsistencies in the ability of autoit to interact with certain controls. What is it which makes this text different than any other readable texts? Is there an alternate method of reading the text in the window/control which could work? Any and all info to help me solve this mystery and satisfy my curiosity would be greatly appreciated. 
      Thanks  -Rob C
      PS: Running Autoit v3.3.14.2 on Win7 Ultimate x64