LubomirJagos / FreeCAD-OpenEMS-Export

Simple GUI plugin for FreeCAD to export current model for EM simulation in OpenEMS
GNU General Public License v3.0
61 stars 5 forks source link

IndexError when loading config after item renamed in FreeCAD #6

Closed MisterHW closed 1 year ago

MisterHW commented 1 year ago

There seems to be a problem with identifying objects by name, or rather when FreeCAD-OpenEMS-Export loads object names from the config file and they no longer match the FreeCAD project:

10:43:15  Traceback (most recent call last):
10:43:15    File "C:/Users/username/AppData/Roaming/FreeCAD/Macro/FreeCAD-OpenEMS-Export/Export for OpenEMS.FCMacro", line 3996, in loadCurrentSettingsButtonClicked
10:43:15      freeCadObj = FreeCAD.ActiveDocument.getObjectsByLabel(itemName)[0]
10:43:15  IndexError: list index out of range

The objects simply vanish from the treeview and the user is not notified beyond an error showing up in the report. One could insert placeholders that ask the user to re-assign the item in question and to document that it even existed.

It would be better though to use a global unique identifier that persists between the macro sessions.

From the discussion over at https://forum.freecadweb.org/viewtopic.php?p=480644#p480644:

for o in App.ActiveDocument.Objects:
    print("guid key: {}_{}".format(o.Document.Uid, o.ID))

"this gave me a unique id even for multiple documents and it works on document reload, and it keeps the same even on copy the document or save as the document. For me this seams similar as every FreeCAD object would have its own guid. For sure if a document would be copied with a file manager objects in both documents would have the same one. But if a object inside a document would be copied it would work and even if multiple documents are used all object identifier would be unique too."

A previous post, also from over a year ago, mentions that o.ID would change between reloads. For unique IDs to work in conjunction with FreeCAD-OpenEMS-Export consistently, objects would need to have persistent guids.

MisterHW commented 1 year ago

closing as issue has been addressed in commit 7b6232e3363b154cb782e40a4e547b795ade0e8c

Future issue: Proper handling of broken object associations due to need for redo / undo support has been identified as a follow-up issue and will be addressed in another commit (association needs to be remembered in the background but shown as disabled or hidden).

Test case: