Expressions... again...

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
jmaustpc
Posts: 9619
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Expressions... again...

Postby jmaustpc » Thu Dec 31, 2015 5:19 am

Your first post, welcome to FreeCAD.
You can define values in a spreadsheet in spreadsheet wb and then reference the spreadsheet cell in your expression .
noraef
Posts: 11
Joined: Thu Dec 31, 2015 3:10 am

Re: Expressions... again...

Postby noraef » Thu Dec 31, 2015 1:42 pm

Thanks: yeah, I saw that, but spreadsheets are included in the fcstd file. I was wondering about macros precisely so to share values between different fcstd files...
wmayer
Site Admin
Posts: 15277
Joined: Thu Feb 19, 2009 10:32 am

Re: Expressions... again...

Postby wmayer » Thu Dec 31, 2015 3:31 pm

It's possible that you have a spreadsheet in one document and reference values from there in other documents.

Important note: You use a document's name to reference it from other documents. Now, when saving the document the first time you choose a certain file name and this is usually different to "Unnamed1" (or its translated version) and then the links are lost after reopening it. So, it's recommended to create first the master document, create a spreadsheet, save and close it. After reopening the name is set to the above file name. Afterwards you can still make changes and save the file but you shouldn't rename it.

Then create the further documents where you want to add expressions to. Assuming you named the master document "master" you can access an alias like this: master#Spreadsheet.Length

Unfortunately, the integrated word completion is more confusing than helping because it always gives parser errors or claims that this or that doesn't exist. So, just continue on typing. When you are finished and the content is correct the OK suddenly becomes active.

Of course, it's up to you to load the corresponding documents later when you want to change anything.

So, it's not based on macros but it's already very close to what you may want.

Edit: Add missing word
User avatar
DeepSOIC
Posts: 7292
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Expressions... again...

Postby DeepSOIC » Thu Dec 31, 2015 3:49 pm

wmayer wrote:It's possible that you have a spreadsheet in one document and reference values from there in other documents.

Important note: You use a document's name to reference it from other documents. Now, when saving the document the first time you choose a certain file name and this is usually different to "Unnamed1" (or its translated version) and then the links are lost after reopening it. So, it's recommended to create first the master document, create a spreadsheet, save and close it. After reopening the name is set to the above file name. Afterwards you can still make changes and save the file but you shouldn't rename it.

Then create the further documents where you want to add expressions to. Assuming you named the master document "master" you can access an alias like this: master#Spreadsheet.Length

Unfortunately, the integrated is more confusing than helping because it always gives parser errors or claims that this or that doesn't exist. So, just continue on typing. When you are finished and the content is correct the OK suddenly becomes active.

Of course, it's up to you to load the corresponding documents later when you want to change anything.
I copied this to Expressions wiki page. Almost as-is.
noraef
Posts: 11
Joined: Thu Dec 31, 2015 3:10 am

Re: Expressions... again...

Postby noraef » Sat Jan 02, 2016 1:05 am

@wmayer : ah, it'd be kind of cool, indeed. Unfortunately, changes to the "main" spreadsheet are not applied automagically to all the expressions that call for it.

It will only apply if I enter the dimension definition (eg, the thickness of an extrusion) and validate it. Funny thing is it seems to then check over all the main spreadsheet values (eg, if I extrude an expressed rectangle and I've also modified the sides dimensions in the spreadsheet, manually validating the thickness value will also apply the modifications to the other dimensions). Not too practical once dimensions start being used everywhere...

Also, I noticed the autocompletion starts working if I replace the # by a dot (the latter which is replaced back by a # when I validate the expression definition). Well, that one is not a biggie anyway.
User avatar
DeepSOIC
Posts: 7292
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Expressions... again...

Postby DeepSOIC » Sat Jan 02, 2016 10:49 am

noraef wrote: Unfortunately, changes to the "main" spreadsheet are not applied automagically to all the expressions that call for it.
Did you try Macro ForceRecompute?
noraef
Posts: 11
Joined: Thu Dec 31, 2015 3:10 am

Re: Expressions... again...

Postby noraef » Sat Jan 02, 2016 2:46 pm

DeepSOIC wrote:Macro ForceRecompute?
Thanks! It works great with that macro. I did realize the recompute icon was greyed out, but didn't search about macros to solve that.

Added it to the global toolbar, and it'll more than do. Thanks again!
triplus
Posts: 9136
Joined: Mon Dec 12, 2011 4:45 pm

Re: Expressions... again...

Postby triplus » Tue Feb 13, 2018 3:39 pm

eivindkvedalen wrote:
Sun Aug 09, 2015 10:39 pm
If you quote them with << and >>, then it works in both cases. You can't use "normal" quote characters like " or ', because that is interpreted as units.
Hi Eivind.

I noticed one issue when helping an user to get a value out of dictionary into a spreadsheet cell:

https://forum.freecadweb.org/viewtopic. ... 76#p214826

If <<>> is used at the end it will get removed from the expression. Consequently after save and open operation resulting in #ERR.

Code: Select all

<<Cube>>.Length
Cube.<<Length>>
Note the difference in persistence of <<>>. Any chance this could get fixed? Is there something else that is recommended to be done in such use case? Or are we just pushing the expressions limits to much? ;)

Thanks.
eivindkvedalen
Posts: 600
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Postby eivindkvedalen » Fri Feb 16, 2018 6:12 pm

triplus wrote:
Tue Feb 13, 2018 3:39 pm
eivindkvedalen wrote:
Sun Aug 09, 2015 10:39 pm
If you quote them with << and >>, then it works in both cases. You can't use "normal" quote characters like " or ', because that is interpreted as units.
Hi Eivind.

I noticed one issue when helping an user to get a value out of dictionary into a spreadsheet cell:

https://forum.freecadweb.org/viewtopic. ... 76#p214826

If <<>> is used at the end it will get removed from the expression. Consequently after save and open operation resulting in #ERR.

Code: Select all

<<Cube>>.Length
Cube.<<Length>>
Note the difference in persistence of <<>>. Any chance this could get fixed? Is there something else that is recommended to be done in such use case? Or are we just pushing the expressions limits to much? ;)

Thanks.
You've actually found a way to inject python code; not good, but not sure if this is a security issue or not. Anyway, this should be fixed. Dictionaries and arrays were planned, but not fully implemented. If (when) time allows, I will finish them. The syntax will then be dictionary[key] or array[index], e.g Panel014.Material.Material[Density] in the example from your thread.

Eivind
triplus
Posts: 9136
Joined: Mon Dec 12, 2011 4:45 pm

Re: Expressions... again...

Postby triplus » Fri Feb 16, 2018 7:48 pm

Thanks for the explanation.