SchrodingersGat / KiBoM

Configurable BoM generation tool for KiCad EDA (http://kicad.org/)
MIT License
352 stars 95 forks source link

New field defined from database query #76

Closed cnieves1 closed 4 years ago

cnieves1 commented 4 years ago

Hi, I'm trying to add KiBoM a new feature: define new fields whose values will be database queries. Right now, I added a [DATABASE] section to the config file, and a database.py new file to handle all the related function. I am able to connect and disconect to the database, and now, I'm trying to define a new field for a component, but I can't understand how to do this...

In KiBoM_CLI.py, I understand that this new code should go after:

# Read out the netlist
net = netlist(input_file, prefs=pref)

# Extract the components
components = net.getInterestingComponents()

I would like to insert a new field with its value for each component. How could I do it?

Thanks,

cnieves1 commented 4 years ago

I managed to do this. For reference, a new field for each componennt can be created using the following code:

    for c in components:
        fields = c.element.getChild('fields')
        if not fields: # If there is no child 'flelds', then this component has no fields. Add it before adding fields
            fields = xmlElement('fields', c.element)
            c.element.addChild(fields)

        newChild = xmlElement('field', fields)
        newChild.addAttribute('name', 'new_field_name') # Add field name 
        newChild.setChars('field_value') # Set field value for this component
        fields.addChild(newChild)