Spreadsheet: set true/false property in model data

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
drmacro
Posts: 1341
Joined: Sun Mar 02, 2014 4:35 pm

Spreadsheet: set true/false property in model data

Postby drmacro » Fri Aug 07, 2020 2:54 pm

In the properties data tab the little blue fx icon allows you to get the value from the spreadsheet.

But, some properties are true/false and give a control that allows the user to select true or false.

Is there a way to set this property from a spread sheet?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."

My beginners, written treatise: Learning FreeCAD
openBrain
Posts: 4507
Joined: Fri Nov 09, 2018 5:38 pm

Re: Spreadsheet: set true/false property in model data

Postby openBrain » Fri Aug 07, 2020 5:13 pm

AFAIK boolean properties aren't expression aware, thus can't be directly bound to a spreadsheet value.
You can do that with a macro, but it will be neither saved in the document nor working on another installation.
drmacro
Posts: 1341
Joined: Sun Mar 02, 2014 4:35 pm

Re: Spreadsheet: set true/false property in model data

Postby drmacro » Fri Aug 07, 2020 6:45 pm

openBrain wrote:
Fri Aug 07, 2020 5:13 pm
AFAIK boolean properties aren't expression aware, thus can't be directly bound to a spreadsheet value.
You can do that with a macro, but it will be neither saved in the document nor working on another installation.
I suspected as much.
:(
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."

My beginners, written treatise: Learning FreeCAD
User avatar
Roy_043
Posts: 1773
Joined: Thu Dec 27, 2018 12:28 pm

Re: Spreadsheet: set true/false property in model data

Postby Roy_043 » Fri Aug 07, 2020 7:01 pm

Something like this works:

Code: Select all

App.ActiveDocument.Box.setExpression('MapReversed', u'Spreadsheet.MyBool')
drmacro
Posts: 1341
Joined: Sun Mar 02, 2014 4:35 pm

Re: Spreadsheet: set true/false property in model data

Postby drmacro » Fri Aug 07, 2020 7:06 pm

Roy_043 wrote:
Fri Aug 07, 2020 7:01 pm
Something like this works:

Code: Select all

App.ActiveDocument.Box.setExpression('MapReversed', u'Spreadsheet.MyBool')
Just to be sure I understand...which can always be questionable. :D

That code would need to be run each time the spreadsheet cell was changed, correct?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."

My beginners, written treatise: Learning FreeCAD
User avatar
Roy_043
Posts: 1773
Joined: Thu Dec 27, 2018 12:28 pm

Re: Spreadsheet: set true/false property in model data

Postby Roy_043 » Fri Aug 07, 2020 7:08 pm

Try it!
openBrain
Posts: 4507
Joined: Fri Nov 09, 2018 5:38 pm

Re: Spreadsheet: set true/false property in model data

Postby openBrain » Fri Aug 07, 2020 7:12 pm

drmacro wrote:
Fri Aug 07, 2020 7:06 pm
That code would need to be run each time the spreadsheet cell was changed, correct?
Nope. 'setExpression' sets an expression binding inside the expression engine, which will be updated on recompute automatically. ;)

I hadn't expected this to be possible. Thx Roy, will try it also. :+1:
drmacro
Posts: 1341
Joined: Sun Mar 02, 2014 4:35 pm

Re: Spreadsheet: set true/false property in model data

Postby drmacro » Tue Aug 11, 2020 5:40 pm

Roy_043 wrote:
Fri Aug 07, 2020 7:08 pm
Try it!
Finally getting back around to this...

I tried it and it works great.

The 'u' in

Code: Select all

...u'Spreadsheet.MyBool'...
, from Python searches, appears to be unicode operator? Is that right? And any non zero value is true?

How does one do that for, say, Pad > Type which is probably an enumerated value?

I tried:

Code: Select all

App.ActiveDocument.Pad.setExpression('Type', u'Spreadsheet.PadType')
with different values 0, 1, 2, 3, 4 but that errors out when it evaluates the cell.

I also tried other than 'u' to no avail.

(I couldn't really find a good reference for the construct in Python docs (probably don't know what to search for...) and I still get cross eyed when I attempt to read the API docs for FreeCAD. :? :roll: )
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."

My beginners, written treatise: Learning FreeCAD
User avatar
Roy_043
Posts: 1773
Joined: Thu Dec 27, 2018 12:28 pm

Re: Spreadsheet: set true/false property in model data

Postby Roy_043 » Tue Aug 11, 2020 8:22 pm

drmacro wrote:
Tue Aug 11, 2020 5:40 pm
appears to be unicode operator?
I think so too. But I am not sure.

Pad Type is a string value. You can easily check this:

Code: Select all

App.ActiveDocument.Pad.Type
User avatar
DeepSOIC
Posts: 7762
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Spreadsheet: set true/false property in model data

Postby DeepSOIC » Tue Aug 11, 2020 11:53 pm

BTW, it doesn't have to be done with a macro/py console. In 0.19, realthunder has added a gui method, too.
1. Right-click in property editor, "Show all"
2. Right-click on the property you want to drive with an expression, click "Expression", type the expression, done!
With this method, it is also possible to assign vectors and placements in one go.