← Back to team overview

kicad-developers team mailing list archive

Re: python sample for manual

 

Le 09/12/2016 à 17:25, kinichiro inoguchi a écrit :
> Hi,
> 
> Could someone provide the suitable python sample script for this
> document section ?
> ”15.4. Listing and Loading Libraries”
> http://docs.kicad-pcb.org/stable/en/pcbnew.html#_listing_and_loading_libraries
> 
> Now we are trying to update python section of pcbnew manual.
> And current python sample is for old .mod and outdated.
> 
> https://github.com/KiCad/kicad-doc/issues/438
> 
> Thanks.
> 
> Kinichiro

Hi Kinichiro,

Here is a example which should works with .pretty libs (just check/edit the library name).
I added a few comments in source.

Note also:
* SEGVIA was renamed VIA since a long time.
* board.GetSegZones() must be removed from doc: this is a very old method only used to read very old
boards.

Guys who are working on doc are doing a very good job.
Thanks.

-- 
Jean-Pierre CHARRAS
#!/usr/bin/python

from pcbnew import *

#libpath = "/usr/share/kicad/modules/Sockets.pretty"
libpath = "f:/kicad/share/kicad/modules/Sockets.pretty"
print ">> enumerate footprints, pads of",libpath

# Load the suitable plugin to read/write the .pretty library
# (containing the .kicad_mod footprint files)
src_type = IO_MGR.GuessPluginTypeFromLibPath( libpath );
# Rem: we can force the plugin type by using IO_MGR.PluginFind( IO_MGR.KICAD )
plugin = IO_MGR.PluginFind( src_type )

# Print plugin type name: (Expecting "KiCad" for a .pretty library)
print( "Selected plugin type: %s" % plugin.PluginName() )

list_of_footprints = plugin.FootprintEnumerate(libpath)

for name in list_of_footprints:
    fp = plugin.FootprintLoad(libpath,name)
    # print the short name of the footprint
    print name  # this is the name inside the loaded library
    # followed by ref field, value field, and decription string:
    # Remember ref and value texts are dummy texts, replaced by the schematic values
    # when reading a netlist.
    print "  ->", fp.GetReference(), fp.GetValue(), fp.GetDescription()

    # print pad info: GetPos0() is the pad position relative to the footrint position
    for pad in fp.Pads():
        print "    pad [%s]" % pad.GetPadName(), "at",\
        "pos0", ToMM(pad.GetPos0().x), ToMM(pad.GetPos0().y),"mm",\
        "shape offset", ToMM(pad.GetOffset().x), ToMM(pad.GetOffset().y), "mm"
    print ""

Follow ups

References