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.

How to import a sketch into a feature script and then in a loop make copies of it along a path.

brad_phelanbrad_phelan Member Posts: 80 ✭✭
Hi there,

I have a sketch in my document. It has 3 variables set


the variables parameterize a cross section profile along a boat hull.

I would like to create a feature script feature that imports the sketch and then in a loop makes copies of it. 

I have imported the profile into a feature like so and have tried to change the MidThickness variable
during build. It was just a guess it might work like this but it doesn't. Can anybody how to suggest how
to change the variables in the imported script before build?

FeatureScript 638;
import(path : "onshape/std/geometry.fs", version : "638.0");
Profiles::import(path : "4b6e33509f1da3e4af0ad49f", version : "04aa852bfe127e381c7cf7bd");

annotation { "Feature Type Name" : "Surfboard" }
export const surfboard = defineFeature(function(context is Context, id is Id, definition is map)
        // Build a thing in a separate context ( and try to set the "MidThickness" variable ( doesn't work )
        var profile is Context = Profiles::build({"#MidThickness":"4in"});

        // Delete its construction planes and origin
        opDeleteBodies(profile, id + "deleteDefaultGeometry", {
            "entities" : qUnion([
                qConstructionFilter(qBodyType(qEverything(EntityType.BODY), BodyType.SHEET), ConstructionObject.YES),

        for (var i = 0; i < 3; i += 1)
            var idI =  id + "addProfile" + i;
            transform(profile, idI, {
                    "entities" : qEverything() ,
                    "transformType" : TransformType.TRANSLATION_3D,
                    "dx" : 0 * inch,
                    "dy" : i * inch,
                    "dz" : 0 * inch,
                    "makeCopy" : true
        //Add thing's context to the current Part Studio
        opMergeContexts(context,id + "merge", {
                "contextFrom" : profile




  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,746
    I haven't looked at your code, but importing a profile is just the evaluated entities, not the sketch with all its dimensions and constraints. Therefore you cannot create the sketch as you would like to. 

    To to do this you would have to programmatically recreate the sketch (skLine, skArc etc.) using values inputted into the feature (or variables if you really want to, but this seems unnecessary), then put that in a loop to create multiple sketches. 
  • brad_phelanbrad_phelan Member Posts: 80 ✭✭
    edited August 2017
    Are you sure. I've tried adding

            var mid = getVariable(profile, "MidThickness");
            println(mid );
            setVariable(profile, "MidThickness", 15 * centimeter);
            mid = getVariable(profile, "MidThickness");
            println(mid );

    and this outputs

    0.050800000000000005 meter
    0.15 meter
    Result: Regeneration complete

    where the first value is the value set in the sketch and the second value is the value set in the importing script. ie: the variable is updated and then is regenerated but the output geometry does not reflect the variable change. The middle diension is still 0.05 meters in the copied profiles.

    The original profile looks like

  • brad_phelanbrad_phelan Member Posts: 80 ✭✭

  • brad_phelanbrad_phelan Member Posts: 80 ✭✭
    I see the problem.

    I assumed that variables would be available like a configuration feature for the part. A bit like iLogic in Inventor. However it seems that you can't really create parametric parts yet in onshape. That's a real pity. 

    I'll come back another time and see if it's possible.
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 2,746

     For such a simple profile could you not use my suggestion?
  • brad_phelanbrad_phelan Member Posts: 80 ✭✭
    edited August 2017
    It's not so simple. Spline with constraints and fillets at the end.
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 959 ✭✭✭✭✭
    edited August 2017
    if you still want to use variable parametrization of user-defined sketch in the loop you may want to take a look at my Curve generator FS - it creates a number of copies of user-defined sketch in place (using identity transforms) varying value of the variable, in your case you need to apply a transform from a certain point of your profile to the point of path to each iteration of the cycle of applyPattern function instead of identity transform (look at documentation of the applyPattern func)

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 963
    edited August 2017
    Brad, you are correct in that the ability to pass parameters to a part studio will arrive with configurations.  However, in the mean time, I think you can get a similar effect with a feature pattern.  Here's a simple example: https://cad.onshape.com/documents/10e2e6d0e1e3d4f9000d0aab/w/ce93354f115b3fb1a7f62400/e/c4cad584ab2fb1fbfe1d9915
    The trick is to fix a point in a sketch (external references other than fix get destroyed by feature patterns) and have the index variable increment itself as part of the pattern.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 963
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • brad_phelanbrad_phelan Member Posts: 80 ✭✭
    The feature pattern video is super cool. I like it very much. Will take a play with it later.
Sign In or Register to comment.