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.
conditionally reference a variable
EvanReese
Member, Mentor Posts: 2,135 ✭✭✭✭✭
I'm working on a multi-variable feature using an array parameter. I'm looking for a way to optionally reference a variable that's earlier in the array. Here's what I'm trying to do, but it doesn't like it.
I think it doesn't like it because the feature isn't finished until I hit the green check, so it can't see Var1 yet. I believe I can use getVariable() to access it in the featurescript, but I'm stumped on how to make that conditional. I want it to be something like if (Variable contains "#" then grab the variable name and use getVariable(), else, just use the number that's there). I have even less of a clue on how to make that work if I wanted to reference 2 variables at once, like Var3 = #Var1+#Var2.
how can I find any word using a # and convert that to a getVariable function? Or is there a much simpler way?
The code is here if you'd like to see it
I think it doesn't like it because the feature isn't finished until I hit the green check, so it can't see Var1 yet. I believe I can use getVariable() to access it in the featurescript, but I'm stumped on how to make that conditional. I want it to be something like if (Variable contains "#" then grab the variable name and use getVariable(), else, just use the number that's there). I have even less of a clue on how to make that work if I wanted to reference 2 variables at once, like Var3 = #Var1+#Var2.
how can I find any word using a # and convert that to a getVariable function? Or is there a much simpler way?
The code is here if you'd like to see it
Evan Reese
0
Answers
Can you create a loop at the beginning of your code that creates a map that has this format: Vars [id, name, value];
that stores all of the created vars sequentially.
I'm not sure if you can parse a string in FeatureScript though. But otherwise I'd parse the value of each variable and to a replace #xxxx with whatever Vars[] has a matching "name".
Vars[2].value = Vars[1].value * 2
I probably don't have the syntax right, but you see what I'm getting at?
Then you setVariable(context, Vars[n].name, Vars[n].value); in a loop at the end
https://forum.onshape.com/discussion/comment/58476/#Comment_58476
IR for AS/NZS 1100