Using spreadsheet values in a macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
viktak
Posts: 24
Joined: Tue Oct 19, 2021 2:36 pm
Contact:

Using spreadsheet values in a macro

Post by viktak »

Dear All,

I created a macro that would create several instances of a ShapeString like this:

Code: Select all

...
for i in range(0,13):
	ss=Draft.makeShapeString(String=myList[i],FontFile="D:/Projects/Word Clock/FreeCad/Fonts/DroidSansMono-enMp.ttf",Size=10.0,Tracking=0.0)
	ss.setExpression('.Placement.Base.x', u'<<params>>.cell_width')
...
The code above is just a small section, but it works fine.

However, I want to move each instance to a different location, based on a value from my Spreadsheet (called "params").
I changed my code like this:

Code: Select all

...
	ss.setExpression('.Placement.Base.x', u'<<params>>.cell_width * i')
...
This, unfortunately, doesn't work, because it doesn't recognize the iterator "i".

Is there a way to "escape" "i", so that I can combine the loop with a value from the spreadsheet?

Thanks in advance!
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Using spreadsheet values in a macro

Post by openBrain »

Actually what you're doing lack of sense. :)
I'm not clear whether you want to end up with an expression or a fixed value in your final file.
If you want an expression, you should have something like

Code: Select all

ss.setExpression('.Placement.Base.x', u'<<params>>.cell_width * {}'.format(i))
viktak
Posts: 24
Joined: Tue Oct 19, 2021 2:36 pm
Contact:

Re: Using spreadsheet values in a macro

Post by viktak »

Figured it out myself. In case anyone is interested, this is one way of doing it:

Code: Select all

	ss.setExpression('.Placement.Base.x', u'<<params>>.cell_width * ' + str(i))
If anyone has a better idea, please don't hold it back! :)
viktak
Posts: 24
Joined: Tue Oct 19, 2021 2:36 pm
Contact:

Re: Using spreadsheet values in a macro

Post by viktak »

openBrain wrote: Wed Oct 20, 2021 11:51 am Actually what you're doing lack of sense. :)
Maybe I didn't explain it right:
I need to make a grid of objects, but I want to keep them parameterized, in case I need to adjust them in the future.

BTW: Your solution is also working, thank you!!
Post Reply