Call user created scripts in another script

Lee_Hesketh
Lee_Hesketh Member, Developers Posts: 148 ✭✭✭
If I have written one script called custom_axis, and am now writing a new script. How could I call custom_axis in this new script? I have tried entering custom_axis in the hopes ti appears in the drop down list, but it doesn't. How could I do this?

Thanks
Lee Hesketh
There are 10 types of people in the world. Those who know binary, those who don't and those who didn't expect base 3!

Best Answer

Answers

  • kevin_o_toole_1
    kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565 image
    A quick addendum for future readers:
    If you're you're still getting an error calling your feature or function after importing the Feature Studio, make sure the imported Feature Studio exports the relevant symbol, e.g.
    export const myFeature = defineFeature(...
    or
    export function myFunction(...
    See also: import documentation.
  • robert_carpenter114
    robert_carpenter114 Member Posts: 8 ✭✭
    Suppose I have this tiny featurescript code snippet:

    FeatureScript 819;
    import(path : "onshape/std/geometry.fs", version : "819.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
           annotation { "Name" : "My Length" }
            isLength(definition.myLength, LENGTH_BOUNDS);
            
        }
        {
            // Define the function's action
             
            
            
            fCuboid(context, id + "cuboid1", {
                    "corner1" : vector(0, 0, 0) * inch,
                    "corner2" : vector(1, 1, definition.myLength / inch) * inch
            });
        });


    Could you write a little code snippet demonstrating how to call this from another featurescript ?

  • Jake_Rosenfeld
    Jake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646 image
    Hi @robert_carpenter114

    In some other FeatureScript tab, you'll want to first click on the import button to set up the import of the other tab:


    Once you have this open, you can navigate through that menu and fine the other feature studio you're interested in importing (note that if you are importing a Feature Studio from another document, that other document must be versioned. You can do this versioning from directly inside that dialog).  When you have finalized the import it will generate a line of code that looks something like this:



    At that point it will be simple to use your other feature.  You'll be able to just write code that looks like:

    annotation {"Name" : "Some other feature"}
    export function someOtherFeature = defineFeature(function(context is Context, id is Id, definition is Map)
        precondition
        {
        }
        {
            // Generates a cuboid that is 10 inches high
            myFeature(context, id + "callToOriginal", { "myLength" : 10 * inch });
        });

    Jake Rosenfeld - Modeling Team
  • robert_carpenter114
    robert_carpenter114 Member Posts: 8 ✭✭
    Works great !  Fantastic ! Thanks Jake, Onshape rules !