I have a code to parse through several text files and extract a given part from each one and write them into another text file. I repeat this process over and over again, extracting the next part of the files and writing them on the other text file.
I'm using the variable cube_i
to move from one file to another and the variable layer
to move from one part of the text files to another. However, the loops I created don't go further than the cube_i=0
and layer=0
. Any suggestions on how I can proceed to increment the value of those variables?
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
if layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
if cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
There are a couple of for loops in the code which have been incorrectly written as if statements. The correct code snipped would be :
rgx_start = re.compile(r'Z down') # Second line before the start of the layer code: "G0Z2 ; Z down"
rgx_end = re.compile(r'Layer #') # Second line after the end of the layer code: "; Layer #<integer>"
for layer in range(0, (layers_total + 1)):
final_code.write('; ------------------------------\n')
label = '; Layer #{}\n'.format(layer)
final_code.write(label)
final_code.write('; ------------------------------\n')
with open(first_file,'r') as gcode: # Opening the first file to write the powder drop
for i, line in enumerate(gcode.readlines()):
if i in range(powdrop_start, (powdrop_end + 1)): # Writing the powder drop
final_code.write(line)
cube_i = 0 # Reseting the cube_i variable
for cube_i in range(0, (parts_total + 1)):
label_cube = '; PART #{}\n'.format(cube_i)
final_code.write(label_cube)
# Opening each text file separately
with open(f_path, 'r') as gcode: # f_path contains cube_i
lines = gcode.readlines()
start_i = prev_start[cube_i]
for i, line in enumerate(lines[start_i + 1:]): # Searching for the beginning of given layer
if rgx_start.search(line):
layer_start = start_i + i + 3
break
for i, line in enumerate(lines[layer_start + 1:]): # Searching for the end of given layer
if rgx_end.search(line):
layer_end = layer_start + i - 1
break
for i, line in enumerate(lines[layer_start:layer_end + 1]):
final_code.write(line)
prev_start.append(layer_start)
prev_end.append(layer_end)
cube_i = + 1
if layer == 0: # Writing the zeroth layer footer
with open(first_file, 'r') as gcode:
for i, line in enumerate(gcode.readlines()):
if i == zeroth_ftr:
final_code.write(line)
layer =+ 1
This should work in your case.