Footprints swig API: access shown text in fields

This commit is contained in:
qu1ck 2023-06-23 10:44:26 -07:00 committed by Jon Evans
parent 7779a01d48
commit ec94439df4
3 changed files with 23 additions and 12 deletions

View File

@ -30,7 +30,6 @@
%template(MAP_STRING_STRING) std::map<wxString, wxString>; %template(MAP_STRING_STRING) std::map<wxString, wxString>;
%rename(GetFieldsNative) FOOTPRINT::GetFields;
%feature("flatnested"); %feature("flatnested");
%include footprint.h %include footprint.h
%feature("flatnested", ""); %feature("flatnested", "");
@ -59,18 +58,30 @@
# the object in the garbage collector # the object in the garbage collector
# #
def GetFields(self): def GetFieldsText(self):
""" Returns footprint fields map. """ """ Returns footprint fields name to text map. """
fields = self.GetFieldsNative() fields = self.GetFields()
return {str(field.GetName()): str(field.GetText()) for field in fields} return {str(field.GetName()): str(field.GetText()) for field in fields}
def GetField(self, key): def GetFieldsShownText(self):
""" Returns Field with a given key if it exists, throws KeyError otherwise. """ """ Returns footprint fields name to shown text map. """
fields = self.GetFields()
return {str(field.GetName()): str(field.GetShownText(False)) for field in fields}
def GetFieldText(self, key):
""" Returns Field text with a given key if it exists, throws KeyError otherwise. """
if self.HasFieldByName(key): if self.HasFieldByName(key):
return self.GetFieldByName(key).GetText() return self.GetFieldByName(key).GetText()
else: else:
raise KeyError("Field not found: " + key) raise KeyError("Field not found: " + key)
def GetFieldShownText(self, key):
""" Returns Field shown text with a given key if it exists, throws KeyError otherwise. """
if self.HasFieldByName(key):
return self.GetFieldByName(key).GetShownText(False)
else:
raise KeyError("Field not found: " + key)
def SetField(self, key, value): def SetField(self, key, value):
if self.HasFieldByName(key): if self.HasFieldByName(key):
self.GetFieldByName(key).SetText(value) self.GetFieldByName(key).SetText(value)

View File

@ -52,7 +52,7 @@ if( NOT (MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") )
# but the kicad binaries are linked to the debug mode python # but the kicad binaries are linked to the debug mode python
# Test that runs the QA tests through scripting # Test that runs the QA tests through scripting
add_test(NAME qa_python add_test(NAME qa_python
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/pcbnewswig ${PYTEST_ARGS_QACLI} COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/pcbnewswig ${PYTEST_ARGS_QAPYTHON}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
) )

View File

@ -128,9 +128,9 @@ class TestBoardClass:
} }
assert footprint.GetSheetfile() == 'custom_fields.kicad_sch' assert footprint.GetSheetfile() == 'custom_fields.kicad_sch'
assert footprint.GetSheetname() == '' assert footprint.GetSheetname() == ''
assert footprint.GetField('myfield') == 'myvalue' assert footprint.GetFieldText('myfield') == 'myvalue'
assert footprint.HasField('myfield') == True assert footprint.HasField('myfield')
assert footprint.HasField('abcd') == False assert not footprint.HasField('abcd')
footprint.SetField('abcd', 'efgh') footprint.SetField('abcd', 'efgh')
assert footprint.HasField('abcd') == True assert footprint.HasField('abcd')
assert footprint.GetField('abcd') == 'efgh' assert footprint.GetFieldText('abcd') == 'efgh'