|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from argparse import ArgumentParser |
| 4 | +import pcbnew |
| 5 | +import logging |
| 6 | +from datetime import datetime |
| 7 | +import wx |
| 8 | + |
| 9 | +class CAMmer(): |
| 10 | + def __init__(self): |
| 11 | + pass |
| 12 | + |
| 13 | + def args_parse(self, args): |
| 14 | + # set up command-line arguments parser |
| 15 | + parser = ArgumentParser(description="A script to generate and zip PCB Gerber and drill files.") |
| 16 | + parser.add_argument( |
| 17 | + "-p", "--path", help="Path to the *.kicad_pcb file" |
| 18 | + ) |
| 19 | + parser.add_argument( |
| 20 | + "-l", "--layers", help="CSV list of copper, silk and resist layers" |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + "-e", "--edges", help="CSV list of edge (outline / keep out) layers" |
| 24 | + ) |
| 25 | + return parser.parse_args(args) |
| 26 | + |
| 27 | + def startCAMmer(self, args, board=None, logger=None): |
| 28 | + """The main method |
| 29 | +
|
| 30 | + Args: |
| 31 | + args - the command line args [1:] - parsed with args_parse |
| 32 | + board - the KiCad BOARD when running in a plugin |
| 33 | + logger - logging logger from parent |
| 34 | +
|
| 35 | + Returns: |
| 36 | + sysExit - the value for sys.exit (if called from __main__) |
| 37 | + report - a helpful text report |
| 38 | + """ |
| 39 | + |
| 40 | + sysExit = -1 # -1 indicates sysExit has not (yet) been set. The code below will set this to 0, 1, 2. |
| 41 | + report = "\nSTART: " + datetime.now().isoformat() + "\n" |
| 42 | + |
| 43 | + if logger is None: |
| 44 | + logger = logging.getLogger() |
| 45 | + logger.setLevel([ logging.WARNING, logging.DEBUG ][args.verbose]) |
| 46 | + |
| 47 | + logger.info('CAMMER START: ' + datetime.now().isoformat()) |
| 48 | + |
| 49 | + # Read the args |
| 50 | + sourceBoardFile = args.path |
| 51 | + layers = args.layers |
| 52 | + edges = args.edge |
| 53 | + |
| 54 | + # Check if this is running in a plugin |
| 55 | + if board is None: |
| 56 | + if sourceBoardFile is None: |
| 57 | + report += "No path to kicad_pcb file. Quitting.\n" |
| 58 | + sysExit = 2 |
| 59 | + return sysExit, report |
| 60 | + else: |
| 61 | + # Check that input board is a *.kicad_pcb file |
| 62 | + sourceFileExtension = os.path.splitext(sourceBoardFile)[1] |
| 63 | + if not sourceFileExtension == ".kicad_pcb": |
| 64 | + report += sourceBoardFile + " is not a *.kicad_pcb file. Quitting.\n" |
| 65 | + sysExit = 2 |
| 66 | + return sysExit, report |
| 67 | + |
| 68 | + # Load source board from file |
| 69 | + board = pcbnew.LoadBoard(sourceBoardFile) |
| 70 | + outputPath = os.path.split(sourceBoardFile)[0] # Get the file path head |
| 71 | + zipFile = os.path.split(sourceBoardFile)[1] # Get the file path tail |
| 72 | + zipFile = os.path.join(outputPath, os.path.splitext(zipFile)[0] + ".zip") |
| 73 | + else: # Running in a plugin |
| 74 | + outputPath = os.path.split(board.GetFileName())[0] # Get the file path head |
| 75 | + zipFile = os.path.split(board.GetFileName())[1] # Get the file path tail |
| 76 | + zipFile = os.path.join(outputPath, os.path.splitext(zipFile)[0] + ".zip") |
| 77 | + |
| 78 | + if board is None: |
| 79 | + report += "Could not load board. Quitting.\n" |
| 80 | + sysExit = 2 |
| 81 | + return sysExit, report |
| 82 | + |
| 83 | + if (not layers) and (not edges): |
| 84 | + report += "No layers and edges defined. Quitting.\n" |
| 85 | + sysExit = 2 |
| 86 | + return sysExit, report |
| 87 | + |
| 88 | + # Check if about to overwrite a zip file |
| 89 | + if os.path.isfile(zipFile): |
| 90 | + if wx.GetApp() is not None: |
| 91 | + resp = wx.MessageBox("You are about to overwrite existing files.\nAre you sure?", |
| 92 | + 'Warning', wx.OK | wx.CANCEL | wx.ICON_WARNING) |
| 93 | + if resp != wx.OK: |
| 94 | + report += "User does not want to overwrite the files. Quitting.\n" |
| 95 | + sysExit = 1 |
| 96 | + return sysExit, report |
| 97 | + |
| 98 | + |
| 99 | + # ADD MUCH STUFF HERE |
| 100 | + |
| 101 | + |
| 102 | + if sysExit < 0: |
| 103 | + sysExit = 0 |
| 104 | + |
| 105 | + return sysExit, report |
| 106 | + |
| 107 | + def startCAMmerCommand(self, command, board=None, logger=None): |
| 108 | + |
| 109 | + parser = self.args_parse(command) |
| 110 | + |
| 111 | + sysExit, report = self.startCAMmer(parser, board, logger) |
| 112 | + |
| 113 | + return sysExit, report |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + |
| 117 | + cammer = CAMmer() |
| 118 | + |
| 119 | + if len(sys.argv) < 2: |
| 120 | + parser = cammer.args_parse(['-h']) # Test args: e.g. ['-p','<path to board>'] |
| 121 | + else: |
| 122 | + parser = cammer.args_parse(sys.argv[1:]) #Parse the args |
| 123 | + |
| 124 | + sysExit, report = cammer.startCAMmer(parser) |
| 125 | + |
| 126 | + print(report) |
| 127 | + |
| 128 | + sys.exit(sysExit) |
0 commit comments