Python API's ExportSpecctraDSN() broken in nightly?

Hi Everyone!

I’m trying to use the python interface of pcbnew to export/import Specctra DSN/SES files, but I’m running into some trouble.

I’ve installed the KiCad nightly PPA on Ubuntu 20.04 following the instructions here. However, when I call ExportSpecctraDSN(“output.dsn”) it returns false. Other commands seem to work fine such as printing out the modules in the file.

Does anyone have any suggestions for how to debug further into what might be going wrong? I’ve checked that exporting works from the pcbnew UI. Importing is broken similarly.

import sys                                                                                                                                                     
sys.path.insert(1, '/usr/lib/kicad-nightly/lib/python3/dist-packages/')                                                                                        
import pcbnew                                                                                                                                                  
                                                                                                                                                               
b = pcbnew.LoadBoard(sys.argv[1])                                                                                                                              
for m in b.GetModules():                                                                                                                                       
  print(m)                                                                                                                                                     
                                                                                                                                                               
assert(pcbnew.ExportSpecctraDSN("TMP.dsn")) 

Thanks!
Gabe

If you look at the code for that function it depends on pcbnew window being open.

You can’t use this API from pure python session, only from pcbnew scripting console or plugin.

Wow, thanks for the quick response! I appreciate it! (Your username is very apt.)

Do you have any other suggestions for how to automate SpecctraDSN exporting from the commandline, before I consider trying to factor out the import/export myself?

If you are up to it, refactoring kicad code to separate specctra related code from UI stuff so that the api doesn’t need a window is the only way I can think of.

There has been developer discussion about these kind of things in general, they want to separate logic from the UI. At the moment those two are too intertwined. But that takes much work and time. Unfortunately the developers haven’t been willing to write out any details which could help outsiders, so you just have to ask about this detail in the mailing list. IIRC producing output files has been one area which has been mentioned explicitly.

Thanks everyone!

I’ve downloaded the source and have pcbnew building with the following config:

cmake  -DCMAKE_BUILD_TYPE=Release \
-DKICAD_SCRIPTING=ON \
-DKICAD_SCRIPTING_PYTHON3=ON \
-DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON \
-DKICAD_SCRIPTING_MODULES=ON \
-DKICAD_SCRIPTING_WXPYTHON=ON ../../

make -j 12 pcbnew

However, when I run the following:

import sys
sys.path.insert(1, "/home/gtaubman/kicad/build/release/pcbnew/")
import pcbnew

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gtaubman/kicad/build/release/pcbnew/pcbnew.py", line 903, in <module>
    PCB_MODULE_ZONE_AREA_T = _pcbnew.PCB_MODULE_ZONE_AREA_T
AttributeError: module '_pcbnew' has no attribute 'PCB_MODULE_ZONE_AREA_T'

Does anyone have any suggestions for what i might be doing wrong when building, such that some symbols are not available but others are?

It’s not enough to import pcbnew.py module, you need to put the python dynamic library that pcbnew.py uses in LD_LIBRARY_PATH or whatever python uses to look for it’s libraries. I think it’s called either pcbnew.so or pcbnew.pyd on linux, should be somewhere in your pcbnew build directory.

You can muck around with environment variables or just run make installto have kicad install script put all the files somewhere system will find them, ususally /usr/local. You also may have to run ldconfig once.

That did it! Time to see if I can do some gentle refactoring now. Thanks again!

For anyone following along at home, the following worked for me:

export LD_LIBRARY_PATH=/home/$USER/kicad/build/release/pcbnew/
python3

>>> import sys
>>> sys.path.insert(1, "/home/gtaubman/kicad/build/release/pcbnew/")
>>> import pcbnew
1 Like