FlatCAM.py 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://caram.cl/software/flatcam #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. ############################################################
  8. import threading
  9. # TODO: Bundle together. This is just for debugging.
  10. from gi.repository import Gtk
  11. from gi.repository import Gdk
  12. from gi.repository import GdkPixbuf
  13. from gi.repository import GLib
  14. from gi.repository import GObject
  15. import simplejson as json
  16. from matplotlib.figure import Figure
  17. from numpy import arange, sin, pi
  18. from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
  19. from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
  20. from camlib import *
  21. import sys
  22. import urllib
  23. import copy
  24. ########################################
  25. ## FlatCAMObj ##
  26. ########################################
  27. class FlatCAMObj:
  28. """
  29. Base type of objects handled in FlatCAM. These become interactive
  30. in the GUI, can be plotted, and their options can be modified
  31. by the user in their respective forms.
  32. """
  33. # Instance of the application to which these are related.
  34. # The app should set this value.
  35. app = None
  36. def __init__(self, name):
  37. self.options = {"name": name}
  38. self.form_kinds = {"name": "entry_text"} # Kind of form element for each option
  39. self.radios = {} # Name value pairs for radio sets
  40. self.radios_inv = {} # Inverse of self.radios
  41. self.axes = None # Matplotlib axes
  42. self.kind = None # Override with proper name
  43. def setup_axes(self, figure):
  44. """
  45. 1) Creates axes if they don't exist. 2) Clears axes. 3) Attaches
  46. them to figure if not part of the figure. 4) Sets transparent
  47. background. 5) Sets 1:1 scale aspect ratio.
  48. :param figure: A Matplotlib.Figure on which to add/configure axes.
  49. :type figure: matplotlib.figure.Figure
  50. :return: None
  51. :rtype: None
  52. """
  53. if self.axes is None:
  54. print "New axes"
  55. self.axes = figure.add_axes([0.05, 0.05, 0.9, 0.9],
  56. label=self.options["name"])
  57. elif self.axes not in figure.axes:
  58. print "Clearing and attaching axes"
  59. self.axes.cla()
  60. figure.add_axes(self.axes)
  61. else:
  62. print "Clearing Axes"
  63. self.axes.cla()
  64. # Remove all decoration. The app's axes will have
  65. # the ticks and grid.
  66. self.axes.set_frame_on(False) # No frame
  67. self.axes.set_xticks([]) # No tick
  68. self.axes.set_yticks([]) # No ticks
  69. self.axes.patch.set_visible(False) # No background
  70. self.axes.set_aspect(1)
  71. def to_form(self):
  72. """
  73. Copies options to the UI form.
  74. :return: None
  75. """
  76. for option in self.options:
  77. self.set_form_item(option)
  78. def read_form(self):
  79. """
  80. Reads form into ``self.options``.
  81. :return: None
  82. :rtype: None
  83. """
  84. for option in self.options:
  85. self.read_form_item(option)
  86. def build_ui(self):
  87. """
  88. Sets up the UI/form for this object.
  89. :return: None
  90. :rtype: None
  91. """
  92. # Where the UI for this object is drawn
  93. box_selected = self.app.builder.get_object("box_selected")
  94. # Remove anything else in the box
  95. box_children = box_selected.get_children()
  96. for child in box_children:
  97. box_selected.remove(child)
  98. osw = self.app.builder.get_object("offscrwindow_" + self.kind) # offscreenwindow
  99. sw = self.app.builder.get_object("sw_" + self.kind) # scrollwindows
  100. osw.remove(sw) # TODO: Is this needed ?
  101. vp = self.app.builder.get_object("vp_" + self.kind) # Viewport
  102. vp.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(1, 1, 1, 1))
  103. # Put in the UI
  104. box_selected.pack_start(sw, True, True, 0)
  105. entry_name = self.app.builder.get_object("entry_text_" + self.kind + "_name")
  106. entry_name.connect("activate", self.app.on_activate_name)
  107. self.to_form()
  108. sw.show()
  109. def set_form_item(self, option):
  110. """
  111. Copies the specified option to the UI form.
  112. :param option: Name of the option (Key in ``self.options``).
  113. :type option: str
  114. :return: None
  115. """
  116. fkind = self.form_kinds[option]
  117. fname = fkind + "_" + self.kind + "_" + option
  118. if fkind == 'entry_eval' or fkind == 'entry_text':
  119. self.app.builder.get_object(fname).set_text(str(self.options[option]))
  120. return
  121. if fkind == 'cb':
  122. self.app.builder.get_object(fname).set_active(self.options[option])
  123. return
  124. if fkind == 'radio':
  125. self.app.builder.get_object(self.radios_inv[option][self.options[option]]).set_active(True)
  126. return
  127. print "Unknown kind of form item:", fkind
  128. def read_form_item(self, option):
  129. """
  130. Reads the specified option from the UI form into ``self.options``.
  131. :param option: Name of the option.
  132. :type option: str
  133. :return: None
  134. """
  135. fkind = self.form_kinds[option]
  136. fname = fkind + "_" + self.kind + "_" + option
  137. if fkind == 'entry_text':
  138. self.options[option] = self.app.builder.get_object(fname).get_text()
  139. return
  140. if fkind == 'entry_eval':
  141. self.options[option] = self.app.get_eval(fname)
  142. return
  143. if fkind == 'cb':
  144. self.options[option] = self.app.builder.get_object(fname).get_active()
  145. return
  146. if fkind == 'radio':
  147. self.options[option] = self.app.get_radio_value(self.radios[option])
  148. return
  149. print "Unknown kind of form item:", fkind
  150. # def plot(self, figure):
  151. # """
  152. # Extend this method! Sets up axes if needed and
  153. # clears them. Descendants must do the actual plotting.
  154. # """
  155. # # Creates the axes if necessary and sets them up.
  156. # self.setup_axes(figure)
  157. def plot(self):
  158. if self.axes is None or self.axes not in self.app.plotcanvas.figure.axes:
  159. self.axes = self.app.plotcanvas.new_axes(self.options['name'])
  160. if not self.options["plot"]:
  161. self.axes.cla()
  162. self.app.plotcanvas.auto_adjust_axes()
  163. return False
  164. return True
  165. def serialize(self):
  166. """
  167. Returns a representation of the object as a dictionary so
  168. it can be later exported as JSON. Override this method.
  169. :return: Dictionary representing the object
  170. :rtype: dict
  171. """
  172. return
  173. def deserialize(self, obj_dict):
  174. """
  175. Re-builds an object from its serialized version.
  176. :param obj_dict: Dictionary representing a FlatCAMObj
  177. :type obj_dict: dict
  178. :return None
  179. """
  180. return
  181. class FlatCAMGerber(FlatCAMObj, Gerber):
  182. """
  183. Represents Gerber code.
  184. """
  185. def __init__(self, name):
  186. Gerber.__init__(self)
  187. FlatCAMObj.__init__(self, name)
  188. self.kind = "gerber"
  189. # The 'name' is already in self.options from FlatCAMObj
  190. self.options.update({
  191. "plot": True,
  192. "mergepolys": True,
  193. "multicolored": False,
  194. "solid": False,
  195. "isotooldia": 0.4 / 25.4,
  196. "cutoutmargin": 0.2,
  197. "cutoutgapsize": 0.15,
  198. "gaps": "tb",
  199. "noncoppermargin": 0.0,
  200. "bboxmargin": 0.0,
  201. "bboxrounded": False
  202. })
  203. # The 'name' is already in self.form_kinds from FlatCAMObj
  204. self.form_kinds.update({
  205. "plot": "cb",
  206. "mergepolys": "cb",
  207. "multicolored": "cb",
  208. "solid": "cb",
  209. "isotooldia": "entry_eval",
  210. "cutoutmargin": "entry_eval",
  211. "cutoutgapsize": "entry_eval",
  212. "gaps": "radio",
  213. "noncoppermargin": "entry_eval",
  214. "bboxmargin": "entry_eval",
  215. "bboxrounded": "cb"
  216. })
  217. self.radios = {"gaps": {"rb_2tb": "tb", "rb_2lr": "lr", "rb_4": "4"}}
  218. self.radios_inv = {"gaps": {"tb": "rb_2tb", "lr": "rb_2lr", "4": "rb_4"}}
  219. # Attributes to be included in serialization
  220. # Always append to it because it carries contents
  221. # from predecessors.
  222. self.ser_attrs += ['options', 'kind']
  223. def convert_units(self, units):
  224. """
  225. Converts the units of the object by scaling dimensions in all geometry
  226. and options.
  227. :param units: Units to which to convert the object: "IN" or "MM".
  228. :type units: str
  229. :return: None
  230. :rtype: None
  231. """
  232. factor = Gerber.convert_units(self, units)
  233. self.options['isotooldia'] *= factor
  234. self.options['cutoutmargin'] *= factor
  235. self.options['cutoutgapsize'] *= factor
  236. self.options['noncoppermargin'] *= factor
  237. self.options['bboxmargin'] *= factor
  238. def plot(self):
  239. # Does all the required setup and returns False
  240. # if the 'ptint' option is set to False.
  241. if not FlatCAMObj.plot(self):
  242. return
  243. if self.options["mergepolys"]:
  244. geometry = self.solid_geometry
  245. else:
  246. geometry = self.buffered_paths + \
  247. [poly['polygon'] for poly in self.regions] + \
  248. self.flash_geometry
  249. if self.options["multicolored"]:
  250. linespec = '-'
  251. else:
  252. linespec = 'k-'
  253. if self.options["solid"]:
  254. for poly in geometry:
  255. # TODO: Too many things hardcoded.
  256. patch = PolygonPatch(poly,
  257. facecolor="#BBF268",
  258. edgecolor="#006E20",
  259. alpha=0.75,
  260. zorder=2)
  261. self.axes.add_patch(patch)
  262. else:
  263. for poly in geometry:
  264. x, y = poly.exterior.xy
  265. self.axes.plot(x, y, linespec)
  266. for ints in poly.interiors:
  267. x, y = ints.coords.xy
  268. self.axes.plot(x, y, linespec)
  269. self.app.plotcanvas.auto_adjust_axes()
  270. def serialize(self):
  271. return {
  272. "options": self.options,
  273. "kind": self.kind
  274. }
  275. class FlatCAMExcellon(FlatCAMObj, Excellon):
  276. """
  277. Represents Excellon/Drill code.
  278. """
  279. def __init__(self, name):
  280. Excellon.__init__(self)
  281. FlatCAMObj.__init__(self, name)
  282. self.kind = "excellon"
  283. self.options.update({
  284. "plot": True,
  285. "solid": False,
  286. "multicolored": False,
  287. "drillz": -0.1,
  288. "travelz": 0.1,
  289. "feedrate": 5.0,
  290. "toolselection": ""
  291. })
  292. self.form_kinds.update({
  293. "plot": "cb",
  294. "solid": "cb",
  295. "multicolored": "cb",
  296. "drillz": "entry_eval",
  297. "travelz": "entry_eval",
  298. "feedrate": "entry_eval",
  299. "toolselection": "entry_text"
  300. })
  301. # TODO: Document this.
  302. self.tool_cbs = {}
  303. # Attributes to be included in serialization
  304. # Always append to it because it carries contents
  305. # from predecessors.
  306. self.ser_attrs += ['options', 'kind']
  307. def convert_units(self, units):
  308. factor = Excellon.convert_units(self, units)
  309. self.options['drillz'] *= factor
  310. self.options['travelz'] *= factor
  311. self.options['feedrate'] *= factor
  312. def plot(self):
  313. # Does all the required setup and returns False
  314. # if the 'ptint' option is set to False.
  315. if not FlatCAMObj.plot(self):
  316. return
  317. try:
  318. _ = iter(self.solid_geometry)
  319. except TypeError:
  320. self.solid_geometry = [self.solid_geometry]
  321. # Plot excellon (All polygons?)
  322. for geo in self.solid_geometry:
  323. x, y = geo.exterior.coords.xy
  324. self.axes.plot(x, y, 'r-')
  325. for ints in geo.interiors:
  326. x, y = ints.coords.xy
  327. self.axes.plot(x, y, 'g-')
  328. self.app.plotcanvas.auto_adjust_axes()
  329. def show_tool_chooser(self):
  330. win = Gtk.Window()
  331. box = Gtk.Box(spacing=2)
  332. box.set_orientation(Gtk.Orientation(1))
  333. win.add(box)
  334. for tool in self.tools:
  335. self.tool_cbs[tool] = Gtk.CheckButton(label=tool + ": " + str(self.tools[tool]))
  336. box.pack_start(self.tool_cbs[tool], False, False, 1)
  337. button = Gtk.Button(label="Accept")
  338. box.pack_start(button, False, False, 1)
  339. win.show_all()
  340. def on_accept(widget):
  341. win.destroy()
  342. tool_list = []
  343. for tool in self.tool_cbs:
  344. if self.tool_cbs[tool].get_active():
  345. tool_list.append(tool)
  346. self.options["toolselection"] = ", ".join(tool_list)
  347. self.to_form()
  348. button.connect("activate", on_accept)
  349. button.connect("clicked", on_accept)
  350. class FlatCAMCNCjob(FlatCAMObj, CNCjob):
  351. """
  352. Represents G-Code.
  353. """
  354. def __init__(self, name, units="in", kind="generic", z_move=0.1,
  355. feedrate=3.0, z_cut=-0.002, tooldia=0.0):
  356. CNCjob.__init__(self, units=units, kind=kind, z_move=z_move,
  357. feedrate=feedrate, z_cut=z_cut, tooldia=tooldia)
  358. FlatCAMObj.__init__(self, name)
  359. self.kind = "cncjob"
  360. self.options.update({
  361. "plot": True,
  362. "tooldia": 0.4 / 25.4 # 0.4mm in inches
  363. })
  364. self.form_kinds.update({
  365. "plot": "cb",
  366. "tooldia": "entry_eval"
  367. })
  368. # Attributes to be included in serialization
  369. # Always append to it because it carries contents
  370. # from predecessors.
  371. self.ser_attrs += ['options', 'kind']
  372. def plot(self):
  373. # Does all the required setup and returns False
  374. # if the 'ptint' option is set to False.
  375. if not FlatCAMObj.plot(self):
  376. return
  377. self.plot2(self.axes, tooldia=self.options["tooldia"])
  378. self.app.plotcanvas.auto_adjust_axes()
  379. def convert_units(self, units):
  380. factor = CNCjob.convert_units(self, units)
  381. print "FlatCAMCNCjob.convert_units()"
  382. self.options["tooldia"] *= factor
  383. class FlatCAMGeometry(FlatCAMObj, Geometry):
  384. """
  385. Geometric object not associated with a specific
  386. format.
  387. """
  388. def __init__(self, name):
  389. FlatCAMObj.__init__(self, name)
  390. Geometry.__init__(self)
  391. self.kind = "geometry"
  392. self.options.update({
  393. "plot": True,
  394. "solid": False,
  395. "multicolored": False,
  396. "cutz": -0.002,
  397. "travelz": 0.1,
  398. "feedrate": 5.0,
  399. "cnctooldia": 0.4 / 25.4,
  400. "painttooldia": 0.0625,
  401. "paintoverlap": 0.15,
  402. "paintmargin": 0.01
  403. })
  404. self.form_kinds.update({
  405. "plot": "cb",
  406. "solid": "cb",
  407. "multicolored": "cb",
  408. "cutz": "entry_eval",
  409. "travelz": "entry_eval",
  410. "feedrate": "entry_eval",
  411. "cnctooldia": "entry_eval",
  412. "painttooldia": "entry_eval",
  413. "paintoverlap": "entry_eval",
  414. "paintmargin": "entry_eval"
  415. })
  416. # Attributes to be included in serialization
  417. # Always append to it because it carries contents
  418. # from predecessors.
  419. self.ser_attrs += ['options', 'kind']
  420. def scale(self, factor):
  421. """
  422. Scales all geometry by a given factor.
  423. :param factor: Factor by which to scale the object's geometry/
  424. :type factor: float
  425. :return: None
  426. :rtype: None
  427. """
  428. if type(self.solid_geometry) == list:
  429. self.solid_geometry = [affinity.scale(g, factor, factor, origin=(0, 0))
  430. for g in self.solid_geometry]
  431. else:
  432. self.solid_geometry = affinity.scale(self.solid_geometry, factor, factor,
  433. origin=(0, 0))
  434. def offset(self, vect):
  435. """
  436. Offsets all geometry by a given vector/
  437. :param vect: (x, y) vector by which to offset the object's geometry.
  438. :type vect: tuple
  439. :return: None
  440. :rtype: None
  441. """
  442. dx, dy = vect
  443. if type(self.solid_geometry) == list:
  444. self.solid_geometry = [affinity.translate(g, xoff=dx, yoff=dy)
  445. for g in self.solid_geometry]
  446. else:
  447. self.solid_geometry = affinity.translate(self.solid_geometry, xoff=dx, yoff=dy)
  448. def convert_units(self, units):
  449. factor = Geometry.convert_units(self, units)
  450. self.options['cutz'] *= factor
  451. self.options['travelz'] *= factor
  452. self.options['feedrate'] *= factor
  453. self.options['cnctooldia'] *= factor
  454. self.options['painttooldia'] *= factor
  455. self.options['paintmargin'] *= factor
  456. return factor
  457. def plot(self):
  458. """
  459. Plots the object into its axes. If None, of if the axes
  460. are not part of the app's figure, it fetches new ones.
  461. :return: None
  462. """
  463. # Does all the required setup and returns False
  464. # if the 'ptint' option is set to False.
  465. if not FlatCAMObj.plot(self):
  466. return
  467. # Make sure solid_geometry is iterable.
  468. try:
  469. _ = iter(self.solid_geometry)
  470. except TypeError:
  471. self.solid_geometry = [self.solid_geometry]
  472. for geo in self.solid_geometry:
  473. if type(geo) == Polygon:
  474. x, y = geo.exterior.coords.xy
  475. self.axes.plot(x, y, 'r-')
  476. for ints in geo.interiors:
  477. x, y = ints.coords.xy
  478. self.axes.plot(x, y, 'r-')
  479. continue
  480. if type(geo) == LineString or type(geo) == LinearRing:
  481. x, y = geo.coords.xy
  482. self.axes.plot(x, y, 'r-')
  483. continue
  484. if type(geo) == MultiPolygon:
  485. for poly in geo:
  486. x, y = poly.exterior.coords.xy
  487. self.axes.plot(x, y, 'r-')
  488. for ints in poly.interiors:
  489. x, y = ints.coords.xy
  490. self.axes.plot(x, y, 'r-')
  491. continue
  492. print "WARNING: Did not plot:", str(type(geo))
  493. self.app.plotcanvas.auto_adjust_axes()
  494. ########################################
  495. ## App ##
  496. ########################################
  497. class App:
  498. """
  499. The main application class. The constructor starts the GUI.
  500. """
  501. def __init__(self):
  502. """
  503. Starts the application. Takes no parameters.
  504. :return: app
  505. :rtype: App
  506. """
  507. # Needed to interact with the GUI from other threads.
  508. GObject.threads_init()
  509. # GLib.log_set_handler()
  510. #### GUI ####
  511. self.gladefile = "FlatCAM.ui"
  512. self.builder = Gtk.Builder()
  513. self.builder.add_from_file(self.gladefile)
  514. self.window = self.builder.get_object("window1")
  515. self.position_label = self.builder.get_object("label3")
  516. self.grid = self.builder.get_object("grid1")
  517. self.notebook = self.builder.get_object("notebook1")
  518. self.info_label = self.builder.get_object("label_status")
  519. self.progress_bar = self.builder.get_object("progressbar")
  520. self.progress_bar.set_show_text(True)
  521. self.units_label = self.builder.get_object("label_units")
  522. # White (transparent) background on the "Options" tab.
  523. self.builder.get_object("vp_options").override_background_color(Gtk.StateType.NORMAL,
  524. Gdk.RGBA(1, 1, 1, 1))
  525. # Combo box to choose between project and application options.
  526. self.combo_options = self.builder.get_object("combo_options")
  527. self.combo_options.set_active(1)
  528. self.setup_project_list() # The "Project" tab
  529. self.setup_component_editor() # The "Selected" tab
  530. self.setup_toolbar()
  531. #### Event handling ####
  532. self.builder.connect_signals(self)
  533. #### Make plot area ####
  534. # self.figure = None
  535. # self.axes = None
  536. # self.canvas = None
  537. # self.setup_plot()
  538. self.plotcanvas = PlotCanvas(self.grid)
  539. self.plotcanvas.mpl_connect('button_press_event', self.on_click_over_plot)
  540. self.plotcanvas.mpl_connect('motion_notify_event', self.on_mouse_move_over_plot)
  541. self.plotcanvas.mpl_connect('key_press_event', self.on_key_over_plot)
  542. self.axes = self.plotcanvas.axes # TODO: Just for testing
  543. self.figure = self.plotcanvas.figure # TODO: Just for testing
  544. self.canvas = self.plotcanvas.canvas # TODO: Just for testing
  545. self.setup_tooltips()
  546. #### DATA ####
  547. self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
  548. self.setup_obj_classes()
  549. self.stuff = {} # FlatCAMObj's by name
  550. self.mouse = None # Mouse coordinates over plot
  551. # What is selected by the user. It is
  552. # a key if self.stuff
  553. self.selected_item_name = None
  554. # Used to inhibit the on_options_update callback when
  555. # the options are being changed by the program and not the user.
  556. self.options_update_ignore = False
  557. self.toggle_units_ignore = False
  558. self.defaults = {
  559. "units": "in"
  560. } # Application defaults
  561. ## Current Project ##
  562. self.options = {} # Project options
  563. self.project_filename = None
  564. self.form_kinds = {
  565. "units": "radio"
  566. }
  567. self.radios = {"units": {"rb_inch": "IN", "rb_mm": "MM"},
  568. "gerber_gaps": {"rb_app_2tb": "tb", "rb_app_2lr": "lr", "rb_app_4": "4"}}
  569. self.radios_inv = {"units": {"IN": "rb_inch", "MM": "rb_mm"},
  570. "gerber_gaps": {"tb": "rb_app_2tb", "lr": "rb_app_2lr", "4": "rb_app_4"}}
  571. # Options for each kind of FlatCAMObj.
  572. # Example: 'gerber_plot': 'cb'. The widget name would be: 'cb_app_gerber_plot'
  573. for FlatCAMClass in [FlatCAMExcellon, FlatCAMGeometry, FlatCAMGerber, FlatCAMCNCjob]:
  574. obj = FlatCAMClass("no_name")
  575. for option in obj.form_kinds:
  576. self.form_kinds[obj.kind + "_" + option] = obj.form_kinds[option]
  577. # if obj.form_kinds[option] == "radio":
  578. # self.radios.update({obj.kind + "_" + option: obj.radios[option]})
  579. # self.radios_inv.update({obj.kind + "_" + option: obj.radios_inv[option]})
  580. ## Event subscriptions ##
  581. self.plot_click_subscribers = {}
  582. self.plot_mousemove_subscribers = {}
  583. ## Tools ##
  584. self.measure = Measurement(self.axes, self.plot_click_subscribers,
  585. self.plot_mousemove_subscribers,
  586. lambda: self.canvas.queue_draw())
  587. #### Initialization ####
  588. self.load_defaults()
  589. self.options.update(self.defaults) # Copy app defaults to project options
  590. self.options2form() # Populate the app defaults form
  591. self.units_label.set_text("[" + self.options["units"] + "]")
  592. #### Check for updates ####
  593. self.version = 2
  594. t1 = threading.Thread(target=self.versionCheck)
  595. t1.daemon = True
  596. t1.start()
  597. #### For debugging only ###
  598. def someThreadFunc(app_obj):
  599. print "Hello World!"
  600. t = threading.Thread(target=someThreadFunc, args=(self,))
  601. t.daemon = True
  602. t.start()
  603. ########################################
  604. ## START ##
  605. ########################################
  606. self.icon256 = GdkPixbuf.Pixbuf.new_from_file('share/flatcam_icon256.png')
  607. self.icon48 = GdkPixbuf.Pixbuf.new_from_file('share/flatcam_icon48.png')
  608. self.icon16 = GdkPixbuf.Pixbuf.new_from_file('share/flatcam_icon16.png')
  609. Gtk.Window.set_default_icon_list([self.icon16, self.icon48, self.icon256])
  610. self.window.set_title("FlatCAM - Alpha 3 UNSTABLE - Check for updates!")
  611. self.window.set_default_size(900, 600)
  612. self.window.show_all()
  613. def setup_toolbar(self):
  614. toolbar = self.builder.get_object("toolbar_main")
  615. # Zoom fit
  616. zf_ico = Gtk.Image.new_from_file('share/zoom_fit32.png')
  617. zoom_fit = Gtk.ToolButton.new(zf_ico, "")
  618. zoom_fit.connect("clicked", self.on_zoom_fit)
  619. zoom_fit.set_tooltip_markup("Zoom Fit.\n(Click on plot and hit <b>1</b>)")
  620. toolbar.insert(zoom_fit, -1)
  621. # Zoom out
  622. zo_ico = Gtk.Image.new_from_file('share/zoom_out32.png')
  623. zoom_out = Gtk.ToolButton.new(zo_ico, "")
  624. zoom_out.connect("clicked", self.on_zoom_out)
  625. zoom_out.set_tooltip_markup("Zoom Out.\n(Click on plot and hit <b>2</b>)")
  626. toolbar.insert(zoom_out, -1)
  627. # Zoom in
  628. zi_ico = Gtk.Image.new_from_file('share/zoom_in32.png')
  629. zoom_in = Gtk.ToolButton.new(zi_ico, "")
  630. zoom_in.connect("clicked", self.on_zoom_in)
  631. zoom_in.set_tooltip_markup("Zoom In.\n(Click on plot and hit <b>3</b>)")
  632. toolbar.insert(zoom_in, -1)
  633. # Clear plot
  634. cp_ico = Gtk.Image.new_from_file('share/clear_plot32.png')
  635. clear_plot = Gtk.ToolButton.new(cp_ico, "")
  636. clear_plot.connect("clicked", self.on_clear_plots)
  637. clear_plot.set_tooltip_markup("Clear Plot")
  638. toolbar.insert(clear_plot, -1)
  639. # Replot
  640. rp_ico = Gtk.Image.new_from_file('share/replot32.png')
  641. replot = Gtk.ToolButton.new(rp_ico, "")
  642. replot.connect("clicked", self.on_toolbar_replot)
  643. replot.set_tooltip_markup("Re-plot all")
  644. toolbar.insert(replot, -1)
  645. # Delete item
  646. del_ico = Gtk.Image.new_from_file('share/delete32.png')
  647. delete = Gtk.ToolButton.new(del_ico, "")
  648. delete.connect("clicked", self.on_delete)
  649. delete.set_tooltip_markup("Delete selected\nobject.")
  650. toolbar.insert(delete, -1)
  651. def setup_obj_classes(self):
  652. """
  653. Sets up application specifics on the FlatCAMObj class.
  654. :return: None
  655. """
  656. FlatCAMObj.app = self
  657. def setup_project_list(self):
  658. """
  659. Sets up list or Tree where whatever has been loaded or created is
  660. displayed.
  661. :return: None
  662. """
  663. self.store = Gtk.ListStore(str)
  664. self.tree = Gtk.TreeView(self.store)
  665. #self.list = Gtk.ListBox()
  666. self.tree.connect("row_activated", self.on_row_activated)
  667. self.tree_select = self.tree.get_selection()
  668. self.signal_id = self.tree_select.connect("changed", self.on_tree_selection_changed)
  669. renderer = Gtk.CellRendererText()
  670. column = Gtk.TreeViewColumn("Title", renderer, text=0)
  671. self.tree.append_column(column)
  672. self.builder.get_object("box_project").pack_start(self.tree, False, False, 1)
  673. def setup_component_editor(self):
  674. """
  675. Initial configuration of the component editor. Creates
  676. a page titled "Selection" on the notebook on the left
  677. side of the main window.
  678. :return: None
  679. """
  680. box_selected = self.builder.get_object("box_selected")
  681. # Remove anything else in the box
  682. box_children = box_selected.get_children()
  683. for child in box_children:
  684. box_selected.remove(child)
  685. box1 = Gtk.Box(Gtk.Orientation.VERTICAL)
  686. label1 = Gtk.Label("Choose an item from Project")
  687. box1.pack_start(label1, True, False, 1)
  688. box_selected.pack_start(box1, True, True, 0)
  689. #box_selected.show()
  690. box1.show()
  691. label1.show()
  692. def info(self, text):
  693. """
  694. Show text on the status bar.
  695. :param text: Text to display.
  696. :type text: str
  697. :return: None
  698. """
  699. self.info_label.set_text(text)
  700. # def zoom(self, factor, center=None):
  701. # """
  702. # Zooms the plot by factor around a given
  703. # center point. Takes care of re-drawing.
  704. #
  705. # :param factor: Number by which to scale the plot.
  706. # :type factor: float
  707. # :param center: Coordinates [x, y] of the point around which to scale the plot.
  708. # :type center: list
  709. # :return: None
  710. # """
  711. # xmin, xmax = self.axes.get_xlim()
  712. # ymin, ymax = self.axes.get_ylim()
  713. # width = xmax - xmin
  714. # height = ymax - ymin
  715. #
  716. # if center is None:
  717. # center = [(xmin + xmax) / 2.0, (ymin + ymax) / 2.0]
  718. #
  719. # # For keeping the point at the pointer location
  720. # relx = (xmax - center[0]) / width
  721. # rely = (ymax - center[1]) / height
  722. #
  723. # new_width = width / factor
  724. # new_height = height / factor
  725. #
  726. # xmin = center[0] - new_width * (1 - relx)
  727. # xmax = center[0] + new_width * relx
  728. # ymin = center[1] - new_height * (1 - rely)
  729. # ymax = center[1] + new_height * rely
  730. #
  731. # for name in self.stuff:
  732. # self.stuff[name].axes.set_xlim((xmin, xmax))
  733. # self.stuff[name].axes.set_ylim((ymin, ymax))
  734. # self.axes.set_xlim((xmin, xmax))
  735. # self.axes.set_ylim((ymin, ymax))
  736. #
  737. # self.canvas.queue_draw()
  738. def build_list(self):
  739. """
  740. Clears and re-populates the list of objects in currently
  741. in the project.
  742. :return: None
  743. """
  744. print "build_list(): clearing"
  745. self.tree_select.unselect_all()
  746. self.store.clear()
  747. print "repopulating...",
  748. for key in self.stuff:
  749. print key,
  750. self.store.append([key])
  751. print
  752. def get_radio_value(self, radio_set):
  753. """
  754. Returns the radio_set[key] of the radiobutton
  755. whose name is key is active.
  756. :param radio_set: A dictionary containing widget_name: value pairs.
  757. :type radio_set: dict
  758. :return: radio_set[key]
  759. """
  760. for name in radio_set:
  761. if self.builder.get_object(name).get_active():
  762. return radio_set[name]
  763. def plot_all(self):
  764. """
  765. Re-generates all plots from all objects.
  766. :return: None
  767. """
  768. self.plotcanvas.clear()
  769. self.set_progress_bar(0.1, "Re-plotting...")
  770. def thread_func(app_obj):
  771. percentage = 0.1
  772. try:
  773. delta = 0.9 / len(self.stuff)
  774. except ZeroDivisionError:
  775. GLib.timeout_add(300, lambda: app_obj.set_progress_bar(0.0, ""))
  776. return
  777. for i in self.stuff:
  778. self.stuff[i].plot()
  779. percentage += delta
  780. GLib.idle_add(lambda: app_obj.set_progress_bar(percentage, "Re-plotting..."))
  781. app_obj.plotcanvas.auto_adjust_axes()
  782. self.on_zoom_fit(None)
  783. GLib.timeout_add(300, lambda: app_obj.set_progress_bar(0.0, ""))
  784. t = threading.Thread(target=thread_func, args=(self,))
  785. t.daemon = True
  786. t.start()
  787. def get_eval(self, widget_name):
  788. """
  789. Runs eval() on the on the text entry of name 'widget_name'
  790. and returns the results.
  791. :param widget_name: Name of Gtk.Entry
  792. :type widget_name: str
  793. :return: Depends on contents of the entry text.
  794. """
  795. value = self.builder.get_object(widget_name).get_text()
  796. if value == "":
  797. value = "None"
  798. try:
  799. evald = eval(value)
  800. return evald
  801. except:
  802. self.info("Could not evaluate: " + value)
  803. return None
  804. def set_list_selection(self, name):
  805. """
  806. Marks a given object as selected in the list ob objects
  807. in the GUI. This selection will in turn trigger
  808. ``self.on_tree_selection_changed()``.
  809. :param name: Name of the object.
  810. :type name: str
  811. :return: None
  812. """
  813. iter = self.store.get_iter_first()
  814. while iter is not None and self.store[iter][0] != name:
  815. iter = self.store.iter_next(iter)
  816. self.tree_select.unselect_all()
  817. self.tree_select.select_iter(iter)
  818. # Need to return False such that GLib.idle_add
  819. # or .timeout_add do not repear.
  820. return False
  821. def new_object(self, kind, name, initialize):
  822. """
  823. Creates a new specalized FlatCAMObj and attaches it to the application,
  824. this is, updates the GUI accordingly, any other records and plots it.
  825. :param kind: The kind of object to create. One of 'gerber',
  826. 'excellon', 'cncjob' and 'geometry'.
  827. :type kind: str
  828. :param name: Name for the object.
  829. :type name: str
  830. :param initialize: Function to run after creation of the object
  831. but before it is attached to the application. The function is
  832. called with 2 parameters: the new object and the App instance.
  833. :type initialize: function
  834. :return: None
  835. :rtype: None
  836. """
  837. # Check for existing name
  838. if name in self.stuff:
  839. self.info("Rename " + name + " in project first.")
  840. return None
  841. # Create object
  842. classdict = {
  843. "gerber": FlatCAMGerber,
  844. "excellon": FlatCAMExcellon,
  845. "cncjob": FlatCAMCNCjob,
  846. "geometry": FlatCAMGeometry
  847. }
  848. obj = classdict[kind](name)
  849. obj.units = self.options["units"] # TODO: The constructor should look at defaults.
  850. # Initialize as per user request
  851. # User must take care to implement initialize
  852. # in a thread-safe way as is is likely that we
  853. # have been invoked in a separate thread.
  854. #initialize(obj, self)
  855. # Set default options from self.options
  856. for option in self.options:
  857. if option.find(kind + "_") == 0:
  858. oname = option[len(kind)+1:]
  859. obj.options[oname] = self.options[option]
  860. # Initialize as per user request
  861. # User must take care to implement initialize
  862. # in a thread-safe way as is is likely that we
  863. # have been invoked in a separate thread.
  864. initialize(obj, self)
  865. # Check units and convert if necessary
  866. if self.options["units"].upper() != obj.units.upper():
  867. GLib.idle_add(lambda: self.info("Converting units to " + self.options["units"] + "."))
  868. obj.convert_units(self.options["units"])
  869. # Add to our records
  870. self.stuff[name] = obj
  871. # Update GUI list and select it (Thread-safe?)
  872. self.store.append([name])
  873. #self.build_list()
  874. GLib.idle_add(lambda: self.set_list_selection(name))
  875. # TODO: Gtk.notebook.set_current_page is not known to
  876. # TODO: return False. Fix this??
  877. GLib.timeout_add(100, lambda: self.notebook.set_current_page(1))
  878. # Plot
  879. # TODO: (Thread-safe?)
  880. obj.plot()
  881. self.on_zoom_fit(None)
  882. return obj
  883. def set_progress_bar(self, percentage, text=""):
  884. """
  885. Sets the application's progress bar to a given frac_digits and text.
  886. :param percentage: The frac_digits (0.0-1.0) of the progress.
  887. :type percentage: float
  888. :param text: Text to display on the progress bar.
  889. :type text: str
  890. :return: None
  891. """
  892. self.progress_bar.set_text(text)
  893. self.progress_bar.set_fraction(percentage)
  894. return False
  895. def get_current(self):
  896. """
  897. Returns the currently selected FlatCAMObj in the application.
  898. :return: Currently selected FlatCAMObj in the application.
  899. :rtype: FlatCAMObj or None
  900. """
  901. # TODO: Could possibly read the form into the object here.
  902. # But there are some cases when the form for the object
  903. # is not up yet. See on_tree_selection_changed.
  904. try:
  905. return self.stuff[self.selected_item_name]
  906. except:
  907. return None
  908. # def adjust_axes(self, xmin, ymin, xmax, ymax):
  909. # """
  910. # Adjusts axes of all plots while maintaining the use of the whole canvas
  911. # and an aspect ratio to 1:1 between x and y axes. The parameters are an original
  912. # request that will be modified to fit these restrictions.
  913. #
  914. # :param xmin: Requested minimum value for the X axis.
  915. # :type xmin: float
  916. # :param ymin: Requested minimum value for the Y axis.
  917. # :type ymin: float
  918. # :param xmax: Requested maximum value for the X axis.
  919. # :type xmax: float
  920. # :param ymax: Requested maximum value for the Y axis.
  921. # :type ymax: float
  922. # :return: None
  923. # """
  924. # m_x = 15 # pixels
  925. # m_y = 25 # pixels
  926. # width = xmax - xmin
  927. # height = ymax - ymin
  928. # try:
  929. # r = width / height
  930. # except:
  931. # print "ERROR: Height is", height
  932. # return
  933. # Fw, Fh = self.canvas.get_width_height()
  934. # Fr = float(Fw) / Fh
  935. # x_ratio = float(m_x) / Fw
  936. # y_ratio = float(m_y) / Fh
  937. #
  938. # if r > Fr:
  939. # ycenter = (ymin + ymax) / 2.0
  940. # newheight = height * r / Fr
  941. # ymin = ycenter - newheight / 2.0
  942. # ymax = ycenter + newheight / 2.0
  943. # else:
  944. # xcenter = (xmax + ymin) / 2.0
  945. # newwidth = width * Fr / r
  946. # xmin = xcenter - newwidth / 2.0
  947. # xmax = xcenter + newwidth / 2.0
  948. #
  949. # for name in self.stuff:
  950. # if self.stuff[name].axes is None:
  951. # continue
  952. # self.stuff[name].axes.set_xlim((xmin, xmax))
  953. # self.stuff[name].axes.set_ylim((ymin, ymax))
  954. # self.stuff[name].axes.set_position([x_ratio, y_ratio,
  955. # 1 - 2 * x_ratio, 1 - 2 * y_ratio])
  956. # self.axes.set_xlim((xmin, xmax))
  957. # self.axes.set_ylim((ymin, ymax))
  958. # self.axes.set_position([x_ratio, y_ratio,
  959. # 1 - 2 * x_ratio, 1 - 2 * y_ratio])
  960. #
  961. # self.canvas.queue_draw()
  962. def load_defaults(self):
  963. """
  964. Loads the aplication's default settings from defaults.json into
  965. ``self.defaults``.
  966. :return: None
  967. """
  968. try:
  969. f = open("defaults.json")
  970. options = f.read()
  971. f.close()
  972. except:
  973. self.info("ERROR: Could not load defaults file.")
  974. return
  975. try:
  976. defaults = json.loads(options)
  977. except:
  978. e = sys.exc_info()[0]
  979. print e
  980. self.info("ERROR: Failed to parse defaults file.")
  981. return
  982. self.defaults.update(defaults)
  983. def read_form(self):
  984. """
  985. Reads the options form into self.defaults/self.options.
  986. :return: None
  987. :rtype: None
  988. """
  989. combo_sel = self.combo_options.get_active()
  990. options_set = [self.options, self.defaults][combo_sel]
  991. for option in options_set:
  992. self.read_form_item(option, options_set)
  993. def read_form_item(self, name, dest):
  994. """
  995. Reads the value of a form item in the defaults/options form and
  996. saves it to the corresponding dictionary.
  997. :param name: Name of the form item. A key in ``self.defaults`` or
  998. ``self.options``.
  999. :type name: str
  1000. :param dest: Dictionary to which to save the value.
  1001. :type dest: dict
  1002. :return: None
  1003. """
  1004. fkind = self.form_kinds[name]
  1005. fname = fkind + "_" + "app" + "_" + name
  1006. if fkind == 'entry_text':
  1007. dest[name] = self.builder.get_object(fname).get_text()
  1008. return
  1009. if fkind == 'entry_eval':
  1010. dest[name] = self.get_eval(fname)
  1011. return
  1012. if fkind == 'cb':
  1013. dest[name] = self.builder.get_object(fname).get_active()
  1014. return
  1015. if fkind == 'radio':
  1016. dest[name] = self.get_radio_value(self.radios[name])
  1017. return
  1018. print "Unknown kind of form item:", fkind
  1019. def options2form(self):
  1020. """
  1021. Sets the 'Project Options' or 'Application Defaults' form with values from
  1022. ``self.options`` or ``self.defaults``.
  1023. :return: None
  1024. :rtype: None
  1025. """
  1026. # Set the on-change callback to do nothing while we do the changes.
  1027. self.options_update_ignore = True
  1028. self.toggle_units_ignore = True
  1029. combo_sel = self.combo_options.get_active()
  1030. options_set = [self.options, self.defaults][combo_sel]
  1031. for option in options_set:
  1032. self.set_form_item(option, options_set[option])
  1033. self.options_update_ignore = False
  1034. self.toggle_units_ignore = False
  1035. def set_form_item(self, name, value):
  1036. """
  1037. Sets a form item 'name' in the GUI with the given 'value'. The syntax of
  1038. form names in the GUI is <kind>_app_<name>, where kind is one of: rb (radio button),
  1039. cb (check button), entry_eval or entry_text (entry), combo (combo box). name is
  1040. whatever name it's been given. For self.defaults, name is a key in the dictionary.
  1041. :param name: Name of the form field.
  1042. :type name: str
  1043. :param value: The value to set the form field to.
  1044. :type value: Depends on field kind.
  1045. :return: None
  1046. """
  1047. if name not in self.form_kinds:
  1048. print "WARNING: Tried to set unknown option/form item:", name
  1049. return
  1050. fkind = self.form_kinds[name]
  1051. fname = fkind + "_" + "app" + "_" + name
  1052. if fkind == 'entry_eval' or fkind == 'entry_text':
  1053. try:
  1054. self.builder.get_object(fname).set_text(str(value))
  1055. except:
  1056. print "ERROR: Failed to set value of %s to %s" % (fname, str(value))
  1057. return
  1058. if fkind == 'cb':
  1059. try:
  1060. self.builder.get_object(fname).set_active(value)
  1061. except:
  1062. print "ERROR: Failed to set value of %s to %s" % (fname, str(value))
  1063. return
  1064. if fkind == 'radio':
  1065. try:
  1066. self.builder.get_object(self.radios_inv[name][value]).set_active(True)
  1067. except:
  1068. print "ERROR: Failed to set value of %s to %s" % (fname, str(value))
  1069. return
  1070. print "Unknown kind of form item:", fkind
  1071. def save_project(self, filename):
  1072. """
  1073. Saves the current project to the specified file.
  1074. :param filename: Name of the file in which to save.
  1075. :type filename: str
  1076. :return: None
  1077. """
  1078. # Capture the latest changes
  1079. try:
  1080. self.get_current().read_form()
  1081. except:
  1082. pass
  1083. d = {"objs": [self.stuff[o].to_dict() for o in self.stuff],
  1084. "options": self.options}
  1085. try:
  1086. f = open(filename, 'w')
  1087. except:
  1088. print "ERROR: Failed to open file for saving:", filename
  1089. return
  1090. try:
  1091. json.dump(d, f, default=to_dict)
  1092. except:
  1093. print "ERROR: File open but failed to write:", filename
  1094. f.close()
  1095. return
  1096. f.close()
  1097. def open_project(self, filename):
  1098. """
  1099. Loads a project from the specified file.
  1100. :param filename: Name of the file from which to load.
  1101. :type filename: str
  1102. :return: None
  1103. """
  1104. try:
  1105. f = open(filename, 'r')
  1106. except:
  1107. print "WARNING: Failed to open project file:", filename
  1108. return
  1109. try:
  1110. d = json.load(f, object_hook=dict2obj)
  1111. except:
  1112. print "WARNING: Failed to parse project file:", filename
  1113. f.close()
  1114. return
  1115. # Clear the current project
  1116. self.on_file_new(None)
  1117. # Project options
  1118. self.options.update(d['options'])
  1119. self.project_filename = filename
  1120. self.units_label.set_text(self.options["units"])
  1121. # Re create objects
  1122. for obj in d['objs']:
  1123. def obj_init(obj_inst, app_inst):
  1124. obj_inst.from_dict(obj)
  1125. self.new_object(obj['kind'], obj['options']['name'], obj_init)
  1126. self.info("Project loaded from: " + filename)
  1127. def populate_objects_combo(self, combo):
  1128. """
  1129. Populates a Gtk.Comboboxtext with the list of the object in the project.
  1130. :param combo: Name or instance of the comboboxtext.
  1131. :type combo: str or Gtk.ComboBoxText
  1132. :return: None
  1133. """
  1134. print "Populating combo!"
  1135. if type(combo) == str:
  1136. combo = self.builder.get_object(combo)
  1137. combo.remove_all()
  1138. for obj in self.stuff:
  1139. combo.append_text(obj)
  1140. def versionCheck(self):
  1141. """
  1142. Checks for the latest version of the program. Alerts the
  1143. user if theirs is outdated. This method is meant to be run
  1144. in a saeparate thread.
  1145. :return: None
  1146. """
  1147. try:
  1148. f = urllib.urlopen("http://caram.cl/flatcam/VERSION") # TODO: Hardcoded.
  1149. except:
  1150. GLib.idle_add(lambda: self.info("ERROR trying to check for latest version."))
  1151. return
  1152. try:
  1153. data = json.load(f)
  1154. except:
  1155. GLib.idle_add(lambda: self.info("ERROR trying to check for latest version."))
  1156. f.close()
  1157. return
  1158. f.close()
  1159. if self.version >= data["version"]:
  1160. GLib.idle_add(lambda: self.info("FlatCAM is up to date!"))
  1161. return
  1162. label = Gtk.Label("There is a newer version of FlatCAM\n" +
  1163. "available for download:\n\n" +
  1164. data["name"] + "\n\n" + data["message"])
  1165. dialog = Gtk.Dialog("Newer Version Available", self.window, 0,
  1166. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  1167. Gtk.STOCK_OK, Gtk.ResponseType.OK))
  1168. dialog.set_default_size(150, 100)
  1169. dialog.set_modal(True)
  1170. box = dialog.get_content_area()
  1171. box.set_border_width(10)
  1172. box.add(label)
  1173. def do_dialog():
  1174. dialog.show_all()
  1175. response = dialog.run()
  1176. dialog.destroy()
  1177. GLib.idle_add(lambda: do_dialog())
  1178. return
  1179. def setup_tooltips(self):
  1180. tooltips = {
  1181. "cb_gerber_plot": "Plot this object on the main window.",
  1182. "cb_gerber_mergepolys": "Show overlapping polygons as single.",
  1183. "cb_gerber_solid": "Paint inside polygons.",
  1184. "cb_gerber_multicolored": "Draw polygons with different polygons."
  1185. }
  1186. for widget in tooltips:
  1187. self.builder.get_object(widget).set_tooltip_markup(tooltips[widget])
  1188. def do_nothing(self, param):
  1189. return
  1190. ########################################
  1191. ## EVENT HANDLERS ##
  1192. ########################################
  1193. def on_offset_object(self, widget):
  1194. """
  1195. Offsets the object's geometry by the vector specified
  1196. in the form. Re-plots.
  1197. :param widget: Ignored
  1198. :return: None
  1199. """
  1200. obj = self.get_current()
  1201. obj.read_form()
  1202. assert isinstance(obj, FlatCAMObj)
  1203. try:
  1204. vect = self.get_eval("entry_eval_" + obj.kind + "_offset")
  1205. except:
  1206. self.info("ERROR: Vector is not in (x, y) format.")
  1207. return
  1208. assert isinstance(obj, Geometry)
  1209. obj.offset(vect)
  1210. obj.plot()
  1211. return
  1212. def on_cb_plot_toggled(self, widget):
  1213. """
  1214. Callback for toggling the "Plot" checkbox. Re-plots.
  1215. :param widget: Ignored.
  1216. :return: None
  1217. """
  1218. self.get_current().read_form()
  1219. self.get_current().plot()
  1220. def on_about(self, widget):
  1221. """
  1222. Opens the 'About' dialog box.
  1223. :param widget: Ignored.
  1224. :return: None
  1225. """
  1226. about = self.builder.get_object("aboutdialog")
  1227. response = about.run()
  1228. #about.destroy()
  1229. about.hide()
  1230. def on_create_mirror(self, widget):
  1231. """
  1232. Creates a mirror image of a Gerber object to be used as a bottom
  1233. copper layer.
  1234. :param widget: Ignored.
  1235. :return: None
  1236. """
  1237. # Layer to mirror
  1238. gerb_name = self.builder.get_object("comboboxtext_bottomlayer").get_active_text()
  1239. gerb = self.stuff[gerb_name]
  1240. # For now, lets limit to Gerbers.
  1241. assert isinstance(gerb, FlatCAMGerber)
  1242. # Mirror axis "X" or "Y
  1243. axis = self.get_radio_value({"rb_mirror_x": "X",
  1244. "rb_mirror_y": "Y"})
  1245. mode = self.get_radio_value({"rb_mirror_box": "box",
  1246. "rb_mirror_point": "point"})
  1247. if mode == "point": # A single point defines the mirror axis
  1248. # TODO: Error handling
  1249. px, py = eval(self.point_entry.get_text())
  1250. else: # The axis is the line dividing the box in the middle
  1251. name = self.box_combo.get_active_text()
  1252. bb_obj = self.stuff[name]
  1253. xmin, ymin, xmax, ymax = bb_obj.bounds()
  1254. px = 0.5*(xmin+xmax)
  1255. py = 0.5*(ymin+ymax)
  1256. # Do the mirroring
  1257. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  1258. mirrored = affinity.scale(gerb.solid_geometry, xscale, yscale, origin=(px, py))
  1259. def obj_init(obj_inst, app_inst):
  1260. obj_inst.solid_geometry = mirrored
  1261. self.new_object("gerber", gerb.options["name"] + "_mirror", obj_init)
  1262. def on_create_aligndrill(self, widget):
  1263. """
  1264. Creates alignment holes Excellon object. Creates mirror duplicates
  1265. of the specified holes around the specified axis.
  1266. :param widget: Ignored.
  1267. :return: None
  1268. """
  1269. # Mirror axis. Same as in on_create_mirror.
  1270. axis = self.get_radio_value({"rb_mirror_x": "X",
  1271. "rb_mirror_y": "Y"})
  1272. # TODO: Error handling
  1273. mode = self.get_radio_value({"rb_mirror_box": "box",
  1274. "rb_mirror_point": "point"})
  1275. if mode == "point":
  1276. px, py = eval(self.point_entry.get_text())
  1277. else:
  1278. name = self.box_combo.get_active_text()
  1279. bb_obj = self.stuff[name]
  1280. xmin, ymin, xmax, ymax = bb_obj.bounds()
  1281. px = 0.5*(xmin+xmax)
  1282. py = 0.5*(ymin+ymax)
  1283. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  1284. # Tools
  1285. tools = {"1": self.get_eval("entry_dblsided_alignholediam")}
  1286. # Parse hole list
  1287. # TODO: Better parsing
  1288. holes = self.builder.get_object("entry_dblsided_alignholes").get_text()
  1289. holes = eval("[" + holes + "]")
  1290. drills = []
  1291. for hole in holes:
  1292. point = Point(hole)
  1293. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  1294. drills.append({"point": point, "tool": "1"})
  1295. drills.append({"point": point_mirror, "tool": "1"})
  1296. def obj_init(obj_inst, app_inst):
  1297. obj_inst.tools = tools
  1298. obj_inst.drills = drills
  1299. obj_inst.create_geometry()
  1300. self.new_object("excellon", "Alignment Drills", obj_init)
  1301. def on_toggle_pointbox(self, widget):
  1302. """
  1303. Callback for radio selection change between point and box in the
  1304. Double-sided PCB tool. Updates the UI accordingly.
  1305. :param widget: Ignored.
  1306. :return: None
  1307. """
  1308. # Where the entry or combo go
  1309. box = self.builder.get_object("box_pointbox")
  1310. # Clear contents
  1311. children = box.get_children()
  1312. for child in children:
  1313. box.remove(child)
  1314. choice = self.get_radio_value({"rb_mirror_point": "point",
  1315. "rb_mirror_box": "box"})
  1316. if choice == "point":
  1317. self.point_entry = Gtk.Entry()
  1318. self.builder.get_object("box_pointbox").pack_start(self.point_entry,
  1319. False, False, 1)
  1320. self.point_entry.show()
  1321. else:
  1322. self.box_combo = Gtk.ComboBoxText()
  1323. self.builder.get_object("box_pointbox").pack_start(self.box_combo,
  1324. False, False, 1)
  1325. self.populate_objects_combo(self.box_combo)
  1326. self.box_combo.show()
  1327. def on_tools_doublesided(self, param):
  1328. """
  1329. Callback for menu item Tools->Double Sided PCB Tool. Launches the
  1330. tool placing its UI in the "Tool" tab in the notebook.
  1331. :param param: Ignored.
  1332. :return: None
  1333. """
  1334. # Were are we drawing the UI
  1335. box_tool = self.builder.get_object("box_tool")
  1336. # Remove anything else in the box
  1337. box_children = box_tool.get_children()
  1338. for child in box_children:
  1339. box_tool.remove(child)
  1340. # Get the UI
  1341. osw = self.builder.get_object("offscreenwindow_dblsided")
  1342. sw = self.builder.get_object("sw_dblsided")
  1343. osw.remove(sw)
  1344. vp = self.builder.get_object("vp_dblsided")
  1345. vp.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(1, 1, 1, 1))
  1346. # Put in the UI
  1347. box_tool.pack_start(sw, True, True, 0)
  1348. # INITIALIZATION
  1349. # Populate combo box
  1350. self.populate_objects_combo("comboboxtext_bottomlayer")
  1351. # Point entry
  1352. self.point_entry = Gtk.Entry()
  1353. box = self.builder.get_object("box_pointbox")
  1354. for child in box.get_children():
  1355. box.remove(child)
  1356. box.pack_start(self.point_entry, False, False, 1)
  1357. # Show the "Tool" tab
  1358. self.notebook.set_current_page(3)
  1359. sw.show_all()
  1360. def on_toggle_units(self, widget):
  1361. """
  1362. Callback for the Units radio-button change in the Options tab.
  1363. Changes the application's default units or the current project's units.
  1364. If changing the project's units, the change propagates to all of
  1365. the objects in the project.
  1366. :param widget: Ignored.
  1367. :return: None
  1368. """
  1369. if self.toggle_units_ignore:
  1370. return
  1371. combo_sel = self.combo_options.get_active()
  1372. options_set = [self.options, self.defaults][combo_sel]
  1373. # Options to scale
  1374. dimensions = ['gerber_isotooldia', 'gerber_cutoutmargin', 'gerber_cutoutgapsize',
  1375. 'gerber_noncoppermargin', 'gerber_bboxmargin', 'excellon_drillz',
  1376. 'excellon_travelz', 'excellon_feedrate', 'cncjob_tooldia',
  1377. 'geometry_cutz', 'geometry_travelz', 'geometry_feedrate',
  1378. 'geometry_cnctooldia', 'geometry_painttooldia', 'geometry_paintoverlap',
  1379. 'geometry_paintmargin']
  1380. def scale_options(factor):
  1381. for dim in dimensions:
  1382. options_set[dim] *= factor
  1383. # The scaling factor depending on choice of units.
  1384. factor = 1/25.4
  1385. if self.builder.get_object('rb_mm').get_active():
  1386. factor = 25.4
  1387. # App units. Convert without warning.
  1388. if combo_sel == 1:
  1389. self.read_form()
  1390. scale_options(factor)
  1391. self.options2form()
  1392. return
  1393. # Changing project units. Warn user.
  1394. label = Gtk.Label("Changing the units of the project causes all geometrical \n" + \
  1395. "properties of all objects to be scaled accordingly. Continue?")
  1396. dialog = Gtk.Dialog("Changing Project Units", self.window, 0,
  1397. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  1398. Gtk.STOCK_OK, Gtk.ResponseType.OK))
  1399. dialog.set_default_size(150, 100)
  1400. dialog.set_modal(True)
  1401. box = dialog.get_content_area()
  1402. box.set_border_width(10)
  1403. box.add(label)
  1404. dialog.show_all()
  1405. response = dialog.run()
  1406. dialog.destroy()
  1407. if response == Gtk.ResponseType.OK:
  1408. #print "Converting units..."
  1409. #print "Converting options..."
  1410. self.read_form()
  1411. scale_options(factor)
  1412. self.options2form()
  1413. for obj in self.stuff:
  1414. units = self.get_radio_value({"rb_mm": "MM", "rb_inch": "IN"})
  1415. #print "Converting ", obj, " to ", units
  1416. self.stuff[obj].convert_units(units)
  1417. current = self.get_current()
  1418. if current is not None:
  1419. current.to_form()
  1420. self.plot_all()
  1421. else:
  1422. # Undo toggling
  1423. self.toggle_units_ignore = True
  1424. if self.builder.get_object('rb_mm').get_active():
  1425. self.builder.get_object('rb_inch').set_active(True)
  1426. else:
  1427. self.builder.get_object('rb_mm').set_active(True)
  1428. self.toggle_units_ignore = False
  1429. self.read_form()
  1430. self.info("Converted units to %s" % self.options["units"])
  1431. self.units_label.set_text("[" + self.options["units"] + "]")
  1432. def on_file_openproject(self, param):
  1433. """
  1434. Callback for menu item File->Open Project. Opens a file chooser and calls
  1435. ``self.open_project()`` after successful selection of a filename.
  1436. :param param: Ignored.
  1437. :return: None
  1438. """
  1439. def on_success(app_obj, filename):
  1440. app_obj.open_project(filename)
  1441. self.file_chooser_action(on_success)
  1442. def on_file_saveproject(self, param):
  1443. """
  1444. Callback for menu item File->Save Project. Saves the project to
  1445. ``self.project_filename`` or calls ``self.on_file_saveprojectas()``
  1446. if set to None. The project is saved by calling ``self.save_project()``.
  1447. :param param: Ignored.
  1448. :return: None
  1449. """
  1450. if self.project_filename is None:
  1451. self.on_file_saveprojectas(None)
  1452. else:
  1453. self.save_project(self.project_filename)
  1454. self.info("Project saved to: " + self.project_filename)
  1455. def on_file_saveprojectas(self, param):
  1456. """
  1457. Callback for menu item File->Save Project As... Opens a file
  1458. chooser and saves the project to the given file via
  1459. ``self.save_project()``.
  1460. :param param: Ignored.
  1461. :return: None
  1462. """
  1463. def on_success(app_obj, filename):
  1464. assert isinstance(app_obj, App)
  1465. app_obj.save_project(filename)
  1466. self.project_filename = filename
  1467. app_obj.info("Project saved to: " + filename)
  1468. self.file_chooser_save_action(on_success)
  1469. def on_file_saveprojectcopy(self, param):
  1470. """
  1471. Callback for menu item File->Save Project Copy... Opens a file
  1472. chooser and saves the project to the given file via
  1473. ``self.save_project``. It does not update ``self.project_filename`` so
  1474. subsequent save requests are done on the previous known filename.
  1475. :param param: Ignore.
  1476. :return: None
  1477. """
  1478. def on_success(app_obj, filename):
  1479. assert isinstance(app_obj, App)
  1480. app_obj.save_project(filename)
  1481. app_obj.info("Project copy saved to: " + filename)
  1482. self.file_chooser_save_action(on_success)
  1483. def on_options_app2project(self, param):
  1484. """
  1485. Callback for Options->Transfer Options->App=>Project. Copies options
  1486. from application defaults to project defaults.
  1487. :param param: Ignored.
  1488. :return: None
  1489. """
  1490. self.options.update(self.defaults)
  1491. self.options2form() # Update UI
  1492. def on_options_project2app(self, param):
  1493. """
  1494. Callback for Options->Transfer Options->Project=>App. Copies options
  1495. from project defaults to application defaults.
  1496. :param param: Ignored.
  1497. :return: None
  1498. """
  1499. self.defaults.update(self.options)
  1500. self.options2form() # Update UI
  1501. def on_options_project2object(self, param):
  1502. """
  1503. Callback for Options->Transfer Options->Project=>Object. Copies options
  1504. from project defaults to the currently selected object.
  1505. :param param: Ignored.
  1506. :return: None
  1507. """
  1508. obj = self.get_current()
  1509. if obj is None:
  1510. self.info("WARNING: No object selected.")
  1511. return
  1512. for option in self.options:
  1513. if option.find(obj.kind + "_") == 0:
  1514. oname = option[len(obj.kind)+1:]
  1515. obj.options[oname] = self.options[option]
  1516. obj.to_form() # Update UI
  1517. def on_options_object2project(self, param):
  1518. """
  1519. Callback for Options->Transfer Options->Object=>Project. Copies options
  1520. from the currently selected object to project defaults.
  1521. :param param: Ignored.
  1522. :return: None
  1523. """
  1524. obj = self.get_current()
  1525. if obj is None:
  1526. self.info("WARNING: No object selected.")
  1527. return
  1528. obj.read_form()
  1529. for option in obj.options:
  1530. if option in ['name']: # TODO: Handle this better...
  1531. continue
  1532. self.options[obj.kind + "_" + option] = obj.options[option]
  1533. self.options2form() # Update UI
  1534. def on_options_object2app(self, param):
  1535. """
  1536. Callback for Options->Transfer Options->Object=>App. Copies options
  1537. from the currently selected object to application defaults.
  1538. :param param: Ignored.
  1539. :return: None
  1540. """
  1541. obj = self.get_current()
  1542. if obj is None:
  1543. self.info("WARNING: No object selected.")
  1544. return
  1545. obj.read_form()
  1546. for option in obj.options:
  1547. if option in ['name']: # TODO: Handle this better...
  1548. continue
  1549. self.defaults[obj.kind + "_" + option] = obj.options[option]
  1550. self.options2form() # Update UI
  1551. def on_options_app2object(self, param):
  1552. """
  1553. Callback for Options->Transfer Options->App=>Object. Copies options
  1554. from application defaults to the currently selected object.
  1555. :param param: Ignored.
  1556. :return: None
  1557. """
  1558. obj = self.get_current()
  1559. if obj is None:
  1560. self.info("WARNING: No object selected.")
  1561. return
  1562. for option in self.defaults:
  1563. if option.find(obj.kind + "_") == 0:
  1564. oname = option[len(obj.kind)+1:]
  1565. obj.options[oname] = self.defaults[option]
  1566. obj.to_form() # Update UI
  1567. def on_file_savedefaults(self, param):
  1568. """
  1569. Callback for menu item File->Save Defaults. Saves application default options
  1570. ``self.defaults`` to defaults.json.
  1571. :param param: Ignored.
  1572. :return: None
  1573. """
  1574. try:
  1575. f = open("defaults.json")
  1576. options = f.read()
  1577. f.close()
  1578. except:
  1579. self.info("ERROR: Could not load defaults file.")
  1580. return
  1581. try:
  1582. defaults = json.loads(options)
  1583. except:
  1584. e = sys.exc_info()[0]
  1585. print e
  1586. self.info("ERROR: Failed to parse defaults file.")
  1587. return
  1588. assert isinstance(defaults, dict)
  1589. defaults.update(self.defaults)
  1590. try:
  1591. f = open("defaults.json", "w")
  1592. json.dump(defaults, f)
  1593. f.close()
  1594. except:
  1595. self.info("ERROR: Failed to write defaults to file.")
  1596. return
  1597. self.info("Defaults saved.")
  1598. def on_options_combo_change(self, widget):
  1599. """
  1600. Called when the combo box to choose between application defaults and
  1601. project option changes value. The corresponding variables are
  1602. copied to the UI.
  1603. :param widget: The widget from which this was called. Ignore.
  1604. :return: None
  1605. """
  1606. #combo_sel = self.combo_options.get_active()
  1607. #print "Options --> ", combo_sel
  1608. self.options2form()
  1609. def on_options_update(self, widget):
  1610. """
  1611. Called whenever a value in the options/defaults form changes.
  1612. All values are updated. Can be inhibited by setting ``self.options_update_ignore = True``,
  1613. which may be necessary when updating the UI from code and not by the user.
  1614. :param widget: The widget from which this was called. Ignore.
  1615. :return: None
  1616. """
  1617. if self.options_update_ignore:
  1618. return
  1619. self.read_form()
  1620. def on_scale_object(self, widget):
  1621. """
  1622. Callback for request to change an objects geometry scale. The object
  1623. is re-scaled and replotted.
  1624. :param widget: Ignored.
  1625. :return: None
  1626. """
  1627. obj = self.get_current()
  1628. factor = self.get_eval("entry_eval_" + obj.kind + "_scalefactor")
  1629. obj.scale(factor)
  1630. obj.to_form()
  1631. self.on_update_plot(None)
  1632. def on_canvas_configure(self, widget, event):
  1633. """
  1634. Called whenever the canvas changes size. The axes are updated such
  1635. as to use the whole canvas.
  1636. :param widget: Ignored.
  1637. :param event: Ignored.
  1638. :return: None
  1639. """
  1640. # xmin, xmax = self.axes.get_xlim()
  1641. # ymin, ymax = self.axes.get_ylim()
  1642. # self.adjust_axes(xmin, ymin, xmax, ymax)
  1643. self.plotcanvas.auto_adjust_axes()
  1644. def on_row_activated(self, widget, path, col):
  1645. """
  1646. Callback for selection activation (Enter or double-click) on the Project list.
  1647. Switches the notebook page to the object properties form. Calls
  1648. ``self.notebook.set_current_page(1)``.
  1649. :param widget: Ignored.
  1650. :param path: Ignored.
  1651. :param col: Ignored.
  1652. :return: None
  1653. """
  1654. self.notebook.set_current_page(1)
  1655. def on_generate_gerber_bounding_box(self, widget):
  1656. """
  1657. Callback for request from the Gerber form to generate a bounding box for the
  1658. geometry in the object. Creates a FlatCAMGeometry with the bounding box.
  1659. The box can have rounded corners if specified in the form.
  1660. :param widget: Ignored.
  1661. :return: None
  1662. """
  1663. # TODO: Use Gerber.get_bounding_box(...)
  1664. gerber = self.get_current()
  1665. gerber.read_form()
  1666. name = gerber.options["name"] + "_bbox"
  1667. def geo_init(geo_obj, app_obj):
  1668. assert isinstance(geo_obj, FlatCAMGeometry)
  1669. # Bounding box with rounded corners
  1670. bounding_box = gerber.solid_geometry.envelope.buffer(gerber.options["bboxmargin"])
  1671. if not gerber.options["bboxrounded"]: # Remove rounded corners
  1672. bounding_box = bounding_box.envelope
  1673. geo_obj.solid_geometry = bounding_box
  1674. self.new_object("geometry", name, geo_init)
  1675. def on_update_plot(self, widget):
  1676. """
  1677. Callback for button on form for all kinds of objects.
  1678. Re-plots the current object only.
  1679. :param widget: The widget from which this was called.
  1680. :return: None
  1681. """
  1682. obj = self.get_current()
  1683. obj.read_form()
  1684. self.set_progress_bar(0.5, "Plotting...")
  1685. def thread_func(app_obj):
  1686. assert isinstance(app_obj, App)
  1687. #GLib.idle_add(lambda: app_obj.set_progress_bar(0.5, "Plotting..."))
  1688. #GLib.idle_add(lambda: app_obj.get_current().plot(app_obj.figure))
  1689. obj.plot()
  1690. GLib.idle_add(lambda: app_obj.on_zoom_fit(None))
  1691. GLib.timeout_add(300, lambda: app_obj.set_progress_bar(0.0, "Idle"))
  1692. t = threading.Thread(target=thread_func, args=(self,))
  1693. t.daemon = True
  1694. t.start()
  1695. def on_generate_excellon_cncjob(self, widget):
  1696. """
  1697. Callback for button active/click on Excellon form to
  1698. create a CNC Job for the Excellon file.
  1699. :param widget: Ignored
  1700. :return: None
  1701. """
  1702. excellon = self.get_current()
  1703. excellon.read_form()
  1704. job_name = excellon.options["name"] + "_cnc"
  1705. # Object initialization function for app.new_object()
  1706. def job_init(job_obj, app_obj):
  1707. # excellon_ = self.get_current()
  1708. # assert isinstance(excellon_, FlatCAMExcellon)
  1709. assert isinstance(job_obj, FlatCAMCNCjob)
  1710. GLib.idle_add(lambda: app_obj.set_progress_bar(0.2, "Creating CNC Job..."))
  1711. job_obj.z_cut = excellon.options["drillz"]
  1712. job_obj.z_move = excellon.options["travelz"]
  1713. job_obj.feedrate = excellon.options["feedrate"]
  1714. # There could be more than one drill size...
  1715. # job_obj.tooldia = # TODO: duplicate variable!
  1716. # job_obj.options["tooldia"] =
  1717. job_obj.generate_from_excellon_by_tool(excellon, excellon.options["toolselection"])
  1718. GLib.idle_add(lambda: app_obj.set_progress_bar(0.5, "Parsing G-Code..."))
  1719. job_obj.gcode_parse()
  1720. GLib.idle_add(lambda: app_obj.set_progress_bar(0.6, "Creating New Geometry..."))
  1721. job_obj.create_geometry()
  1722. GLib.idle_add(lambda: app_obj.set_progress_bar(0.8, "Plotting..."))
  1723. # To be run in separate thread
  1724. def job_thread(app_obj):
  1725. app_obj.new_object("cncjob", job_name, job_init)
  1726. GLib.idle_add(lambda: app_obj.set_progress_bar(1.0, "Done!"))
  1727. GLib.timeout_add_seconds(1, lambda: app_obj.set_progress_bar(0.0, ""))
  1728. # Start the thread
  1729. t = threading.Thread(target=job_thread, args=(self,))
  1730. t.daemon = True
  1731. t.start()
  1732. def on_excellon_tool_choose(self, widget):
  1733. """
  1734. Callback for button on Excellon form to open up a window for
  1735. selecting tools.
  1736. :param widget: The widget from which this was called.
  1737. :return: None
  1738. """
  1739. excellon = self.get_current()
  1740. assert isinstance(excellon, FlatCAMExcellon)
  1741. excellon.show_tool_chooser()
  1742. def on_entry_eval_activate(self, widget):
  1743. """
  1744. Called when an entry is activated (eg. by hitting enter) if
  1745. set to do so. Its text is eval()'d and set to the returned value.
  1746. The current object is updated.
  1747. :param widget:
  1748. :return:
  1749. """
  1750. self.on_eval_update(widget)
  1751. obj = self.get_current()
  1752. assert isinstance(obj, FlatCAMObj)
  1753. obj.read_form()
  1754. def on_gerber_generate_noncopper(self, widget):
  1755. """
  1756. Callback for button on Gerber form to create a geometry object
  1757. with polygons covering the area without copper or negative of the
  1758. Gerber.
  1759. :param widget: The widget from which this was called.
  1760. :return: None
  1761. """
  1762. gerb = self.get_current()
  1763. gerb.read_form()
  1764. name = gerb.options["name"] + "_noncopper"
  1765. def geo_init(geo_obj, app_obj):
  1766. assert isinstance(geo_obj, FlatCAMGeometry)
  1767. bounding_box = gerb.solid_geometry.envelope.buffer(gerb.options["noncoppermargin"])
  1768. non_copper = bounding_box.difference(gerb.solid_geometry)
  1769. geo_obj.solid_geometry = non_copper
  1770. # TODO: Check for None
  1771. self.new_object("geometry", name, geo_init)
  1772. def on_gerber_generate_cutout(self, widget):
  1773. """
  1774. Callback for button on Gerber form to create geometry with lines
  1775. for cutting off the board.
  1776. :param widget: The widget from which this was called.
  1777. :return: None
  1778. """
  1779. gerb = self.get_current()
  1780. gerb.read_form()
  1781. name = gerb.options["name"] + "_cutout"
  1782. def geo_init(geo_obj, app_obj):
  1783. margin = gerb.options["cutoutmargin"]
  1784. gap_size = gerb.options["cutoutgapsize"]
  1785. minx, miny, maxx, maxy = gerb.bounds()
  1786. minx -= margin
  1787. maxx += margin
  1788. miny -= margin
  1789. maxy += margin
  1790. midx = 0.5 * (minx + maxx)
  1791. midy = 0.5 * (miny + maxy)
  1792. hgap = 0.5 * gap_size
  1793. pts = [[midx - hgap, maxy],
  1794. [minx, maxy],
  1795. [minx, midy + hgap],
  1796. [minx, midy - hgap],
  1797. [minx, miny],
  1798. [midx - hgap, miny],
  1799. [midx + hgap, miny],
  1800. [maxx, miny],
  1801. [maxx, midy - hgap],
  1802. [maxx, midy + hgap],
  1803. [maxx, maxy],
  1804. [midx + hgap, maxy]]
  1805. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  1806. [pts[6], pts[7], pts[10], pts[11]]],
  1807. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  1808. [pts[3], pts[4], pts[7], pts[8]]],
  1809. "4": [[pts[0], pts[1], pts[2]],
  1810. [pts[3], pts[4], pts[5]],
  1811. [pts[6], pts[7], pts[8]],
  1812. [pts[9], pts[10], pts[11]]]}
  1813. cuts = cases[app.get_radio_value({"rb_2tb": "tb", "rb_2lr": "lr", "rb_4": "4"})]
  1814. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  1815. # TODO: Check for None
  1816. self.new_object("geometry", name, geo_init)
  1817. def on_eval_update(self, widget):
  1818. """
  1819. Modifies the content of a Gtk.Entry by running
  1820. eval() on its contents and puting it back as a
  1821. string.
  1822. :param widget: The widget from which this was called.
  1823. :return: None
  1824. """
  1825. # TODO: error handling here
  1826. widget.set_text(str(eval(widget.get_text())))
  1827. def on_generate_isolation(self, widget):
  1828. """
  1829. Callback for button on Gerber form to create isolation routing geometry.
  1830. :param widget: The widget from which this was called.
  1831. :return: None
  1832. """
  1833. gerb = self.get_current()
  1834. gerb.read_form()
  1835. iso_name = gerb.options["name"] + "_iso"
  1836. def iso_init(geo_obj, app_obj):
  1837. # Propagate options
  1838. geo_obj.options["cnctooldia"] = gerb.options["isotooldia"]
  1839. geo_obj.solid_geometry = gerb.isolation_geometry(gerb.options["isotooldia"] / 2.0)
  1840. app_obj.info("Isolation geometry created: %s" % geo_obj.options["name"])
  1841. # TODO: Do something if this is None. Offer changing name?
  1842. self.new_object("geometry", iso_name, iso_init)
  1843. def on_generate_cncjob(self, widget):
  1844. """
  1845. Callback for button on geometry form to generate CNC job.
  1846. :param widget: The widget from which this was called.
  1847. :return: None
  1848. """
  1849. source_geo = self.get_current()
  1850. source_geo.read_form()
  1851. job_name = source_geo.options["name"] + "_cnc"
  1852. # Object initialization function for app.new_object()
  1853. # RUNNING ON SEPARATE THREAD!
  1854. def job_init(job_obj, app_obj):
  1855. assert isinstance(job_obj, FlatCAMCNCjob)
  1856. # Propagate options
  1857. job_obj.options["tooldia"] = source_geo.options["cnctooldia"]
  1858. GLib.idle_add(lambda: app_obj.set_progress_bar(0.2, "Creating CNC Job..."))
  1859. job_obj.z_cut = source_geo.options["cutz"]
  1860. job_obj.z_move = source_geo.options["travelz"]
  1861. job_obj.feedrate = source_geo.options["feedrate"]
  1862. GLib.idle_add(lambda: app_obj.set_progress_bar(0.4, "Analyzing Geometry..."))
  1863. # TODO: The tolerance should not be hard coded. Just for testing.
  1864. job_obj.generate_from_geometry(source_geo, tolerance=0.0005)
  1865. GLib.idle_add(lambda: app_obj.set_progress_bar(0.5, "Parsing G-Code..."))
  1866. job_obj.gcode_parse()
  1867. # TODO: job_obj.create_geometry creates stuff that is not used.
  1868. #GLib.idle_add(lambda: app_obj.set_progress_bar(0.6, "Creating New Geometry..."))
  1869. #job_obj.create_geometry()
  1870. GLib.idle_add(lambda: app_obj.set_progress_bar(0.8, "Plotting..."))
  1871. # To be run in separate thread
  1872. def job_thread(app_obj):
  1873. app_obj.new_object("cncjob", job_name, job_init)
  1874. GLib.idle_add(lambda: app_obj.info("CNCjob created: %s" % job_name))
  1875. GLib.idle_add(lambda: app_obj.set_progress_bar(1.0, "Done!"))
  1876. GLib.timeout_add_seconds(1, lambda: app_obj.set_progress_bar(0.0, ""))
  1877. # Start the thread
  1878. t = threading.Thread(target=job_thread, args=(self,))
  1879. t.daemon = True
  1880. t.start()
  1881. def on_generate_paintarea(self, widget):
  1882. """
  1883. Callback for button on geometry form.
  1884. Subscribes to the "Click on plot" event and continues
  1885. after the click. Finds the polygon containing
  1886. the clicked point and runs clear_poly() on it, resulting
  1887. in a new FlatCAMGeometry object.
  1888. :param widget: The widget from which this was called.
  1889. :return: None
  1890. """
  1891. self.info("Click inside the desired polygon.")
  1892. geo = self.get_current()
  1893. geo.read_form()
  1894. assert isinstance(geo, FlatCAMGeometry)
  1895. tooldia = geo.options["painttooldia"]
  1896. overlap = geo.options["paintoverlap"]
  1897. # To be called after clicking on the plot.
  1898. def doit(event):
  1899. self.plot_click_subscribers.pop("generate_paintarea")
  1900. self.info("")
  1901. point = [event.xdata, event.ydata]
  1902. poly = find_polygon(geo.solid_geometry, point)
  1903. # Initializes the new geometry object
  1904. def gen_paintarea(geo_obj, app_obj):
  1905. assert isinstance(geo_obj, FlatCAMGeometry)
  1906. assert isinstance(app_obj, App)
  1907. cp = clear_poly(poly.buffer(-geo.options["paintmargin"]), tooldia, overlap)
  1908. geo_obj.solid_geometry = cp
  1909. geo_obj.options["cnctooldia"] = tooldia
  1910. name = self.selected_item_name + "_paint"
  1911. self.new_object("geometry", name, gen_paintarea)
  1912. self.plot_click_subscribers["generate_paintarea"] = doit
  1913. def on_cncjob_exportgcode(self, widget):
  1914. """
  1915. Called from button on CNCjob form to save the G-Code from the object.
  1916. :param widget: The widget from which this was called.
  1917. :return: None
  1918. """
  1919. def on_success(app_obj, filename):
  1920. cncjob = app_obj.get_current()
  1921. f = open(filename, 'w')
  1922. f.write(cncjob.gcode)
  1923. f.close()
  1924. app_obj.info("Saved to: " + filename)
  1925. self.file_chooser_save_action(on_success)
  1926. def on_delete(self, widget):
  1927. """
  1928. Delete the currently selected FlatCAMObj.
  1929. :param widget: The widget from which this was called.
  1930. :return: None
  1931. """
  1932. # Keep this for later
  1933. name = copy.copy(self.selected_item_name)
  1934. # Remove plot
  1935. self.plotcanvas.figure.delaxes(self.get_current().axes)
  1936. self.plotcanvas.auto_adjust_axes()
  1937. # Remove from dictionary
  1938. self.stuff.pop(self.selected_item_name)
  1939. # Update UI
  1940. self.build_list() # Update the items list
  1941. self.info("Object deleted: %s" % name)
  1942. def on_toolbar_replot(self, widget):
  1943. """
  1944. Callback for toolbar button. Re-plots all objects.
  1945. :param widget: The widget from which this was called.
  1946. :return: None
  1947. """
  1948. self.get_current().read_form()
  1949. self.plot_all()
  1950. def on_clear_plots(self, widget):
  1951. """
  1952. Callback for toolbar button. Clears all plots.
  1953. :param widget: The widget from which this was called.
  1954. :return: None
  1955. """
  1956. self.plotcanvas.clear()
  1957. def on_activate_name(self, entry):
  1958. """
  1959. Hitting 'Enter' after changing the name of an item
  1960. updates the item dictionary and re-builds the item list.
  1961. :param entry: The widget from which this was called.
  1962. :return: None
  1963. """
  1964. # Disconnect event listener
  1965. self.tree.get_selection().disconnect(self.signal_id)
  1966. new_name = entry.get_text() # Get from form
  1967. self.stuff[new_name] = self.stuff.pop(self.selected_item_name) # Update dictionary
  1968. self.stuff[new_name].options["name"] = new_name # update object
  1969. self.info('Name change: ' + self.selected_item_name + " to " + new_name)
  1970. self.selected_item_name = new_name # Update selection name
  1971. self.build_list() # Update the items list
  1972. # Reconnect event listener
  1973. self.signal_id = self.tree.get_selection().connect(
  1974. "changed", self.on_tree_selection_changed)
  1975. def on_tree_selection_changed(self, selection):
  1976. """
  1977. Callback for selection change in the project list. This changes
  1978. the currently selected FlatCAMObj.
  1979. :param selection: Selection associated to the project tree or list
  1980. :type selection: Gtk.TreeSelection
  1981. :return: None
  1982. """
  1983. print "DEBUG: on_tree_selection_change(): ",
  1984. model, treeiter = selection.get_selected()
  1985. if treeiter is not None:
  1986. # Save data for previous selection
  1987. obj = self.get_current()
  1988. if obj is not None:
  1989. obj.read_form()
  1990. print "DEBUG: You selected", model[treeiter][0]
  1991. self.selected_item_name = model[treeiter][0]
  1992. obj_new = self.get_current()
  1993. if obj_new is not None:
  1994. GLib.idle_add(lambda: obj_new.build_ui())
  1995. else:
  1996. print "DEBUG: Nothing selected"
  1997. self.selected_item_name = None
  1998. self.setup_component_editor()
  1999. def on_file_new(self, param):
  2000. """
  2001. Callback for menu item File->New. Returns the application to its
  2002. startup state.
  2003. :param param: Whatever is passed by the event. Ignore.
  2004. :return: None
  2005. """
  2006. # Remove everythong from memory
  2007. # Clear plot
  2008. self.plotcanvas.clear()
  2009. # Clear object editor
  2010. #self.setup_component_editor()
  2011. # Clear data
  2012. self.stuff = {}
  2013. # Clear list
  2014. #self.tree_select.unselect_all()
  2015. self.build_list()
  2016. # Clear project filename
  2017. self.project_filename = None
  2018. # Re-fresh project options
  2019. self.on_options_app2project(None)
  2020. def on_filequit(self, param):
  2021. """
  2022. Callback for menu item File->Quit. Closes the application.
  2023. :param param: Whatever is passed by the event. Ignore.
  2024. :return: None
  2025. """
  2026. self.window.destroy()
  2027. Gtk.main_quit()
  2028. def on_closewindow(self, param):
  2029. """
  2030. Callback for closing the main window.
  2031. :param param: Whatever is passed by the event. Ignore.
  2032. :return: None
  2033. """
  2034. self.window.destroy()
  2035. Gtk.main_quit()
  2036. def file_chooser_action(self, on_success):
  2037. """
  2038. Opens the file chooser and runs on_success on a separate thread
  2039. upon completion of valid file choice.
  2040. :param on_success: A function to run upon completion of a valid file
  2041. selection. Takes 2 parameters: The app instance and the filename.
  2042. Note that it is run on a separate thread, therefore it must take the
  2043. appropriate precautions when accessing shared resources.
  2044. :type on_success: func
  2045. :return: None
  2046. """
  2047. dialog = Gtk.FileChooserDialog("Please choose a file", self.window,
  2048. Gtk.FileChooserAction.OPEN,
  2049. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  2050. Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
  2051. response = dialog.run()
  2052. if response == Gtk.ResponseType.OK:
  2053. filename = dialog.get_filename()
  2054. dialog.destroy()
  2055. t = threading.Thread(target=on_success, args=(self, filename))
  2056. t.daemon = True
  2057. t.start()
  2058. #on_success(self, filename)
  2059. elif response == Gtk.ResponseType.CANCEL:
  2060. self.info("Open cancelled.") # print("Cancel clicked")
  2061. dialog.destroy()
  2062. def file_chooser_save_action(self, on_success):
  2063. """
  2064. Opens the file chooser and runs on_success upon completion of valid file choice.
  2065. :param on_success: A function to run upon selection of a filename. Takes 2
  2066. parameters: The instance of the application (App) and the chosen filename. This
  2067. gets run immediately in the same thread.
  2068. :return: None
  2069. """
  2070. dialog = Gtk.FileChooserDialog("Save file", self.window,
  2071. Gtk.FileChooserAction.SAVE,
  2072. (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  2073. Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
  2074. dialog.set_current_name("Untitled")
  2075. response = dialog.run()
  2076. if response == Gtk.ResponseType.OK:
  2077. filename = dialog.get_filename()
  2078. dialog.destroy()
  2079. on_success(self, filename)
  2080. elif response == Gtk.ResponseType.CANCEL:
  2081. self.info("Save cancelled.") # print("Cancel clicked")
  2082. dialog.destroy()
  2083. def on_fileopengerber(self, param):
  2084. """
  2085. Callback for menu item File->Open Gerber. Defines a function that is then passed
  2086. to ``self.file_chooser_action()``. It requests the creation of a FlatCAMGerber object
  2087. and updates the progress bar throughout the process.
  2088. :param param: Ignore
  2089. :return: None
  2090. """
  2091. # IMPORTANT: on_success will run on a separate thread. Use
  2092. # GLib.idle_add(function, **kwargs) to launch actions that will
  2093. # updata the GUI.
  2094. def on_success(app_obj, filename):
  2095. assert isinstance(app_obj, App)
  2096. GLib.idle_add(lambda: app_obj.set_progress_bar(0.1, "Opening Gerber ..."))
  2097. def obj_init(gerber_obj, app_obj):
  2098. assert isinstance(gerber_obj, FlatCAMGerber)
  2099. GLib.idle_add(lambda: app_obj.set_progress_bar(0.2, "Parsing ..."))
  2100. gerber_obj.parse_file(filename)
  2101. GLib.idle_add(lambda: app_obj.set_progress_bar(0.5, "Creating Geometry ..."))
  2102. gerber_obj.create_geometry()
  2103. GLib.idle_add(lambda: app_obj.set_progress_bar(0.6, "Plotting ..."))
  2104. name = filename.split('/')[-1].split('\\')[-1]
  2105. app_obj.new_object("gerber", name, obj_init)
  2106. GLib.idle_add(lambda: app_obj.set_progress_bar(1.0, "Done!"))
  2107. GLib.timeout_add_seconds(1, lambda: app_obj.set_progress_bar(0.0, ""))
  2108. # on_success gets run on a separate thread
  2109. self.file_chooser_action(on_success)
  2110. def on_fileopenexcellon(self, param):
  2111. """
  2112. Callback for menu item File->Open Excellon. Defines a function that is then passed
  2113. to ``self.file_chooser_action()``. It requests the creation of a FlatCAMExcellon object
  2114. and updates the progress bar throughout the process.
  2115. :param param: Ignore
  2116. :return: None
  2117. """
  2118. # IMPORTANT: on_success will run on a separate thread. Use
  2119. # GLib.idle_add(function, **kwargs) to launch actions that will
  2120. # updata the GUI.
  2121. def on_success(app_obj, filename):
  2122. assert isinstance(app_obj, App)
  2123. GLib.idle_add(lambda: app_obj.set_progress_bar(0.1, "Opening Excellon ..."))
  2124. def obj_init(excellon_obj, app_obj):
  2125. GLib.idle_add(lambda: app_obj.set_progress_bar(0.2, "Parsing ..."))
  2126. excellon_obj.parse_file(filename)
  2127. excellon_obj.create_geometry()
  2128. GLib.idle_add(lambda: app_obj.set_progress_bar(0.6, "Plotting ..."))
  2129. name = filename.split('/')[-1].split('\\')[-1]
  2130. app_obj.new_object("excellon", name, obj_init)
  2131. GLib.idle_add(lambda: app_obj.set_progress_bar(1.0, "Done!"))
  2132. GLib.timeout_add_seconds(1, lambda: app_obj.set_progress_bar(0.0, ""))
  2133. # on_success gets run on a separate thread
  2134. self.file_chooser_action(on_success)
  2135. def on_fileopengcode(self, param):
  2136. """
  2137. Callback for menu item File->Open G-Code. Defines a function that is then passed
  2138. to ``self.file_chooser_action()``. It requests the creation of a FlatCAMCNCjob object
  2139. and updates the progress bar throughout the process.
  2140. :param param: Ignore
  2141. :return: None
  2142. """
  2143. # IMPORTANT: on_success will run on a separate thread. Use
  2144. # GLib.idle_add(function, **kwargs) to launch actions that will
  2145. # updata the GUI.
  2146. def on_success(app_obj, filename):
  2147. assert isinstance(app_obj, App)
  2148. def obj_init(job_obj, app_obj_):
  2149. """
  2150. :type app_obj_: App
  2151. """
  2152. assert isinstance(app_obj_, App)
  2153. GLib.idle_add(lambda: app_obj_.set_progress_bar(0.1, "Opening G-Code ..."))
  2154. f = open(filename)
  2155. gcode = f.read()
  2156. f.close()
  2157. job_obj.gcode = gcode
  2158. GLib.idle_add(lambda: app_obj_.set_progress_bar(0.2, "Parsing ..."))
  2159. job_obj.gcode_parse()
  2160. GLib.idle_add(lambda: app_obj_.set_progress_bar(0.6, "Creating geometry ..."))
  2161. job_obj.create_geometry()
  2162. GLib.idle_add(lambda: app_obj_.set_progress_bar(0.6, "Plotting ..."))
  2163. name = filename.split('/')[-1].split('\\')[-1]
  2164. app_obj.new_object("cncjob", name, obj_init)
  2165. GLib.idle_add(lambda: app_obj.set_progress_bar(1.0, "Done!"))
  2166. GLib.timeout_add_seconds(1, lambda: app_obj.set_progress_bar(0.0, ""))
  2167. # on_success gets run on a separate thread
  2168. self.file_chooser_action(on_success)
  2169. def on_mouse_move_over_plot(self, event):
  2170. """
  2171. Callback for the mouse motion event over the plot. This event is generated
  2172. by the Matplotlib backend and has been registered in ``self.__init__()``.
  2173. For details, see: http://matplotlib.org/users/event_handling.html
  2174. :param event: Contains information about the event.
  2175. :return: None
  2176. """
  2177. try: # May fail in case mouse not within axes
  2178. self.position_label.set_label("X: %.4f Y: %.4f" % (
  2179. event.xdata, event.ydata))
  2180. self.mouse = [event.xdata, event.ydata]
  2181. for subscriber in self.plot_mousemove_subscribers:
  2182. self.plot_mousemove_subscribers[subscriber](event)
  2183. except:
  2184. self.position_label.set_label("")
  2185. self.mouse = None
  2186. def on_click_over_plot(self, event):
  2187. """
  2188. Callback for the mouse click event over the plot. This event is generated
  2189. by the Matplotlib backend and has been registered in ``self.__init__()``.
  2190. For details, see: http://matplotlib.org/users/event_handling.html
  2191. Default actions are:
  2192. * Copy coordinates to clipboard. Ex.: (65.5473, -13.2679)
  2193. :param event: Contains information about the event, like which button
  2194. was clicked, the pixel coordinates and the axes coordinates.
  2195. :return: None
  2196. """
  2197. # For key presses
  2198. self.canvas.grab_focus()
  2199. try:
  2200. print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (
  2201. event.button, event.x, event.y, event.xdata, event.ydata)
  2202. # TODO: This custom subscription mechanism is probably not necessary.
  2203. for subscriber in self.plot_click_subscribers:
  2204. self.plot_click_subscribers[subscriber](event)
  2205. self.clipboard.set_text("(%.4f, %.4f)" % (event.xdata, event.ydata), -1)
  2206. except Exception, e:
  2207. print "Outside plot!"
  2208. def on_zoom_in(self, event):
  2209. """
  2210. Callback for zoom-in request. This can be either from the corresponding
  2211. toolbar button or the '3' key when the canvas is focused. Calls ``self.zoom()``.
  2212. :param event: Ignored.
  2213. :return: None
  2214. """
  2215. self.plotcanvas.zoom(1.5)
  2216. return
  2217. def on_zoom_out(self, event):
  2218. """
  2219. Callback for zoom-out request. This can be either from the corresponding
  2220. toolbar button or the '2' key when the canvas is focused. Calls ``self.zoom()``.
  2221. :param event: Ignored.
  2222. :return: None
  2223. """
  2224. self.plotcanvas.zoom(1 / 1.5)
  2225. def on_zoom_fit(self, event):
  2226. """
  2227. Callback for zoom-out request. This can be either from the corresponding
  2228. toolbar button or the '1' key when the canvas is focused. Calls ``self.adjust_axes()``
  2229. with axes limits from the geometry bounds of all objects.
  2230. :param event: Ignored.
  2231. :return: None
  2232. """
  2233. xmin, ymin, xmax, ymax = get_bounds(self.stuff)
  2234. width = xmax - xmin
  2235. height = ymax - ymin
  2236. xmin -= 0.05 * width
  2237. xmax += 0.05 * width
  2238. ymin -= 0.05 * height
  2239. ymax += 0.05 * height
  2240. self.plotcanvas.adjust_axes(xmin, ymin, xmax, ymax)
  2241. def on_key_over_plot(self, event):
  2242. """
  2243. Callback for the key pressed event when the canvas is focused. Keyboard
  2244. shortcuts are handled here. So far, these are the shortcuts:
  2245. ========== ============================================
  2246. Key Action
  2247. ========== ============================================
  2248. '1' Zoom-fit. Fits the axes limits to the data.
  2249. '2' Zoom-out.
  2250. '3' Zoom-in.
  2251. ========== ============================================
  2252. :param event: Ignored.
  2253. :return: None
  2254. """
  2255. #print 'you pressed', event.key, event.xdata, event.ydata
  2256. if event.key == '1': # 1
  2257. self.on_zoom_fit(None)
  2258. return
  2259. if event.key == '2': # 2
  2260. self.plotcanvas.zoom(1 / 1.5, self.mouse)
  2261. return
  2262. if event.key == '3': # 3
  2263. self.plotcanvas.zoom(1.5, self.mouse)
  2264. return
  2265. if event.key == 'm':
  2266. if self.measure.toggle_active():
  2267. self.info("Measuring tool ON")
  2268. else:
  2269. self.info("Measuring tool OFF")
  2270. return
  2271. class Measurement:
  2272. def __init__(self, axes, click_subscibers, move_subscribers, update=None):
  2273. self.update = update
  2274. self.axes = axes
  2275. self.click_subscribers = click_subscibers
  2276. self.move_subscribers = move_subscribers
  2277. self.point1 = None
  2278. self.point2 = None
  2279. self.active = False
  2280. self.at = None # AnchoredText object on plot
  2281. def toggle_active(self):
  2282. if self.active:
  2283. self.active = False
  2284. self.move_subscribers.pop("meas")
  2285. self.click_subscribers.pop("meas")
  2286. self.at.remove()
  2287. if self.update is not None:
  2288. self.update()
  2289. return False
  2290. else:
  2291. self.active = True
  2292. self.click_subscribers["meas"] = self.on_click
  2293. self.move_subscribers["meas"] = self.on_move
  2294. return True
  2295. def on_move(self, event):
  2296. try:
  2297. self.at.remove()
  2298. except:
  2299. pass
  2300. if self.point1 is None:
  2301. self.at = AnchoredText("Click on a reference point...")
  2302. else:
  2303. dx = event.xdata - self.point1[0]
  2304. dy = event.ydata - self.point1[1]
  2305. d = sqrt(dx**2 + dy**2)
  2306. self.at = AnchoredText("D = %.4f\nD(x) = %.4f\nD(y) = %.4f" % (d, dx, dy),
  2307. loc=2, prop={'size': 14}, frameon=False)
  2308. self.axes.add_artist(self.at)
  2309. if self.update is not None:
  2310. self.update()
  2311. def on_click(self, event):
  2312. if self.point1 is None:
  2313. self.point1 = (event.xdata, event.ydata)
  2314. return
  2315. else:
  2316. self.point2 = copy.copy(self.point1)
  2317. self.point1 = (event.xdata, event.ydata)
  2318. class PlotCanvas:
  2319. """
  2320. Class handling the plotting area in the application.
  2321. """
  2322. def __init__(self, container):
  2323. """
  2324. The constructor configures the Matplotlib figure that
  2325. will contain all plots, creates the base axes and connects
  2326. events to the plotting area.
  2327. :param container: The parent container in which to draw plots.
  2328. :rtype: PlotCanvas
  2329. """
  2330. # Options
  2331. self.x_margin = 15 # pixels
  2332. self.y_margin = 25 # Pixels
  2333. # Parent container
  2334. self.container = container
  2335. # Plots go onto a single matplotlib.figure
  2336. self.figure = Figure(dpi=50) # TODO: dpi needed?
  2337. self.figure.patch.set_visible(False)
  2338. # These axes show the ticks and grid. No plotting done here.
  2339. # New axes must have a label, otherwise mpl returns an existing one.
  2340. self.axes = self.figure.add_axes([0.05, 0.05, 0.9, 0.9], label="base", alpha=0.0)
  2341. self.axes.set_aspect(1)
  2342. self.axes.grid(True)
  2343. # The canvas is the top level container (Gtk.DrawingArea)
  2344. self.canvas = FigureCanvas(self.figure)
  2345. self.canvas.set_hexpand(1)
  2346. self.canvas.set_vexpand(1)
  2347. self.canvas.set_can_focus(True) # For key press
  2348. # Attach to parent
  2349. self.container.attach(self.canvas, 0, 0, 600, 400)
  2350. # Events
  2351. self.canvas.mpl_connect('motion_notify_event', self.on_mouse_move)
  2352. self.canvas.connect('configure-event', self.auto_adjust_axes)
  2353. self.canvas.add_events(Gdk.EventMask.SMOOTH_SCROLL_MASK)
  2354. self.canvas.connect("scroll-event", self.on_scroll)
  2355. self.mouse = [0,0]
  2356. def mpl_connect(self, event_name, callback):
  2357. """
  2358. Attach an event handler to the canvas through the Matplotlib interface.
  2359. :param event_name: Name of the event
  2360. :type event_name: str
  2361. :param callback: Function to call
  2362. :type callback: func
  2363. :return: Nothing
  2364. """
  2365. self.canvas.mpl_connect(event_name, callback)
  2366. def connect(self, event_name, callback):
  2367. """
  2368. Attach an event handler to the canvas through the native GTK interface.
  2369. :param event_name: Name of the event
  2370. :type event_name: str
  2371. :param callback: Function to call
  2372. :type callback: function
  2373. :return: Nothing
  2374. """
  2375. self.canvas.connect(event_name, callback)
  2376. def clear(self):
  2377. """
  2378. Clears axes and figure.
  2379. :return: None
  2380. """
  2381. # Clear
  2382. self.axes.cla()
  2383. self.figure.clf()
  2384. # Re-build
  2385. self.figure.add_axes(self.axes)
  2386. self.axes.set_aspect(1)
  2387. self.axes.grid(True)
  2388. # Re-draw
  2389. self.canvas.queue_draw()
  2390. def adjust_axes(self, xmin, ymin, xmax, ymax):
  2391. """
  2392. Adjusts all axes while maintaining the use of the whole canvas
  2393. and an aspect ratio to 1:1 between x and y axes. The parameters are an original
  2394. request that will be modified to fit these restrictions.
  2395. :param xmin: Requested minimum value for the X axis.
  2396. :type xmin: float
  2397. :param ymin: Requested minimum value for the Y axis.
  2398. :type ymin: float
  2399. :param xmax: Requested maximum value for the X axis.
  2400. :type xmax: float
  2401. :param ymax: Requested maximum value for the Y axis.
  2402. :type ymax: float
  2403. :return: None
  2404. """
  2405. width = xmax - xmin
  2406. height = ymax - ymin
  2407. try:
  2408. r = width / height
  2409. except:
  2410. print "ERROR: Height is", height
  2411. return
  2412. canvas_w, canvas_h = self.canvas.get_width_height()
  2413. canvas_r = float(canvas_w) / canvas_h
  2414. x_ratio = float(self.x_margin) / canvas_w
  2415. y_ratio = float(self.y_margin) / canvas_h
  2416. if r > canvas_r:
  2417. ycenter = (ymin + ymax) / 2.0
  2418. newheight = height * r / canvas_r
  2419. ymin = ycenter - newheight / 2.0
  2420. ymax = ycenter + newheight / 2.0
  2421. else:
  2422. xcenter = (xmax + ymin) / 2.0
  2423. newwidth = width * canvas_r / r
  2424. xmin = xcenter - newwidth / 2.0
  2425. xmax = xcenter + newwidth / 2.0
  2426. # Adjust axes
  2427. for ax in self.figure.get_axes():
  2428. if ax._label != 'base':
  2429. ax.set_frame_on(False) # No frame
  2430. ax.set_xticks([]) # No tick
  2431. ax.set_yticks([]) # No ticks
  2432. ax.patch.set_visible(False) # No background
  2433. ax.set_aspect(1)
  2434. ax.set_xlim((xmin, xmax))
  2435. ax.set_ylim((ymin, ymax))
  2436. ax.set_position([x_ratio, y_ratio, 1 - 2 * x_ratio, 1 - 2 * y_ratio])
  2437. # Re-draw
  2438. self.canvas.queue_draw()
  2439. def auto_adjust_axes(self, *args):
  2440. """
  2441. Calls ``adjust_axes()`` using the extents of the base axes.
  2442. :rtype : None
  2443. :return: None
  2444. """
  2445. xmin, xmax = self.axes.get_xlim()
  2446. ymin, ymax = self.axes.get_ylim()
  2447. self.adjust_axes(xmin, ymin, xmax, ymax)
  2448. def zoom(self, factor, center=None):
  2449. """
  2450. Zooms the plot by factor around a given
  2451. center point. Takes care of re-drawing.
  2452. :param factor: Number by which to scale the plot.
  2453. :type factor: float
  2454. :param center: Coordinates [x, y] of the point around which to scale the plot.
  2455. :type center: list
  2456. :return: None
  2457. """
  2458. xmin, xmax = self.axes.get_xlim()
  2459. ymin, ymax = self.axes.get_ylim()
  2460. width = xmax - xmin
  2461. height = ymax - ymin
  2462. if center is None:
  2463. center = [(xmin + xmax) / 2.0, (ymin + ymax) / 2.0]
  2464. # For keeping the point at the pointer location
  2465. relx = (xmax - center[0]) / width
  2466. rely = (ymax - center[1]) / height
  2467. new_width = width / factor
  2468. new_height = height / factor
  2469. xmin = center[0] - new_width * (1 - relx)
  2470. xmax = center[0] + new_width * relx
  2471. ymin = center[1] - new_height * (1 - rely)
  2472. ymax = center[1] + new_height * rely
  2473. # Adjust axes
  2474. for ax in self.figure.get_axes():
  2475. ax.set_xlim((xmin, xmax))
  2476. ax.set_ylim((ymin, ymax))
  2477. # Re-draw
  2478. self.canvas.queue_draw()
  2479. def new_axes(self, name):
  2480. """
  2481. Creates and returns an Axes object attached to this object's Figure.
  2482. :param name: Unique label for the axes.
  2483. :return: Axes attached to the figure.
  2484. :rtype: Axes
  2485. """
  2486. return self.figure.add_axes([0.05, 0.05, 0.9, 0.9], label=name)
  2487. # def plot_axes(self, axes):
  2488. #
  2489. # if axes not in self.figure.axes:
  2490. # self.figure.add_axes(axes)
  2491. #
  2492. # # Basic configuration
  2493. # axes.set_frame_on(False) # No frame
  2494. # axes.set_xticks([]) # No tick
  2495. # axes.set_yticks([]) # No ticks
  2496. # axes.patch.set_visible(False) # No background
  2497. # axes.set_aspect(1)
  2498. #
  2499. # # Adjust limits
  2500. # self.auto_adjust_axes()
  2501. def on_scroll(self, canvas, event):
  2502. """
  2503. Scroll event handler.
  2504. :param canvas: The widget generating the event. Ignored.
  2505. :param event: Event object containing the event information.
  2506. :return: None
  2507. """
  2508. z, direction = event.get_scroll_direction()
  2509. if direction is Gdk.ScrollDirection.UP:
  2510. self.zoom(1.5, self.mouse)
  2511. else:
  2512. self.zoom(1/1.5, self.mouse)
  2513. def on_mouse_move(self, event):
  2514. """
  2515. Mouse movement event hadler.
  2516. :param event: Contains information about the event.
  2517. :return: None
  2518. """
  2519. self.mouse = [event.xdata, event.ydata]
  2520. app = App()
  2521. Gtk.main()