Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document renames cause problems #13841

Open
2 tasks done
krushia opened this issue May 5, 2024 · 1 comment
Open
2 tasks done

Document renames cause problems #13841

krushia opened this issue May 5, 2024 · 1 comment
Labels
Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD Expressions Missing: example file Missing: steps to reproduce

Comments

@krushia
Copy link

krushia commented May 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

After doing "Save As..." to a different file, the expression engine might point to the OLD file until FreeCAD is restarted. This is tricky to reproduce, but can lead to data corruption.

Partial example:

  1. Create a new project
  2. Create a sphere
  3. Create a cube
  4. Change Box.Label to the expression <<Not>> + Sphere.Label
  5. Save the document
  6. In the Tree View, right click > Expression Actions > Copy Selected Document. Note that in the copied text, the references will point to the unsaved document instead of the current document name, like Unnamed#Box.ExpressionEngine

At this point, I tried different combinations of renaming the saved document and opening it, and somehow managed to get changes in Sphere.Label to affect Box.Label, but I can't reproduce it reliably. It might be some weird race condition? In my case I ended up with two open tabs, one a "wrong-doc-bugtest" and the other a "wrong-doc-bugtest1" that were cross-referencing each other in memory.

When in the "failing" mode, both open documents showed:
##@@ Label wrong_doc_bugtest1#Box.ExpressionEngine (NotSpherex) ##@@ <<Not>> + Sphere.Label

I ran into this in a larger project, where I Opened the file "foo-mk3.FCStd" and after doing Save As foo-mk4, foo-mk5, foo-mk6... I found out when re-opening foo-mk3 that my still open foo-mk6 was suddenly using old values from a spreadsheet in foo-mk3. Am I making any sense?

Sorry, I wish I could reproduce it reliably :(

Full version info

OS: Gentoo Linux (KDE/plasma)
Word size of FreeCAD: 64-bit
Version: 0.22.0dev.37100 (Git) AppImage
Build type: Release
Branch: main
Hash: ede6c811fd39d68a44949601f2eb7ecc8a67fdb7
Python 3.11.9, Qt 5.15.13, Coin 4.0.2, Vtk 9.2.6, OCC 7.7.2
Locale: English/United States (en_US)
Installed mods: 
  * parts_library
  * A2plus 0.4.64a (Disabled)
  * DynamicData 2.62.0
  * Assembly4 0.50.12 (Disabled)
  * Alternate_OpenSCAD 1.0.0
  * Nodes 0.1.36 (Disabled)
  * MnesarcoUtils 0.2.5 (Disabled)
  * Manipulator 1.5.7
  * Assembly3 0.12.2 (Disabled)
  * lattice2 1.0.0
  * Launcher

Subproject(s) affected?

None

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@maxwxyz maxwxyz added Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD Expressions Missing: example file Missing: steps to reproduce labels May 6, 2024
@krushia
Copy link
Author

krushia commented May 7, 2024

I'm not sure it will be possible to make an example file. There seem to be 2 separate problems here. The first is an annoyance, and may have resulted in user error on my part.

The Document.Name in FreeCAD is not restricted to a Type[Number] like other objects (Box001, Sphere003, etc). Instead it is AnyString[Number], with AnyString being "Unnamed" (or translation?) for a new document, and filename without extension when opening an existing file. If AnyString matches an existing name, FreeCAD tries to change the name by removing any trailing digits, then adding a digit. Problems arise when AnyString has digits at the end already.

  1. Following original "Partial example" save the file in step 5 as foo_1.FCStd
  2. Completely close FreeCAD
  3. Open foo_1.FCStd in a fresh FreeCAD instance
  4. Do some stuff (optional), save it as foo_2.FCStd
  5. Open foo_1.FCStd
  6. At this point, you now have a mess
>>> App.getDocument('foo_1').FileName.rsplit('/')[-1]
'foo_2.FCStd'
>>> App.getDocument('foo_2').FileName.rsplit('/')[-1]
'foo_1.FCStd'

My suggestion here would be to name every Document.Name in FreeCAD with the format 'Document'[number] to avoid dummys (me) from treating it as reference to a filename.

As for the second issue, here is a summary:

  1. Document.Label is checked for uniqueness when first opened, but not afterwards.
  2. Document.OldLabel is never checked for uniqueness.
  3. When Document.Label is changed, it is possible for Document.OldLabel to be the same as another Document.Label
  4. The expression engine will perform erroneous replacements in such a case

Reproducing this is difficult, as it has to do with timing (I think). Still working on that.

@krushia krushia changed the title Expression engine internally points to the wrong open document Document renames cause problems May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD Expressions Missing: example file Missing: steps to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants