@@ -30,6 +30,10 @@ def __init__(self, file_name, config, logger, board=None):
3030 self .board = board
3131 if self .board is None :
3232 self .board = pcbnew .LoadBoard (self .file_name ) # type: pcbnew.BOARD
33+ if not self .board :
34+ raise Exception ('Failed to load board file' )
35+ if hasattr (self .board , "SetCurrentVariant" ):
36+ self .board .SetCurrentVariant (config .kicad_variant )
3337 if hasattr (self .board , 'GetModules' ):
3438 # type: list[pcbnew.MODULE]
3539 self .footprints = list (self .board .GetModules ())
@@ -48,8 +52,7 @@ def get_extra_field_data(self, file_name):
4852
4953 return ExtraFieldData (data [0 ], data [1 ])
5054
51- @staticmethod
52- def get_footprint_fields (f ):
55+ def get_footprint_fields (self , f ):
5356 # type: (pcbnew.FOOTPRINT) -> dict
5457 props = {}
5558 if hasattr (f , "GetProperties" ):
@@ -62,6 +65,15 @@ def get_footprint_fields(f):
6265 if hasattr (f , "IsDNP" ):
6366 if f .IsDNP ():
6467 props ["kicad_dnp" ] = "DNP"
68+ if hasattr (f , 'GetVariant' ):
69+ variant = f .GetVariant (self .config .kicad_variant )
70+ if variant :
71+ var_fields = variant .GetFields ()
72+ for k in var_fields .keys ():
73+ props [str (k )] = str (f .GetFieldShownText (str (k )))
74+ if variant .GetDNP ():
75+ props ["kicad_dnp" ] = "DNP"
76+
6577 return props
6678
6779 def parse_extra_data_from_pcb (self ):
@@ -742,8 +754,7 @@ def parse_netlist(net_info):
742754 nets = sorted ([str (s ) for s in nets ])
743755 return nets
744756
745- @staticmethod
746- def footprint_to_component (footprint , extra_fields ):
757+ def footprint_to_component (self , footprint , extra_fields ):
747758 try :
748759 footprint_name = str (footprint .GetFPID ().GetFootprintName ())
749760 except AttributeError :
@@ -753,6 +764,10 @@ def footprint_to_component(footprint, extra_fields):
753764 if hasattr (pcbnew , 'FP_EXCLUDE_FROM_BOM' ):
754765 if footprint .GetAttributes () & pcbnew .FP_EXCLUDE_FROM_BOM :
755766 attr = 'Virtual'
767+ if hasattr (footprint , 'GetExcludedFromBOMForVariant' ):
768+ if footprint .GetExcludedFromBOMForVariant (
769+ self .config .kicad_variant ):
770+ attr = 'Virtual'
756771 elif hasattr (pcbnew , 'MOD_VIRTUAL' ):
757772 if footprint .GetAttributes () == pcbnew .MOD_VIRTUAL :
758773 attr = 'Virtual'
@@ -912,14 +927,17 @@ def Run(self):
912927 from ..errors import ParsingException
913928
914929 logger = ibom .Logger ()
915- board = pcbnew .GetBoard ()
930+ board = pcbnew .GetBoard () # type: pcbnew.BOARD
916931 pcb_file_name = board .GetFileName ()
917932
918933 if not pcb_file_name :
919934 logger .error ('Please save the board file before generating BOM.' )
920935 return
921936
922937 config = Config (version , os .path .dirname (pcb_file_name ))
938+ if hasattr (board , 'GetCurrentVariant' ):
939+ config .kicad_variant = board .GetCurrentVariant ()
940+
923941 parser = PcbnewParser (pcb_file_name , config , logger , board )
924942
925943 try :
0 commit comments