May-18-2017, 02:31 PM
Something like this
from collections import deque def process_code(in_code, insert_code): insert_at = deque() new_code = [] next_insert = -1 for i, line in enumerate(in_code.split('\n')): if insert_at and i >= next_insert: next_insert = insert_at.popleft() new_code.append(line) if i == next_insert: new_code.append(insert_code) if line.startswith(';LAYER:'): insert_at.append(i+2) return '\n'.join(new_code) if __name__ == '__main__': in_code=""";LAYER_COUNT:191 ;LAYER:0 M106 S255 G0 F3600 X99.873 Y99.731 Z0.3 ;TYPE:WALL-INNER G1 F1800 X100.034 Y99.574 E0.02699 G1 X100.225 Y99.441 E0.05491 G1 X100.445 Y99.339 E0.08401 ;TIME_ELAPSED:18.234693 ;LAYER:1 M107 G0 F5400 X99.456 Y98.484 Z0.45 ;TYPE:WALL-INNER M106 255 G1 F3300 X99.604 Y98.363 E47.83319 G1 X99.774 Y98.299 E47.84409 G1 X100.033 Y98.238 E47.86005 G0 X99.568 Y98.521 ;TIME_ELAPSED:37.123855 ;LAYER:2 M107 G0 F7200 X98.896 Y97.697 Z0.6 ;TYPE:WALL-INNER M106 255 G1 F4800 X99.103 Y97.5 E80.19273 G1 X99.378 Y97.417 E80.20996 G1 X99.486 Y97.403 E80.2165 G0 X107.174 Y111.477 ;TIME_ELAPSED:1240.100936 M107 ;End of Gcode """ insert_code = """G0 X100 Y50 G1 F100 X100 Y10 G1 F20 X110 Y10 G1 F100 X110 Y170 G1 F100 X110 Y10 G1 F20 X100 Y10 G0 X100 Y50""" print process_code(in_code, insert_code)