Simple text clean up



  • I have exported g.code from cura to a txt file. My mission is to create a animation of 3D printing in blender with python. So I just need to do some basic deleting of the g.code and I want to leave the coordinate info behind. So I downloaded notepad++ and this is what I have to do

    G1 X184.218 Y123.524 E1.81219 - I want to keep only the info after X Y
    G1 X183.179 Y125.34 E1.83974
    M204 S625 - I want to delete everything in this section except the the last line
    M205 X10 Y10
    G0 F2571.4 X182.84 Y125.092
    M204 S500
    G1 X183.179 Y125.34 E1.83974 - Then it all repeats
    G1 X183.179 Y125.34 E1.83974

    The question is how do I do that, I know this is really simple but for the love of god I cant figure it out, so do you know how to do it?

    https://drive.google.com/file/d/1iRhHJdNw8RQ2fv7W6x-kJwG3bHUDW0qS/view?usp=sharing - The g.code



  • @Adam-Labuš

    Can’t you just show a before text (which I think you have) and a desired after text?



  • So this is how it looks like now
    G1 X184.218 Y123.524 E1.81219
    G1 X183.179 Y125.34 E1.83974
    M204 S625
    M205 X10 Y10
    G0 F2571.4 X182.84 Y125.092
    M204 S500
    G1 X183.179 Y125.34 E1.83974

    This is how it should look like

    X184.218 Y123.524
    X183.179 Y125.34
    M204 S500
    X183.179 Y125.34



  • Hello, @adam-labuš, @alan-kilborn and All,

    Try this regex S/R :

    SEARCH (?-si)^((?!G1).+\R)?G1\x20(X\d+\.\d+\x20Y\d+\.\d+).+|.+\R

    REPLACE \1\2

    The algorithm used is :

    • Any line, right before a line G1 X...., is just kept

    • Any line, beginning with the G1 command, is replaced with the coordinates X and Y, only

    • Any other line is simply deleted

    Best Regards,

    guy038



  • @Adam-Labuš said in Simple text clean up:

    G1 X184.218 Y123.524 E1.81219 - I want to keep only the info after X Y

    Not sure if @guy038 already got this worked out the way you wanted, but I was going to take a gander at this, and have downloaded a copy of your gcode for further inspection, though I have to admit that it’s far from clear to me what to keep and what to discard. Does every line that contains the X and Y values you want to keep begin with G1?

    There are also many other lines in your gcode that don’t resemble any of those in your posted examples, especially at the beginning and end of the file. Are those to be ignored in whatever operation we come up with?

    M204 S625 - I want to delete everything in this section except the the last line

    You appear to indicate that the ‘last line’ of ‘this section’ is M204 S500, since that’s the line you show as being retained in your second post, but in your gcode file, I see lots instances of lines with M205 X5 Y5 — not shown in any of your posted examples — immediately following those with M204 S500, as in the following data extracted from your gcode:

    M204 S625
    M205 X10 Y10
    G0 F2571.4 X182.84 Y125.092
    M204 S500
    M205 X5 Y5
    

    So what, exactly, defines ‘this section’?



  • OK so sorry for making this forum thread a mess but after a little reasearch I figured out this
    I need to keep all lines starting with G1 and G0 like these two

    G1 X181.281 Y127.145 E1.8737
    G0 F2571.4 X182.162 Y124.596

    I want to delete all of the other stuff in these lines and just keep this

    X181.281 Y127.145
    X182.162 Y124.596

    For the other lines I want to delete everything else except lines that looks like this

    ;LAYER:1

    I have tried what guy038 said and it worked WONDERFULLY but because of my unclear instructions (I am not a native speaker sorry) the result wasnt what I wanted and because I dont understand the language completely I cannnot modify its instructions to keep the lines that start with G1 and ;LAYER:1 also.
    So once again can you guys help?



  • Hello, @adam-labuš, @alan-kilborn, @m_andre_z_eckenrode and All,

    Ah, OK ! With your new specifications, it’s easier to get the right regex, at once ;-))

    But when you said :

    For the other lines I want to delete everything else except lines that looks like this

    ;LAYER:1

    We don’t know if you speak of lines which begin, contain or end with ;LAYER:1 ! I suppose that it is which begin…


    Then, from this simple example, below :

    G1 X184.218 Y123.524 E1.81219
    G1 X183.179 Y125.34 E1.83974
    M204 S625
    ;LAYER:1 bla bla blah
    M205 X10 Y10
    G0 F2571.4 X182.84 Y125.092
    M204 S500
    G1 X183.179 Y125.34 E1.83974
    

    The following regex S/R

    SEARCH (?-si)^G[01].+(X\d+\.\d+\x20Y\d+\.\d+).*|^(?!;LAYER:1).+\R

    REPLACE \1

    would output this text :

    X184.218 Y123.524
    X183.179 Y125.34
    ;LAYER:1 bla bla blah
    X182.84 Y125.092
    X183.179 Y125.34
    

    Notes :

    • The in line modifiers (?-si) mean that :

      • Any dot regex symbol represents a single standard character and not EOL chars, as \r or \n (?-s)

      • The search is run in a non-insensitive way (?-i)

    • Then the search regex contains two alternatives, separated with the | symbol :

      • The regex ^G[01].+(X\d+\.\d+\x20Y\d+\.\d+).*

        • The part ^G[01] looks for strings G0 or G1, beginning current line

        • The part .+ represents any non-null range of standard characters

        • The part (X\d+\.\d+\x20Y\d+\.\d+) search for the exact letter X, followed with some digits \d+, followed with a literal dot \. followed by decimal digits \d+ and a space char \x20 then the exact letter Y and … ( same process than for X ). As all the part is surrounded by parentheses, its contents are stored as group 1

        • Finally, the part .* represents any range of standard characters, possibly null

      • The regex ^(?!;LAYER:1).+\R

        • The part ^(?!;LAYER:1) search all the lines which not begin with the string ;LAYER:1

        • And the part .+\R catches all the characters of any non-null line .+, with its line-break chars \R ( = \r\n for Windows files, \n for Unix files, or \r for Mac files )

    • In replacement :

      • If the left regex matches, then the group 1 is defined and corresponds to the XY coordinates, which must be rewritten => \1

      • Else, the right regex is used. As no group is defined, in the second alternative, the pointer to group 1 \1 return a null string. So, the current matched line is just deleted !

    Best Regards,

    guy038


Log in to reply