Script to copy G-code layer blocks from one file to another file

Script to copy G-code layer blocks from one file to another file

12
Basic UserBasic User
12

    Sep 21, 2022#1

    The main reason why I need the layer blocks in G-code files folded is that I have to exchange certain layers with ones from other files.

    Let's say I have two G-code files that contain the same number of layers, with just slightly different machine movements within these layers.

    I now want to exchange every fourth layer of one file with the corresponding of the other file. Folded layers make it way easier than it was before, but maybe there is a way to automate this step too. Currently, I am doing it all manually. It should be possible to write a macro or script that does just that, shouldn't it?

    Now this kind of macro is probably a little more complex than a macro for folding on startup, but maybe you can show me a guide or something on how to achieve this, or does UE already have a function included that could help with this matter?

    6,688587
    Grand MasterGrand Master
    6,688587

      Sep 21, 2022#2

      An UltraEdit macro or even better an UltraEdit script can be coded to automate often made tasks. I wrote already lots of macros and scripts for users. I just need to know the requirements.
      1. Is always every fourth layer in file A to replace by the corresponding layer in file B or could it be also every second, every third, every fifth layer?
      2. Have the two files always the same number of layers or is it possible that the number of layers differ and the corresponding layer in file B must be identified by the layer number for the current layer to replace in file A?
      3. Would be macro/script useful which on execution determines the layer in file A at current caret position, selects that layer in file A, searches in file B for the layer with same layer number, selects that layer in file B, copies it and pastes it over the selection in file A?
      A small example of file A (file to modify) and a small example of file B (file to use a source) and the expected contents of file A after macro/script execution would be helpful for coding the macro/script and testing it.
      Best regards from an UC/UE/UES for Windows user from Austria

      12
      Basic UserBasic User
      12

        Sep 22, 2022#3

        1: It is always every fourth layer
        2: They always have the exact same number of layers
        3: It might be useful in the future. Right now a hard coded layer exchange of layers 3,7,11,15,19,23,...(it's not starting with four because layers count from 0) would suffice. The two files where the script/macro is executed on always have the same number of layers, but it may differ on total layer numbers on different file pairs.

        Here are two example files with layers up to number 40 and a file with the expected result.

        6,688587
        Grand MasterGrand Master
        6,688587

          Sep 27, 2022#4

          The attached ZIP archive file contains the UltraEdit script file Copy_G-code_Layers.js which should be extracted for example to %APPDATA%\IDMComp\UltraEdit\scripts or whatever directory you prefer and added to Script list for usage on G-code files.

          The first two opened files with the most left file tabs on file tabs bar must be the two G-code files to process by the script. The file to copy layers to must be the first file (most left) and the file to copy layers from the second file on starting the execution of the script by hotkey, chord, with a double click on the script in Script list view or from ribbon/menu Advanced (ribbon mode or toolbar/menu mode with contemporary menus) or menu Scripting (toolbar/menu with traditional menus).

          There are two variables near top of the file (could be moved also up to top of the file) which are important for you:
          1. nFirstLayerToCopy defines the number of the first layer to copy from second to first file. The number has nothing to do with the layer number in the files. The number 4 means the fourth layer is to copy first from second to first file which means for the example files to copy LAYER:3.
          2. nLayersToSkip defines the number of the layers to skip between the layers to copy. The number 3 in the script file means for the example files that after copying LAYER:3 the next three layers 4, 5 and 6 are skipped in both files and next copied layer is LAYER:7
          There is always shown the output window with either the information on how many layers were updated in first file from second file and the total number of layers processed or an error information if there are not at least two *.gcode files opened being the first two files.
          copy_g-code_layers.zip (1.59 KiB)   0
          This ZIP file contains the UltraEdit script to copy G-code layers.
          Best regards from an UC/UE/UES for Windows user from Austria

          12
          Basic UserBasic User
          12

            Sep 27, 2022#5

            Thank you, but I'm afraid it's only working under certain circumstances. I have tested it, and it's exchanging the layers as it should, but it seems that it only copies part of a layer if said layer has too many lines. I have tested a file where the lines one layer contains get fewer the higher the layer number goes. The last few layers (with the fewest lines) get exchanged perfectly well, but the first layers get cut off and not the whole layer will be copied and exchanged. This is kind of a big issue since the files I will be working with have many more lines per layer than these example files. Tens of thousands more to be a little more specific. I have attached the files I have tested it on.
            To_Copy_to_2.gcode (1.82 MiB)   2
            To_Copy_From_2.gcode (586.84 KiB)   2

            6,688587
            Grand MasterGrand Master
            6,688587

              Sep 27, 2022#6

              I updated the ZIP file in my previous post. There is used now by the script a user clipboard of UltraEdit to copy the lines of a layer from second to first file. The user clipboards of UltraEdit are not limited in comparison to memory used by the JavaScript core interpreter embedded in UltraEdit.

              I see a difference between the results file created by first version of the script on running it on the files To_Copy_to_2.gcode and To_Copy_From_2.gcode and the results file created by the updated version of the script using user clipboard 9. The updated script produces the same output as the first version of the script for the files To_Copy_To.gcode and To_Copy_From.gcode.

              I have three more questions regarding to end of a layer block.
              1. Is there another marker for end of a layer than a line beginning with ;LAYER:?
                I see also ;End of Gcode in the G-code files.
              2. Should that be interpreted also as end of a layer block if the last layer in first file should be updated by last layer in second file?
              3. Is there always a comment line ;End of Gcode in G-code files marking end of last layer block (= end of code)?
              Best regards from an UC/UE/UES for Windows user from Austria

              12
              Basic UserBasic User
              12

                Sep 28, 2022#7

                1. the comment ;TIME_ELAPSED:XXXXXXXXX always marks the last line of a layer.
                2. I think its better to use the time elapsed comment as last line for the last layer block. ;End of Gcode could be used but there is no need to as the command between the last TIME_ELAPSED and End of Gcode are always exactly the same in both files.
                3. Its always there, yes.

                6,688587
                Grand MasterGrand Master
                6,688587

                  Oct 02, 2022#8

                  I updated once again the script attached to my last but one post in the ZIP file and it works now as expected, but not with UltraEdit for Windows version 2022.0.0.* to 2022.1.0.108 (a not public published user verification build). All versions of UltraEdit from 2022.0.0.70 (first public released 2022.0) to currently latest public available 2022.1.0.100 produce unpredictable results on running the script on two G-code files. This is definitely a bug of UltraEdit for Windows version 2022.*. I have just reported this bug to UltraEdit support by email with the sample files, the expected and the produced wrong result files, the script file and the description on how to reproduce this issue.

                  The script works fine and produces always the correct result on using UltraEdit for Windows v28.20.0.92 or even older versions. I tested the script also with UE v22.20.0.49 which was the last UltraEdit version for Windows XP.

                  So if you want to use the script, you either need to wait until a version of UltraEdit is released with a fix of this bug or you backup %APPDATA%\IDMComp\UltraEdit, for example by making a copy of this directory, uninstall UltraEdit version 2022.1 without deactivation of the license and without removal of the application data, restart Windows (to complete the uninstall) and install UltraEdit v28.20.0.92 (last public available build of version 28.20). The installer executable of version 28.20 is not available anymore for download somewhere. So you need to contact UltraEdit support and ask for the installer executable for UltraEdit 28.20.0.92 in language X (English, German, ...) if you don't have the installer executable of UE v28.20.0.92 archived somewhere.

                  PS: I detected and reported also some other issues while helping you with the syntax highlighting wordfile for G-code files and developing the scripts. Most of the reported issues are fixed already in the user verification build 2022.1.0.108, but not the issue with wrong copying of the blocks from second to first file which I have reported just some minutes before writing this post on Sunday.
                  Best regards from an UC/UE/UES for Windows user from Austria

                  12
                  Basic UserBasic User
                  12

                    Oct 06, 2022#9

                    You are a legend. I am currently doing it manually since the code folding alone helps a ton. I will however definitely use your script soon. I just dont know yet If I should revert to an older version, or wait a little in the hopes that they fix it, so that I can use it with the newest version.

                    6,688587
                    Grand MasterGrand Master
                    6,688587

                      Oct 30, 2022#10

                      I looked on the cause of the not working script using Perl regular expression finds. I could find out that the bug in UltraEdit for Windows v2022.0.0.70 to v2022.1.0.112 can be worked around using the UltraEdit regular expression engine with slightly different search expressions and four additional lines in script to set the caret to beginning of the next line below the line containing the found string. Please use the script in the attached ZIP file which works also with currently latest UltraEdit for Windows v2022.1.0.112 .
                      copy_g-code_layers_ue.zip (1.6 KiB)   1
                      This ZIP file contains the UltraEdit script to copy G-code layers using UltraEdit instead of Perl regular expression finds.
                      Best regards from an UC/UE/UES for Windows user from Austria