<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/env python

import sys
import re

def main(argv):
	
	openfile = open(argv[0], 'r')
	file_in = openfile.readlines()
	openfile.close()
	i = 1
	ii = 0
	n = 0
	nn = 0
	onof = -1
	file_out = []
	for line in file_in:
		if line.find('RUN_FROM_LINE_FILTER') != -1:
			onof = int(re.search(r'\d+', line).group())
			i+=1
			if onof != -1:
				print ";RUN_FROM_LINE_FILTER:" ,onof
		if onof ==1:
			if line.find('RUN_FROM_LINE_TARGETLINE') != -1:
				targetl = int(re.search(r'\d+', line).group())
				i+=1
				print ";RUN_FROM_LINE_TARGETLINE:" ,targetl
			elif line.find('ADDITIONAL_LINE_BEFORE_TARGET') != -1:
				adli = [int(s) for s in re.findall(r'\b\d+\b',line)]
				print ";ADDITIONAL_LINE_BEFORE_TARGET:" ,adli
				n = len(adli)
				i+=1
			elif line.find('ADDITIONAL_CODE_BEFORE_TARGET') != -1:
				adcod = (re.findall(r'\w[-+]?[0-9]*\.?[0-9]+',line))
				print ";ADDITIONAL_CODE_BEFORE_TARGET:" ,adcod
				nn = len(adcod)
				i+=1                 
			elif line.find('END_FILTER') != -1:
				print ""
				break    
	if onof == -1:
		onof = 0
	i = 1
	ii = 0
	for line in file_in:
		if onof == 1:
			if ii &lt; n and adli[ii] == i:
				print line.rstrip('\n') ,"          ;#####   Additional Line:", adli[ii]
				i+=1
				ii+=1
			else:
				if i == targetl:
					if i == targetl and nn &gt; 0:
						print "",' '.join(map(str, adcod)) , "          ;#####   Added Code before Target Line"
					print line.rstrip('\n') ,"          ;#####   Target Line:", targetl
					i+=1
				elif i &gt;= targetl:
					print line.rstrip('\n')
					i+=1
				else:
					i+=1
		elif onof == 0:
			print line.rstrip('\n')
			i+=1	          					          
if __name__ == "__main__":
	main(sys.argv[1:])



# If you are working with many subroutines, there likely would happen some problems with run from line
# for safe workflow this filter can be used, you decide from where the file begins until END. Additional 1 line of input gcode, 1 line before Target
# line can be added, as well additional lines, any line before Target line. This way you need only to edit the filter options, for correct start condition, then just run the file by nc-start.

# ini  [FILTER]
# PROGRAM_EXTENSION = .py Python Script

# py = python
# rfl = /home/path/to/folder/runfromline.py

# sudo chmod 755 /home/path/to/folder/runfromline.py and set executable.

# The .ngc file to be used, added ending .rfl to pass the filter:  eg.   ArmLagerHalter.ngc.rfl


# This lines are on top of the .ngc file, to control the filter. If filter is set off (0), file is printed by the filter without modifications.
# (RUN_FROM_LINE_FILTER = 1)                                ; Turn Filter On / Off  (1 / 0)

# (RUN_FROM_LINE_TARGETLINE = 237)                           ; TargetLine Number as seen in GEANY

# (ADDITIONAL_LINE_BEFORE_TARGET = 187,193,194,195)          ; Additional added Lines, Numbers as seen in GEANY, BEFORE TargetLine, containing S, F etc....Divided by comma

# (ADDITIONAL_CODE_BEFORE_TARGET = G0 g55 g43 X-24.3888 Y38.3571 f500)  ; Possible to add 1 line of specified code, added before Target line
# (END_FILTER)

# Because the filter output can not be opened with edit file in Axis, open the source file from the filemanager



</pre></body></html>