This is a suggestion offered by realthunder at https://forum.freecadweb.org/viewtopic.php?p=560433#p560433 and this commit https://github.com/realthunder/FreeCAD/commit/0e20656ffae This commit additionally moves the relevant code into a separate class.