Inserting g code using replace



  • Hello, everyone, I’m having a bit of trouble inserting a code program thingy into my file if I search;LAYER: and want to insert this code on separate lines it will only put it on one line, and so it will not run, where I want it to list as below so the machine can recognise the actions I want it to do, I then have to copy and paste it under;Layer: on every layer, which is very time consuming if there are over a 1000 layers, any and all help very much appreciated with this,
    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS



  • @Crafty-Crafty said in Inserting g code using replace:

    In such a case I would record a macro with these steps.

    1. make sure your find dialog has only normal search mode enabled.
      Everything else unchecked. -> Close File dialog

    2. On top of your file create the needed layout

    ;Layer:
    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS
    
    1. Select ;Layer:

    2. Macro->start recording

    3. Press F3 (assuming you do not have remapped this shortcut, see menu Search->Find Next)

    4. press End key (you should see that the word is deselected and at the end of the line)

    5. press ENTER -> Now you should have the layout you want to have

    6. Stop recording

    7. Now you can test if it does what it should by executing Macro->Playback if it does, run Macro->Run a macro multiple times, check Run until end of file and press OK.

    8. Undo step 2

    Sounds like a lot to do but in reality it just take 10 seconds.



  • Hello @crafty-crafty, @ekopalypse, and All,

    @ekopalypse : May be I’m missing something but your macro just inserts a new line and not all the lines of G-code ?


    Here is my solution :

    • Open a new tab ( Ctrl + N )

    • Select your block of code and copy it in clipboard ( Ctrl + C )

    • Paste this block in the new tab ( Ctrl + V )

    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS
    
    • Move again to beginning of file ( Ctrl + Home )

    • Open the Replace dialog ( Ctrl + H )

    • Run the following regex S/R :

      • SEARCH \R

      • REPLACE \\r\\n

      • Select the Regular expression search mode

      • Click on the Replace All button

    => It should change that code into the single line, below :

    G91 ; USE RELATIVE POSITION\r\nG1 E-3 F1800 ; RETRACT 3MM\r\nG1 Z2 F2000 ; MOVE UP 2 MM\r\nG90 ; ABSOLUTE POSITION\r\nG1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT\r\nG1 F1000 X205 ; PRESS SWITCH\r\nG1 F9000 X199 ; MOVE SLIGHT LEFT\r\nG4 P500 ; PAUSE FOR 0.5 SECS\r\nG91 ; RELATIVE POSITION\r\nG1 E9 F1800 ; RESTORE FILAMENT\r\nG1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER\r\nG90 ; GO BACK TO ABSOLUTE CORDS
    
    • If necessary, delete the last string \r\n at the end of this single line

    As you can see, it simply adds the string \r\n after each line of your G-code and join them in a single line !


    Now, assuming, for instance, this test data, below :

    ========
    ;LAYER:
    ========
    
    ########
    ;LAYER:
    ########
    
    @@@@@@@@
    ;LAYER:
    @@@@@@@@
    
    • Run this regex S/R :

      • SEARCH (?-i);LAYER:

      • REPLACE G91 ; USE RELATIVE POSITION\r\nG1 E-3 F1800 ; RETRACT 3MM\r\nG1 Z2 F2000 ; MOVE UP 2 MM\r\nG90 ; ABSOLUTE POSITION\r\nG1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT\r\nG1 F1000 X205 ; PRESS SWITCH\r\nG1 F9000 X199 ; MOVE SLIGHT LEFT\r\nG4 P500 ; PAUSE FOR 0.5 SECS\r\nG91 ; RELATIVE POSITION\r\nG1 E9 F1800 ; RESTORE FILAMENT\r\nG1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER\r\nG90 ; GO BACK TO ABSOLUTE CORDS

    • Note that we inserted, in the Replace with: zone, all the contents of the single line, created before by the first regex S/R !

    • Click on the Replace All button

    => Any line ;LAYER:, with its exact case, is replaced with the G-Code block and gives the expected text, below :

    ========
    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS
    ========
    
    ########
    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS
    ########
    
    @@@@@@@@
    G91 ; USE RELATIVE POSITION
    G1 E-3 F1800 ; RETRACT 3MM
    G1 Z2 F2000 ; MOVE UP 2 MM
    G90 ; ABSOLUTE POSITION
    G1 F13000 X200 Y200 ; QUICK MOVE REAR RIGHT
    G1 F1000 X205 ; PRESS SWITCH
    G1 F9000 X199 ; MOVE SLIGHT LEFT
    G4 P500 ; PAUSE FOR 0.5 SECS
    G91 ; RELATIVE POSITION
    G1 E9 F1800 ; RESTORE FILAMENT
    G1 Z-2 F2000 ; RETURN Z TO CURRENT LAYER
    G90 ; GO BACK TO ABSOLUTE CORDS
    @@@@@@@@
    

    Best Regards,

    guy038



  • @guy038 - hahaha of course you are right the missing part is for step 2 and 7

    1. … and Copy the new text
    2. press ENTER and paste in the new text -> Now you should have the layout you want to have


  • Hi, @crafty-crafty, @ekopalypse, and All,

    From the @ekopalypse macro solution, here is an similar solution which can be generalized to :

    • Any number of lines which are to be replaced, in the limit of 2,048 characters

    • Any number of lines which are to be inserted, during the replacement


    But, first, let’s suppose that you simply have a specific line, repeated many times in your file, which must be replaced with a specific block of lines. Assuming that this line is named #Line# :

    • Open a new tab ( Ctrl+ N )

    • Type in the specific line #Line# line, followed with a line-break

    • Select the entire line #Line#, with its line-break

    • Open the Find dialog ( CTRL + F )

      • Select, if necessary, the Normal search mode

      • Leave all the other options unchecked

      • Click on the Find Next button ( Of course, no other occurrence is found, but it does not matter ! )

      • Close the Find dialog ( Esc )

    • Run the option Macro > Start Recording

      • Hit the F3 key ( Whatever an occurrence is found, or not, does not matter ! )

      • Hit the Ctrl + V shortcut ( The present contents of clipboard do not matter, too ! )

    • Run the option Macro > Stop Recording

    • Then, run the option Macro > Save Currently Recorded Macro...

      • Type in Replace Next Occurrence with Clipboard Contents, in the Name zone

      • Preferably, create a shortcut for this macro

      • Click on the OK button to valid the dialog

    • Create or select the block of lines, in any file, which must replace the #Line# line

    • Copy this block of lines in the clipboard ( Ctrl + C )

    • Now, open or select your specific file

    • If necessary, move to the very beginning of file ( Ctrl + Home )

    • Run the option Macro > Run a Macro Multiple Times...

      • Select the Replace Next Occurrence with Clipboard Contents macro

      • Tick the Run until the end of file option

      • Click on the Run button

    Voila ;-))

    Remark :

    After finding all the occurrences of the specific line, a last search operation is carried on, without success. So, the last #Line# line is replaced by two or more consecutive contents of the clipboard. Thus, a last task has to be done :

    • Delete the few extra blocks of text that have been wrongly inserted, after the last match !

    As said above, this macro is not specific and can be used to insert any contents of the clipboard, in replacement of any specific text, even split on several lines, as long as the total amount of text is smaller than 2,048 characters ( Max size of the Find what: zone )

    So, now that the macro Replace Next Occurrence with Clipboard Contents has been created, the road map becomes :

    • Create and select the text range which is to be replaced

    • Open the Find dialog ( CTRL + F )

      • Select, if necessary, the Normal search mode

      • Leave all the other options unchecked

      • Click on the Find Next button ( Whatever an occurrence of the range is found or not does not matter ! )

      • Close the Find dialog ( Esc )

    • Create and select the text range, in any file, even split on several lines, which will replace the initial range

    • Copy this text range in the clipboard ( Ctrl + C )

    • Now, open or select the specific file, where the replacements must occur

      • If necessary, move to the very beginning of the file ( Ctrl + Home )

      • Run the option Macro > Run a Macro Multiple Times...

        • Select the Replace Next Occurrence with Clipboard Contents macro

        • Tick the Run until the end of file option

        • Click on the Run button

      • Finally, delete the few extra range of text that have been wrongly inserted, after the last matched range !

    Best Regards

    guy038


Log in to reply