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.

Pass Boolean through FS dialog

CAD_SSPCAD_SSP Member Posts: 45 ✭✭
At present you cannot pass a boolean through a feature script dialog, this is ideal if you want to hide a feature in certain configurations (rather than forcing them to fail which seems an odd workaround to me).

Something like:
<span style="background-color: transparent; color: inherit; font-size: inherit; white-space: pre-wrap;">annotation { "Name" : "My Boolean Variable" }
</span><span style="background-color: transparent; color: inherit; font-size: inherit; white-space: pre-wrap;">definition.myBooleanVariable is booleanVariable;</span>
A current workaround is to pass the variable name in as a string (without "#") and then check if it is a boolean and so on but is not very intuitive as you want to include the "#" but that stops it working.

Tagged:

Comments

  • mahirmahir Member, Developers Posts: 1,291 ✭✭✭✭✭
    I'm not sure I understand this request. FeatureScripts can defintely use boolean inputs. They look like checkboxes.

    annotation { "Name" : "My Boolean" }
            definition.myBoolean is boolean;
  • CAD_SSPCAD_SSP Member Posts: 45 ✭✭
    edited January 2018
    @mahir what I am trying to achieve is this:
    If your partstudio contains a variable that is a boolean such as "#Showitem = false" you cannot pass this boolean value into featurescript (as far as I can see).
    A boolean input as you state is a checkbox, you cannot assign a variable value or a formula to it.

    The only workaround I have found is to us a string query and pass the boolean name in as text, then you can use the use "getVariable" within FS to check the value i.e.
    <div>annotation { "Name" : "Hide If:" }
    definition.hideIf is string;</div>

    pass in "Showitem" without the "#" and then do a check to see what if anything was passed in:
    <div>var hideIfcheck is boolean = false;
    <span>if (definition.hideIf != "")
    </span>{
        try&nbsp;
        {
            if (getVariable(context, definition.hideIf))
                {
                     hideIfcheck = true;
                }
        }
        catch (error)
        {
            reportFeatureWarning(context, id, "you have used a non Boolean or non-existant variable");
        }
    }</div>
    This is a messy workaround as you instinctively want to put the variable name when you type it with the "#" but I cannot find a way of removing that within featurescript.


  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    edited January 2018
    Have you tried isAnything? That will take a variable as input. 
    Senior Director, Technical Services, EMEAI
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,310
    edited January 2018
    isAnything(definition.myBoolean)
    Senior Director, Technical Services, EMEAI
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    A code snippet for Neil's suggestion:

    annotation { "Feature Type Name" : "Take Boolean" }
    export const takeBoolean = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Boolean" }
            isAnything(definition.bool);
            
        }
        {
                if (!(definition.bool is boolean))
                    throw regenError("Boolean should be a boolean", ["bool"]);
        });

    But you may want to consider just using a normal checkbox for your feature and then using configurations to toggle it rather than variables:
    https://www.onshape.com/videos/introducing-onshape-configurations
    Jake Rosenfeld - Modeling Team
  • mahirmahir Member, Developers Posts: 1,291 ✭✭✭✭✭
    edited January 2018

    But you may want to consider just using a normal checkbox for your feature and then using configurations to toggle it rather than variables:
    https://www.onshape.com/videos/introducing-onshape-configurations
    That's what I was thinking. Here's an example of configuring the Unsupressed property of a feature, which seems to be exactly what you'd like to do. In this case I used a list input to control suppression state of the circle and square, but I used checkbox inputs to independently control the suppression state of the triangle and pentagon.
    https://cad.onshape.com/documents/57acdfaae4b005c413ed9b6f/w/3fd585a46d3af1b3ba413c53/e/b02ec721136c6121c1a02cc1
  • CAD_SSPCAD_SSP Member Posts: 45 ✭✭
    Thanks, isAnything is exactly what I want.

    Configurations are perfect for most applications but don't suit this particular case.
Sign In or Register to comment.