KiCad 7 API Set

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.

My only understanding of “API” is “American Petroleum Institute.” But I did not get up oily this morning, so I do not understand your question. :crazy_face:

I suppose my response is of little help directly even though my first sentence is true. Perhaps it will help draw enough attention so that someone else can assist with a more intelligent response.

Actually my second sentence is also sort of true, even though it does not make a lot of sense.

Don’t forget that there’s also the American Betroleum Institute. :crazy_face:

But seriously, what problem X are you trying to solve, OP, rather than showing us your attempted solution to Y?

Application Programming Interface

I think the OP wants to create a PlugIn or Script to set the layer that a component is placed on.

I’ll just wait for a more detailed explanation of the OPs question. :slightly_smiling_face:

1 Like

Try if this helps: Plugin howtos · Wiki · eelik-kicad / KiCad Documentation · GitLab

1 Like

As far as I can see, there are only two possible layers, F.Cu and B.Cu. If you add a footprint in python it should be in F.Cu by default. If you want to change it to B.Cu, just use the Flip method:

>>> fp.Flip(fp.GetPosition(), False)
>>> pcbnew.Refresh()
>>> fp.GetLayer()
31
>>> pcbnew.LayerName(31)
'B.Cu'
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.