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.

FeatureScript: opFunction with boolean for optional parameters

jelte_steur_info
jelte_steur_info Member Posts: 676 PRO

Hi,

I'm working on a featurescript using opFitSpline functionality.

I always use the endDerivative and startDerivative, but have added a boolean option to check/uncheck in the UI for the intermediary derivatives.

my code now looks like this:

   // create a spline using the arrays
    
    if (definition.derivativeEndOnly)
    {
    opFitSpline(context, id + "fitSpline1", {
                "points" : pathPoints,
                "startDerivative" : startDerivative,
                "endDerivative" : endDerivative,
                "derivatives" : derivatives
            });
    }
    else
    {
              opFitSpline(context, id + "fitSpline1", {
                "points" : pathPoints,
                "startDerivative" : startDerivative,
                "endDerivative" : endDerivative,
            });  
    }

is there a cleaner way to disable the "derivatives" in one opFitSpline without having two different opFitSpline functions?

leaving the derivatives map empty hasn't worked so far.

Best Answer

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,944 PRO
    edited October 2024 Answer ✓

    See how the definition of the opFitSpline is a map { }. You can create that map beforehand then add only what you need to it, then feed it into the opFitSpline:

    var splineDef = {};
    splineDef.points = pathPoints;
    splineDef.startDerivative = startDerivative;
    splineDef.endDerivative = endDerivative;
    
    if (definition.derivativeEndOnly) splineDef.derivatives = derivatives;
    
    opFitSpline(context, id + "fitSpline1", splineDef);
    

    RENDERCAD
    rendercad.ai - Photorealistic product rendering.

    ▚▞▚▞▚▞▚▞▚
    ________________________________________________________________________

Answers

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,944 PRO
    edited October 2024 Answer ✓

    See how the definition of the opFitSpline is a map { }. You can create that map beforehand then add only what you need to it, then feed it into the opFitSpline:

    var splineDef = {};
    splineDef.points = pathPoints;
    splineDef.startDerivative = startDerivative;
    splineDef.endDerivative = endDerivative;
    
    if (definition.derivativeEndOnly) splineDef.derivatives = derivatives;
    
    opFitSpline(context, id + "fitSpline1", splineDef);
    

    RENDERCAD
    rendercad.ai - Photorealistic product rendering.

    ▚▞▚▞▚▞▚▞▚
    ________________________________________________________________________
  • jelte_steur_info
    jelte_steur_info Member Posts: 676 PRO

    Got it!

    thanks Micheal!

    var fitSplineMap = {

            "points" : pathPoints,
            "startDerivative" : startDerivative,
            "endDerivative" : endDerivative,
        };

        if (!definition.derivativeEndsOnly)
        {
            fitSplineMap = mergeMaps(fitSplineMap, { "derivatives" : derivatives });
        }

        opFitSpline(context, id + "fitSpline1", fitSplineMap);
    
  • jelte_steur_info
    jelte_steur_info Member Posts: 676 PRO

    ah yes,

    splineDef.derivatives = derivatives
    

    is cleaner than mergeMaps, but the idea is the same indeed