FeatureScript: opFunction with boolean for optional parameters

jelte_steur_info
jelte_steur_info Member Posts: 683 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,965 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 - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚

Answers

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,965 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 - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • jelte_steur_info
    jelte_steur_info Member Posts: 683 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: 683 PRO

    ah yes,

    splineDef.derivatives = derivatives
    

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