diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index 98783ee7..c9653ac5 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -1109,7 +1109,7 @@ class PcbPlotter(): value = footprint.GetValue().strip() if not LEGACY_KICAD: # Look for a tolerance in the properties - prop = footprint.GetProperties() + prop = GS.get_fields(footprint) tol = next(filter(lambda x: x, map(prop.get, GS.global_field_tolerance)), None) if tol: value = value+' '+tol.strip() diff --git a/kibot/gs.py b/kibot/gs.py index 5b84407a..181c69da 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -855,3 +855,26 @@ class GS(object): def write_to_file(content, name): with GS.create_file(name, bin=isinstance(content, bytes)) as f: f.write(content) + + @staticmethod + def get_fields(footprint): + """ Returns a dict with the field/value for the fields in a FOOTPRINT (aka MODULE) """ + if GS.ki8: + # KiCad 8 defines a special object (PCB_FIELD) and its iterator + return {f.GetName(): f.GetText() for f in footprint.GetFields()} + if GS.ki6: + return footprint.GetProperties() + # KiCad 5 didn't have fields in the modules + return {} + + @staticmethod + def set_fields(footprint, flds): + """ Sets the fields in a FOOTPRINT (aka MODULE) from a dict """ + if GS.ki8: + new_fields = [fld for fld in flds.keys() if not footprint.HasField(fld)] + footprint.SetFields(flds) + # New fields are added as visible, so we must hide them (OMG!) + for fld in new_fields: + footprint.GetFieldByName(fld).SetVisible(False) + elif GS.ki6: + footprint.SetProperties(flds) diff --git a/kibot/kiplot.py b/kibot/kiplot.py index 406dfcbc..c9af20ff 100644 --- a/kibot/kiplot.py +++ b/kibot/kiplot.py @@ -301,14 +301,13 @@ def create_component_from_footprint(m, ref): f.number = 3 c.add_field(f) # Other fields - if GS.ki6: - for name, val in m.GetProperties().items(): - f = SchematicField() - f.name = name - f.value = val - f.number = -1 - f.visible(False) - c.add_field(f) + for name, val in GS.get_fields(m).items(): + f = SchematicField() + f.name = name + f.value = val + f.number = -1 + f.visible(False) + c.add_field(f) c._solve_fields(None) c.split_ref() return c diff --git a/kibot/out_base.py b/kibot/out_base.py index 3b4c4a94..f395e79b 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -869,22 +869,16 @@ class VariantOptions(BaseOptions): ref = m.GetReference() comp = comps_hash.get(ref, None) if comp is not None: - properties = {f.name: f.value for f in comp.fields} old_value = m.GetValue() - m.SetValue(properties['Value']) - if GS.ki6: - old_properties = m.GetProperties() - m.SetProperties(properties) - if has_GetFPIDAsString: - # Introduced in 6.0.6 - old_fp = m.GetFPIDAsString() - m.SetFPIDAsString(properties['Footprint']) - data = (old_value, old_properties, old_fp) - else: - data = (old_value, old_properties) - else: - data = old_value - self.sch_fields_to_pcb_bkp[ref] = data + old_fields = GS.get_fields(m) + # Introduced in 6.0.6 + old_fp = m.GetFPIDAsString() if has_GetFPIDAsString else None + fields = {f.name: f.value for f in comp.fields} + GS.set_fields(m, fields) + m.SetValue(fields['Value']) + if has_GetFPIDAsString: + m.SetFPIDAsString(fields['Footprint']) + self.sch_fields_to_pcb_bkp[ref] = (old_value, old_fields, old_fp) self._has_GetFPIDAsString = has_GetFPIDAsString def restore_sch_fields_to_pcb(self, board): @@ -894,13 +888,10 @@ class VariantOptions(BaseOptions): ref = m.GetReference() data = self.sch_fields_to_pcb_bkp.get(ref, None) if data is not None: - if GS.ki6: - m.SetValue(data[0]) - m.SetProperties(data[1]) - if has_GetFPIDAsString: - m.SetFPIDAsString(data[2]) - else: - m.SetValue(data) + m.SetValue(data[0]) + if has_GetFPIDAsString: + m.SetFPIDAsString(data[2]) + GS.set_fields(m, data[1]) def save_tmp_board(self, dir=None): """ Save the PCB to a temporal file. diff --git a/kibot/pre_update_xml.py b/kibot/pre_update_xml.py index 79766917..e4c1a60c 100644 --- a/kibot/pre_update_xml.py +++ b/kibot/pre_update_xml.py @@ -78,7 +78,7 @@ class Update_XML(BasePreFlight): # noqa: F821 excluded = set() for m in GS.get_modules(): ref = m.GetReference() - pcb_props = m.GetProperties() + pcb_props = GS.get_fields(m) found_comps.add(ref) if ref not in comps: if GS.ki6_only and pcb_props.get('exclude_from_bom') is not None: