Mail Archives: geda-user/2016/01/11/10:28:37
On Fri, 8 Jan 2016, Matt Rhys-Roberts (matt DOT rhys-roberts AT envinsci DOT co DOT uk)
[via geda-user AT delorie DOT com] wrote:
> How could I search a directory of .sch schematic sheets for components
> that contain a specific attribute, e.g. OrderCode=1234567, please?
This is a nice example of where the new Xorn libraries come in handy. :)
To use this, you need a recent git version of gEDA/gaf, and you have to
either install gEDA/gaf to a standard location or add the subdirectory
"xorn/built-packages" to the environment variable PYTHONPATH. Then you
can search for matching components like this:
import xorn.geda.attrib
import xorn.geda.read
sch = xorn.geda.read.read('schematic.sch')
for ob in sch.toplevel_objects():
if isinstance(ob.data(), xorn.storage.Component) and \
'1234567' in xorn.geda.attrib.search_attached(ob, 'OrderCode'):
print xorn.geda.attrib.search_attached(ob, 'refdes')[0]
If you want to search a complete directory, here is how you could write a
command-line utility which does that:
#!/usr/bin/python2.7
import os, sys
import xorn.geda.attrib
import xorn.geda.read
path = sys.argv[1]
name = sys.argv[2]
value = sys.argv[3]
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if not filename.endswith('.sch'):
continue
sch = xorn.geda.read.read(os.path.join(path, filename))
for ob in sch.toplevel_objects():
if isinstance(ob.data(), xorn.storage.Component) and \
value in xorn.geda.attrib.search_attached(ob, name):
print xorn.geda.attrib.search_attached(ob, 'refdes')[0]
You can call it like "./ordercode . OrderCode 1234567" to search the
current directory for components with OrderCode=1234567.
> Basically, I need to fit components manually to a board, one specific
> type at a time, to keep production neat. So I need to list them by
> unique order code, ideally.
So what you ultimately want is a list of refdes's for each order code?
You could compile that list like this:
#!/usr/bin/python2.7
import os, sys
import xorn.geda.attrib
import xorn.geda.read
path = sys.argv[1]
name = sys.argv[2]
result = {}
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if not filename.endswith('.sch'):
continue
sch = xorn.geda.read.read(os.path.join(path, filename))
for ob in sch.toplevel_objects():
if not isinstance(ob.data(), xorn.storage.Component):
continue
try:
refdes = xorn.geda.attrib.search_attached(ob, 'refdes')[0]
except IndexError:
refdes = None
for value in xorn.geda.attrib.search_attached(ob, name):
if value not in result:
result[value] = []
result[value].append(refdes)
for k in sorted(result.keys()):
print '%s: %s' % (k, ' '.join(result[k]))
Call this script like "./ordercodes . OrderCode" to print a list of order
codes and associated refdes' for the current directory.
Please note that, like any of the proposed solutions, this does not find
OrderCode= attributes which are inherited from non-embedded symbols. If
you want to search these, too, have a look at this mail:
http://article.gmane.org/gmane.comp.cad.geda.user/50021
If you loaded the schematic this way, you can replace "search_attached"
with "search_all" to get inherited attributes, too.
- Raw text -