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.

can you create a subroutine?

billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
Let's say I want to format the output for a vector

function printVector(v) {<br>   println("vector :"~roundToPrecision(v[0], 4)*1000~", "~roundToPrecision(v[1], 4)*1000~", "~roundToPrecision(v[2], 4)*1000);
}
<br>

How do I call the function?
printVector(v);

Comments

  • MichaelPascoeMichaelPascoe Member Posts: 1,988 PRO
    edited July 2022
    printVector(yourVectorHere); should call your function. Perhaps I'm missing something.

    If your function is stored in another studio, you will need to place "export" before the function. Then you can import that studio into your current studio to use it like you normally would.

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    FeatureScript 1803;
    import(path : "onshape/std/geometry.fs", version : "1803.0");
    
    annotation { "Feature Type Name" : "function" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
        }
        {
            function printOut(m) {
                print(m);
                }
                
            printOut("hello world");
        });
    <br>

    It's seems simple but I can't find any reference to functions or subroutines, I have seen other code where guys are passing the context into functions outside the "function action area" and was wondering if there is something printed in the manuals about this simple concept.

  • chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 142 PRO
    Ah, that's because you want to create the function outside of the precondition and feature body blocks of code. You're looking for 
    FeatureScript 1803;
    import(path : "onshape/std/geometry.fs", version : "1803.0");
    
    annotation { "Feature Type Name" : "function" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
        }
        {
            printOut("Hello world");
        });
    
    function printOut(m) 
    {
        print(m);
    }
                


    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 142 PRO
    edited July 2022
    Another quick tip while my brain is thinking about this, sometimes you'll deal with vectors that have units, other times you'll have vectors without units. I would recommend checking the data type in the print function. Usually you would create an overloading function, but Vectors are the same data type whether the contents of the array are a number or a ValueWithUnits. 


    FeatureScript 1803;
    import(path : "onshape/std/geometry.fs", version : "1803.0");
    
    annotation { "Feature Type Name" : "My Feature" }
    export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            // Define the parameters of the feature type
        }
        {
            // Define the function's action
            var vectorWithUnits = vector(0, 0, 1) * inch;
            var vectorWithoutUnits = vector(0, 0, 1);
    
            printVector(vectorWithUnits);
            printVector(vectorWithoutUnits);
        });
    
    export function printVector(v is Vector)
    {
        if (isLengthVector(v))
        {
            println("vector :" ~ roundToPrecision(v[0] / inch, 4) * 1000 ~ ", " ~ roundToPrecision(v[1] / inch, 4) * 1000 ~ ", " ~ roundToPrecision(v[2] / inch, 4) * 1000);
        }
        else
        {
            println("vector :" ~ roundToPrecision(v[0], 4) * 1000 ~ ", " ~ roundToPrecision(v[1], 4) * 1000 ~ ", " ~ roundToPrecision(v[2], 4) * 1000);
        }
    }


    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    @chadstoltzfus perfect. That's what I was looking for. Thanks




Sign In or Register to comment.