def convert_and_delete_modules(midline, remove_layer, brd):

    import faceplate_footprint_lib
    footprint_lib = faceplate_footprint_lib.get_lib_location()

    footprint_convert = {
        # POTS
        'TRIM-T73YE': 'Faceplate_Hole_Trim_3.175mm_With_Mask_Opening',
        'Potentiometer_Alps_RK09L_Double_Vertical': 'Faceplate_Hole_Pot_16mm',
        'Potentiometer_Alpha_RV112_Dual_Vert': 'Faceplate_Hole_Pot_16mm',
        'Potentiometer_Alpha_RV112_Dual_Vert_Knurled':
        'Faceplate_Hole_Pot_16mm',
        '16MM-RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
        'Pot_16mm_21Det_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
        'Pot_16mm_NoDet_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
        'Pot_16mm_CtrDet_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
        'POT-9MM-ALPHA': 'Faceplate_Hole_Pot_9mm_Metal_Collar',
        'Pot_9mm_DShaft': 'Faceplate_Hole_Pot_9mm_Metal_Collar',
        'Pot_9mm_DShaft_Det': 'Faceplate_Hole_Pot_9mm_Metal_Collar',
        'POT-9MM-SONGHUEI': 'Faceplate_Hole_Pot_9mm',
        'Pot_9mm_Knurl_Det': 'Faceplate_Hole_Pot_9mm',
        'Pot_9mm_Knurl_NoDet': 'Faceplate_Hole_Pot_9mm',

        # SLIDERS:
        'Pot_Slider_LED_20mm_RA2045F': 'Faceplate_Hole_Slider_25mm_Slot',
        'POT-SLIDER-LED-ALPHA-RA2045F-20': 'Faceplate_Hole_Slider_25mm_Slot',
        'POT-SLIDER-ALPHA-RA2045F-20': 'Faceplate_Hole_Slider_25mm_Slot',

        # ENCODERS:
        'ENC_SPST_12mm': 'Faceplate_Hole_Encoder_290',
        'ENC_SPST_12mm_NUDGED': 'Faceplate_Hole_Encoder_290',
        # Alternative:
        #'ENC_SPST_12mm_NUDGED': 'Faceplate_Hole_Encoder_KnurledShaft_Alpha'
        'ROTENC-12MM-BUT': 'Faceplate_Hole_Encoder_290',
        'RGB_ROTARY_ENCODER': 'Faceplate_Hole_Encoder_RGB_NoBushing',
        'ENC_RGB_SPST_12mm': 'Faceplate_Hole_Encoder_RGB_NoBushing',
        'ENC_RGB_SPST_12mm_NUDGED': 'Faceplate_Hole_Encoder_RGB_NoBushing',

        # JACKS:
        'PJ301M-12': 'Faceplate_Hole_Jack_3.5mm',
        'PJ366ST': 'Faceplate_Hole_Jack_3.5mm',
        'PJ301M-12-FIX': 'Faceplate_Hole_Jack_3.5mm',
        'EighthInch_PJ398SM': 'Faceplate_Hole_Jack_3.5mm',
        'EighthInch_Stereo_PJ366ST': 'Faceplate_Hole_Jack_3.5mm',
        'Barrel_Vert_PJ064': 'Faceplate_Hole_Jack_Barrel_315',
        'EighthInch_PJ398SM_Alt-GND': 'Faceplate_Hole_Jack_3.5mm',
        'XLR-NCJ6FA-V-0': 'Faceplate_Hole_XLR_Quarter_Inch',

        # LEDS AND LIGHTPIPES:
        'LED-PLCC4': 'Faceplate_Hole_Lightpipe_With_Mask_Opening',
        'LED_PLCC-4': 'Faceplate_Hole_Lightpipe_With_Mask_Opening',
        'LED_0603_1608Metric': 'Faceplate_Hole_Lightpipe_With_Mask_Opening',
        'PLCC4': 'Faceplate_Hole_Lightpipe_With_Mask_Opening',
        'LED-C1-A2-3MM-VERT': 'Faceplate_Hole_LED_3mm',
        'LED_D3.0mm-3': 'Faceplate_Hole_LED_3mm',
        'LED-3MM-SQUARE-ANODE': 'Faceplate_Hole_LED_3mm',
        'LED-T1X2': 'Faceplate_Hole_LED_3mm',
        'LED-T1': 'Faceplate_Hole_LED_3mm',
        'LED_3mm_C1A2': 'Faceplate_Hole_LED_3mm',

        # FLIP SWITCHES
        'Switch_Toggle_SPDT_Mini_SolderLug':
        'Switch_Toggle_Mini_6.35mm_With_Mask_Opening',
        'SPDT-SUB': 'Faceplate_Hole_SubMini_Toggle',
        'Switch_Toggle_SPDT_SubMini': 'Faceplate_Hole_SubMini_Toggle',

        # BUTTONS:
        'BUTTON-LED-PB61303':
        'Faceplate_Hole_LED_Button_7mm_With_Mask_Opening',
        'RGB-SPST-LED-TC002':
        'Faceplate_Hole_LED_Button_5.4mm_With_Mask_Opening',
        'Button_RgbLED_SPST_TC002':
        'Faceplate_Hole_LED_Button_5.4mm_With_Mask_Opening',
        'Button_LED_PB61303_Adjusted+':
        'Faceplate_Hole_LED_Button_7mm_With_Mask_Opening',
        'Button_LED_PB61303':
        'Faceplate_Hole_LED_Button_7mm_With_Mask_Opening',
        'Button_RgbLED_SPST_PB615303HL-7mm':
        'Faceplate_Hole_LED_Button_7mm_With_Mask_Opening',

        # SLIDE SWITCHES
        'Slide_Switch_SS22D06-G6-H_Runrun':
        'Faceplate_Slide_Switch_SS22D06_Runrun',
        'Slide_Switch_Runrun_SS22D06': 'Faceplate_Slide_Switch_SS22D06_Runrun',
        'SWITCHCRAFT-STEREO-SW_with_outline':
        'Faceplate_Hole_Jack_Quarter_Inch',

        # SPACERS/MOUNTS/SLOTS
        'Faceplate_Rail_Mount_Slot': 'Faceplate_Rail_Mount_Slot',
        'Faceplate_Hole_Spacer_Mount_256': 'Faceplate_Hole_Spacer_Mount_256',
        'Faceplate_Hole_FSR_slot': 'Faceplate_Hole_FSR_slot',
        'last_item': 'last_item'
    }
    remove_fps = [
        'R0603', 'C0603', 'PAD-06', 'SOT-363_SC-70-6', 'SOT23-3_PO132',
        'R_0603', 'C_0603', 'C_1206'
    ]

    io = pcbnew.PCB_IO()
    SCALE = 1000000.0

    msg = ""

    for m in brd.GetModules():
        center = m.GetPosition()
        try:
            footpr = str(m.GetFPID().GetFootprintName())
        except:
            footpr = str(m.GetFPID().GetLibItemName())

        if footpr in footprint_convert:
            msg += "Found Back layer footprint: {} at {}mm,{}mm. Changing to {}".format(
                footpr, center.x / SCALE, center.y / SCALE,
                footprint_convert[footpr])
            msg += "\n"
            faceplate_mod = io.FootprintLoad(footprint_lib,
                                             footprint_convert[footpr])

            # Reflect over midline y-axis
            new_x = midline - (center.x - midline)
            center.x = new_x
            faceplate_mod.SetPosition(center)

            # pads = faceplate_mod.Pads()
            # for pad in pads:
            #   pad.SetNet(net)

            brd.Add(faceplate_mod)

            brd.Remove(m)
            continue

        if m.GetLayer() == remove_layer:
            brd.Remove(m)
            continue

        if footpr in remove_fps:
            brd.Remove(m)
            continue

        msg += "Unknown Back layer footprint: {} at {}mm,{}mm. ".format(
            footpr, center.x / SCALE, center.y / SCALE)
        msg += "\n"

    return msg
    def Run(self):
        import faceplate_footprint_lib
        footprint_lib = faceplate_footprint_lib.get_lib_location()

        railmount_fp = "Faceplate_Rail_Mount_Slot"

        SCALE = 1000000.0
        msg = ""

        board = pcbnew.GetBoard()
        io = pcbnew.PCB_IO()

        # Find the pcb outline and a list of the drawings on the edgecuts layer
        pcboutline, edgecuts_dwgs = find_pcb_outline_bbox(board)

        # Find the center of the pcb outline
        pcbcenter = pcboutline.Centre()

        # Move the previous edge cuts to comments layer
        move_drawings(edgecuts_dwgs, pcbnew.Cmts_User)

        # Set the fp width to the smallest standard HP size that's larger than the pcb width
        pcbwidth = pcboutline.GetWidth()
        fphp, fpwidth = find_width_to_hp(pcbwidth / SCALE)

        msg += "Faceplate is %d HP wide by 128.5mm high\n" % fphp

        # Calculate the left and right edges of the faceplate
        fpleft = pcbcenter.x - fpwidth * SCALE / 2.0
        fpright = fpleft + fpwidth * SCALE

        # Calculate the top and bottom edges of the faceplate (128.5mm height)
        fpbottom = pcbcenter.y + 128.5 * SCALE / 2.0
        fptop = fpbottom - 128.5 * SCALE

        # Calculate the four corners
        bottomleft = pcbnew.wxPoint(int(fpleft), int(fpbottom))
        bottomright = pcbnew.wxPoint(int(fpright), int(fpbottom))
        topleft = pcbnew.wxPoint(int(fpleft), int(fptop))
        topright = pcbnew.wxPoint(int(fpright), int(fptop))

        # Draw the board outline segments
        bottomline = pcbnew.DRAWSEGMENT(board)
        board.Add(bottomline)
        bottomline.SetLayer(pcbnew.Edge_Cuts)
        bottomline.SetStart(bottomleft)
        bottomline.SetEnd(bottomright)

        topline = pcbnew.DRAWSEGMENT(board)
        board.Add(topline)
        topline.SetLayer(pcbnew.Edge_Cuts)
        topline.SetStart(topleft)
        topline.SetEnd(topright)

        leftline = pcbnew.DRAWSEGMENT(board)
        board.Add(leftline)
        leftline.SetLayer(pcbnew.Edge_Cuts)
        leftline.SetStart(topleft)
        leftline.SetEnd(bottomleft)

        rightline = pcbnew.DRAWSEGMENT(board)
        board.Add(rightline)
        rightline.SetLayer(pcbnew.Edge_Cuts)
        rightline.SetStart(topright)
        rightline.SetEnd(bottomright)

        #add rail mount slots
        railmount_topleft = pcbnew.wxPoint(topleft.x + 0.295 * 25.4 * SCALE,
                                           topleft.y + 0.118 * 25.4 * SCALE)
        railmount_topright = pcbnew.wxPoint(topright.x - 0.295 * 25.4 * SCALE,
                                            topright.y + 0.118 * 25.4 * SCALE)
        railmount_bottomleft = pcbnew.wxPoint(
            bottomleft.x + 0.295 * 25.4 * SCALE,
            bottomleft.y - 0.118 * 25.4 * SCALE)
        railmount_bottomright = pcbnew.wxPoint(
            bottomright.x - 0.295 * 25.4 * SCALE,
            bottomright.y - 0.118 * 25.4 * SCALE)

        mod = io.FootprintLoad(footprint_lib, railmount_fp)
        mod.SetPosition(railmount_topleft)
        board.Add(mod)

        mod = io.FootprintLoad(footprint_lib, railmount_fp)
        mod.SetPosition(railmount_topright)
        board.Add(mod)

        mod = io.FootprintLoad(footprint_lib, railmount_fp)
        mod.SetPosition(railmount_bottomleft)
        board.Add(mod)

        mod = io.FootprintLoad(footprint_lib, railmount_fp)
        mod.SetPosition(railmount_bottomright)
        board.Add(mod)

        msg += "Creating four rail mount slots (for 8HP and smaller faceplates, delete two of these)" + "\n"
        msg += "You may need to refresh the display now. Select Legacy mode, then Modern mode" + "\n"
# Run this 3rd
# 
#
# Delete all modules except for known faceplate modules
# 
# Todo: perhaps we could set an attribute of each module
# that tells what its faceplate equivlent is
# Then delete everything without this attribute

import pcbnew
import wx

import faceplate_footprint_lib
footprint_lib = faceplate_footprint_lib.get_lib_location()

SCALE = 1000000.0

footprint_convert={
    # POTS
    'TRIM-T73YE': 'Faceplate_Hole_Trim_3.175mm_With_Mask_Opening',

    'Potentiometer_Alps_RK09L_Double_Vertical': 'Faceplate_Hole_Pot_16mm',

    'Potentiometer_Alpha_RV112_Dual_Vert': 'Faceplate_Hole_Pot_16mm',
    'Potentiometer_Alpha_RV112_Dual_Vert_Knurled': 'Faceplate_Hole_Pot_16mm',

    '16MM-RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
    'Pot_16mm_21Det_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
    'Pot_16mm_NoDet_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',
    'Pot_16mm_CtrDet_RV16AF-4A': 'Faceplate_Hole_Pot_16mm',