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.

Make a Configuration Variable that can accept a FS array/ any FS expression

ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
I must be missing something. I'm trying to have one of my configuration variables accept an FS array, but when I attempt to specify the configuration variable type, I just get the options of Length, Angle, Integer, Real or Text. Is there no way to put in a general FS expression? The general PS variables have an "Any" type. Am I stuck using those and abandoning a configuration for this use case? Or is there some workaround?

Best Answer

  • ilya_baranilya_baran Posts: 929
    Accepted Answer
    Sorry, that is the available set for configuration variables -- there is no Any type for configs.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc

Answers

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 929
    Accepted Answer
    Sorry, that is the available set for configuration variables -- there is no Any type for configs.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
    edited August 2018
    @ilya_baran to work around this, I'm using a Text type variable that I then parse with a VERY simple parser into an array. Here's my stupid attempt at turning a string of a list into a list of numbers in FS:
     
    const stringToList = function(s) { var arr is array = []; var n is string = ""; for (var c in splitIntoCharacters(s)) { // If it's a comma, start collecting a new number and append the old if (match(c, ",").hasMatch) { // Append the number if the accummulated value is a number if (match(n, REGEX_NUMBER).hasMatch) { arr = append(arr, stringToNumber(n)); } // Reset the collecting string n = ""; } else { \\ Collect the char n = n ~ c; } } return arr; };
    This only matches numbers between commas - so it will convert "1,2,3" into [1,2,3], but I can't find a way to convert "[1,2,3]" into [1,2,3] because I don't know how to match with the "[", so that only converts to [2]. Anytime I try to escape "[" with a "\", I get an unexpected token error... are the allowed characters for FS regex documented somewhere? I was struggling with how to write FS acceptable regex. Also, any pointers on the code I posted/ this workaround in general would be great!
  • MBartlett21MBartlett21 Member Posts: 1,615 EDU
  • ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
    @mbartlett21
    Thanks for the help! I'm now rather sheepishly realizing there was no need to use regex, I couldv'e just tested for equality between "[" and the char. Regardless, are there any references for what is allowed in FS regex? I'll use your function for now! Looking forward to when functionality like this for strings is built into FS. 
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 929
    The regexp we use is described here: https://en.cppreference.com/w/cpp/regex/ecmascript
    The issue you ran into is insufficient escaping, a common pitfall when working with regexes.  Specifically, to match the character
    [

    you need the regex to be

    \[

    but to enter the backslash into a string literal (in FS or other C-like languages) you have to escape it, so the string needs to look something like:

    match(myString, "\\[")

    The definitive reference is provided here: https://xkcd.com/1638/  Hope this helps.

    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • ethan_keller924ethan_keller924 Member Posts: 34 ✭✭
    That xkcd is wonderful. This is now ringing bells. Thanks!
  • Don_Van_ZileDon_Van_Zile Member Posts: 195 PRO

    This stuff looks interesting and I wish I had more time to dig into FS to figure out what you guys are talking about! Good stuff I'm sure.

    Does Onshape plan on documenting more examples for users to explore and learn from such as the techniques being questioned and discussed here in these forum threads?
Sign In or Register to comment.