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, csevp Posts: 39 EDU
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 Onshape Employees, Developers, HDM Posts: 1,173
    Answer ✓
    Sorry, that is the available set for configuration variables -- there is no Any type for configs.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc

Answers

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,173
    Answer ✓
    Sorry, that is the available set for configuration variables -- there is no Any type for configs.
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • ethan_keller924ethan_keller924 Member, csevp Posts: 39 EDU
    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;
        }; <br>
    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!
  • ethan_keller924ethan_keller924 Member, csevp Posts: 39 EDU
    @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: 1,173
    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 \ VP, Architecture and FeatureScript \ Onshape Inc
  • ethan_keller924ethan_keller924 Member, csevp Posts: 39 EDU
    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?
  • George_AndersonGeorge_Anderson Member Posts: 71 ✭✭
    Just to check, is this string-parsing workaround still the only way to instantiate a configured part studio, using an array for input?

    I'm trying to use a configured part studio with instantiator to get better caching. My configured part studio just contains a single feature that gets variables from the context and runs a featurescript function. I need several arbitrarily long arrays of values (50-100 values total), but it looks like I can't send general variables (including arrays and maps) via the instantiator.

    I can think of another hack, like setting 50-100 available named fields, and then reinterpreting them intelligently.

    An even more elegant method would be to define a function *as* a (hidden) configured part studio.
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    @George_Anderson
    If that configured part studio genuinely needs to depend on an arbitrarily long array of inputs, then you are correct: The best solution is passing and parsing a string.

    If you can limit the length to a more reasonable number of length inputs, that will also work. Keep in mind that you should make sure the instantiating code sets the unused values in the same way every time, to make sure you're hitting the cache.
Sign In or Register to comment.