[KiCad 8] Added abstraction for Properties vs Fields

Now footprint properties are PCB_FIELDS
This commit is contained in:
Salvador E. Tropea 2024-02-07 09:25:08 -03:00
parent 048ca3d158
commit 0c03f33912
5 changed files with 45 additions and 32 deletions

View File

@ -1109,7 +1109,7 @@ class PcbPlotter():
value = footprint.GetValue().strip() value = footprint.GetValue().strip()
if not LEGACY_KICAD: if not LEGACY_KICAD:
# Look for a tolerance in the properties # 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) tol = next(filter(lambda x: x, map(prop.get, GS.global_field_tolerance)), None)
if tol: if tol:
value = value+' '+tol.strip() value = value+' '+tol.strip()

View File

@ -855,3 +855,26 @@ class GS(object):
def write_to_file(content, name): def write_to_file(content, name):
with GS.create_file(name, bin=isinstance(content, bytes)) as f: with GS.create_file(name, bin=isinstance(content, bytes)) as f:
f.write(content) 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)

View File

@ -301,8 +301,7 @@ def create_component_from_footprint(m, ref):
f.number = 3 f.number = 3
c.add_field(f) c.add_field(f)
# Other fields # Other fields
if GS.ki6: for name, val in GS.get_fields(m).items():
for name, val in m.GetProperties().items():
f = SchematicField() f = SchematicField()
f.name = name f.name = name
f.value = val f.value = val

View File

@ -869,22 +869,16 @@ class VariantOptions(BaseOptions):
ref = m.GetReference() ref = m.GetReference()
comp = comps_hash.get(ref, None) comp = comps_hash.get(ref, None)
if comp is not None: if comp is not None:
properties = {f.name: f.value for f in comp.fields}
old_value = m.GetValue() old_value = m.GetValue()
m.SetValue(properties['Value']) old_fields = GS.get_fields(m)
if GS.ki6:
old_properties = m.GetProperties()
m.SetProperties(properties)
if has_GetFPIDAsString:
# Introduced in 6.0.6 # Introduced in 6.0.6
old_fp = m.GetFPIDAsString() old_fp = m.GetFPIDAsString() if has_GetFPIDAsString else None
m.SetFPIDAsString(properties['Footprint']) fields = {f.name: f.value for f in comp.fields}
data = (old_value, old_properties, old_fp) GS.set_fields(m, fields)
else: m.SetValue(fields['Value'])
data = (old_value, old_properties) if has_GetFPIDAsString:
else: m.SetFPIDAsString(fields['Footprint'])
data = old_value self.sch_fields_to_pcb_bkp[ref] = (old_value, old_fields, old_fp)
self.sch_fields_to_pcb_bkp[ref] = data
self._has_GetFPIDAsString = has_GetFPIDAsString self._has_GetFPIDAsString = has_GetFPIDAsString
def restore_sch_fields_to_pcb(self, board): def restore_sch_fields_to_pcb(self, board):
@ -894,13 +888,10 @@ class VariantOptions(BaseOptions):
ref = m.GetReference() ref = m.GetReference()
data = self.sch_fields_to_pcb_bkp.get(ref, None) data = self.sch_fields_to_pcb_bkp.get(ref, None)
if data is not None: if data is not None:
if GS.ki6:
m.SetValue(data[0]) m.SetValue(data[0])
m.SetProperties(data[1])
if has_GetFPIDAsString: if has_GetFPIDAsString:
m.SetFPIDAsString(data[2]) m.SetFPIDAsString(data[2])
else: GS.set_fields(m, data[1])
m.SetValue(data)
def save_tmp_board(self, dir=None): def save_tmp_board(self, dir=None):
""" Save the PCB to a temporal file. """ Save the PCB to a temporal file.

View File

@ -78,7 +78,7 @@ class Update_XML(BasePreFlight): # noqa: F821
excluded = set() excluded = set()
for m in GS.get_modules(): for m in GS.get_modules():
ref = m.GetReference() ref = m.GetReference()
pcb_props = m.GetProperties() pcb_props = GS.get_fields(m)
found_comps.add(ref) found_comps.add(ref)
if ref not in comps: if ref not in comps:
if GS.ki6_only and pcb_props.get('exclude_from_bom') is not None: if GS.ki6_only and pcb_props.get('exclude_from_bom') is not None: