33
44import pcbnew
55
6- from .common import EcadParser , Component
6+ from .common import EcadParser , Component , ExtraFieldData
77from .kicad_extra import find_latest_schematic_data , parse_schematic_data
88from .svgpath import create_path
99from ..core import ibom
@@ -29,22 +29,29 @@ def get_extra_field_data(self, file_name):
2929 return self .parse_extra_data_from_pcb ()
3030 if os .path .splitext (file_name )[1 ] == '.kicad_pcb' :
3131 return None
32- return parse_schematic_data (file_name )
32+
33+ data = parse_schematic_data (file_name )
34+
35+ return ExtraFieldData (data [0 ], data [1 ])
3336
3437 def parse_extra_data_from_pcb (self ):
3538 field_set = set ()
36- comp_dict = {}
39+ by_ref = {}
3740
3841 for f in self .footprints : # type: pcbnew.FOOTPRINT
3942 props = f .GetProperties ()
4043 ref = f .GetReference ()
41- ref_fields = comp_dict .setdefault (ref , {})
44+ ref_fields = by_ref .setdefault (ref , {})
4245
4346 for k , v in props .items ():
4447 field_set .add (k )
4548 ref_fields [k ] = v
4649
47- return list (field_set ), comp_dict
50+ by_index = {
51+ i : f .GetProperties () for (i , f ) in enumerate (self .footprints )
52+ }
53+
54+ return ExtraFieldData (list (field_set ), by_ref , by_index )
4855
4956 def latest_extra_data (self , extra_dirs = None ):
5057 base_name = os .path .splitext (os .path .basename (self .file_name ))[0 ]
@@ -685,8 +692,6 @@ def parse(self):
685692 raise ParsingException (
686693 'Failed parsing %s' % self .config .extra_data_file )
687694
688- extra_field_data = extra_field_data [1 ] if extra_field_data else None
689-
690695 title_block = self .board .GetTitleBlock ()
691696 title = title_block .GetTitle ()
692697 revision = title_block .GetRevision ()
@@ -752,26 +757,33 @@ def parse(self):
752757 if self .config .include_nets and hasattr (self .board , "GetNetInfo" ):
753758 pcbdata ["nets" ] = self .parse_netlist (self .board .GetNetInfo ())
754759
755- warning_shown = False
756760 if extra_field_data and need_extra_fields :
757- e = []
758- for f in self .footprints :
759- e .append (extra_field_data .get (f .GetReference (), {}))
760- if f .GetReference () not in extra_field_data :
761- # Some components are on pcb but not in schematic data.
762- # Show a warning about possibly outdated netlist/xml file.
763- self .logger .warn (
764- 'Component %s is missing from schematic data.'
765- % f .GetReference ())
766- warning_shown = True
761+ extra_fields = extra_field_data .fields_by_index
762+ if extra_fields :
763+ extra_fields = extra_fields .values ()
764+
765+ if extra_fields is None :
766+ extra_fields = []
767+ field_map = extra_field_data .fields_by_ref
768+ warning_shown = False
769+
770+ for f in self .footprints :
771+ extra_fields .append (field_map .get (f .GetReference (), {}))
772+ if f .GetReference () not in field_map :
773+ # Some components are on pcb but not in schematic data.
774+ # Show a warning about outdated extra data file.
775+ self .logger .warn (
776+ 'Component %s is missing from schematic data.'
777+ % f .GetReference ())
778+ warning_shown = True
779+
780+ if warning_shown :
781+ self .logger .warn ('Netlist/xml file is likely out of date.' )
767782 else :
768- e = [{}] * len (self .footprints )
769-
770- if warning_shown :
771- self .logger .warn ('Netlist/xml file is likely out of date.' )
783+ extra_fields = [{}] * len (self .footprints )
772784
773- components = [self .footprint_to_component (f , ee )
774- for (f , ee ) in zip (self .footprints , e )]
785+ components = [self .footprint_to_component (f , e )
786+ for (f , e ) in zip (self .footprints , extra_fields )]
775787
776788 return pcbdata , components
777789
0 commit comments