is it possible to use local variables in Formula editor ?

Report observations made with the new Toponaming branch.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Marc Bassini
Posts: 135
Joined: Fri Aug 13, 2021 10:08 am

is it possible to use local variables in Formula editor ?

Post by Marc Bassini »

Hello.

I need to compute an expression in the Formula editor which is pretty long and to use the result in the same expression.
To avoid to enter twice the epxression I would like to use a local variable.
As I don't know much of python in FreeCAD (and generally) I'm unsure it can ben done in Formula editor
Something like :

Code: Select all

result = (a very long expression that I dont want to enter twice), (result > somevalue? somefunc(result): anotherfunc(result))

Code: Select all

OS: Windows 10 build 19045
Word size of FreeCAD: 64-bit
Version: 2024.113.0.14555 (Git shallow)
Build type: Release
Branch: HEAD
Hash: 4235ee9c92d9822b434ec2bc8da7cee729fd9f3b
Python 3.11.7, Qt 5.15.6, Coin 4.0.1, Vtk 9.2.5, OCC 7.7.2
Locale: French/France (fr_FR)
chrisb
Veteran
Posts: 53471
Joined: Tue Mar 17, 2015 9:14 am

Re: is it possible to use local variables in Formula editor ?

Post by chrisb »

In Python it's possible, in Expressions not.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Marc Bassini
Posts: 135
Joined: Fri Aug 13, 2021 10:08 am

Re: is it possible to use local variables in Formula editor ?

Post by Marc Bassini »

in an expression, is it possible to rewrite my above example so that result is computed only once without local variables ?
chrisb
Veteran
Posts: 53471
Joined: Tue Mar 17, 2015 9:14 am

Re: is it possible to use local variables in Formula editor ?

Post by chrisb »

The Expressions page tells you what's possible.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Syres
Veteran
Posts: 2886
Joined: Thu Aug 09, 2018 11:14 am

Re: is it possible to use local variables in Formula editor ?

Post by Syres »

@Marc Bassini Expressions can be let's say, simple Python, this took me a full morning to perfect with plenty of syntax errors and incorrect information being passed between the various conditions:

Code: Select all

App.ActiveDocument.SectionView.setExpression('Scale', App.ActiveDocument.ProjGroup.ExpressionEngine[int(''.join( repr(e) for e in [idx for idx, (i,j) in enumerate(App.ActiveDocument.ProjGroup.ExpressionEngine) if i == 'Scale']))][1])
Not sure if it helps in the slightest but my method for looking up in stackoverflow.com was to use the search phrase python one liner followed by for example for loop or if else

With all due respect @chrisb that page was of no help for me when I was creating this little monster of an expression. If I can think of an easy way to break down how I built it approx two months ago I'll have a go but it'll be later this month.
Post Reply