Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import re
  5.  
  6. if len(sys.argv) != 2:
  7. print("Usage: dptxt2gcode.py <input.dptxt>")
  8. sys.exit(1)
  9.  
  10. dptxt = open(sys.argv[1], 'r')
  11.  
  12. ## Context
  13. BRUSH_DIA = [1,1]
  14. PEN_DOWN = False
  15.  
  16. _brush_dia_re = re.compile(r'size[hl]?=\d+')
  17.  
  18. ## Print header boilerplate
  19. print("""(BOILERPLATE)
  20.  
  21. G17 G40 G49 G80 G90 G94 G21
  22. G54
  23.  
  24. (Scale values: pixels to mm)
  25. #1 = 0.5 (X scale)
  26. #2 = -#1 (Y scale)
  27. #3 = [-#1/2] (Z scale)
  28.  
  29. #4 = 0 (X offset)
  30. #5 = 0 (Y offset)
  31.  
  32. #6 = 2 (Safe Z height)
  33.  
  34. (BEGIN)
  35. F600
  36. G00 Z#6
  37. """)
  38.  
  39. ## Process recording
  40. for line in dptxt:
  41. line = line.strip()
  42. if not line or line[0] == '#':
  43. continue
  44.  
  45. if line.startswith('ctx '):
  46. # Update brush diameter
  47. sizes = _brush_dia_re.findall(line)
  48. for size in sizes:
  49. k,v = size.split('=')
  50. v = int(v)
  51. if k=='size':
  52. BRUSH_DIA = [v,v]
  53. elif k=='sizeh':
  54. BRUSH_DIA[0] = v
  55. else:
  56. BRUSH_DIA[1] = v
  57.  
  58. elif line.startswith('move '):
  59. # Pen motion
  60. # We don't do anything with the context ID, but it
  61. # could be used for automatic tool changes.
  62. pointstr = line[line.find(' ', 5):]
  63. points = pointstr.split(';')
  64. for pstr in points:
  65. if pstr:
  66. p = pstr.strip().split(' ')
  67. if len(p) == 2:
  68. x,y,z = float(p[0]), float(p[1]), 1.0
  69. else:
  70. x,y,z = (float(v) for v in p)
  71.  
  72. # convert pressure value [0..1] to pixel brush diameter pixel value
  73. z = BRUSH_DIA[1] + (BRUSH_DIA[0]-BRUSH_DIA[1]) * z
  74.  
  75. xy_str = "X[#4 + #1*{:.4f}] Y[#5 + #2*{:.4f}]".format(x,y)
  76. z_str = "Z[#3*{:.4f}]".format(z)
  77. if PEN_DOWN:
  78. print("G01", xy_str, z_str)
  79.  
  80. else:
  81. PEN_DOWN = True
  82. print("G00", xy_str)
  83. print("G01", z_str)
  84.  
  85.  
  86. elif line.startswith("penup "):
  87. # Pen up
  88. # Context ID is ignored. Only single-user recordings are supported.
  89. PEN_DOWN = False
  90. print("G00 Z#6")
  91. print("")
  92.  
  93. ## Print footer boiler plate
  94. print("""
  95. M2
  96. (END)
  97. """)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement