def add_keepout_to_board(pcb_filename, left_mm, top_mm, width_mm, height_mm): l = left_mm * MM_TO_KC t = top_mm * MM_TO_KC r = (left_mm + width_mm) * MM_TO_KC b = (top_mm + height_mm) * MM_TO_KC l = int(l) t = int(t) r = int(r) b = int(b) pcb = pcbnew.LoadBoard(pcb_filename) layer = pcbnew.F_Cu area = pcb.InsertArea(0, 0, layer, l, t, pcbnew.ZONE_CONTAINER.DIAGONAL_EDGE) area.SetIsKeepout(True) area.SetDoNotAllowTracks(True) area.SetDoNotAllowVias(True) area.SetDoNotAllowCopperPour(True) outline = area.Outline() outline.Append(r, t) outline.Append(r, b) outline.Append(l, b) # Thanks # https://github.com/NilujePerchut/kicad_scripts/blob/master/teardrops/td.py pcbnew.SaveBoard(pcb_filename, pcb)
def add_labels_to_board(pcb_filename, labels): pcb = pcbnew.LoadBoard(pcb_filename) for label in labels: draw_text(pcb, label["text"], label["x_mm"] * MM_TO_KC, label["y_mm"] * MM_TO_KC) pcbnew.SaveBoard(pcb_filename, pcb)
def test_replicate(x, y, within, polar): import difflib import os filename = '' if within is True and polar is False: filename = 'test_board_only_within.kicad_pcb' if within is False and polar is False: filename = 'test_board_all.kicad_pcb' if within is False and polar is True: filename = 'test_board_polar.kicad_pcb' # load test board board = pcbnew.LoadBoard('test_board.kicad_pcb') # run the replicator replicator = Replicator(board=board, pivot_module_reference='Q2002') replicator.replicate_layout(x, y, replicate_containing_only=within, remove_existing_nets_zones=True, replicate_tracks=True, replicate_zones=True, polar=polar) # save the board saved = pcbnew.SaveBoard('temp_'+filename, board) # compare files errnum = 0 with open('temp_'+filename, 'r') as correct_board: with open(filename, 'r') as tested_board: diff = difflib.unified_diff( correct_board.readlines(), tested_board.readlines(), fromfile='correct_board', tofile='tested_board', n=0) # remove temp file #os.remove('temp'+filename) # only timestamps on zones and file version information should differ diffstring = [] for line in diff: diffstring.append(line) # get rid of diff information del diffstring[0] del diffstring[0] # walktrough diff list and check for any significant differences for line in diffstring: index = diffstring.index(line) if '@@' in line: if ((('version' in diffstring[index + 1]) and ('version' in diffstring[index + 2])) or (('tstamp' in diffstring[index + 1]) and ('tstamp' in diffstring[index + 2]))): # this is not a problem pass else: # this is a problem errnum = errnum + 1 return errnum
def test_file(in_filename, out_filename, pivot_mod_ref, level, sheets, containing, remove): board = pcbnew.LoadBoard(in_filename) # get board information replicator = Replicator(board) # get pivot module info pivot_mod = replicator.get_mod_by_ref(pivot_mod_ref) # have the user select replication level levels = pivot_mod.filename # get the level index from user index = levels.index(levels[level]) # get list of sheets sheet_list = replicator.get_sheets_to_replicate(pivot_mod, pivot_mod.sheet_id[index]) # get acnhor modules anchor_modules = replicator.get_list_of_modules_with_same_id( pivot_mod.mod_id) # find matching anchors to maching sheets ref_list = [] for sheet in sheet_list: for mod in anchor_modules: if mod.sheet_id == sheet: ref_list.append(mod.ref) break alt_list = [('/').join(x[0]) + " (" + x[1] + ")" for x in zip(sheet_list, ref_list)] # get the list selection from user sheets_for_replication = [sheet_list[i] for i in sheets] # now we are ready for replication replicator.replicate_layout(pivot_mod, pivot_mod.sheet_id[0:index + 1], sheets_for_replication, containing=containing, remove=remove, tracks=True, zones=True, text=True, drawings=True) saved1 = pcbnew.SaveBoard(out_filename, board) test_file = out_filename.replace("temp", "test") return compare_boards.compare_boards(out_filename, test_file)
def pour_fills_on_board(pcb_filename): pcb = pcbnew.LoadBoard(pcb_filename) pcb.ComputeBoundingBox(False) bb = pcb.GetBoundingBox() l, t, r, b = bb.GetLeft(), bb.GetTop(), bb.GetRight(), bb.GetBottom() layertable = {} numlayers = pcbnew.PCB_LAYER_ID_COUNT for i in range(numlayers): layertable[pcb.GetLayerName(i)] = i nets = pcb.GetNetsByName() powernets = [] for name in ["GND"]: if (nets.has_key(name)): powernets.append((name, "F.Cu")) powernets.append((name, "B.Cu")) break for netname, layername in (powernets): net = nets.find(netname).value()[1] layer = layertable[layername] newarea = pcb.InsertArea(net.GetNet(), 0, layer, l, t, pcbnew.ZONE_CONTAINER.DIAGONAL_EDGE) newoutline = newarea.Outline() newoutline.Append(l, b) newoutline.Append(r, b) newoutline.Append(r, t) newarea.Hatch() filler = pcbnew.ZONE_FILLER(pcb) zones = pcb.Zones() filler.Fill(zones) pcbnew.SaveBoard(pcb_filename, pcb)
def add_outline_to_board(pcb_filename, left_mm, top_mm, width_mm, height_mm, usb_cutout_position=-1, usb_cutout_width=-1, modify_existing=True, margin_mm=0, corner_radius_mm=3): l = left_mm * MM_TO_KC t = top_mm * MM_TO_KC r = (left_mm + width_mm) * MM_TO_KC b = (top_mm + height_mm) * MM_TO_KC l = int(l) t = int(t) r = int(r) b = int(b) margin_kc = margin_mm * MM_TO_KC corner_rad_kc = corner_radius_mm * MM_TO_KC points = [ (l - margin_kc, t - margin_kc), (r + margin_kc, t - margin_kc), (r + margin_kc, b + margin_kc), (l - margin_kc, b + margin_kc), ] if modify_existing: pcb = pcbnew.LoadBoard(pcb_filename) else: pcb = pcbnew.BOARD() if usb_cutout_position >= 0: usb_cutout_left = (usb_cutout_position - usb_cutout_width / 2) * MM_TO_KC usb_cutout_right = (usb_cutout_position + usb_cutout_width / 2) * MM_TO_KC draw_segment(pcb, points[0][0] + corner_rad_kc, points[0][1], usb_cutout_left, points[0][1]) draw_segment(pcb, usb_cutout_right, points[1][1], points[1][0] - corner_rad_kc, points[1][1]) else: draw_segment(pcb, points[0][0] + corner_rad_kc, points[0][1], points[1][0] - corner_rad_kc, points[1][1]) draw_segment(pcb, points[1][0], points[1][1] + corner_rad_kc, points[2][0], points[2][1] - corner_rad_kc) draw_segment(pcb, points[2][0] - corner_rad_kc, points[2][1], points[3][0] + corner_rad_kc, points[3][1]) draw_segment(pcb, points[3][0], points[3][1] - corner_rad_kc, points[0][0], points[0][1] + corner_rad_kc) draw_arc(pcb, points[0][0] + corner_rad_kc, points[0][1] + corner_rad_kc, points[0][0], points[0][1] + corner_rad_kc, 90) draw_arc(pcb, points[1][0] - corner_rad_kc, points[1][1] + corner_rad_kc, points[1][0] - corner_rad_kc, points[1][1], 90) draw_arc(pcb, points[2][0] - corner_rad_kc, points[2][1] - corner_rad_kc, points[2][0], points[2][1] - corner_rad_kc, 90) draw_arc(pcb, points[3][0] + corner_rad_kc, points[3][1] - corner_rad_kc, points[3][0] + corner_rad_kc, points[3][1], 90) pcbnew.SaveBoard(pcb_filename, pcb)