Edit a spreadsheet value and save document

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
qlerebours
Posts: 9
Joined: Thu May 06, 2021 3:45 am

Edit a spreadsheet value and save document

Post by qlerebours »

Hello,
I'm trying to edit a value in a Spreadsheet with Python, recompute and save a document to be able to export it.
The problem is that I'm not able to see the changes after saving the document.

Here are the lines I use in freecadcmd:

Code: Select all

import FreeCAD
import Spreadsheet

doc = FreeCAD.openDocument('/path/to/my/file.FCStd')
input_sheet = FreeCAD.ActiveDocument.getObjectsByLabel('DataInput')[0]
input_sheet.A2 = 21
# If I run "input_sheet.A2" it shows "21"
input_sheet.recompute()
doc.recompute()
doc.save()
After doing this, if I open the Spreadsheet "DataInput" with the GUI, the A2 cell didn't change, even though I confirmed through the "input_sheet.A2" command that the new value was set.
Moreover, this cell is used to give the size of a shape and the shape still has its old size.

I did the same test using the GUI python console but the result is the same, I can see that the value is changed in the spreadsheet, but the spreadsheet appears as not saved, and if I close and reopen, I notice that the new value isn't persisted.

Do you have any idea?
Thanks

My version of Freecad:
OS: Ubuntu 20.04.2 LTS (ubuntu:GNOME/ubuntu)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.19.2)
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: French/France (fr_FR)
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Edit a spreadsheet value and save document

Post by openBrain »

Code: Select all

inputSheet.set('A2','21')
;)
If needed I can explain a bit more while your way fails.
qlerebours
Posts: 9
Joined: Thu May 06, 2021 3:45 am

Re: Edit a spreadsheet value and save document

Post by qlerebours »

openBrain wrote: Thu May 06, 2021 12:31 pm

Code: Select all

inputSheet.set('A2','21')
;)
If needed I can explain a bit more while your way fails.
Thank you very much, setting the cell value like this works!
I saw this solution on internet before and tried but I failed my test because when putting an extra space after the "," in .set('a', 'b'), freecadcmd fails.

However, I still have an issue which isn't really related, I can open a new post if required:
When I open the document in the GUI, then execute a macro that will change the cell value and save, everything works perfectly, however if I do the same with freecadcmd, or freecad -c, the document is broken.
In fact, I just tested, just running those two lines will break the document

Code: Select all

doc = FreeCAD.openDocument('/path/to/file.FCStd')
doc.save()
What I mean when I say that the doc is broken:

The document before:
Image

The document after:
Image

Do you know why?

A few more info:
When I run freecadcmd, I run with /path/to/freecad.AppImage freecadcmd
and when I run freecad, I run with /path/to/freecad.AppImage
and it works when I execute the macro in Freecad with the GUI, the document isn't broken
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Edit a spreadsheet value and save document

Post by openBrain »

qlerebours wrote: Fri May 07, 2021 4:10 am when putting an extra space after the "," in .set('a', 'b'), freecadcmd fails.

This is a bit weird as it should work the same with or without space after the comma.

However, I still have an issue which isn't really related, I can open a new post if required:
When I open the document in the GUI, then execute a macro that will change the cell value and save, everything works perfectly, however if I do the same with freecadcmd, or freecad -c, the document is broken.
In fact, I just tested, just running those two lines will break the document

Code: Select all

doc = FreeCAD.openDocument('/path/to/file.FCStd')
doc.save()
Could you share full macro code and sample FreeCAD file?
qlerebours
Posts: 9
Joined: Thu May 06, 2021 3:45 am

Re: Edit a spreadsheet value and save document

Post by qlerebours »

This is a bit weird as it should work the same with or without space after the comma.
Yes but I guess that it considers the space as a special character in the command line. I don't really know but it's not a big deal
Could you share full macro code and sample FreeCAD file?
Yes sure, you wlll be able to find the file here https://mega.nz/file/polSUTib#ChCKBxKZ0 ... zQpoRKz6mM

And the two simple lines I run to break the documents are

Code: Select all

doc = FreeCAD.openDocument('/path/to/v11.FCStd')
doc.save()
but as I said, if I run this as a macro, it's not breaking the document, however when I run it using the v0.19.2 AppImage with

Code: Select all

 ./path/to/freecad.AppImage freecadcmd
and I enter the lines in it, it breaks it
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Edit a spreadsheet value and save document

Post by openBrain »

qlerebours wrote: Fri May 07, 2021 5:38 am Yes sure, you wlll be able to find the file here https://mega.nz/file/polSUTib#ChCKBxKZ0 ... zQpoRKz6mM
Better attach files to post when it's less than 1 MB. How to is here : http://forum.freecadweb.org/viewtopic.php?f=3&t=2264
And the two simple lines I run to break the documents are

Code: Select all

doc = FreeCAD.openDocument('/path/to/v11.FCStd')
doc.save()
but as I said, if I run this as a macro, it's not breaking the document, however when I run it using the v0.19.2 AppImage with

Code: Select all

 ./path/to/freecad.AppImage freecadcmd
and I enter the lines in it, it breaks it
OK I can reproduce.
Actually file isn't broken but most objects went hidden.
Sample file is very interesting as it seems that all objects whose name start with 'Sketch' are hidden, whatever is the real type (event the Part shapes named 'Sketchxxxxx' that aren't actually sketches are hidden).
I think we better heard wmayer's opinion on that :
wmayer wrote: Ping -- every objects whose name starts with 'Sketch' are hidden on a simple doc open/save when run in console mode
wmayer
Founder
Posts: 20300
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Edit a spreadsheet value and save document

Post by wmayer »

openBrain wrote: Fri May 07, 2021 8:31 am every objects whose name starts with 'Sketch' are hidden on a simple doc open/save when run in console mode
Any object is hidden when you save a project in console mode. The point is that when loading a project then all objects in the 3d view are hidden to avoid superfluous updates. When a file was created with the GUI version then the file GuiDocument.xml is part of the project and this then restores the visibility of the objects while when the file was created in console mode the file is missing and thus the object remain invisible.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Edit a spreadsheet value and save document

Post by openBrain »

wmayer wrote: Fri May 07, 2021 12:15 pm Any object is hidden when you save a project in console mode.
There seems to be an exception at least for 'Part::Part2DObjectPython' typed objects. ;)
The point is that when loading a project then all objects in the 3d view are hidden to avoid superfluous updates. When a file was created with the GUI version then the file GuiDocument.xml is part of the project and this then restores the visibility of the objects while when the file was created in console mode the file is missing and thus the object remain invisible.
Got it. Thanks for help.
qlerebours
Posts: 9
Joined: Thu May 06, 2021 3:45 am

Re: Edit a spreadsheet value and save document

Post by qlerebours »

Better attach files to post when it's less than 1 MB.
Sorry I forgot that I could, I will re-read the guidelines :)
The point is that when loading a project then all objects in the 3d view are hidden to avoid superfluous updates
I understand now, it really makes sense!
However, I was wondering if there was a way to prevent this behavior, or a way to show the objects before saving?
I quickly tried to choose a random object, like Sketch007 and call the show method before saving but it fails because "object has no attribute show"
Thank you very much for your help
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Edit a spreadsheet value and save document

Post by openBrain »

qlerebours wrote: Fri May 07, 2021 4:43 pm However, I was wondering if there was a way to prevent this behavior, or a way to show the objects before saving?
I'm afraid there is no way. 'visibility' is a GUI only property, that FreeCAD absolutely is totally blind when in console mode.
Post Reply