Hey guys, does anyone have knowledge on setting a layer for a component on Kicad 7 API ? ChatGPT cannot get it done lol. The output is aslways invalid layer:
import pcbnew
board = pcbnew.GetBoard()
library_path = "C:\\Program Files\\KiCad\\7.0\\share\\kicad\\footprints\\Resistor_SMD.pretty"
component_name = "R_0402_1005Metric"
plugin_type = pcbnew.IO_MGR.GuessPluginTypeFromLibPath(library_path)
io_mgr = pcbnew.IO_MGR.PluginFind(plugin_type)
footprint = io_mgr.FootprintLoad(library_path, component_name)
# Create a dictionary to map layer names to their IDs
layer_map = {
"F.Cu": 0,
"B.Cu": 31
}
if not footprint:
print("Failed to load the footprint.")
else:
# Set properties for the footprint
footprint.SetReference("R" + str(len(board.Footprints()) + 1))
value = input("Enter the value for the resistor (e.g., 10K, 100Ω): ").strip()
footprint.SetValue(value)
# Set position using VECTOR2I
position = pcbnew.VECTOR2I(int(10*1e6), int(10*1e6)) # Convert mm to nm (nanometers)
footprint.SetPosition(position)
# Ask for layer and set it
while True:
layer_input = input("Enter the layer for the resistor (e.g., F.Cu for the front, B.Cu for the back): ").strip()
if layer_input in layer_map and board.IsLayerEnabled(layer_map[layer_input]):
footprint.SetLayer(layer_map[layer_input])
break
else:
print("Invalid layer or layer not enabled. Please enter a valid layer.")
# Add the footprint to the board
board.Add(footprint)
print(f"Resistor of {value} added to {layer_input} layer.")
I hereby certify that I am not simply asking someone else to design a footprint for me.
This is an auto-generated message that is in place on the “footprints” section of the KiCad.info forum. If I remove it and ask for a footprint to be designed anyway, I understand that I will be subject to forum members telling me to go design my own footprint or referring me to a 3rd party footprint site.