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_steur814jelte_steur814 Member Posts: 182 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

  • MichaelPascoeMichaelPascoe Member Posts: 1,979 PRO
    edited October 25 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);
    


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴

Answers

  • MichaelPascoeMichaelPascoe Member Posts: 1,979 PRO
    edited October 25 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);
    


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • jelte_steur814jelte_steur814 Member Posts: 182 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_steur814jelte_steur814 Member Posts: 182 PRO

    ah yes,

    splineDef.derivatives = derivatives
    

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

Sign In or Register to comment.