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.

Options

Can Featurescript pull sketches from other documents?

I am trying to write a Featurescript that can make extrusions of predefined shapes centered on points defined by the user.
I would like to have a document full of sketch profiles that can be selected from, but i don't know how to cross reference documents and I haven't found anything in the documentation yet.

How can I get a sketch from one document and use it in a Featurescript for another?

Best Answers

  • Options
    chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 131 PRO
    Answer ✓
    There's a couple of ways to do this, and it depends on how you want the user to interact with the feature. Two of the primary ways of cross referencing documents is either with Reference parameters or Imports. 

    If you want to have the user be able to select the sketch by selecting a Part Studio (let's say each Part Studio contains a single sketch) you could use Reference parameters, which will have a similar UI/UX to Derive. Onshape has provided an example document of working with reference params here.

    If you want the the user to select from a drop down and have the feature find the sketch, then you will need to use Imports. From here you can just set up some if/else statements (or a case statement) to pick which import to derive based on the user's selection from the drop down. If you are using this option, it would be simpler to have it so each sketch is in its own Part Studio, though there are some clever workarounds for this. If you happen to have all of the sketches in one Part Studio, you will need to have some way of letting your custom feature find which sketch to import, this can be accomplished with Attributes

    I'm happy to go into more detail on either of these approaches if you are able to let me know which approach you would prefer. 
    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    edited April 2023 Answer ✓
    Yes, however they will not behave exactly the same as sketches created by the sketch feature. For example, you can't import these into the assembly or drawings. You can use them for extrudes and for getting queries.

    You can either hard code the part studios into your feature like this so that the user doesn't have to derive, or you can have the user derive like this:



    FeatureScript 1993;
    import(path : "onshape/std/common.fs", version : "1993.0");
    
    annotation { "Feature Type Name" : "Instantiate Test" }
    export const instantiateTest = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Derived" }
            definition.derived is PartStudioData;
    
            annotation { "Name" : "Location", "Filter" : BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 1 }
            definition.location is Query;
        }
        {
            const instantiator = newInstantiator(id + "inst", {});
            const toPlane = evMateConnector(context, { "mateConnector" : definition.location })->plane();
            const instance = addInstance(instantiator, definition.derived, { "configuration" : {}, "transform" : transform(XY_PLANE, toPlane)});
            instantiate(context, instantiator);
            const instantiatedFaces = qCreatedBy(id + "inst", EntityType.FACE);
    
            debug(context, instantiatedFaces, DebugColor.GREEN);
        });


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎

Answers

  • Options
    chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 131 PRO
    Answer ✓
    There's a couple of ways to do this, and it depends on how you want the user to interact with the feature. Two of the primary ways of cross referencing documents is either with Reference parameters or Imports. 

    If you want to have the user be able to select the sketch by selecting a Part Studio (let's say each Part Studio contains a single sketch) you could use Reference parameters, which will have a similar UI/UX to Derive. Onshape has provided an example document of working with reference params here.

    If you want the the user to select from a drop down and have the feature find the sketch, then you will need to use Imports. From here you can just set up some if/else statements (or a case statement) to pick which import to derive based on the user's selection from the drop down. If you are using this option, it would be simpler to have it so each sketch is in its own Part Studio, though there are some clever workarounds for this. If you happen to have all of the sketches in one Part Studio, you will need to have some way of letting your custom feature find which sketch to import, this can be accomplished with Attributes

    I'm happy to go into more detail on either of these approaches if you are able to let me know which approach you would prefer. 
    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    edited April 2023 Answer ✓
    Yes, however they will not behave exactly the same as sketches created by the sketch feature. For example, you can't import these into the assembly or drawings. You can use them for extrudes and for getting queries.

    You can either hard code the part studios into your feature like this so that the user doesn't have to derive, or you can have the user derive like this:



    FeatureScript 1993;
    import(path : "onshape/std/common.fs", version : "1993.0");
    
    annotation { "Feature Type Name" : "Instantiate Test" }
    export const instantiateTest = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Derived" }
            definition.derived is PartStudioData;
    
            annotation { "Name" : "Location", "Filter" : BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 1 }
            definition.location is Query;
        }
        {
            const instantiator = newInstantiator(id + "inst", {});
            const toPlane = evMateConnector(context, { "mateConnector" : definition.location })->plane();
            const instance = addInstance(instantiator, definition.derived, { "configuration" : {}, "transform" : transform(XY_PLANE, toPlane)});
            instantiate(context, instantiator);
            const instantiatedFaces = qCreatedBy(id + "inst", EntityType.FACE);
    
            debug(context, instantiatedFaces, DebugColor.GREEN);
        });


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    MichaelPascoeMichaelPascoe Member Posts: 1,713 PRO
    edited April 2023

    lol my page refreshed and it looks like @chadstoltzfus beat me to it! :D


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   cadsharp.com/featurescripts 💎
  • Options
    Michael_UrsuliakMichael_Ursuliak Member Posts: 14
    I would love to have it show as a drop down menu that automatically updates when new sketches are added. but I am unsure of how to do this. The onshape featurescript tutorial only goes into importing from the same document rather than an entirely separate one. also to clarify, I am essentially trying to make a hole punch with predefined hole shapes and sizes.

Sign In or Register to comment.