Community
    • Login

    Is this possible, suffix text based on a list.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    16 Posts 3 Posters 702 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • PeterJonesP
      PeterJones @Mark Littlefair
      last edited by

      @Mark-Littlefair ,

      It hasn’t worked Peter, is this likely because the file is a .nc file rather than a .txt file ?

      If it failed, then it has nothing to do with the extension of the file. Unless the script specifically calls out certain extensions (this script didn’t), a PythonScript script will act the same regardless of file extension.

      I created a file that had the two lines from your example,

      G90
      G21 G40 G80
      

      and after running the script, I get:

      G90 ;Absolute programming
      G21 ;Programming in millimeters mm G40 ;Tool radius compensation off G80 ;Cancel canned cycle
      

      I did just notice a bug – the parentheses around (mm) in the replacement were lost… I’ll fix that in a future post.

      But in general, the script works for me.

      Debugging steps:

      1. Does the menu entry Plugins > Python Script exist? If no, then revisit the INSTRUCTIONS
      2. Does the menu entry Plugins > Python Script > Scripts > addSuffixes exist? If no, then revisit the INSTRUCTIONS
      3. Try running Plugins > Python Script > Show Console. Does a new console window show up in Notepad++?
      4. Select the .nc file and then run Plugins > Python Script > Scripts > addSuffixes . If there is any output in the Python Script console, copy/paste or screenshot it and include it in your reply

      BTW: @guy038’s regex solution will also work for a small number of replacements, like in your example.
      But it will get unwieldy if you have 10 or more suffix replacements to do, whereas this script should handle any number of search/suffix pairs you want to throw at it.

      PeterJonesP Mark LittlefairM 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @PeterJones
        last edited by

        @PeterJones said in Is this possible, suffix text based on a list.:

        I’ll fix that in a future post.

        The easiest fix is to always use \( and \) for parentheses in the suffixes map

            'G21': "Programming in millimeters \(mm\)",
        
        Mark LittlefairM 1 Reply Last reply Reply Quote 0
        • Mark LittlefairM
          Mark Littlefair
          last edited by

          @PeterJones
          1,2 and 3 all ok

          Here is the screenshot.

          Capture.JPG

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • Mark LittlefairM
            Mark Littlefair @PeterJones
            last edited by

            @PeterJones

            Sorry I’ve just seen it myself now, not sure how that 1 got in there sorry again :)

            1 Reply Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @Mark Littlefair
              last edited by

              @Mark-Littlefair ,

              Your screenshot didn’t show line 59 of addSuffixes.py, but that’s where the error lies.

              If you really have the same input as mine, line 59 was the last line of the file, beyond editor.endUndoAction() … if so, just make sure there are no characters on the last line of the file (make it an empty line, without any spaces).

              Looking at the error message, you appear to have an l or a 1 or something at the end of the file, which may or may not be indented. There shouldn’t be.

              f5c20e96-27d0-4f02-9e03-2002113fa36a-image.png

              Mark LittlefairM 1 Reply Last reply Reply Quote 0
              • Mark LittlefairM
                Mark Littlefair @PeterJones
                last edited by

                @PeterJones

                all good

                Capture.JPG

                1 Reply Last reply Reply Quote 1
                • Mark LittlefairM
                  Mark Littlefair @PeterJones
                  last edited by

                  @PeterJones

                  Sorry to be a pain Peter,

                  My mistake the semi colon should be open bracket then the text suffix then close bracket

                  I’ve tried to edit the code where i thought it should work and failed.

                  Capture.JPG

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @Mark Littlefair
                    last edited by PeterJones

                    @Mark-Littlefair ,

                    It’s probably failing because in the replacement, parentheses are a special character, so to get a literal paren wrapped around the replacement, you will need to use the \( and \) … so to replicate the final line on the left in your screenshot, you would want

                        editor.rereplace(search_re, '$0 \({}\)'.format(suffix))
                    

                    … yes, with that line in the script, the output for your original example input yielded:

                    G90 (Absolute programming)
                    G21 (Programming in millimeters (mm)) G40 (Tool radius compensation off) G80 (Cancel canned cycle)
                    

                    The PythonScript plugin uses the boost regex engine (the same that Notepad++ uses) for the editor.research() calls, so the substitution follows all the same rules as for Notepad++ replacements, as described here

                    Mark LittlefairM 1 Reply Last reply Reply Quote 1
                    • guy038G
                      guy038
                      last edited by guy038

                      Hi, @mark-littlefair, @peterjones and All,

                      Given your new conditions about the ouptut text, the regex S/R, provided in my last post, becomes :

                      SEARCH (G90)|(G21)|(G40)|(G80)

                      REPLACE $0 \((?1Absolute programming)(?2Programming in millimeters \(mm\))(?3Tool radius compensation off)(?4Cancel canned cycle)\)

                      BR

                      guy038

                      Mark LittlefairM 1 Reply Last reply Reply Quote 0
                      • Mark LittlefairM
                        Mark Littlefair @guy038
                        last edited by

                        @guy038

                        Thank you Guy, this is the problem with your method though if I’m limited in the replacement zone to 2048 characters

                        Capture2.JPG

                        1 Reply Last reply Reply Quote 1
                        • Mark LittlefairM
                          Mark Littlefair @PeterJones
                          last edited by

                          @PeterJones

                          That’s run through great now, thank you. :)

                          Capture2.JPG

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors