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: 89 ✭✭
Hi there,

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

MidThickness
Width
Height

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)
    precondition
    {
      
        
    }
    {
            
        // 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),
                qCreatedBy(makeId("Origin"))
            ])
        });

        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
        });
        

    });

Tagged:

Comments

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    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. 
    Senior Director, Technical Services, EMEAI
  • brad_phelanbrad_phelan Member Posts: 89 ✭✭
    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: 89 ✭✭

  • brad_phelanbrad_phelan Member Posts: 89 ✭✭
    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: 5,671
    Correct. 

     For such a simple profile could you not use my suggestion?
    Senior Director, Technical Services, EMEAI
  • brad_phelanbrad_phelan Member Posts: 89 ✭✭
    edited August 2017
    It's not so simple. Spline with constraints and fillets at the end.
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    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)
    https://cad.onshape.com/documents/d8aab1e0e7ae10038a6830e0/w/9dfdd631d025960813b8f2df/e/621b5be97d38074a1f1546e5

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    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 \ VP, Architecture and FeatureScript \ Onshape Inc
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 1,210
    Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc
  • brad_phelanbrad_phelan Member Posts: 89 ✭✭
    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.