Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import re
- if len(sys.argv) != 2:
- print("Usage: dptxt2gcode.py <input.dptxt>")
- sys.exit(1)
- dptxt = open(sys.argv[1], 'r')
- ## Context
- BRUSH_DIA = [1,1]
- PEN_DOWN = False
- _brush_dia_re = re.compile(r'size[hl]?=\d+')
- ## Print header boilerplate
- print("""(BOILERPLATE)
- G17 G40 G49 G80 G90 G94 G21
- G54
- (Scale values: pixels to mm)
- #1 = 0.5 (X scale)
- #2 = -#1 (Y scale)
- #3 = [-#1/2] (Z scale)
- #4 = 0 (X offset)
- #5 = 0 (Y offset)
- #6 = 2 (Safe Z height)
- (BEGIN)
- F600
- G00 Z#6
- """)
- ## Process recording
- for line in dptxt:
- line = line.strip()
- if not line or line[0] == '#':
- continue
- if line.startswith('ctx '):
- # Update brush diameter
- sizes = _brush_dia_re.findall(line)
- for size in sizes:
- k,v = size.split('=')
- v = int(v)
- if k=='size':
- BRUSH_DIA = [v,v]
- elif k=='sizeh':
- BRUSH_DIA[0] = v
- else:
- BRUSH_DIA[1] = v
- elif line.startswith('move '):
- # Pen motion
- # We don't do anything with the context ID, but it
- # could be used for automatic tool changes.
- pointstr = line[line.find(' ', 5):]
- points = pointstr.split(';')
- for pstr in points:
- if pstr:
- p = pstr.strip().split(' ')
- if len(p) == 2:
- x,y,z = float(p[0]), float(p[1]), 1.0
- else:
- x,y,z = (float(v) for v in p)
- # convert pressure value [0..1] to pixel brush diameter pixel value
- z = BRUSH_DIA[1] + (BRUSH_DIA[0]-BRUSH_DIA[1]) * z
- xy_str = "X[#4 + #1*{:.4f}] Y[#5 + #2*{:.4f}]".format(x,y)
- z_str = "Z[#3*{:.4f}]".format(z)
- if PEN_DOWN:
- print("G01", xy_str, z_str)
- else:
- PEN_DOWN = True
- print("G00", xy_str)
- print("G01", z_str)
- elif line.startswith("penup "):
- # Pen up
- # Context ID is ignored. Only single-user recordings are supported.
- PEN_DOWN = False
- print("G00 Z#6")
- print("")
- ## Print footer boiler plate
- print("""
- M2
- (END)
- """)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement