| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737 |
- import sys
- import urllib
- import getopt
- import random
- import logging
- import simplejson as json
- import re
- import webbrowser
- import os
- import Tkinter
- from PyQt4 import QtCore
- import time # Just used for debugging. Double check before removing.
- ########################################
- ## Imports part of FlatCAM ##
- ########################################
- from FlatCAMWorker import Worker
- from ObjectCollection import *
- from FlatCAMObj import *
- from PlotCanvas import *
- from FlatCAMGUI import *
- from FlatCAMCommon import LoudDict
- from FlatCAMTool import *
- from FlatCAMShell import FCShell
- from FlatCAMDraw import FlatCAMDraw
- from FlatCAMProcess import *
- ########################################
- ## App ##
- ########################################
- class App(QtCore.QObject):
- """
- The main application class. The constructor starts the GUI.
- """
- ## Get Cmd Line Options
- cmd_line_shellfile = ''
- cmd_line_help = "FlatCam.py --shellfile=<cmd_line_shellfile>"
- try:
- cmd_line_options, args = getopt.getopt(sys.argv[1:], "h:", "shellfile=")
- except getopt.GetoptError:
- print cmd_line_help
- sys.exit(2)
- for opt, arg in cmd_line_options:
- if opt == '-h':
- print cmd_line_help
- sys.exit()
- elif opt == '--shellfile':
- cmd_line_shellfile = arg
- ## Logging ##
- log = logging.getLogger('base')
- log.setLevel(logging.DEBUG)
- #log.setLevel(logging.WARNING)
- formatter = logging.Formatter('[%(levelname)s][%(threadName)s] %(message)s')
- handler = logging.StreamHandler()
- handler.setFormatter(formatter)
- log.addHandler(handler)
- ## Version
- version = 8.11
- version_date = "2015/01"
- ## URL for update checks and statistics
- version_url = "http://flatcam.org/version"
- ## App URL
- app_url = "http://flatcam.org"
- ## Manual URL
- manual_url = "http://flatcam.org/manual/index.html"
- ## Signals
- inform = QtCore.pyqtSignal(str) # Message
- worker_task = QtCore.pyqtSignal(dict) # Worker task
- file_opened = QtCore.pyqtSignal(str, str) # File type and filename
- progress = QtCore.pyqtSignal(int) # Percentage of progress
- plots_updated = QtCore.pyqtSignal()
- object_created = QtCore.pyqtSignal(object)
- message = QtCore.pyqtSignal(str, str, str)
- def __init__(self):
- """
- Starts the application. Takes no parameters.
- :return: app
- :rtype: App
- """
- App.log.info("FlatCAM Starting...")
- ###################
- ### OS-specific ###
- ###################
- if sys.platform == 'win32':
- from win32com.shell import shell, shellcon
- App.log.debug("Win32!")
- self.data_path = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, None, 0) + \
- '/FlatCAM'
- self.os = 'windows'
- else: # Linux/Unix/MacOS
- self.data_path = os.path.expanduser('~') + \
- '/.FlatCAM'
- self.os = 'unix'
- ###############################
- ### Setup folders and files ###
- ###############################
- if not os.path.exists(self.data_path):
- os.makedirs(self.data_path)
- App.log.debug('Created data folder: ' + self.data_path)
- try:
- f = open(self.data_path + '/defaults.json')
- f.close()
- except IOError:
- App.log.debug('Creating empty defaults.json')
- f = open(self.data_path + '/defaults.json', 'w')
- json.dump({}, f)
- f.close()
- try:
- f = open(self.data_path + '/recent.json')
- f.close()
- except IOError:
- App.log.debug('Creating empty recent.json')
- f = open(self.data_path + '/recent.json', 'w')
- json.dump([], f)
- f.close()
- # Application directory. Chdir to it.
- self.app_home = os.path.dirname(os.path.realpath(__file__))
- App.log.debug("Application path is " + self.app_home)
- App.log.debug("Started in " + os.getcwd())
- os.chdir(self.app_home)
- ####################
- ## Initialize GUI ##
- ####################
- QtCore.QObject.__init__(self)
- self.ui = FlatCAMGUI(self.version)
- #### Plot Area ####
- # self.plotcanvas = PlotCanvas(self.ui.splitter)
- self.plotcanvas = PlotCanvas(self.ui.right_layout)
- self.plotcanvas.mpl_connect('button_press_event', self.on_click_over_plot)
- self.plotcanvas.mpl_connect('motion_notify_event', self.on_mouse_move_over_plot)
- self.plotcanvas.mpl_connect('key_press_event', self.on_key_over_plot)
- self.ui.splitter.setStretchFactor(1, 2)
- ##############
- #### Data ####
- ##############
- self.recent = []
- self.clipboard = QtGui.QApplication.clipboard()
- self.proc_container = FCVisibleProcessContainer(self.ui.activity_view)
- self.project_filename = None
- self.toggle_units_ignore = False
- self.defaults_form = GlobalOptionsUI()
- self.defaults_form_fields = {
- "units": self.defaults_form.units_radio,
- "gerber_plot": self.defaults_form.gerber_group.plot_cb,
- "gerber_solid": self.defaults_form.gerber_group.solid_cb,
- "gerber_multicolored": self.defaults_form.gerber_group.multicolored_cb,
- "gerber_isotooldia": self.defaults_form.gerber_group.iso_tool_dia_entry,
- "gerber_isopasses": self.defaults_form.gerber_group.iso_width_entry,
- "gerber_isooverlap": self.defaults_form.gerber_group.iso_overlap_entry,
- "gerber_cutouttooldia": self.defaults_form.gerber_group.cutout_tooldia_entry,
- "gerber_cutoutmargin": self.defaults_form.gerber_group.cutout_margin_entry,
- "gerber_cutoutgapsize": self.defaults_form.gerber_group.cutout_gap_entry,
- "gerber_gaps": self.defaults_form.gerber_group.gaps_radio,
- "gerber_noncoppermargin": self.defaults_form.gerber_group.noncopper_margin_entry,
- "gerber_noncopperrounded": self.defaults_form.gerber_group.noncopper_rounded_cb,
- "gerber_bboxmargin": self.defaults_form.gerber_group.bbmargin_entry,
- "gerber_bboxrounded": self.defaults_form.gerber_group.bbrounded_cb,
- "excellon_plot": self.defaults_form.excellon_group.plot_cb,
- "excellon_solid": self.defaults_form.excellon_group.solid_cb,
- "excellon_drillz": self.defaults_form.excellon_group.cutz_entry,
- "excellon_travelz": self.defaults_form.excellon_group.travelz_entry,
- "excellon_feedrate": self.defaults_form.excellon_group.feedrate_entry,
- "geometry_plot": self.defaults_form.geometry_group.plot_cb,
- "geometry_cutz": self.defaults_form.geometry_group.cutz_entry,
- "geometry_travelz": self.defaults_form.geometry_group.travelz_entry,
- "geometry_feedrate": self.defaults_form.geometry_group.cncfeedrate_entry,
- "geometry_cnctooldia": self.defaults_form.geometry_group.cnctooldia_entry,
- "geometry_painttooldia": self.defaults_form.geometry_group.painttooldia_entry,
- "geometry_paintoverlap": self.defaults_form.geometry_group.paintoverlap_entry,
- "geometry_paintmargin": self.defaults_form.geometry_group.paintmargin_entry,
- "cncjob_plot": self.defaults_form.cncjob_group.plot_cb,
- "cncjob_tooldia": self.defaults_form.cncjob_group.tooldia_entry,
- "cncjob_append": self.defaults_form.cncjob_group.append_text
- }
- self.defaults = LoudDict()
- self.defaults.set_change_callback(self.on_defaults_dict_change) # When the dictionary changes.
- self.defaults.update({
- "serial": 0,
- "stats": {},
- "units": "IN",
- "gerber_plot": True,
- "gerber_solid": True,
- "gerber_multicolored": False,
- "gerber_isotooldia": 0.016,
- "gerber_isopasses": 1,
- "gerber_isooverlap": 0.15,
- "gerber_cutouttooldia": 0.07,
- "gerber_cutoutmargin": 0.1,
- "gerber_cutoutgapsize": 0.15,
- "gerber_gaps": "4",
- "gerber_noncoppermargin": 0.0,
- "gerber_noncopperrounded": False,
- "gerber_bboxmargin": 0.0,
- "gerber_bboxrounded": False,
- "excellon_plot": True,
- "excellon_solid": False,
- "excellon_drillz": -0.1,
- "excellon_travelz": 0.1,
- "excellon_feedrate": 3.0,
- "geometry_plot": True,
- "geometry_cutz": -0.002,
- "geometry_travelz": 0.1,
- "geometry_feedrate": 3.0,
- "geometry_cnctooldia": 0.016,
- "geometry_painttooldia": 0.07,
- "geometry_paintoverlap": 0.15,
- "geometry_paintmargin": 0.0,
- "cncjob_plot": True,
- "cncjob_tooldia": 0.016,
- "cncjob_append": "",
- # Persistence
- "last_folder": None,
- # Constants...
- "defaults_save_period_ms": 20000, # Time between default saves.
- "shell_shape": [500, 300], # Shape of the shell in pixels.
- "shell_at_startup": False, # Show the shell at startup.
- "recent_limit": 10, # Max. items in recent list.
- "fit_key": '1',
- "zoom_out_key": '2',
- "zoom_in_key": '3',
- "zoom_ratio": 1.5,
- "point_clipboard_format": "(%.4f, %.4f)",
- "zdownrate": None,
- "excellon_zeros": "L",
- "cncjob_coordinate_format": "X%.4fY%.4f"
- })
- ###############################
- ### Load defaults from file ###
- self.load_defaults()
- chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
- if self.defaults['serial'] == 0 or len(str(self.defaults['serial'])) < 10:
- self.defaults['serial'] = ''.join([random.choice(chars) for i in range(20)])
- self.save_defaults(silent=True)
- self.propagate_defaults()
- def auto_save_defaults():
- try:
- self.save_defaults(silent=True)
- finally:
- QtCore.QTimer.singleShot(self.defaults["defaults_save_period_ms"], auto_save_defaults)
- QtCore.QTimer.singleShot(self.defaults["defaults_save_period_ms"], auto_save_defaults)
- self.options_form = GlobalOptionsUI()
- self.options_form_fields = {
- "units": self.options_form.units_radio,
- "gerber_plot": self.options_form.gerber_group.plot_cb,
- "gerber_solid": self.options_form.gerber_group.solid_cb,
- "gerber_multicolored": self.options_form.gerber_group.multicolored_cb,
- "gerber_isotooldia": self.options_form.gerber_group.iso_tool_dia_entry,
- "gerber_isopasses": self.options_form.gerber_group.iso_width_entry,
- "gerber_isooverlap": self.options_form.gerber_group.iso_overlap_entry,
- "gerber_cutouttooldia": self.options_form.gerber_group.cutout_tooldia_entry,
- "gerber_cutoutmargin": self.options_form.gerber_group.cutout_margin_entry,
- "gerber_cutoutgapsize": self.options_form.gerber_group.cutout_gap_entry,
- "gerber_gaps": self.options_form.gerber_group.gaps_radio,
- "gerber_noncoppermargin": self.options_form.gerber_group.noncopper_margin_entry,
- "gerber_noncopperrounded": self.options_form.gerber_group.noncopper_rounded_cb,
- "gerber_bboxmargin": self.options_form.gerber_group.bbmargin_entry,
- "gerber_bboxrounded": self.options_form.gerber_group.bbrounded_cb,
- "excellon_plot": self.options_form.excellon_group.plot_cb,
- "excellon_solid": self.options_form.excellon_group.solid_cb,
- "excellon_drillz": self.options_form.excellon_group.cutz_entry,
- "excellon_travelz": self.options_form.excellon_group.travelz_entry,
- "excellon_feedrate": self.options_form.excellon_group.feedrate_entry,
- "geometry_plot": self.options_form.geometry_group.plot_cb,
- "geometry_cutz": self.options_form.geometry_group.cutz_entry,
- "geometry_travelz": self.options_form.geometry_group.travelz_entry,
- "geometry_feedrate": self.options_form.geometry_group.cncfeedrate_entry,
- "geometry_cnctooldia": self.options_form.geometry_group.cnctooldia_entry,
- "geometry_painttooldia": self.options_form.geometry_group.painttooldia_entry,
- "geometry_paintoverlap": self.options_form.geometry_group.paintoverlap_entry,
- "geometry_paintmargin": self.options_form.geometry_group.paintmargin_entry,
- "cncjob_plot": self.options_form.cncjob_group.plot_cb,
- "cncjob_tooldia": self.options_form.cncjob_group.tooldia_entry,
- "cncjob_append": self.options_form.cncjob_group.append_text
- }
- self.options = LoudDict()
- self.options.set_change_callback(self.on_options_dict_change)
- self.options.update({
- "units": "IN",
- "gerber_plot": True,
- "gerber_solid": True,
- "gerber_multicolored": False,
- "gerber_isotooldia": 0.016,
- "gerber_isopasses": 1,
- "gerber_isooverlap": 0.15,
- "gerber_cutouttooldia": 0.07,
- "gerber_cutoutmargin": 0.1,
- "gerber_cutoutgapsize": 0.15,
- "gerber_gaps": "4",
- "gerber_noncoppermargin": 0.0,
- "gerber_noncopperrounded": False,
- "gerber_bboxmargin": 0.0,
- "gerber_bboxrounded": False,
- "excellon_plot": True,
- "excellon_solid": False,
- "excellon_drillz": -0.1,
- "excellon_travelz": 0.1,
- "excellon_feedrate": 3.0,
- "geometry_plot": True,
- "geometry_cutz": -0.002,
- "geometry_travelz": 0.1,
- "geometry_feedrate": 3.0,
- "geometry_cnctooldia": 0.016,
- "geometry_painttooldia": 0.07,
- "geometry_paintoverlap": 0.15,
- "geometry_paintmargin": 0.0,
- "cncjob_plot": True,
- "cncjob_tooldia": 0.016,
- "cncjob_append": ""
- })
- self.options.update(self.defaults) # Copy app defaults to project options
- #self.options_write_form()
- self.on_options_combo_change(0) # Will show the initial form
- self.collection = ObjectCollection()
- self.ui.project_tab_layout.addWidget(self.collection.view)
- #### End of Data ####
- #### Worker ####
- App.log.info("Starting Worker...")
- self.worker = Worker(self)
- self.thr1 = QtCore.QThread()
- self.worker.moveToThread(self.thr1)
- self.connect(self.thr1, QtCore.SIGNAL("started()"), self.worker.run)
- self.thr1.start()
- #### Check for updates ####
- # Separate thread (Not worker)
- App.log.info("Checking for updates in backgroud (this is version %s)." % str(self.version))
- self.worker2 = Worker(self, name="worker2")
- self.thr2 = QtCore.QThread()
- self.worker2.moveToThread(self.thr2)
- self.connect(self.thr2, QtCore.SIGNAL("started()"), self.worker2.run)
- self.connect(self.thr2, QtCore.SIGNAL("started()"),
- lambda: self.worker_task.emit({'fcn': self.version_check,
- 'params': [],
- 'worker_name': "worker2"}))
- self.thr2.start()
- ### Signal handling ###
- ## Custom signals
- self.inform.connect(self.info)
- self.message.connect(self.message_dialog)
- self.progress.connect(self.set_progress_bar)
- self.object_created.connect(self.on_object_created)
- self.plots_updated.connect(self.on_plots_updated)
- self.file_opened.connect(self.register_recent)
- self.file_opened.connect(lambda kind, filename: self.register_folder(filename))
- ## Standard signals
- # Menu
- self.ui.menufilenew.triggered.connect(self.on_file_new)
- self.ui.menufileopengerber.triggered.connect(self.on_fileopengerber)
- self.ui.menufileopenexcellon.triggered.connect(self.on_fileopenexcellon)
- self.ui.menufileopengcode.triggered.connect(self.on_fileopengcode)
- self.ui.menufileopenproject.triggered.connect(self.on_file_openproject)
- self.ui.menufilesaveproject.triggered.connect(self.on_file_saveproject)
- self.ui.menufilesaveprojectas.triggered.connect(self.on_file_saveprojectas)
- self.ui.menufilesaveprojectcopy.triggered.connect(lambda: self.on_file_saveprojectas(make_copy=True))
- self.ui.menufilesavedefaults.triggered.connect(self.on_file_savedefaults)
- self.ui.menueditnew.triggered.connect(lambda: self.new_object('geometry', 'New Geometry', lambda x, y: None))
- self.ui.menueditedit.triggered.connect(self.edit_geometry)
- self.ui.menueditok.triggered.connect(self.editor2geometry)
- self.ui.menueditjoin.triggered.connect(self.on_edit_join)
- self.ui.menueditdelete.triggered.connect(self.on_delete)
- self.ui.menuoptions_transfer_a2o.triggered.connect(self.on_options_app2object)
- self.ui.menuoptions_transfer_a2p.triggered.connect(self.on_options_app2project)
- self.ui.menuoptions_transfer_o2a.triggered.connect(self.on_options_object2app)
- self.ui.menuoptions_transfer_p2a.triggered.connect(self.on_options_project2app)
- self.ui.menuoptions_transfer_o2p.triggered.connect(self.on_options_object2project)
- self.ui.menuoptions_transfer_p2o.triggered.connect(self.on_options_project2object)
- self.ui.menuviewdisableall.triggered.connect(self.disable_plots)
- self.ui.menuviewdisableother.triggered.connect(lambda: self.disable_plots(except_current=True))
- self.ui.menuviewenable.triggered.connect(self.enable_all_plots)
- self.ui.menutoolshell.triggered.connect(lambda: self.shell.show())
- self.ui.menuhelp_about.triggered.connect(self.on_about)
- self.ui.menuhelp_home.triggered.connect(lambda: webbrowser.open(self.app_url))
- self.ui.menuhelp_manual.triggered.connect(lambda: webbrowser.open(self.manual_url))
- # Toolbar
- self.ui.zoom_fit_btn.triggered.connect(self.on_zoom_fit)
- self.ui.zoom_in_btn.triggered.connect(lambda: self.plotcanvas.zoom(1.5))
- self.ui.zoom_out_btn.triggered.connect(lambda: self.plotcanvas.zoom(1 / 1.5))
- self.ui.clear_plot_btn.triggered.connect(self.plotcanvas.clear)
- self.ui.replot_btn.triggered.connect(self.on_toolbar_replot)
- self.ui.newgeo_btn.triggered.connect(lambda: self.new_object('geometry', 'New Geometry', lambda x, y: None))
- self.ui.editgeo_btn.triggered.connect(self.edit_geometry)
- self.ui.updategeo_btn.triggered.connect(self.editor2geometry)
- self.ui.delete_btn.triggered.connect(self.on_delete)
- self.ui.shell_btn.triggered.connect(lambda: self.shell.show())
- # Object list
- self.collection.view.activated.connect(self.on_row_activated)
- # Options
- self.ui.options_combo.activated.connect(self.on_options_combo_change)
- self.options_form.units_radio.group_toggle_fn = self.on_toggle_units
- ####################
- ### Other setups ###
- ####################
- self.setup_obj_classes()
- self.setup_recent_items()
- self.setup_component_editor()
- #########################
- ### Tools and Plugins ###
- #########################
- self.dblsidedtool = DblSidedTool(self)
- self.dblsidedtool.install()
- self.measeurement_tool = Measurement(self)
- self.measeurement_tool.install()
- self.draw = FlatCAMDraw(self, disabled=True)
- #############
- ### Shell ###
- #############
- # TODO: Move this to its own class
- self.shell = FCShell(self)
- self.shell.setWindowIcon(self.ui.app_icon)
- self.shell.setWindowTitle("FlatCAM Shell")
- if self.defaults["shell_at_startup"]:
- self.shell.show()
- self.shell.resize(*self.defaults["shell_shape"])
- self.shell.append_output("FlatCAM %s\n(c) 2014-2015 Juan Pablo Caram\n\n" % self.version)
- self.shell.append_output("Type help to get started.\n\n")
- self.tcl = Tkinter.Tcl()
- self.setup_shell()
- if self.cmd_line_shellfile:
- try:
- with open(self.cmd_line_shellfile, "r") as myfile:
- cmd_line_shellfile_text = myfile.read()
- self.shell._sysShell.exec_command(cmd_line_shellfile_text)
- except Exception as ext:
- print "ERROR: ", ext
- sys.exit(2)
- App.log.debug("END of constructor. Releasing control.")
- def defaults_read_form(self):
- for option in self.defaults_form_fields:
- self.defaults[option] = self.defaults_form_fields[option].get_value()
- def defaults_write_form(self):
- for option in self.defaults:
- self.defaults_write_form_field(option)
- # try:
- # self.defaults_form_fields[option].set_value(self.defaults[option])
- # except KeyError:
- # #self.log.debug("defaults_write_form(): No field for: %s" % option)
- # # TODO: Rethink this?
- # pass
- def defaults_write_form_field(self, field):
- try:
- self.defaults_form_fields[field].set_value(self.defaults[field])
- except KeyError:
- #self.log.debug("defaults_write_form(): No field for: %s" % option)
- # TODO: Rethink this?
- pass
- def disable_plots(self, except_current=False):
- """
- Disables all plots with exception of the current object if specified.
- :param except_current: Wether to skip the current object.
- :rtype except_current: boolean
- :return: None
- """
- # TODO: This method is very similar to replot_all. Try to merge.
- self.progress.emit(10)
- def worker_task(app_obj):
- percentage = 0.1
- try:
- delta = 0.9 / len(self.collection.get_list())
- except ZeroDivisionError:
- self.progress.emit(0)
- return
- for obj in self.collection.get_list():
- if obj != self.collection.get_active() or not except_current:
- obj.options['plot'] = False
- obj.plot()
- percentage += delta
- self.progress.emit(int(percentage*100))
- self.progress.emit(0)
- self.plots_updated.emit()
- # Send to worker
- self.worker_task.emit({'fcn': worker_task, 'params': [self]})
- def edit_geometry(self):
- """
- Send the current geometry object (if any) into the editor.
- :return: None
- """
- if not isinstance(self.collection.get_active(), FlatCAMGeometry):
- self.info("Select a Geometry Object to edit.")
- return
- self.ui.updategeo_btn.setEnabled(True)
- self.draw.edit_fcgeometry(self.collection.get_active())
- def editor2geometry(self):
- """
- Transfers the geometry in the editor to the current geometry object.
- :return: None
- """
- geo = self.collection.get_active()
- if not isinstance(geo, FlatCAMGeometry):
- self.info("Select a Geometry Object to update.")
- return
- self.draw.update_fcgeometry(geo)
- self.draw.deactivate()
- self.ui.updategeo_btn.setEnabled(False)
- geo.plot()
- def get_last_folder(self):
- return self.defaults["last_folder"]
- def report_usage(self, resource):
- """
- Increments usage counter for the given resource
- in self.defaults['stats'].
- :param resource: Name of the resource.
- :return: None
- """
- if resource in self.defaults['stats']:
- self.defaults['stats'][resource] += 1
- else:
- self.defaults['stats'][resource] = 1
- def exec_command(self, text):
- """
- Handles input from the shell. See FlatCAMApp.setup_shell for shell commands.
- :param text: Input command
- :return: None
- """
- self.report_usage('exec_command')
- text = str(text)
- try:
- result = self.tcl.eval(str(text))
- self.shell.append_output(result + '\n')
- except Tkinter.TclError, e:
- self.shell.append_error('ERROR: ' + str(e) + '\n')
- return
- """
- Code below is unsused. Saved for later.
- """
- parts = re.findall(r'([\w\\:\.]+|".*?")+', text)
- parts = [p.replace('\n', '').replace('"', '') for p in parts]
- self.log.debug(parts)
- try:
- if parts[0] not in commands:
- self.shell.append_error("Unknown command\n")
- return
- #import inspect
- #inspect.getargspec(someMethod)
- if (type(commands[parts[0]]["params"]) is not list and len(parts)-1 != commands[parts[0]]["params"]) or \
- (type(commands[parts[0]]["params"]) is list and len(parts)-1 not in commands[parts[0]]["params"]):
- self.shell.append_error(
- "Command %s takes %d arguments. %d given.\n" %
- (parts[0], commands[parts[0]]["params"], len(parts)-1)
- )
- return
- cmdfcn = commands[parts[0]]["fcn"]
- cmdconv = commands[parts[0]]["converters"]
- if len(parts) - 1 > 0:
- retval = cmdfcn(*[cmdconv[i](parts[i + 1]) for i in range(len(parts)-1)])
- else:
- retval = cmdfcn()
- retfcn = commands[parts[0]]["retfcn"]
- if retval and retfcn(retval):
- self.shell.append_output(retfcn(retval) + "\n")
- except Exception, e:
- #self.shell.append_error(''.join(traceback.format_exc()))
- #self.shell.append_error("?\n")
- self.shell.append_error(str(e) + "\n")
- def info(self, msg):
- """
- Writes on the status bar.
- :param msg: Text to write.
- :return: None
- """
- match = re.search("\[([^\]]+)\](.*)", msg)
- if match:
- self.ui.fcinfo.set_status(QtCore.QString(match.group(2)), level=match.group(1))
- else:
- self.ui.fcinfo.set_status(QtCore.QString(msg), level="info")
- def load_defaults(self):
- """
- Loads the aplication's default settings from defaults.json into
- ``self.defaults``.
- :return: None
- """
- try:
- f = open(self.data_path + "/defaults.json")
- options = f.read()
- f.close()
- except IOError:
- self.log.error("Could not load defaults file.")
- self.inform.emit("ERROR: Could not load defaults file.")
- return
- try:
- defaults = json.loads(options)
- except:
- e = sys.exc_info()[0]
- App.log.error(str(e))
- self.inform.emit("ERROR: Failed to parse defaults file.")
- return
- self.defaults.update(defaults)
- def message_dialog(self, title, message, kind="info"):
- icon = {"info": QtGui.QMessageBox.Information,
- "warning": QtGui.QMessageBox.Warning,
- "error": QtGui.QMessageBox.Critical}[str(kind)]
- dlg = QtGui.QMessageBox(icon, title, message, parent=self.ui)
- dlg.setText(message)
- dlg.exec_()
- def register_recent(self, kind, filename):
- self.log.debug("register_recent()")
- self.log.debug(" %s" % kind)
- self.log.debug(" %s" % filename)
- record = {'kind': str(kind), 'filename': str(filename)}
- if record in self.recent:
- return
- self.recent.insert(0, record)
- if len(self.recent) > self.defaults['recent_limit']: # Limit reached
- self.recent.pop()
- try:
- f = open(self.data_path + '/recent.json', 'w')
- except IOError:
- App.log.error("Failed to open recent items file for writing.")
- self.inform.emit('Failed to open recent files file for writing.')
- return
- #try:
- json.dump(self.recent, f)
- # except:
- # App.log.error("Failed to write to recent items file.")
- # self.inform.emit('ERROR: Failed to write to recent items file.')
- # f.close()
- f.close()
- # Re-buid the recent items menu
- self.setup_recent_items()
- def new_object(self, kind, name, initialize, active=True, fit=True, plot=True):
- """
- Creates a new specalized FlatCAMObj and attaches it to the application,
- this is, updates the GUI accordingly, any other records and plots it.
- This method is thread-safe.
- :param kind: The kind of object to create. One of 'gerber',
- 'excellon', 'cncjob' and 'geometry'.
- :type kind: str
- :param name: Name for the object.
- :type name: str
- :param initialize: Function to run after creation of the object
- but before it is attached to the application. The function is
- called with 2 parameters: the new object and the App instance.
- :type initialize: function
- :return: None
- :rtype: None
- """
- App.log.debug("new_object()")
- t0 = time.time() # Debug
- ### Check for existing name
- while name in self.collection.get_names():
- ## Create a new name
- # Ends with number?
- App.log.debug("new_object(): Object name (%s) exists, changing." % name)
- match = re.search(r'(.*[^\d])?(\d+)$', name)
- if match: # Yes: Increment the number!
- base = match.group(1) or ''
- num = int(match.group(2))
- name = base + str(num + 1)
- else: # No: add a number!
- name += "_1"
- ## Create object
- classdict = {
- "gerber": FlatCAMGerber,
- "excellon": FlatCAMExcellon,
- "cncjob": FlatCAMCNCjob,
- "geometry": FlatCAMGeometry
- }
- App.log.debug("Calling object constructor...")
- obj = classdict[kind](name)
- obj.units = self.options["units"] # TODO: The constructor should look at defaults.
- # Set default options from self.options
- for option in self.options:
- if option.find(kind + "_") == 0:
- oname = option[len(kind) + 1:]
- obj.options[oname] = self.options[option]
- # Initialize as per user request
- # User must take care to implement initialize
- # in a thread-safe way as is is likely that we
- # have been invoked in a separate thread.
- t1 = time.time()
- self.log.debug("%f seconds before initialize()." % (t1 - t0))
- initialize(obj, self)
- t2 = time.time()
- self.log.debug("%f seconds executing initialize()." % (t2 - t1))
- # Check units and convert if necessary
- # This condition CAN be true because initialize() can change obj.units
- if self.options["units"].upper() != obj.units.upper():
- self.inform.emit("Converting units to " + self.options["units"] + ".")
- obj.convert_units(self.options["units"])
- t3 = time.time()
- self.log.debug("%f seconds converting units." % (t3 - t2))
- FlatCAMApp.App.log.debug("Moving new object back to main thread.")
- # Move the object to the main thread and let the app know that it is available.
- obj.moveToThread(QtGui.QApplication.instance().thread())
- self.object_created.emit(obj)
- return obj
- def options_read_form(self):
- for option in self.options_form_fields:
- self.options[option] = self.options_form_fields[option].get_value()
- def options_write_form(self):
- for option in self.options:
- self.options_write_form_field(option)
- def options_write_form_field(self, field):
- try:
- self.options_form_fields[field].set_value(self.options[field])
- except KeyError:
- # Changed from error to debug. This allows to have data stored
- # which is not user-editable.
- self.log.debug("options_write_form_field(): No field for: %s" % field)
- def on_about(self):
- """
- Displays the "about" dialog.
- :return: None
- """
- self.report_usage("on_about")
- version = self.version
- version_date = self.version_date
- class AboutDialog(QtGui.QDialog):
- def __init__(self, parent=None):
- QtGui.QDialog.__init__(self, parent)
- # Icon and title
- self.setWindowIcon(parent.app_icon)
- self.setWindowTitle("FlatCAM")
- layout1 = QtGui.QVBoxLayout()
- self.setLayout(layout1)
- layout2 = QtGui.QHBoxLayout()
- layout1.addLayout(layout2)
- logo = QtGui.QLabel()
- logo.setPixmap(QtGui.QPixmap('share/flatcam_icon256.png'))
- layout2.addWidget(logo, stretch=0)
- title = QtGui.QLabel(
- "<font size=8><B>FlatCAM</B></font><BR>"
- "Version %s (%s)<BR>"
- "<BR>"
- "2D Computer-Aided Printed Circuit Board<BR>"
- "Manufacturing.<BR>"
- "<BR>"
- "(c) 2014-2015 Juan Pablo Caram" % (version, version_date)
- )
- layout2.addWidget(title, stretch=1)
- layout3 = QtGui.QHBoxLayout()
- layout1.addLayout(layout3)
- layout3.addStretch()
- okbtn = QtGui.QPushButton("Close")
- layout3.addWidget(okbtn)
- okbtn.clicked.connect(self.accept)
- AboutDialog(self.ui).exec_()
- def on_file_savedefaults(self):
- """
- Callback for menu item File->Save Defaults. Saves application default options
- ``self.defaults`` to defaults.json.
- :return: None
- """
- self.save_defaults()
- def save_defaults(self, silent=False):
- """
- Saves application default options
- ``self.defaults`` to defaults.json.
- :return: None
- """
- self.report_usage("save_defaults")
- ## Read options from file ##
- try:
- f = open(self.data_path + "/defaults.json")
- options = f.read()
- f.close()
- except:
- e = sys.exc_info()[0]
- App.log.error("Could not load defaults file.")
- App.log.error(str(e))
- self.inform.emit("[error] Could not load defaults file.")
- return
- try:
- defaults = json.loads(options)
- except:
- e = sys.exc_info()[0]
- App.log.error("Failed to parse defaults file.")
- App.log.error(str(e))
- self.inform.emit("[error] Failed to parse defaults file.")
- return
- # Update options
- self.defaults_read_form()
- defaults.update(self.defaults)
- # Save update options
- try:
- f = open(self.data_path + "/defaults.json", "w")
- json.dump(defaults, f)
- f.close()
- except:
- self.inform.emit("[error] Failed to write defaults to file.")
- return
- if not silent:
- self.inform.emit("Defaults saved.")
- def on_edit_join(self):
- """
- Callback for Edit->Join. Joins the selected geometry objects into
- a new one.
- :return: None
- """
- objs = self.collection.get_selected()
- def initialize(obj, app):
- FlatCAMGeometry.merge(objs, obj)
- self.new_object("geometry", "Combo", initialize)
- def on_options_app2project(self):
- """
- Callback for Options->Transfer Options->App=>Project. Copies options
- from application defaults to project defaults.
- :return: None
- """
- self.report_usage("on_options_app2project")
- self.defaults_read_form()
- self.options.update(self.defaults)
- self.options_write_form()
- def on_options_project2app(self):
- """
- Callback for Options->Transfer Options->Project=>App. Copies options
- from project defaults to application defaults.
- :return: None
- """
- self.report_usage("on_options_project2app")
- self.options_read_form()
- self.defaults.update(self.options)
- self.defaults_write_form()
- def on_options_project2object(self):
- """
- Callback for Options->Transfer Options->Project=>Object. Copies options
- from project defaults to the currently selected object.
- :return: None
- """
- self.report_usage("on_options_project2object")
- self.options_read_form()
- obj = self.collection.get_active()
- if obj is None:
- self.inform.emit("WARNING: No object selected.")
- return
- for option in self.options:
- if option.find(obj.kind + "_") == 0:
- oname = option[len(obj.kind)+1:]
- obj.options[oname] = self.options[option]
- obj.to_form() # Update UI
- def on_options_object2project(self):
- """
- Callback for Options->Transfer Options->Object=>Project. Copies options
- from the currently selected object to project defaults.
- :return: None
- """
- self.report_usage("on_options_object2project")
- obj = self.collection.get_active()
- if obj is None:
- self.inform.emit("WARNING: No object selected.")
- return
- obj.read_form()
- for option in obj.options:
- if option in ['name']: # TODO: Handle this better...
- continue
- self.options[obj.kind + "_" + option] = obj.options[option]
- self.options_write_form()
- def on_options_object2app(self):
- """
- Callback for Options->Transfer Options->Object=>App. Copies options
- from the currently selected object to application defaults.
- :return: None
- """
- self.report_usage("on_options_object2app")
- obj = self.collection.get_active()
- if obj is None:
- self.inform.emit("WARNING: No object selected.")
- return
- obj.read_form()
- for option in obj.options:
- if option in ['name']: # TODO: Handle this better...
- continue
- self.defaults[obj.kind + "_" + option] = obj.options[option]
- self.defaults_write_form()
- def on_options_app2object(self):
- """
- Callback for Options->Transfer Options->App=>Object. Copies options
- from application defaults to the currently selected object.
- :return: None
- """
- self.report_usage("on_options_app2object")
- self.defaults_read_form()
- obj = self.collection.get_active()
- if obj is None:
- self.inform.emit("WARNING: No object selected.")
- return
- for option in self.defaults:
- if option.find(obj.kind + "_") == 0:
- oname = option[len(obj.kind)+1:]
- obj.options[oname] = self.defaults[option]
- obj.to_form() # Update UI
- def on_options_dict_change(self, field):
- self.options_write_form_field(field)
- if field == "units":
- self.set_screen_units(self.options['units'])
- def on_defaults_dict_change(self, field):
- self.defaults_write_form_field(field)
- def set_screen_units(self, units):
- self.ui.units_label.setText("[" + self.options["units"].lower() + "]")
- def on_toggle_units(self):
- """
- Callback for the Units radio-button change in the Options tab.
- Changes the application's default units or the current project's units.
- If changing the project's units, the change propagates to all of
- the objects in the project.
- :return: None
- """
- self.report_usage("on_toggle_units")
- if self.toggle_units_ignore:
- return
- # If option is the same, then ignore
- if self.options_form.units_radio.get_value().upper() == self.options['units'].upper():
- self.log.debug("on_toggle_units(): Same as options, so ignoring.")
- return
- # Options to scale
- dimensions = ['gerber_isotooldia', 'gerber_cutoutmargin', 'gerber_cutoutgapsize',
- 'gerber_noncoppermargin', 'gerber_bboxmargin', 'excellon_drillz',
- 'excellon_travelz', 'excellon_feedrate', 'cncjob_tooldia',
- 'geometry_cutz', 'geometry_travelz', 'geometry_feedrate',
- 'geometry_cnctooldia', 'geometry_painttooldia', 'geometry_paintoverlap',
- 'geometry_paintmargin']
- def scale_options(sfactor):
- for dim in dimensions:
- self.options[dim] *= sfactor
- # The scaling factor depending on choice of units.
- factor = 1/25.4
- if self.options_form.units_radio.get_value().upper() == 'MM':
- factor = 25.4
- # Changing project units. Warn user.
- msgbox = QtGui.QMessageBox()
- msgbox.setText("<B>Change project units ...</B>")
- msgbox.setInformativeText("Changing the units of the project causes all geometrical "
- "properties of all objects to be scaled accordingly. Continue?")
- msgbox.setStandardButtons(QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Ok)
- msgbox.setDefaultButton(QtGui.QMessageBox.Ok)
- response = msgbox.exec_()
- if response == QtGui.QMessageBox.Ok:
- self.options_read_form()
- scale_options(factor)
- self.options_write_form()
- for obj in self.collection.get_list():
- units = self.options_form.units_radio.get_value().upper()
- obj.convert_units(units)
- current = self.collection.get_active()
- if current is not None:
- current.to_form()
- self.plot_all()
- else:
- # Undo toggling
- self.toggle_units_ignore = True
- if self.options_form.units_radio.get_value().upper() == 'MM':
- self.options_form.units_radio.set_value('IN')
- else:
- self.options_form.units_radio.set_value('MM')
- self.toggle_units_ignore = False
- self.options_read_form()
- self.inform.emit("Converted units to %s" % self.options["units"])
- #self.ui.units_label.setText("[" + self.options["units"] + "]")
- self.set_screen_units(self.options["units"])
- def on_options_combo_change(self, sel):
- """
- Called when the combo box to choose between application defaults and
- project option changes value. The corresponding variables are
- copied to the UI.
- :param sel: The option index that was chosen.
- :return: None
- """
- # combo_sel = self.ui.notebook.combo_options.get_active()
- App.log.debug("Options --> %s" % sel)
- # Remove anything else in the box
- # box_children = self.options_box.get_children()
- # box_children = self.ui.notebook.options_contents.get_children()
- # for child in box_children:
- # self.ui.notebook.options_contents.remove(child)
- # try:
- # self.ui.options_area.removeWidget(self.defaults_form)
- # except:
- # pass
- #
- # try:
- # self.ui.options_area.removeWidget(self.options_form)
- # except:
- # pass
- form = [self.defaults_form, self.options_form][sel]
- # self.ui.notebook.options_contents.pack_start(form, False, False, 1)
- try:
- self.ui.options_scroll_area.takeWidget()
- except:
- self.log.debug("Nothing to remove")
- self.ui.options_scroll_area.setWidget(form)
- form.show()
- # self.options2form()
- def on_delete(self):
- """
- Delete the currently selected FlatCAMObj.
- :return: None
- """
- self.log.debug("on_delete()")
- self.report_usage("on_delete")
- # Keep this for later
- try:
- #name = copy(self.collection.get_active().options["name"])
- # Shouldn't need to copy. String are immutable.
- name = self.collection.get_active().options["name"]
- except AttributeError:
- self.log.debug("Nothing selected for deletion")
- return
- # Remove plot
- self.plotcanvas.figure.delaxes(self.collection.get_active().axes)
- self.plotcanvas.auto_adjust_axes()
- # Clear form
- self.setup_component_editor()
- # Remove from dictionary
- self.collection.delete_active()
- self.inform.emit("Object deleted: %s" % name)
- def on_plots_updated(self):
- """
- Callback used to report when the plots have changed.
- Adjust axes and zooms to fit.
- :return: None
- """
- self.plotcanvas.auto_adjust_axes()
- self.on_zoom_fit(None)
- def on_toolbar_replot(self):
- """
- Callback for toolbar button. Re-plots all objects.
- :return: None
- """
- self.report_usage("on_toolbar_replot")
- self.log.debug("on_toolbar_replot()")
- try:
- self.collection.get_active().read_form()
- except AttributeError:
- self.log.debug("on_toolbar_replot(): AttributeError")
- pass
- self.plot_all()
- def on_row_activated(self, index):
- self.ui.notebook.setCurrentWidget(self.ui.selected_tab)
- def on_object_created(self, obj):
- """
- Event callback for object creation.
- :param obj: The newly created FlatCAM object.
- :return: None
- """
- t0 = time.time() # DEBUG
- self.log.debug("on_object_created()")
- self.inform.emit("Object (%s) created: %s" % (obj.kind, obj.options['name']))
- self.collection.append(obj)
- obj.plot()
- self.on_zoom_fit(None)
- t1 = time.time() # DEBUG
- self.log.debug("%f seconds adding object and plotting." % (t1 - t0))
- def on_zoom_fit(self, event):
- """
- Callback for zoom-out request. This can be either from the corresponding
- toolbar button or the '1' key when the canvas is focused. Calls ``self.adjust_axes()``
- with axes limits from the geometry bounds of all objects.
- :param event: Ignored.
- :return: None
- """
- xmin, ymin, xmax, ymax = self.collection.get_bounds()
- width = xmax - xmin
- height = ymax - ymin
- xmin -= 0.05 * width
- xmax += 0.05 * width
- ymin -= 0.05 * height
- ymax += 0.05 * height
- self.plotcanvas.adjust_axes(xmin, ymin, xmax, ymax)
- def on_key_over_plot(self, event):
- """
- Callback for the key pressed event when the canvas is focused. Keyboard
- shortcuts are handled here. So far, these are the shortcuts:
- ========== ============================================
- Key Action
- ========== ============================================
- '1' Zoom-fit. Fits the axes limits to the data.
- '2' Zoom-out.
- '3' Zoom-in.
- 'm' Toggle on-off the measuring tool.
- ========== ============================================
- :param event: Ignored.
- :return: None
- """
- if event.key == self.defaults['fit_key']: # 1
- self.on_zoom_fit(None)
- return
- if event.key == self.defaults['zoom_out_key']: # 2
- self.plotcanvas.zoom(1 / self.defaults['zoom_ratio'], self.mouse)
- return
- if event.key == self.defaults['zoom_in_key']: # 3
- self.plotcanvas.zoom(self.defaults['zoom_ratio'], self.mouse)
- return
- # if event.key == 'm':
- # if self.measure.toggle_active():
- # self.inform.emit("Measuring tool ON")
- # else:
- # self.inform.emit("Measuring tool OFF")
- # return
- def on_click_over_plot(self, event):
- """
- Callback for the mouse click event over the plot. This event is generated
- by the Matplotlib backend and has been registered in ``self.__init__()``.
- For details, see: http://matplotlib.org/users/event_handling.html
- Default actions are:
- * Copy coordinates to clipboard. Ex.: (65.5473, -13.2679)
- :param event: Contains information about the event, like which button
- was clicked, the pixel coordinates and the axes coordinates.
- :return: None
- """
- # So it can receive key presses
- self.plotcanvas.canvas.setFocus()
- try:
- App.log.debug('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (
- event.button, event.x, event.y, event.xdata, event.ydata))
- self.clipboard.setText(self.defaults["point_clipboard_format"] % (event.xdata, event.ydata))
- except Exception, e:
- App.log.debug("Outside plot?")
- App.log.debug(str(e))
- def on_mouse_move_over_plot(self, event):
- """
- Callback for the mouse motion event over the plot. This event is generated
- by the Matplotlib backend and has been registered in ``self.__init__()``.
- For details, see: http://matplotlib.org/users/event_handling.html
- :param event: Contains information about the event.
- :return: None
- """
- try: # May fail in case mouse not within axes
- self.ui.position_label.setText("X: %.4f Y: %.4f" % (
- event.xdata, event.ydata))
- self.mouse = [event.xdata, event.ydata]
- except:
- self.ui.position_label.setText("")
- self.mouse = None
- def on_file_new(self):
- """
- Callback for menu item File->New. Returns the application to its
- startup state. This method is thread-safe.
- :return: None
- """
- self.report_usage("on_file_new")
- # Remove everything from memory
- App.log.debug("on_file_new()")
- self.plotcanvas.clear()
- self.collection.delete_all()
- self.setup_component_editor()
- # Clear project filename
- self.project_filename = None
- # Re-fresh project options
- self.on_options_app2project()
- def on_fileopengerber(self):
- """
- File menu callback for opening a Gerber.
- :return: None
- """
- self.report_usage("on_fileopengerber")
- App.log.debug("on_fileopengerber()")
- try:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Gerber",
- directory=self.get_last_folder())
- except TypeError:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Gerber")
- # The Qt methods above will return a QString which can cause problems later.
- # So far json.dump() will fail to serialize it.
- # TODO: Improve the serialization methods and remove this fix.
- filename = str(filename)
- if str(filename) == "":
- self.inform.emit("Open cancelled.")
- else:
- self.worker_task.emit({'fcn': self.open_gerber,
- 'params': [filename]})
- def on_fileopenexcellon(self):
- """
- File menu callback for opening an Excellon file.
- :return: None
- """
- self.report_usage("on_fileopenexcellon")
- App.log.debug("on_fileopenexcellon()")
- try:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Excellon",
- directory=self.get_last_folder())
- except TypeError:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Excellon")
- # The Qt methods above will return a QString which can cause problems later.
- # So far json.dump() will fail to serialize it.
- # TODO: Improve the serialization methods and remove this fix.
- filename = str(filename)
- if str(filename) == "":
- self.inform.emit("Open cancelled.")
- else:
- self.worker_task.emit({'fcn': self.open_excellon,
- 'params': [filename]})
- def on_fileopengcode(self):
- """
- File menu call back for opening gcode.
- :return: None
- """
- self.report_usage("on_fileopengcode")
- App.log.debug("on_fileopengcode()")
- try:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open G-Code",
- directory=self.get_last_folder())
- except TypeError:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open G-Code")
- # The Qt methods above will return a QString which can cause problems later.
- # So far json.dump() will fail to serialize it.
- # TODO: Improve the serialization methods and remove this fix.
- filename = str(filename)
- if str(filename) == "":
- self.inform.emit("Open cancelled.")
- else:
- self.worker_task.emit({'fcn': self.open_gcode,
- 'params': [filename]})
- def on_file_openproject(self):
- """
- File menu callback for opening a project.
- :return: None
- """
- self.report_usage("on_file_openproject")
- App.log.debug("on_file_openproject()")
- try:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Project",
- directory=self.get_last_folder())
- except TypeError:
- filename = QtGui.QFileDialog.getOpenFileName(caption="Open Project")
- # The Qt methods above will return a QString which can cause problems later.
- # So far json.dump() will fail to serialize it.
- # TODO: Improve the serialization methods and remove this fix.
- filename = str(filename)
- if str(filename) == "":
- self.inform.emit("Open cancelled.")
- else:
- # self.worker_task.emit({'fcn': self.open_project,
- # 'params': [filename]})
- # The above was failing because open_project() is not
- # thread safe. The new_project()
- self.open_project(filename)
- def on_file_saveproject(self):
- """
- Callback for menu item File->Save Project. Saves the project to
- ``self.project_filename`` or calls ``self.on_file_saveprojectas()``
- if set to None. The project is saved by calling ``self.save_project()``.
- :return: None
- """
- self.report_usage("on_file_saveproject")
- if self.project_filename is None:
- self.on_file_saveprojectas()
- else:
- self.save_project(self.project_filename)
- self.file_opened.emit("project", self.project_filename)
- self.inform.emit("Project saved to: " + self.project_filename)
- def on_file_saveprojectas(self, make_copy=False):
- """
- Callback for menu item File->Save Project As... Opens a file
- chooser and saves the project to the given file via
- ``self.save_project()``.
- :return: None
- """
- self.report_usage("on_file_saveprojectas")
- try:
- filename = QtGui.QFileDialog.getSaveFileName(caption="Save Project As ...",
- directory=self.get_last_folder())
- except TypeError:
- filename = QtGui.QFileDialog.getSaveFileName(caption="Save Project As ...")
- try:
- f = open(filename, 'r')
- f.close()
- exists = True
- except IOError:
- exists = False
- msg = "File exists. Overwrite?"
- if exists:
- msgbox = QtGui.QMessageBox()
- msgbox.setInformativeText(msg)
- msgbox.setStandardButtons(QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Ok)
- msgbox.setDefaultButton(QtGui.QMessageBox.Cancel)
- result = msgbox.exec_()
- if result == QtGui.QMessageBox.Cancel:
- return
- self.save_project(filename)
- self.file_opened.emit("project", filename)
- if not make_copy:
- self.project_filename = filename
- self.inform.emit("Project saved to: " + self.project_filename)
- else:
- self.inform.emit("Project copy saved to: " + self.project_filename)
- def open_gerber(self, filename, follow=False, outname=None):
- """
- Opens a Gerber file, parses it and creates a new object for
- it in the program. Thread-safe.
- :param filename: Gerber file filename
- :type filename: str
- :param follow: If true, the parser will not create polygons, just lines
- following the gerber path.
- :type follow: bool
- :return: None
- """
- App.log.debug("open_gerber()")
- proc = self.proc_container.new("Opening Gerber")
- self.progress.emit(10)
- # How the object should be initialized
- def obj_init(gerber_obj, app_obj):
- assert isinstance(gerber_obj, FlatCAMGerber)
- # Opening the file happens here
- self.progress.emit(30)
- try:
- gerber_obj.parse_file(filename, follow=follow)
- except IOError:
- app_obj.inform.emit("[error] Failed to open file: " + filename)
- app_obj.progress.emit(0)
- raise IOError('Failed to open file: ' + filename)
- except ParseError, e:
- app_obj.inform.emit("[error] Failed to parse file: " + filename)
- app_obj.progress.emit(0)
- self.log.error(str(e))
- raise
- #return
- # Further parsing
- self.progress.emit(70) # TODO: Note the mixture of self and app_obj used here
- # Object name
- name = outname or filename.split('/')[-1].split('\\')[-1]
- self.new_object("gerber", name, obj_init)
- # New object creation and file processing
- # try:
- # self.new_object("gerber", name, obj_init)
- # except:
- # e = sys.exc_info()
- # print "ERROR:", e[0]
- # traceback.print_exc()
- # self.message_dialog("Failed to create Gerber Object",
- # "Attempting to create a FlatCAM Gerber Object from " +
- # "Gerber file failed during processing:\n" +
- # str(e[0]) + " " + str(e[1]), kind="error")
- # GLib.timeout_add_seconds(1, lambda: self.set_progress_bar(0.0, "Idle"))
- # self.collection.delete_active()
- # return
- # Register recent file
- self.file_opened.emit("gerber", filename)
- self.progress.emit(100)
- proc.done()
- # GUI feedback
- self.inform.emit("Opened: " + filename)
- def open_excellon(self, filename, outname=None):
- """
- Opens an Excellon file, parses it and creates a new object for
- it in the program. Thread-safe.
- :param filename: Excellon file filename
- :type filename: str
- :return: None
- """
- App.log.debug("open_excellon()")
- self.progress.emit(10)
- # How the object should be initialized
- def obj_init(excellon_obj, app_obj):
- self.progress.emit(20)
- try:
- excellon_obj.parse_file(filename)
- except IOError:
- app_obj.inform.emit("[error] Cannot open file: " + filename)
- self.progress.emit(0) # TODO: self and app_bjj mixed
- raise IOError("Cannot open file: " + filename)
- try:
- excellon_obj.create_geometry()
- except Exception as e:
- app_obj.inform.emit("[error] Failed to create geometry after parsing: " + filename)
- self.progress.emit(0)
- raise e
- self.progress.emit(70)
- # Object name
- name = outname or filename.split('/')[-1].split('\\')[-1]
- self.new_object("excellon", name, obj_init)
- # New object creation and file processing
- # try:
- # self.new_object("excellon", name, obj_init)
- # except:
- # e = sys.exc_info()
- # App.log.error(str(e))
- # self.message_dialog("Failed to create Excellon Object",
- # "Attempting to create a FlatCAM Excellon Object from " +
- # "Excellon file failed during processing:\n" +
- # str(e[0]) + " " + str(e[1]), kind="error")
- # self.progress.emit(0)
- # self.collection.delete_active()
- # return
- # Register recent file
- self.file_opened.emit("excellon", filename)
- # GUI feedback
- self.inform.emit("Opened: " + filename)
- self.progress.emit(100)
- def open_gcode(self, filename, outname=None):
- """
- Opens a G-gcode file, parses it and creates a new object for
- it in the program. Thread-safe.
- :param filename: G-code file filename
- :type filename: str
- :return: None
- """
- App.log.debug("open_gcode()")
- # How the object should be initialized
- def obj_init(job_obj, app_obj_):
- """
- :type app_obj_: App
- """
- assert isinstance(app_obj_, App)
- self.progress.emit(10)
- try:
- f = open(filename)
- gcode = f.read()
- f.close()
- except IOError:
- app_obj_.inform.emit("[error] Failed to open " + filename)
- self.progress.emit(0)
- raise IOError("Failed to open " + filename)
- job_obj.gcode = gcode
- self.progress.emit(20)
- job_obj.gcode_parse()
- self.progress.emit(60)
- job_obj.create_geometry()
- # Object name
- name = outname or filename.split('/')[-1].split('\\')[-1]
- # New object creation and file processing
- try:
- self.new_object("cncjob", name, obj_init)
- except:
- e = sys.exc_info()
- App.log.error(str(e))
- self.message_dialog("Failed to create CNCJob Object",
- "Attempting to create a FlatCAM CNCJob Object from " +
- "G-Code file failed during processing:\n" +
- str(e[0]) + " " + str(e[1]), kind="error")
- self.progress.emit(0)
- self.collection.delete_active()
- return
- # Register recent file
- self.file_opened.emit("cncjob", filename)
- # GUI feedback
- self.inform.emit("Opened: " + filename)
- self.progress.emit(100)
- def open_project(self, filename):
- """
- Loads a project from the specified file.
- 1) Loads and parses file
- 2) Registers the file as recently opened.
- 3) Calls on_file_new()
- 4) Updates options
- 5) Calls new_object() with the object's from_dict() as init method.
- 6) Calls plot_all()
- :param filename: Name of the file from which to load.
- :type filename: str
- :return: None
- """
- App.log.debug("Opening project: " + filename)
- ## Open and parse
- try:
- f = open(filename, 'r')
- except IOError:
- App.log.error("Failed to open project file: %s" % filename)
- self.inform.emit("[error] Failed to open project file: %s" % filename)
- return
- try:
- d = json.load(f, object_hook=dict2obj)
- except:
- App.log.error("Failed to parse project file: %s" % filename)
- self.inform.emit("[error] Failed to parse project file: %s" % filename)
- f.close()
- return
- self.file_opened.emit("project", filename)
- ## Clear the current project
- ## NOT THREAD SAFE ##
- self.on_file_new()
- ##Project options
- self.options.update(d['options'])
- self.project_filename = filename
- #self.ui.units_label.setText("[" + self.options["units"] + "]")
- self.set_screen_units(self.options["units"])
- ## Re create objects
- App.log.debug("Re-creating objects...")
- for obj in d['objs']:
- def obj_init(obj_inst, app_inst):
- obj_inst.from_dict(obj)
- App.log.debug(obj['kind'] + ": " + obj['options']['name'])
- self.new_object(obj['kind'], obj['options']['name'], obj_init, active=False, fit=False, plot=False)
- self.plot_all()
- self.inform.emit("Project loaded from: " + filename)
- App.log.debug("Project loaded")
- def propagate_defaults(self):
- self.log.debug("propagate_defaults()")
- # Which objects to update the given parameters.
- routes = {
- "zdownrate": CNCjob,
- "excellon_zeros": Excellon,
- "cncjob_coordinate_format": CNCjob
- }
- for param in routes:
- if param in routes[param].defaults:
- routes[param].defaults[param] = self.defaults[param]
- self.log.debug(" " + param + " OK")
- else:
- # Try extracting the name:
- # classname_param here is param in the object
- if param.find(routes[param].__name__.lower() + "_") == 0:
- p = param[len(routes[param].__name__) + 1:]
- if p in routes[param].defaults:
- routes[param].defaults[p] = self.defaults[param]
- self.log.debug(" " + param + " OK!")
- def plot_all(self):
- """
- Re-generates all plots from all objects.
- :return: None
- """
- self.log.debug("plot_all()")
- self.plotcanvas.clear()
- self.progress.emit(10)
- def worker_task(app_obj):
- percentage = 0.1
- try:
- delta = 0.9 / len(self.collection.get_list())
- except ZeroDivisionError:
- self.progress.emit(0)
- return
- for obj in self.collection.get_list():
- obj.plot()
- percentage += delta
- self.progress.emit(int(percentage*100))
- self.progress.emit(0)
- self.plots_updated.emit()
- # Send to worker
- #self.worker.add_task(worker_task, [self])
- self.worker_task.emit({'fcn': worker_task, 'params': [self]})
- def register_folder(self, filename):
- self.defaults["last_folder"] = os.path.split(str(filename))[0]
- def set_progress_bar(self, percentage, text=""):
- self.ui.progress_bar.setValue(int(percentage))
- def setup_shell(self):
- """
- Creates shell functions. Runs once at startup.
- :return: None
- """
- self.log.debug("setup_shell()")
- def shelp(p=None):
- if not p:
- return "Available commands:\n" + '\n'.join([' ' + cmd for cmd in commands]) + \
- "\n\nType help <command_name> for usage.\n Example: help open_gerber"
- if p not in commands:
- return "Unknown command: %s" % p
- return commands[p]["help"]
- def options(name):
- ops = self.collection.get_by_name(str(name)).options
- return '\n'.join(["%s: %s" % (o, ops[o]) for o in ops])
- def h(*args):
- """
- Pre-processes arguments to detect '-keyword value' pairs into dictionary
- and standalone parameters into list.
- """
- kwa = {}
- a = []
- n = len(args)
- name = None
- for i in range(n):
- match = re.search(r'^-([a-zA-Z].*)', args[i])
- if match:
- assert name is None
- name = match.group(1)
- continue
- if name is None:
- a.append(args[i])
- else:
- kwa[name] = args[i]
- name = None
- return a, kwa
- def open_gerber(filename, *args):
- a, kwa = h(*args)
- types = {'follow': bool,
- 'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- self.open_gerber(str(filename), **kwa)
- def open_excellon(filename, *args):
- a, kwa = h(*args)
- types = {'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- self.open_excellon(str(filename), **kwa)
- def open_gcode(filename, *args):
- a, kwa = h(*args)
- types = {'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- self.open_gcode(str(filename), **kwa)
- def cutout(name, *args):
- a, kwa = h(*args)
- types = {'dia': float,
- 'margin': float,
- 'gapsize': float,
- 'gaps': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(name))
- except:
- return "Could not retrieve object: %s" % name
- def geo_init_me(geo_obj, app_obj):
- margin = kwa['margin']+kwa['dia']/2
- gap_size = kwa['dia']+kwa['gapsize']
- minx, miny, maxx, maxy = obj.bounds()
- minx -= margin
- maxx += margin
- miny -= margin
- maxy += margin
- midx = 0.5 * (minx + maxx)
- midy = 0.5 * (miny + maxy)
- hgap = 0.5 * gap_size
- pts = [[midx - hgap, maxy],
- [minx, maxy],
- [minx, midy + hgap],
- [minx, midy - hgap],
- [minx, miny],
- [midx - hgap, miny],
- [midx + hgap, miny],
- [maxx, miny],
- [maxx, midy - hgap],
- [maxx, midy + hgap],
- [maxx, maxy],
- [midx + hgap, maxy]]
- cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
- [pts[6], pts[7], pts[10], pts[11]]],
- "lr": [[pts[9], pts[10], pts[1], pts[2]],
- [pts[3], pts[4], pts[7], pts[8]]],
- "4": [[pts[0], pts[1], pts[2]],
- [pts[3], pts[4], pts[5]],
- [pts[6], pts[7], pts[8]],
- [pts[9], pts[10], pts[11]]]}
- cuts = cases[kwa['gaps']]
- geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
- try:
- obj.app.new_object("geometry", name+ "_cutout", geo_init_me)
- except Exception, e:
- return "Operation failed: %s" % str(e)
- return 'Ok'
- def mirror(name, *args):
- a, kwa = h(*args)
- types = {'box': str,
- 'axis': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(name))
- except:
- return "Could not retrieve object: %s" % name
- try:
- box = self.collection.get_by_name(kwa['box'])
- except:
- return "Could not retrieve object box: %s" % kwa['box']
- if obj is None:
- return "Object not found: %s" % name
- if box is None:
- return "Object box not found: %s" % kwa['box']
- if not isinstance(obj, FlatCAMGerber) and not isinstance(obj, FlatCAMExcellon):
- return "ERROR: Only Gerber and Excellon objects can be mirrored."
- try:
- xmin, ymin, xmax, ymax = box.bounds()
- px = 0.5 * (xmin + xmax)
- py = 0.5 * (ymin + ymax)
-
- obj.mirror(kwa['axis'], [px, py])
- obj.plot()
- except Exception, e:
- return "Operation failed: %s" % str(e)
- return 'Ok'
- def drillcncjob(name, *args):
- a, kwa = h(*args)
- types = {'tools': str,
- 'outname': str,
- 'drillz': float,
- 'travelz': float,
- 'feedrate': float}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(name))
- except:
- return "Could not retrieve object: %s" % name
- if obj is None:
- return "Object not found: %s" % name
- if not isinstance(obj, FlatCAMExcellon):
- return "ERROR: Only Excellon objects can be drilled."
- try:
-
- # Get the tools from the list
- job_name = kwa["outname"]
-
- # Object initialization function for app.new_object()
- def job_init(job_obj, app_obj):
- assert isinstance(job_obj, FlatCAMCNCjob)
-
- job_obj.z_cut = kwa["drillz"]
- job_obj.z_move = kwa["travelz"]
- job_obj.feedrate = kwa["feedrate"]
- job_obj.generate_from_excellon_by_tool(obj, kwa["tools"])
-
- job_obj.gcode_parse()
-
- job_obj.create_geometry()
-
- obj.app.new_object("cncjob", job_name, job_init)
- except Exception, e:
- return "Operation failed: %s" % str(e)
- return 'Ok'
- def isolate(name, *args):
- a, kwa = h(*args)
- types = {'dia': float,
- 'passes': int,
- 'overlap': float,
- 'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(name))
- except:
- return "Could not retrieve object: %s" % name
- if obj is None:
- return "Object not found: %s" % name
- try:
- obj.isolate(**kwa)
- except Exception, e:
- return "Operation failed: %s" % str(e)
- return 'Ok'
- def cncjob(obj_name, *args):
- a, kwa = h(*args)
- types = {'z_cut': float,
- 'z_move': float,
- 'feedrate': float,
- 'tooldia': float,
- 'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- try:
- obj.generatecncjob(**kwa)
- except Exception, e:
- return "Operation failed: %s" % str(e)
- return 'Ok'
- def write_gcode(obj_name, filename, preamble='', postamble=''):
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- try:
- obj.export_gcode(str(filename), str(preamble), str(postamble))
- except Exception, e:
- return "Operation failed: %s" % str(e)
- def paint_poly(obj_name, inside_pt_x, inside_pt_y, tooldia, overlap):
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- obj.paint_poly([float(inside_pt_x), float(inside_pt_y)], float(tooldia), float(overlap))
- def add_poly(obj_name, *args):
- if len(args) % 2 != 0:
- return "Incomplete coordinate."
- points = [[float(args[2*i]), float(args[2*i+1])] for i in range(len(args)/2)]
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- obj.add_polygon(points)
- def add_rectangle(obj_name, botleft_x, botleft_y, topright_x, topright_y):
- return add_poly(obj_name, botleft_x, botleft_y, botleft_x, topright_y,
- topright_x, topright_y, topright_x, botleft_y)
- def add_circle(obj_name, center_x, center_y, radius):
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- obj.add_circle([float(center_x), float(center_y)], float(radius))
- def set_active(obj_name):
- try:
- self.collection.set_active(str(obj_name))
- except Exception, e:
- return "Command failed: %s" % str(e)
- def delete(obj_name):
- try:
- self.collection.set_active(str(obj_name))
- self.on_delete()
- except Exception, e:
- return "Command failed: %s" % str(e)
- def geo_union(obj_name):
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- obj.union()
- def make_docs():
- output = ''
- import collections
- od = collections.OrderedDict(sorted(commands.items()))
- for cmd, val in od.iteritems():
- #print cmd, '\n', ''.join(['~']*len(cmd))
- output += cmd + ' \n' + ''.join(['~'] * len(cmd)) + '\n'
- t = val['help']
- usage_i = t.find('>')
- if usage_i < 0:
- expl = t
- #print expl + '\n'
- output += expl + '\n\n'
- continue
- expl = t[:usage_i - 1]
- #print expl + '\n'
- output += expl + '\n\n'
- end_usage_i = t[usage_i:].find('\n')
- if end_usage_i < 0:
- end_usage_i = len(t[usage_i:])
- #print ' ' + t[usage_i:]
- #print ' No parameters.\n'
- output += ' ' + t[usage_i:] + '\n No parameters.\n'
- else:
- extras = t[usage_i+end_usage_i+1:]
- parts = [s.strip() for s in extras.split('\n')]
- #print ' ' + t[usage_i:usage_i+end_usage_i]
- output += ' ' + t[usage_i:usage_i+end_usage_i] + '\n'
- for p in parts:
- #print ' ' + p + '\n'
- output += ' ' + p + '\n\n'
- return output
- def follow(obj_name, *args):
- a, kwa = h(*args)
- types = {'outname': str}
- for key in kwa:
- if key not in types:
- return 'Unknown parameter: %s' % key
- kwa[key] = types[key](kwa[key])
- try:
- obj = self.collection.get_by_name(str(obj_name))
- except:
- return "Could not retrieve object: %s" % obj_name
- if obj is None:
- return "Object not found: %s" % obj_name
- try:
- obj.follow(**kwa)
- except Exception, e:
- return "ERROR: %s" % str(e)
- def get_sys(param):
- if param in self.defaults:
- return self.defaults[param]
- return "ERROR: No such system parameter."
- def set_sys(param, value):
- # tcl string to python keywords:
- tcl2py = {
- "None": None,
- "none": None,
- "false": False,
- "False": False,
- "true": True,
- "True": True
- }
- if param in self.defaults:
- try:
- value = tcl2py[value]
- except KeyError:
- pass
- self.defaults[param] = value
- self.propagate_defaults()
- return "Ok"
- return "ERROR: No such system parameter."
- commands = {
- 'help': {
- 'fcn': shelp,
- 'help': "Shows list of commands."
- },
- 'open_gerber': {
- 'fcn': open_gerber,
- 'help': "Opens a Gerber file.\n' +"
- "> open_gerber <filename> [-follow <0|1>] [-outname <o>]\n' +"
- " filename: Path to file to open.\n" +
- " follow: If 1, does not create polygons, just follows the gerber path.\n" +
- " outname: Name of the created gerber object."
- },
- 'open_excellon': {
- 'fcn': open_excellon,
- 'help': "Opens an Excellon file.\n" +
- "> open_excellon <filename> [-outname <o>]\n" +
- " filename: Path to file to open.\n" +
- " outname: Name of the created excellon object."
- },
- 'open_gcode': {
- 'fcn': open_gcode,
- 'help': "Opens an G-Code file.\n" +
- "> open_gcode <filename> [-outname <o>]\n" +
- " filename: Path to file to open.\n" +
- " outname: Name of the created CNC Job object."
- },
- 'open_project': {
- 'fcn': self.open_project,
- "help": "Opens a FlatCAM project.\n" +
- "> open_project <filename>\n" +
- " filename: Path to file to open."
- },
- 'save_project': {
- 'fcn': self.save_project,
- 'help': "Saves the FlatCAM project to file.\n" +
- "> save_project <filename>\n" +
- " filename: Path to file to save."
- },
- 'set_active': {
- 'fcn': set_active,
- 'help': "Sets a FlatCAM object as active.\n" +
- "> set_active <name>\n" +
- " name: Name of the object."
- },
- 'get_names': {
- 'fcn': lambda: '\n'.join(self.collection.get_names()),
- 'help': "Lists the names of objects in the project.\n" +
- "> get_names"
- },
- 'new': {
- 'fcn': self.on_file_new,
- 'help': "Starts a new project. Clears objects from memory.\n" +
- "> new"
- },
- 'options': {
- 'fcn': options,
- 'help': "Shows the settings for an object.\n" +
- "> options <name>\n" +
- " name: Object name."
- },
- 'isolate': {
- 'fcn': isolate,
- 'help': "Creates isolation routing geometry for the given Gerber.\n" +
- "> isolate <name> [-dia <d>] [-passes <p>] [-overlap <o>]\n" +
- " name: Name of the object\n"
- " dia: Tool diameter\n passes: # of tool width\n" +
- " overlap: Fraction of tool diameter to overlap passes"
- },
- 'cutout': {
- 'fcn': cutout,
- 'help': "Creates cutout board.\n" +
- "> cutout <name> [-dia <3.0 (float)>] [-margin <0.0 (float)>] [-gapsize <0.5 (float)>] [-gaps <lr (4|tb|lr)>]\n" +
- " name: Name of the object\n" +
- " dia: Tool diameter\n" +
- " margin: Margin over bounds\n" +
- " gapsize: size of gap\n" +
- " gaps: type of gaps"
- },
- 'mirror': {
- 'fcn': mirror,
- 'help': "Mirror board.\n" +
- "> mirror <nameMirroredObject> -box <nameOfBox> [-axis <X|Y>]\n" +
- " name: Name of the object (Gerber or Excellon) to mirror\n" +
- " box: Name of object which act as box (cutout for example)\n" +
- " axis: Axis mirror over X or Y"
- },
- 'drillcncjob': {
- 'fcn': drillcncjob,
- 'help': "Drill CNC job.\n" +
- "> drillcncjob <name> -tools <str> -drillz <float> -travelz <float> -feedrate <float> -outname <str> \n" +
- " name: Name of the object\n" +
- " tools: Comma separated indexes of tools (example: 1,3 or 2)\n" +
- " drillz: Drill depth into material (example: -2.0)\n" +
- " travelz: Travel distance above material (example: 2.0)\n" +
- " feedrate: Drilling feed rate\n" +
- " outname: Name of object to create\n"
- },
- 'scale': {
- 'fcn': lambda name, factor: self.collection.get_by_name(str(name)).scale(float(factor)),
- 'help': "Resizes the object by a factor.\n" +
- "> scale <name> <factor>\n" +
- " name: Name of the object\n factor: Fraction by which to scale"
- },
- 'offset': {
- 'fcn': lambda name, x, y: self.collection.get_by_name(str(name)).offset([float(x), float(y)]),
- 'help': "Changes the position of the object.\n" +
- "> offset <name> <x> <y>\n" +
- " name: Name of the object\n" +
- " x: X-axis distance\n" +
- " y: Y-axis distance"
- },
- 'plot': {
- 'fcn': self.plot_all,
- 'help': 'Updates the plot on the user interface'
- },
- 'cncjob': {
- 'fcn': cncjob,
- 'help': 'Generates a CNC Job from a Geometry Object.\n' +
- '> cncjob <name> [-z_cut <c>] [-z_move <m>] [-feedrate <f>] [-tooldia <t>] [-outname <n>]\n' +
- ' name: Name of the source object\n' +
- ' z_cut: Z-axis cutting position\n' +
- ' z_move: Z-axis moving position\n' +
- ' feedrate: Moving speed when cutting\n' +
- ' tooldia: Tool diameter to show on screen\n' +
- ' outname: Name of the output object'
- },
- 'write_gcode': {
- 'fcn': write_gcode,
- 'help': 'Saves G-code of a CNC Job object to file.\n' +
- '> write_gcode <name> <filename>\n' +
- ' name: Source CNC Job object\n' +
- ' filename: Output filename'
- },
- 'paint_poly': {
- 'fcn': paint_poly,
- 'help': 'Creates a geometry object with toolpath to cover the inside of a polygon.\n' +
- '> paint_poly <name> <inside_pt_x> <inside_pt_y> <tooldia> <overlap>\n' +
- ' name: Name of the sourge geometry object.\n' +
- ' inside_pt_x, inside_pt_y: Coordinates of a point inside the polygon.\n' +
- ' tooldia: Diameter of the tool to be used.\n' +
- ' overlap: Fraction of the tool diameter to overlap cuts.'
- },
- 'new_geometry': {
- 'fcn': lambda name: self.new_object('geometry', str(name), lambda x, y: None),
- 'help': 'Creates a new empty geometry object.\n' +
- '> new_geometry <name>\n' +
- ' name: New object name'
- },
- 'add_poly': {
- 'fcn': add_poly,
- 'help': 'Creates a polygon in the given Geometry object.\n' +
- '> create_poly <name> <x0> <y0> <x1> <y1> <x2> <y2> [x3 y3 [...]]\n' +
- ' name: Name of the geometry object to which to append the polygon.\n' +
- ' xi, yi: Coordinates of points in the polygon.'
- },
- 'delete': {
- 'fcn': delete,
- 'help': 'Deletes the give object.\n' +
- '> delete <name>\n' +
- ' name: Name of the object to delete.'
- },
- 'geo_union': {
- 'fcn': geo_union,
- 'help': 'Runs a union operation (addition) on the components ' +
- 'of the geometry object. For example, if it contains ' +
- '2 intersecting polygons, this opperation adds them into' +
- 'a single larger polygon.\n' +
- '> geo_union <name>\n' +
- ' name: Name of the geometry object.'
- },
- 'add_rect': {
- 'fcn': add_rectangle,
- 'help': 'Creates a rectange in the given Geometry object.\n' +
- '> add_rect <name> <botleft_x> <botleft_y> <topright_x> <topright_y>\n' +
- ' name: Name of the geometry object to which to append the rectangle.\n' +
- ' botleft_x, botleft_y: Coordinates of the bottom left corner.\n' +
- ' topright_x, topright_y Coordinates of the top right corner.'
- },
- 'add_circle': {
- 'fcn': add_circle,
- 'help': 'Creates a circle in the given Geometry object.\n' +
- '> add_circle <name> <center_x> <center_y> <radius>\n' +
- ' name: Name of the geometry object to which to append the circle.\n' +
- ' center_x, center_y: Coordinates of the center of the circle.\n' +
- ' radius: Radius of the circle.'
- },
- 'make_docs': {
- 'fcn': make_docs,
- 'help': 'Prints command rererence in reStructuredText format.'
- },
- 'follow': {
- 'fcn': follow,
- 'help': 'Creates a geometry object following gerber paths.\n' +
- '> follow <name> [-outname <oname>]\n' +
- ' name: Name of the gerber object.\n' +
- ' outname: Name of the output geometry object.'
- },
- 'get_sys': {
- 'fcn': get_sys,
- 'help': 'Get the value of a system parameter (FlatCAM constant)\n' +
- '> get_sys <sysparam>\n' +
- ' sysparam: Name of the parameter.'
- },
- 'set_sys': {
- 'fcn': set_sys,
- 'help': 'Set the value of a system parameter (FlatCAM constant)\n' +
- '> set_sys <sysparam> <paramvalue>\n' +
- ' sysparam: Name of the parameter.\n' +
- ' paramvalue: Value to set.'
- }
- }
- # Add commands to the tcl interpreter
- for cmd in commands:
- self.tcl.createcommand(cmd, commands[cmd]['fcn'])
- # Make the tcl puts function return instead of print to stdout
- self.tcl.eval('''
- rename puts original_puts
- proc puts {args} {
- if {[llength $args] == 1} {
- return "[lindex $args 0]"
- } else {
- eval original_puts $args
- }
- }
- ''')
- def setup_recent_items(self):
- self.log.debug("setup_recent_items()")
- # TODO: Move this to constructor
- icons = {
- "gerber": "share/flatcam_icon16.png",
- "excellon": "share/drill16.png",
- "cncjob": "share/cnc16.png",
- "project": "share/project16.png"
- }
- openers = {
- 'gerber': lambda fname: self.worker_task.emit({'fcn': self.open_gerber, 'params': [fname]}),
- 'excellon': lambda fname: self.worker_task.emit({'fcn': self.open_excellon, 'params': [fname]}),
- 'cncjob': lambda fname: self.worker_task.emit({'fcn': self.open_gcode, 'params': [fname]}),
- 'project': self.open_project
- }
- # Open file
- try:
- f = open(self.data_path + '/recent.json')
- except IOError:
- App.log.error("Failed to load recent item list.")
- self.inform.emit("[error] Failed to load recent item list.")
- return
- try:
- self.recent = json.load(f)
- except json.scanner.JSONDecodeError:
- App.log.error("Failed to parse recent item list.")
- self.inform.emit("[error] Failed to parse recent item list.")
- f.close()
- return
- f.close()
- # Closure needed to create callbacks in a loop.
- # Otherwise late binding occurs.
- def make_callback(func, fname):
- def opener():
- func(fname)
- return opener
- # Reset menu
- self.ui.recent.clear()
- # Create menu items
- for recent in self.recent:
- filename = recent['filename'].split('/')[-1].split('\\')[-1]
- action = QtGui.QAction(QtGui.QIcon(icons[recent["kind"]]), filename, self)
- # Attach callback
- o = make_callback(openers[recent["kind"]], recent['filename'])
- action.triggered.connect(o)
- self.ui.recent.addAction(action)
- # self.builder.get_object('open_recent').set_submenu(recent_menu)
- # self.ui.menufilerecent.set_submenu(recent_menu)
- # recent_menu.show_all()
- # self.ui.recent.show()
- def setup_component_editor(self):
- label = QtGui.QLabel("Choose an item from Project")
- label.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
- self.ui.selected_scroll_area.setWidget(label)
- def setup_obj_classes(self):
- """
- Sets up application specifics on the FlatCAMObj class.
- :return: None
- """
- FlatCAMObj.app = self
- def version_check(self):
- """
- Checks for the latest version of the program. Alerts the
- user if theirs is outdated. This method is meant to be run
- in a separate thread.
- :return: None
- """
- self.log.debug("version_check()")
- full_url = App.version_url + \
- "?s=" + str(self.defaults['serial']) + \
- "&v=" + str(self.version) + \
- "&os=" + str(self.os) + \
- "&" + urllib.urlencode(self.defaults["stats"])
- App.log.debug("Checking for updates @ %s" % full_url)
- ### Get the data
- try:
- f = urllib.urlopen(full_url)
- except:
- App.log.warning("Failed checking for latest version. Could not connect.")
- self.inform.emit("[warning] Failed checking for latest version. Could not connect.")
- return
- try:
- data = json.load(f)
- except Exception, e:
- App.log.error("Could not parse information about latest version.")
- self.inform.emit("[error] Could not parse information about latest version.")
- App.log.debug("json.load(): %s" % str(e))
- f.close()
- return
- f.close()
- ### Latest version?
- if self.version >= data["version"]:
- App.log.debug("FlatCAM is up to date!")
- self.inform.emit("[success] FlatCAM is up to date!")
- return
- App.log.debug("Newer version available.")
- self.message.emit(
- "Newer Version Available",
- QtCore.QString("There is a newer version of FlatCAM " +
- "available for download:<br><br>" +
- "<B>" + data["name"] + "</b><br>" +
- data["message"].replace("\n", "<br>")),
- "info"
- )
- def enable_all_plots(self, *args):
- self.plotcanvas.clear()
- def worker_task(app_obj):
- percentage = 0.1
- try:
- delta = 0.9 / len(self.collection.get_list())
- except ZeroDivisionError:
- self.progress.emit(0)
- return
- for obj in self.collection.get_list():
- obj.options['plot'] = True
- obj.plot()
- percentage += delta
- self.progress.emit(int(percentage*100))
- self.progress.emit(0)
- self.plots_updated.emit()
- # Send to worker
- # self.worker.add_task(worker_task, [self])
- self.worker_task.emit({'fcn': worker_task, 'params': [self]})
- def save_project(self, filename):
- """
- Saves the current project to the specified file.
- :param filename: Name of the file in which to save.
- :type filename: str
- :return: None
- """
- self.log.debug("save_project()")
- ## Capture the latest changes
- # Current object
- try:
- self.collection.get_active().read_form()
- except:
- self.log.debug("[warning] There was no active object")
- pass
- # Project options
- self.options_read_form()
- # Serialize the whole project
- d = {"objs": [obj.to_dict() for obj in self.collection.get_list()],
- "options": self.options,
- "version": self.version}
- # Open file
- try:
- f = open(filename, 'w')
- except IOError:
- App.log.error("[error] Failed to open file for saving: %s", filename)
- return
- # Write
- json.dump(d, f, default=to_dict)
- # try:
- # json.dump(d, f, default=to_dict)
- # except Exception, e:
- # print str(e)
- # App.log.error("[error] File open but failed to write: %s", filename)
- # f.close()
- # return
- f.close()
- self.inform.emit("Project saved to: %s" % filename)
- # def main():
- #
- # app = QtGui.QApplication(sys.argv)
- # fc = App()
- # sys.exit(app.exec_())
- #
- #
- # if __name__ == '__main__':
- # main()
|