Hi all, first time poster here so apologies if I’m in the wrong place.
I’m having issues using the Python APIs to swap out a footprint for a pair of footprints - I want to replace a single footprint with two different footprints, and move/rotate the new footprints a bit to keep them in the correct spots. Here’s my code:
import pcbnew # Initialize board = pcbnew.LoadBoard("E:\\path\\to\\pcb.kicad_pcb") modules = board.GetModules() # Get relevant footprints def GetFPID(lib, name): libpath = "E:\\KiCad\\share\\kicad\\modules\\" + lib src_type = pcbnew.IO_MGR.GuessPluginTypeFromLibPath(libpath); plugin = pcbnew.IO_MGR.PluginFind(src_type) fp = plugin.FootprintLoad(libpath, name) return fp.GetFPID() new_keysw = GetFPID("Button_Switch_Keyboard.pretty", "SW_Cherry_MX_1.00u_PCB") new_led = GetFPID("LED_THT.pretty", "LED_D1.8mm_W3.3mm_H2.4mm") # Do the conversion count = 0 for module in modules: if module.GetFPID().GetUniStringLibItemName() == "SW_Cherry_MX_1.00u_PCB_LED": print("Replacing " + module.GetReference()) module.SetFPID(new_keysw) module.Move(pcbnew.wxPoint(2.54, -5)) led = pcbnew.MODULE(board) led.SetFPID(new_led) led_pos = pcbnew.wxPoint(module.GetPosition().x - 3.81, module.GetPosition().y + 10.13) led.SetPosition(led_pos) led.SetOrientation(180) count = count + 1 board.Save("pcb.kicad_pcb") break print("Replaced " + count + " footprints")
I have to run this in a silly line-by-line fashion in the shell otherwise it executes the first line and nothing else (not sure what’s up there) but where the real problems start is the SetFPID call doesn’t seem to actually replace the footprint. I can see that the object in the PCB references the new footprint, but it is visually not that footprint. I have to go to ‘Update footprint’ from its properties for the change to take effect. Also, creating the LED module doesn’t seem to work at all, but I’m at a loss as to what to do with just the docs to guide me. Please let me know if there’s just a function I’m missing or if I’m going about this entirely the wrong way. Thanks.