Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. 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

EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
edited December 2019 in FeatureScript
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
Evan Reese

Answers

  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,936 PRO
    Just a shot in the dark but.

    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

  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭
    Just if anyone comes across this thread, we ended up getting it to work here:
    https://forum.onshape.com/discussion/comment/58476/#Comment_58476
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Just if anyone comes across this thread, we ended up getting it to work here:
    https://forum.onshape.com/discussion/comment/58476/#Comment_58476
    haha, "we". @m@MBartlett21 got it to work and I watched. I do still use that feature in most of my models though!
    Evan Reese
  • john_mcclaryjohn_mcclary Member, Developers Posts: 3,936 PRO
    Yea, thats about how it goes when he helps me too.  Just get out of the way and watch him go. 
Sign In or Register to comment.