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.

Code in variable definition line

ben_partouchben_partouch Member, csevp Posts: 135 PRO
Hello,

I want a configurable part to have a dimension driven by a variable in the document it's derived to.
However, until it's derived to that doc' it needs a default value.
What should I type in the definition line that will tell the part to use a variable name "depth" and if it's not existing use a default number of 600mm?

I hope I make any sense at all.

Thank you

Comments

  • Nath_McCNath_McC Member Posts: 127 PRO
    The simplest way I would have tried to complete this is by using a configuration variable in which you can set a default dimension. 



    I'm not 100% if the above is the correct answer. Another way could be to have more than one variable I would say something like the below so you would essentially have three variables?

    variable 1: configDepth = currently linked to config variable// linked to part - will turn into the derived variable
    variable 2: standardDepth = 600mm
    variable 3: depth = ((configDepth <= 0) ? standardDepth ; configDepth)

    https://cad.onshape.com/documents/a77252951090311b449a810f/w/52c8aaca092ce6b6b227b2bd/e/775912e49d659ac70e4036ae

    if you update the configuration depth value it will auto-change the depth variable otherwise if it is 0 it will default at 600mm





  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    Thank you @Nath_McC.

    I need the code to try and use a variable that will only exist in other doc's and when it's not, it will use default.
    So in my original configuration there is not variable available. Maybe not possible?
     
  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    edited September 2022
     Which other doc's will the variable be located? There are multiple ways to store and retrieve variables in other docs. 

    This approach will look in the current studio, but not other studios.
    //Set your variable with the default value you would like
    var myVariable = 600 * millimeter;
    
    //Use a try statement to try and get the existing variable "depth"
    try
    {
        //Get the existing part studio variable "depth"
        const existingVariable = getVariable(context, "depth");
       
        //Set your variable to the existing one     
        myVariable = existingVariable;
    }
    catch
    {
        //If the try statement fails, catch it and report the missing info
        reportFeatureInfo(context, id, "No variable found: depth");
    }


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    Thanks Michael!

    Can you write this into a definition line in the part studio? I can't use a custom feature for this action. 
    Also, instead of a warning I want to default to a pre-set dimension. 

    Eventually when import this configured part into a new part studio it will automatically find the variable or go to default. That way I don't need to manually type the variable name everytime in the configuration menu when deriving the part. 

    Thank you,
  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    edited September 2022
    Currently, logical expressions have limited functionality within a configuration dialogue box. Also, I'm not sure we are talking about the same thing.

    Can you provide more info why you don't want to use a custom feature? To me, this seems like the perfect scenario for a custom feature.

    You can have a custom feature that will derive and check for a local variable. Part studios do not interact with the studio they are being derived to unless you specifically type in a value in a configuration in the derived input boxes, or you use a custom feature.



    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    I want to derive my configured part into a studio with pre-set variables. Than, the derived configured part will automatically look for the variable and use it. If it can't find it, it'll use a default. If I don't have this function the configured part is failing. 

    How would you use custom feature in this case?
    I just thought it's much simpler without custom feature.
    When will I activate it? Will it be a part of the configured part?

    Thank you for your time Michael, I really appreciate it.

  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    edited September 2022
    Yes, this is possible. Here is an example of how you could do this:

    Notice how when the variable "myVariable" is suppressed, the feature automatically uses the default 600mm that was set in the featurescript. This feature would allow the user to pick which part studio they wanted to derive. However, you could permanently set this to a specific studio if you were always deriving the same studio over and over again.

    If this is for your company, consider using CADSharp to automate your workflow. We write custom features and API to save tons of time for part studios, assemblies, and entire document libraries. I've seen some tasks so automated that the user simply presses a button and watches their assembly build.
    FeatureScript 1837;
    import(path : "onshape/std/geometry.fs", version : "1837.0");
    
    // If part is never changed you can import it from the feature without users input:
    // firstPartStudio::import(...);
    // secondPartStudio::import(...);
    
    annotation { "Feature Type Name" : "Custom derived test" }
    export const customDerived = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Derived" }
            definition.derived is PartStudioData;
        }
        {
            // Your default length
            var myVariable = 600 * millimeter;
    
            // Try to get the variable from the current studio
            try silent
            {
                myVariable = getVariable(context, "myVariable");
            }
    
            // Create a new instantiator
            const instantiator = newInstantiator(id + "myInstantiator");
    
            // Get the users selected part studio
            var studioToInstantiate = definition.derived.buildFunction;
    
            // Custom configuration for the users selected part studio
            var configMap = {
                "myVariable" : myVariable
            };
    
            // Add an instance to the instantiator
            var firstQuery = addInstance(instantiator, studioToInstantiate, {
                    "configuration" : configMap,
                    "transform" : transform(vector(0, 0, 0) * inch)
                });
    
            // Add a second instance if you want more than one part studio derived
            // var secondQuery = addInstance(instantiator, secondPartStudio::build, {
            //         "configuration" : secondConfiguration,
            //         "transform" : someOtherTransform,
            //         "mateConnector" : queryForMateConnectorInSecondPartStudio // Specifies the origin
            //     });
    
            // repeat the above as necessary
    
            // Create the part
            instantiate(context, instantiator);
        });

    Custom Derived Featurescript Test - Onshape Link



    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    Using it inside the part sudio is what I'll have to do.
    I have quite a few variable to "automate" this way so I assume I'll need a separate feature for each one..

    Thank you very much Michael!
  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    edited September 2022
    @ben_partouch, if it is one part studio, you can do them all in one feature B), no need to create more than one.

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    Nice. Many thanks Michael!
  • ben_partouchben_partouch Member, csevp Posts: 135 PRO
    @MichaelPascoe
    Took me a while to implement this but it's great. Thank you!

  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO

    @ben_partouch, nice! The first time is the most difficult. FeatureScript is a language, it gets easier the more you use it.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
Sign In or Register to comment.