spacepaste

  1.  
  2. # create a canvas on top of a blank bitmap
  3. # any canvas drawings can now be saved to a standard image file
  4. # tested with Python27 and wxPython28 by vegaseat 05jan2011
  5. import wxversion
  6. wxversion.select('3.0')
  7. import sys
  8. import os
  9. import wx
  10. class MyFrame(wx.Frame):
  11. def __init__(self, parent=None, id=-1, title=None, gerber=None):
  12. wx.Frame.__init__(self, parent, id, title, size=(380,400))
  13. self.statbmp = wx.StaticBitmap(self)
  14. self.gerber = gerber
  15. self.draw_image()
  16. self.save_image()
  17. #self.Destroy()
  18. def draw_image(self):
  19. if self.gerber:
  20. print self.gerber
  21. if os.path.isfile(os.path.abspath(self.gerber)):
  22. import re
  23. with open(self.gerber, 'r') as f:
  24. lines = f.readlines()
  25. _max_dim = None
  26. _min_dim = None
  27. minx = miny = maxy = maxx = None
  28. pts = []
  29. for line in lines:
  30. m = re.match(r"^X(-?\d+)Y(-?\d+)D(\d+)", line)
  31. if m:
  32. x, y, d = m.groups()
  33. x = int(x)
  34. y = int(y)
  35. d = int(d)
  36. if _max_dim is None:
  37. _min_dim = [x, y]
  38. _max_dim = [x, y]
  39. else:
  40. if x<_min_dim[0]:
  41. _min_dim[0] = x
  42. if y<_min_dim[1]:
  43. _min_dim[1] = y
  44. if x>_max_dim[0]:
  45. _max_dim[0] = x
  46. if y>_max_dim[1]:
  47. _max_dim[1] = y
  48. pts.append((x, y, d))
  49. print 'max [{}] min [{}]'.format(_max_dim, _min_dim)
  50. # select the width and height of the blank bitmap
  51. # should fit the client frame
  52. w, h = _max_dim[0] - _min_dim[0], _max_dim[1] - _min_dim[1]
  53. divisor = 10000
  54. w = w/divisor
  55. h = h/divisor
  56. print 'w, h [{}]'.format((w, h))
  57. # create the blank bitmap as a draw background
  58. draw_bmp = wx.EmptyBitmap(w, h)
  59. # create the canvas on top of the draw_bmp
  60. canvas_dc = wx.MemoryDC(draw_bmp)
  61. # fill the canvas white
  62. canvas_dc.SetBrush(wx.Brush('white'))
  63. canvas_dc.Clear()
  64. # draw a bunch of circles ...
  65. # pen colour
  66. canvas_dc.SetPen(wx.Pen('red', 1))
  67. # fill colour
  68. canvas_dc.SetBrush(wx.Brush('orange'))
  69. last = None
  70. for pt in pts:
  71. x = ((pt[0] - _min_dim[0]) / divisor)
  72. y = ((pt[1] - _min_dim[1]) / divisor)
  73. d = pt[2]
  74. if d == 2:
  75. last = (x, y)
  76. elif d == 1:
  77. print 'DrawLine ({}, {}) ({}, {})'.format(last[0], last[1], x, y)
  78. canvas_dc.DrawLine(last[0], last[1], x, y)
  79. elif d == 3:
  80. canvas_dc.DrawCircle(x, y, 10)
  81. else:
  82. # select the width and height of the blank bitmap
  83. # should fit the client frame
  84. w, h = 340, 340
  85. # create the blank bitmap as a draw background
  86. draw_bmp = wx.EmptyBitmap(w, h)
  87. # create the canvas on top of the draw_bmp
  88. canvas_dc = wx.MemoryDC(draw_bmp)
  89. # fill the canvas white
  90. canvas_dc.SetBrush(wx.Brush('white'))
  91. canvas_dc.Clear()
  92. # draw a bunch of circles ...
  93. # pen colour
  94. canvas_dc.SetPen(wx.Pen('red', 1))
  95. # fill colour
  96. canvas_dc.SetBrush(wx.Brush('orange'))
  97. for x in range(10, 180, 10):
  98. y = x
  99. r = x%30
  100. canvas_dc.DrawCircle(x, y, r)
  101. # now put the canvas drawing into a bitmap to display it
  102. # remember the canvas is on top of the draw_bmp
  103. self.statbmp.SetBitmap(draw_bmp)
  104. def save_image(self):
  105. """save the drawing"""
  106. finished_image = self.statbmp.GetBitmap()
  107. #finished_image.SaveFile("mydrawing.png", wx.BITMAP_TYPE_PNG)
  108. finished_image.SaveFile("mydrawing.jpg", wx.BITMAP_TYPE_JPEG)
  109. app = wx.App(0)
  110. MyFrame(title='canvas draw and save', gerber=sys.argv[1]).Show()
  111. app.MainLoop()
  112. # help(wx.PaintDC)
  113.