- # create a canvas on top of a blank bitmap
- # any canvas drawings can now be saved to a standard image file
- # tested with Python27 and wxPython28 by vegaseat 05jan2011
- import wxversion
- wxversion.select('3.0')
- import sys
- import os
- import wx
- class MyFrame(wx.Frame):
- def __init__(self, parent=None, id=-1, title=None, gerber=None):
- wx.Frame.__init__(self, parent, id, title, size=(380,400))
- self.statbmp = wx.StaticBitmap(self)
- self.gerber = gerber
- self.draw_image()
- self.save_image()
- #self.Destroy()
- def draw_image(self):
- if self.gerber:
- print self.gerber
- if os.path.isfile(os.path.abspath(self.gerber)):
- import re
- with open(self.gerber, 'r') as f:
- lines = f.readlines()
- _max_dim = None
- _min_dim = None
- minx = miny = maxy = maxx = None
- pts = []
- for line in lines:
- m = re.match(r"^X(-?\d+)Y(-?\d+)D(\d+)", line)
- if m:
- x, y, d = m.groups()
- x = int(x)
- y = int(y)
- d = int(d)
- if _max_dim is None:
- _min_dim = [x, y]
- _max_dim = [x, y]
- else:
- if x<_min_dim[0]:
- _min_dim[0] = x
- if y<_min_dim[1]:
- _min_dim[1] = y
- if x>_max_dim[0]:
- _max_dim[0] = x
- if y>_max_dim[1]:
- _max_dim[1] = y
- pts.append((x, y, d))
- print 'max [{}] min [{}]'.format(_max_dim, _min_dim)
- # select the width and height of the blank bitmap
- # should fit the client frame
- w, h = _max_dim[0] - _min_dim[0], _max_dim[1] - _min_dim[1]
- divisor = 10000
- w = w/divisor
- h = h/divisor
- print 'w, h [{}]'.format((w, h))
- # create the blank bitmap as a draw background
- draw_bmp = wx.EmptyBitmap(w, h)
- # create the canvas on top of the draw_bmp
- canvas_dc = wx.MemoryDC(draw_bmp)
- # fill the canvas white
- canvas_dc.SetBrush(wx.Brush('white'))
- canvas_dc.Clear()
- # draw a bunch of circles ...
- # pen colour
- canvas_dc.SetPen(wx.Pen('red', 1))
- # fill colour
- canvas_dc.SetBrush(wx.Brush('orange'))
- last = None
- for pt in pts:
- x = ((pt[0] - _min_dim[0]) / divisor)
- y = ((pt[1] - _min_dim[1]) / divisor)
- d = pt[2]
- if d == 2:
- last = (x, y)
- elif d == 1:
- print 'DrawLine ({}, {}) ({}, {})'.format(last[0], last[1], x, y)
- canvas_dc.DrawLine(last[0], last[1], x, y)
- elif d == 3:
- canvas_dc.DrawCircle(x, y, 10)
- else:
- # select the width and height of the blank bitmap
- # should fit the client frame
- w, h = 340, 340
- # create the blank bitmap as a draw background
- draw_bmp = wx.EmptyBitmap(w, h)
- # create the canvas on top of the draw_bmp
- canvas_dc = wx.MemoryDC(draw_bmp)
- # fill the canvas white
- canvas_dc.SetBrush(wx.Brush('white'))
- canvas_dc.Clear()
- # draw a bunch of circles ...
- # pen colour
- canvas_dc.SetPen(wx.Pen('red', 1))
- # fill colour
- canvas_dc.SetBrush(wx.Brush('orange'))
- for x in range(10, 180, 10):
- y = x
- r = x%30
- canvas_dc.DrawCircle(x, y, r)
- # now put the canvas drawing into a bitmap to display it
- # remember the canvas is on top of the draw_bmp
- self.statbmp.SetBitmap(draw_bmp)
- def save_image(self):
- """save the drawing"""
- finished_image = self.statbmp.GetBitmap()
- #finished_image.SaveFile("mydrawing.png", wx.BITMAP_TYPE_PNG)
- finished_image.SaveFile("mydrawing.jpg", wx.BITMAP_TYPE_JPEG)
- app = wx.App(0)
- MyFrame(title='canvas draw and save', gerber=sys.argv[1]).Show()
- app.MainLoop()
- # help(wx.PaintDC)