Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Using Variables in text
mark_morgen
Member Posts: 12 ✭
I could not get text to work at all with a #variable. I tried for over an hour, but nothing worked. My goal is to emboss a text string that includes a single dimension of the part onto a face, such as "Cube Height is #Height inches". It only works as a simple text string, but I want to update the text automatically when the dimension is changed in the drawing.
I used Onshape instructions, but they failed me...
Thank you!
1
Answers
Don't convert it back to text. Leave it as an expression. And, since your variable is just a number,
is sufficient.
I don't know if all of the FeatureScript math functions will work in a text box, but here is a good list of math functions:
https://cad.onshape.com/FsDoc/library.html#module-math.fs
Had this same issue but it wasn't working for me until I concatenated with an empty text string. Then it started working as intended, even after I deleted the empty text string. ¯\_(ツ)_/¯
Explaining why things did/didn't work for particular values of text;
"#height"- doesn't expand the var because you have quotes around it which means "#height" is the string.#height- gives an error because the#heightvar it uses as the string is not a string, it's a length with a dimension (probably mm).toString(#height)- works becausetoString()converts the length into a string.roundToPrecision(#Shorten/mm,1)- gives an error because this expression returns a number without a dimension, not a string. It doesn't have a dimension because#Shorten/mmremoves themmdimension.roundToPrecision(#Shorten/mm,1) ~ ""works because the~string-concatenation operator converts both sides into strings before joining them if they are not strings.The only other thing to be wary of is Sketch Text expressions don't get re-evaluated in feature-patterns with `Reapply features` selected. This is a very old bug that is still not fixed (2026-02-26). This means you cannot use text expressions to dynamicaly generate different text for different feature-pattern-instances by updating var's in the feature-pattern. I think the only workaround is use third-party featurescript extensions.
For the record, I found another workaround for getting text expressions re-evaluated in feature-patterns; instantiate a "derived" sketch with a configuration-variable for the text input. Exact steps are;
You can also add extra configuration variables for other attributes of the text like size, angle, etc, which can also be set using expressions when you Derive the sketch instance. Note most configuration arguments are expressions by default, and only string config args default to "text" and need to be right-click-converted into expressions.
One thing that might bite you using this technique; you cannot instantiate multiple instances into the same part studio with the same configuration. This means you will probably get errors if the configuration argument expressions evaluate to the same values for any of your part-pattern-instances.