Community
    • Login

    Gcode text file analyzing

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    8 Posts 4 Posters 283 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.
    • Holger SuchiH
      Holger Suchi
      last edited by

      Dear community,
      I am working with Gcode files to control my selfmade CNC mill and laser.
      Recently I changed the software that generates my Gcode from drawings. In the old SW I got an information about the working space used in the Gcode, meaning the maximum and minimum values of any X-Y-Z coordinate in the file.
      Unfortunately, the new code generator is lacking that feature.

      I’m now looking for an alternative way to determine these, and as Gcode is plain text, I thought that NPP might be the tool of choice.
      Maybe there is already available a plugin that does the following (or could be a basis to mod):

      • search the file for letter “X” (capital or not, alternatively “Y” and “Z”)
      • parse the following value-string
      • remember the string
      • check if the actual string value is lager/smalle than previous rembered max/min
      • update the max/min values
      • when eof than a comment line with the determined values shall be inserted to the top of the file

      I wonder if such or similar task has never been programmed her…

      Would be great if you can help me with this. I’m newbe to Windows programming or plugin development, but with a startpoint given I would like to learn about (micro controller programming in Arduino and C++ is not unknown to me).
      Thank you so much!
      Holger

      PeterJonesP Michael VincentM 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Holger Suchi
        last edited by

        @Holger-Suchi ,

        There aren’t any general-purpose GCode plugins available, to my knowledge.

        The algrithm you described should be easy enough to code in one of Notepad++'s scripting plugins – the PythonScript plugin uses Python to automate Notepad++, and Python is pretty easy to pick up if you know other programming languages.

        Here is some initial pythonscript code that gets you started on the right idea: I don’t guarantee it works, but it’s someplace to start.

        from Npp import editor
        
        rememberMin = 999999999
        rememberMax = -rememberMin
        
        def checkMatchForSize(m):
            """
            For a given match, check the integer value after the X,
            and compare it to the remembered Min/Max values
            """
            matchInt = int(m.group(1))
            global rememberMin
            global rememberMax
            if matchInt < rememberMin: rememberMin = matchInt
            if matchInt > rememberMax: rememberMax = matchInt
        
        # look for all instances of capital X followed by 1 or more digits, and call the checkMatchForSize function with that match
        editor.research(r'X(\d+)', checkMatchForSize)
        
        # add a line at position 0
        editor.insertText(0, "# The min to max X values are: {} ... {}\r\n".format(rememberMin, rememberMax))
        

        Installation instructions in our PythonScript FAQ

        Holger SuchiH 1 Reply Last reply Reply Quote 3
        • Holger SuchiH
          Holger Suchi @PeterJones
          last edited by

          @PeterJones
          Thank you Peter for this prompt reply! I’ll definately give it a try study a bit more of how to implement Py scripts to NPP and also of Py syntax, that looks a bit strange at the first glance.
          I’d appreciate so much if you could guide me where to find an overview of these NPP specific commands and syntax, like e.g. editor.research( ) or editor.insertText( ) . I guess that’s somewhere else than in the FAQ?
          …I’ll try to get familiar with this and I will report success here.
          Thank you!
          Holger

          EkopalypseE 1 Reply Last reply Reply Quote 0
          • EkopalypseE
            Ekopalypse @Holger Suchi
            last edited by

            @Holger-Suchi

            As soon as you have installed the PythonScript plugin, you will see a context help menu item in the plugins->Python Script menu.

            I personally would not install the plugin via the plugins admin dialog as it installs the version that uses Python 2.7, instead I would choose one of the newer versions from here.

            Holger SuchiH 1 Reply Last reply Reply Quote 1
            • Holger SuchiH
              Holger Suchi @Ekopalypse
              last edited by

              Hi,
              got the py script installed and Peter’s demo running.
              Trying to tweak it to my needs now:
              The coordinates values are not integer but float types.
              Test text contains just this three lines:

              (Test)
              G00 X-2.35 Y-11
              G00 X23.8 Y 52

              The script inserts these two lines at the top:
              (maxX : 23.0)
              (minX : 0)

              Mmhh, right direction, but …

              • negative number ignored
              • decimals behind 23 ignored

              I’m lacking background:

              • What does "editor.research(r’X(\d+)’ " do exactly? Does it grab the complete string between X and Y? Info about the coordinates format: Always uses a dot for decimals; number of decimals can vary from zero to 4…6; coordinate ends with any “not a number” char, this might be a blank, an Y or Z or other letter or new line

              • What does m.group(1) mean/do?

              It’s a bit of blind tapping in the dark for me without knowledge of the commands and it’s syntax…
              help is most appreciated.

              My code below:

              from Npp import editor
              
              minX = 0
              maxX = 0
              
              def checkMatchForSize(m):
                  """
                  For a given match, check the integer value after the X,
                  and compare it to the remembered Min/Max values
                  """
              #    matchInt = int(m.group(1))
               
                  matchLong  = float(m.group(1))
                  
                  global minX
                  global maxX
                  if matchLong < minX: minX = matchLong
                  if matchLong > maxX: maxX = matchLong
              
              # look for all instances of capital X followed by 1 or more digits, and call the checkMatchForSize function with that match
              editor.research(r'X(\d+)', checkMatchForSize)
              
              # add a line at position 5
              editor.insertText(0, "(minX : {})\r\n".format(minX))
              editor.insertText(0, "(maxX : {})\r\n".format(maxX))
              
              
              PeterJonesP 1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Holger Suchi
                last edited by PeterJones

                @Holger-Suchi said in Gcode text file analyzing:

                I’m lacking background:

                What does "editor.research(r’X(\d+)’ " do exactly?

                It uses the regular expression X(\d+) to match text, which looks for the literal captial X followed by one or more digits. It puts everything inside the parentheses into “match group #1”.

                What does m.group(1) mean/do?

                That extracts the string value of “match group #1”

                Since you want to be able to handle either integers or floats, and need to handle signed numbers as well, the regular expression needs to be tweaked: X([+-]?\d+(?:\.\d*)?) – this looks for the

                • the X
                • followed by an optional + or - sign using [+-]?
                • followed by one or more digits using \d+
                • followed optionally by what’s described below, using (?:...)?
                  • a literal . , – indicated by \., because . alone means “match any character” to regular expresions
                  • then zero or more digits, using \d*
                • and puts everything but the X into match group #1 (by the outer parentheses)

                It’s a bit of blind tapping in the dark for me without knowledge of the commands and it’s syntax…

                @Ekopalypse already pointed out how to find the PythonScript documentation. The editor.research command can be found on the Editor Object page (which, on a normal installation of Notepad++ and the PythonScript plugin, can be found on file:///C:/Program%20Files/Notepad++/plugins/PythonScript/doc/scintilla.html or by following the “context help” menu that @Ekopalypse already mentioned to you).

                The regular expression syntax itself is common regex syntax. Notepad++ itself uses the boost regex library, and PythonScript does as well (for the research command, whose command name is derived from “regular expression search”). Notepad++'s online User Manual has a big section on regular expression syntax, where you can learn about the \d and \. and () and the like.

                1 Reply Last reply Reply Quote 1
                • Holger SuchiH
                  Holger Suchi
                  last edited by

                  @PeterJones
                  Thanky you so much for your clear explanation, thanks for spending your time!
                  With this, I was able to modify the code to do what I need.
                  Here’s my final code, for anyone who may need it:

                  from Npp import editor
                  
                  minX = 0
                  maxX = 0
                  minY = 0
                  maxY = 0
                  minZ = 0
                  maxZ = 0
                  
                  def checkMatchForSize(m):
                      matchLong  = float(m.group(1))
                      global minX
                      global maxX
                      if matchLong < minX: minX = matchLong
                      if matchLong > maxX: maxX = matchLong
                  
                  # look for all instances of capital X followed by 1 or more digits, and call the checkMatchForSize function with that match
                  
                  # search for Zminmax
                  editor.research(r'Z([+-]?\d+(?:\.\d*)?)', checkMatchForSize)
                  minZ = minX
                  maxZ = maxX
                  minX = 0  # compare function uses minX/maxX 
                  maxX = 0
                  
                  # repeat for Y
                  editor.research(r'Y([+-]?\d+(?:\.\d*)?)', checkMatchForSize)
                  minY = minX
                  maxY = maxX
                  minX = 0  # compare function uses minX/maxX 
                  maxX = 0
                  
                  # finally repeat for X
                  editor.research(r'X([+-]?\d+(?:\.\d*)?)', checkMatchForSize)
                      # compare function uses minX/maxX 
                      # now, all six values determined
                      # print them nicely
                  
                  # add lines at position 0
                  editor.insertText(0, "\r\n")
                  editor.insertText(0, "(\t\t\t minY: {:+}\t\t\t\t\t minZ: {:+})\r\n".format(minY,minZ))
                  editor.insertText(0, "(minX: {:+}\t\t\t maxX: {:+})\r\n".format(minX,maxX))
                  editor.insertText(0, "(\t\t\t maxY: {:+}\t\t\t\t\t maxZ: {:+})\r\n".format(maxY,maxZ))
                  

                  (How to attach a file here? )
                  If someone wants to test/play, here is a gcode example file: (I don’t know how to attach a file as a whole, so I copy as code):

                  (Projekt SMTest20x20)
                  (Erstellt mit Estlcam 12,122)
                  (Laufzeit ca. 00:00:29 Stunden)
                  
                  (Benötigte Werkzeuge:)
                  (Schleppmesser)
                  
                  
                  G00 Z2.0000
                  
                  (Nr. 1 Gravur bearbeiten: Gravur 1)
                  G00 X4.2846 Y1.9098 Z2.0000
                  G00 Z0.5000
                  G01 Z0.0000 F600 S24000
                  G01 Z-2.0000
                  G01 X10.1625 Y6.1803
                  G01 X16.0403 Y1.9098
                  G01 X13.7952 Y8.8197
                  G01 X19.6730 Y13.0902
                  G01 X12.4076
                  G01 X10.1625 Y20.0000
                  G01 X7.9173 Y13.0902
                  G01 X6.8073
                  G03 X5.6317 Y9.4721 I0.0000 J-2.0000
                  G01 X6.5297 Y8.8197
                  G01 X4.2846 Y1.9098
                  G00 Z2.0000
                  
                  (Nr. 2 Gravur bearbeiten: Gravur 3)
                  G00 X4.0000 Y3.5000 Z2.0000
                  G00 Z0.5000
                  G01 Z0.0000 F600
                  G01 Z-2.0000
                  G01 X6.0000 Y8.5000
                  G01 X3.0000
                  G01 Y3.5000
                  G01 X4.0000
                  G00 Z2.0000
                  
                  (Nr. 3 Gravur bearbeiten: Gravur 2)
                  G00 X4.8879 Y15.5042 Z2.0000
                  G00 Z0.5000
                  G01 Z0.0000 F600
                  G01 Z-2.0000
                  G03 X6.3515 Y17.6508 I0.1121 J1.4958
                  G03 X3.7606 Y17.8450 I-1.3515 J-0.6508
                  G03 X4.8879 Y15.5042 I1.2394 J-0.8450
                  G00 Z2.0000
                  
                  (Nr. 4 Gravur bearbeiten: Gravur 4)
                  G00 X2.0000 Y15.5042 Z2.0000
                  G00 Z0.5000
                  G01 Z0.0000 F600
                  G01 Z-2.0000
                  G01 Y20.0000
                  G01 X20.0000
                  G01 Y2.0000
                  G01 X2.0000
                  G01 Y15.5042
                  G00 Z2.0000
                  
                  (Nr. 5 Gravur bearbeiten: Gravur 5)
                  G00 X1.0000 Y15.5042 Z2.0000
                  G00 Z0.5000
                  G01 Z0.0000 F600
                  G01 Z-2.0000
                  G01 Y21.0000
                  G01 X21.0000
                  G01 Y1.0000
                  G01 X1.0000
                  G01 Y15.5042
                  G00 Z2.0000
                  
                  (Test)
                  G00 X-2.35 Y-11
                  G00 X23.8 Y 52
                  

                  Running the script on that text file, it inserts four lines at the top, like this:

                  (		         maxY: +21.0					 maxZ: +2.0)
                  (minX: -2.35			             maxX: +23.8)
                  (			 minY: -11.0					 minZ: -2.0)
                  

                  That’s perfectly what I need. Thank you for guiding me to this!
                  With best regards
                  Holger

                  1 Reply Last reply Reply Quote 2
                  • Michael VincentM
                    Michael Vincent @Holger Suchi
                    last edited by Michael Vincent

                    @Holger-Suchi said in Gcode text file analyzing:

                    Maybe there is already available a plugin that does the following (or could be a basis to mod):

                    @PeterJones

                    There aren’t any general-purpose GCode plugins available, to my knowledge.

                    There is https://github.com/NCalu/NCneticNpp

                    Not sure it’s in Plugin Admin so you need to go looking and manually install.

                    UPDATE: Also never used it myself so have no idea if it’s of any use - I don’t do any GCode work at all myself.

                    Cheers.

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