cirkuix.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.drawingarea = self.builder.get_object("drawingarea1")
  19. #self.drawingarea.connect("draw", self.cairopaint)
  20. self.grid = self.builder.get_object("grid1")
  21. self.builder.connect_signals(self)
  22. self.mplpaint()
  23. self.window.show_all()
  24. self.gerbers = []
  25. Gtk.main()
  26. def mplpaint(self):
  27. f = Figure(dpi=50)
  28. a = f.add_subplot(111)
  29. a.set_aspect(1)
  30. t = arange(0.0,5.0,0.01)
  31. s = sin(2*pi*t)
  32. a.plot(t,s)
  33. a.grid()
  34. #a.patch.set_visible(False) Background of the axes
  35. f.patch.set_visible(False)
  36. f.tight_layout()
  37. canvas = FigureCanvas(f) # a Gtk.DrawingArea
  38. canvas.set_size_request(600,400)
  39. self.grid.attach(canvas,1,1,600,400)
  40. def cairopaint(self, da, cr):
  41. width = 200
  42. height = 200
  43. #cr = widget.window.cairo_create() # Context
  44. cr.set_source_rgb(0.5, 0.5, 0.5)
  45. cr.rectangle(0, 0, width, height)
  46. cr.fill()
  47. # draw a rectangle
  48. cr.set_source_rgb(1.0, 1.0, 1.0)
  49. cr.rectangle(10, 10, width - 20, height - 20)
  50. cr.fill()
  51. # draw lines
  52. cr.set_source_rgb(0.0, 0.0, 0.8)
  53. cr.move_to(width / 3.0, height / 3.0)
  54. cr.rel_line_to(0, height / 6.0)
  55. cr.move_to(2 * width / 3.0, height / 3.0)
  56. cr.rel_line_to(0, height / 6.0)
  57. cr.stroke()
  58. # and a circle
  59. cr.set_source_rgb(1.0, 0.0, 0.0)
  60. radius = min(width, height)
  61. cr.arc(width / 2.0, height / 2.0, radius / 2.0 - 20, 0, 2 * pi)
  62. cr.stroke()
  63. cr.arc(width / 2.0, height / 2.0, radius / 3.0 - 10, pi / 3, 2 * pi / 3)
  64. cr.stroke()
  65. def on_filequit(self, param):
  66. print "quit from menu"
  67. self.window.destroy()
  68. Gtk.main_quit()
  69. def on_closewindow(self, param):
  70. print "quit from X"
  71. self.window.destroy()
  72. Gtk.main_quit()
  73. def on_fileopengeometry(self, param):
  74. print "File->Open Geometry"
  75. dialog = Gtk.FileChooserDialog("Please choose a file", self.window,
  76. Gtk.FileChooserAction.OPEN,
  77. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  78. Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
  79. response = dialog.run()
  80. if response == Gtk.ResponseType.OK:
  81. print("Open clicked")
  82. print("File selected: " + dialog.get_filename())
  83. gerber = Gerber()
  84. gerber.parse_file(dialog.get_filename())
  85. gerber.create_geometry()
  86. self.gerbers.append(gerber)
  87. self.plot_gerber(gerber)
  88. elif response == Gtk.ResponseType.CANCEL:
  89. print("Cancel clicked")
  90. dialog.destroy()
  91. def plot_gerber(self, gerber):
  92. f = Figure(dpi=75)
  93. a = f.add_subplot(111)
  94. a.set_aspect(1)
  95. for poly in gerber.solid_geometry:
  96. x, y = poly.exterior.xy
  97. a.plot(x, y)
  98. for ints in poly.interiors:
  99. x, y = ints.coords.xy
  100. a.plot(x, y)
  101. a.grid()
  102. f.tight_layout()
  103. canvas = FigureCanvas(f) # a Gtk.DrawingArea
  104. canvas.set_size_request(600,400)
  105. self.grid.attach(canvas,1,1,600,400)
  106. self.window.show_all()
  107. app = App()