cirkuix.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from gi.repository import Gtk
  2. import matplotlib.pyplot as plt
  3. plt.ioff()
  4. from matplotlib.figure import Figure
  5. from numpy import arange, sin, pi
  6. from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
  7. #from matplotlib.backends.backend_gtk3 import NavigationToolbar2GTK3 as NavigationToolbar
  8. #from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
  9. #from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
  10. #import cairo
  11. from camlib import *
  12. class App:
  13. def __init__(self):
  14. self.gladefile = "cirkuix.ui"
  15. self.builder = Gtk.Builder()
  16. self.builder.add_from_file(self.gladefile)
  17. self.window = self.builder.get_object("window1")
  18. self.positionLabel = self.builder.get_object("label3")
  19. #self.drawingarea = self.builder.get_object("drawingarea1")
  20. #self.drawingarea.connect("draw", self.cairopaint)
  21. self.grid = self.builder.get_object("grid1")
  22. self.builder.connect_signals(self)
  23. self.mplpaint()
  24. self.window.show_all()
  25. self.gerbers = []
  26. Gtk.main()
  27. def mplpaint(self):
  28. f = Figure(dpi=50)
  29. a = f.add_subplot(111)
  30. a.set_aspect(1)
  31. t = arange(0.0,5.0,0.01)
  32. s = sin(2*pi*t)
  33. a.plot(t,s)
  34. a.grid()
  35. #a.patch.set_visible(False) Background of the axes
  36. f.patch.set_visible(False)
  37. f.tight_layout()
  38. canvas = FigureCanvas(f) # a Gtk.DrawingArea
  39. canvas.set_size_request(600,400)
  40. canvas.mpl_connect('button_press_event', self.on_click_over_plot)
  41. canvas.mpl_connect('motion_notify_event', self.on_mouse_move_over_plot)
  42. self.grid.attach(canvas,1,1,600,400)
  43. def cairopaint(self, da, cr):
  44. width = 200
  45. height = 200
  46. #cr = widget.window.cairo_create() # Context
  47. cr.set_source_rgb(0.5, 0.5, 0.5)
  48. cr.rectangle(0, 0, width, height)
  49. cr.fill()
  50. # draw a rectangle
  51. cr.set_source_rgb(1.0, 1.0, 1.0)
  52. cr.rectangle(10, 10, width - 20, height - 20)
  53. cr.fill()
  54. # draw lines
  55. cr.set_source_rgb(0.0, 0.0, 0.8)
  56. cr.move_to(width / 3.0, height / 3.0)
  57. cr.rel_line_to(0, height / 6.0)
  58. cr.move_to(2 * width / 3.0, height / 3.0)
  59. cr.rel_line_to(0, height / 6.0)
  60. cr.stroke()
  61. # and a circle
  62. cr.set_source_rgb(1.0, 0.0, 0.0)
  63. radius = min(width, height)
  64. cr.arc(width / 2.0, height / 2.0, radius / 2.0 - 20, 0, 2 * pi)
  65. cr.stroke()
  66. cr.arc(width / 2.0, height / 2.0, radius / 3.0 - 10, pi / 3, 2 * pi / 3)
  67. cr.stroke()
  68. def on_filequit(self, param):
  69. print "quit from menu"
  70. self.window.destroy()
  71. Gtk.main_quit()
  72. def on_closewindow(self, param):
  73. print "quit from X"
  74. self.window.destroy()
  75. Gtk.main_quit()
  76. def on_fileopengeometry(self, param):
  77. print "File->Open Geometry"
  78. dialog = Gtk.FileChooserDialog("Please choose a file", self.window,
  79. Gtk.FileChooserAction.OPEN,
  80. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  81. Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
  82. response = dialog.run()
  83. if response == Gtk.ResponseType.OK:
  84. print("Open clicked")
  85. print("File selected: " + dialog.get_filename())
  86. gerber = Gerber()
  87. gerber.parse_file(dialog.get_filename())
  88. gerber.create_geometry()
  89. self.gerbers.append(gerber)
  90. self.plot_gerber(gerber)
  91. elif response == Gtk.ResponseType.CANCEL:
  92. print("Cancel clicked")
  93. dialog.destroy()
  94. def plot_gerber(self, gerber):
  95. f = Figure(dpi=75)
  96. a = f.add_subplot(111)
  97. a.set_aspect(1)
  98. for poly in gerber.solid_geometry:
  99. x, y = poly.exterior.xy
  100. a.plot(x, y)
  101. for ints in poly.interiors:
  102. x, y = ints.coords.xy
  103. a.plot(x, y)
  104. a.grid()
  105. f.tight_layout()
  106. canvas = FigureCanvas(f) # a Gtk.DrawingArea
  107. canvas.set_size_request(600,400)
  108. self.grid.attach(canvas,1,1,600,400)
  109. self.window.show_all()
  110. def on_mouse_move_over_plot(self, event):
  111. self.positionLabel.set_label("X: %.4f Y: %.4f"%(event.xdata, event.ydata))
  112. def on_click_over_plot(self, event):
  113. print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
  114. event.button, event.x, event.y, event.xdata, event.ydata)
  115. app = App()