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.

Importing Part Studios and Feature Studios

kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 455
edited April 2016 in FeatureScript
One powerful feature of Onshape's FeatureScript-based architecture is the ability to freely import Part Studios and Feature Studios into one another.

Importing a Feature Studio into a Part Studio is what happens under the hood when you add FeatureScript features:


Importing a Part Studio into a Part Studio is what happens under the hood when you use the "Derived part":



Importing a Feature Studio into a Feature Studio
Importing into Feature Studios will eventually have a GUI too, but in the meantime, you can perform these imports by typing the import text directly into your Feature Studio, as shown below:

 

  1. Get the element id of the imported Feature Studio by copying the 24-character string following "/e/" from your URL (e.g. a4b15a1a4bba13206e42cd64).

  2. On a line after or before the Onshape Standard Library import, type an import for that Feature Studio:
    import(path : "a4b15a1a4bba13206e42cd64", version : "");
    
    You can alternatively import this module into its own namespace by adding a namespace prefix:
    MY_MODULE::import(path : "a4b15a1a4bba13206e42cd64", version : "");
    

  3. Commit the Feature Studio. Onshape will automagically populate the version field with the latest microversion (and continue to update it as you make changes to the imported Feature Studio):
    import(path : "a4b15a1a4bba13206e42cd64", version : "e679dc5c60bcb740e22a115e");
    

  4. Make sure any feature, function, type, or enum you want to use from the imported module is exported, e.g.
    export function myFunction(...
    export const myFeature = defineFeature(...
    

  5. Now you can call the exported symbols directly, like this:
            myFunction("foo");
            myFeature(context, id, {
                "bar": "bar"
            });
    
    Or, if you used a namespace, like this:
            MY_MODULE::myFunction("foo");
            MY_MODULE::myFeature(context, id, {
                "bar": "bar"
            });
    
Importing a Part Studio into a Feature Studio:

You can build geometry from a Part Studio right inside your feature by importing the Part Studio. You can import the Part Studio by the element id, just as above.


When typing the import, you'll notice an error when first importing the Part Studio without a version, as in the gif above. This is a known bug, but it can be worked around by refreshing the page and immediately pressing commit.  This has been fixed in Onshape version 1.41

The function you need from the Part Studio is named "build", which returns a context containing the Part Studio's geometry. 

Here's an example feature using this method:
FeatureScript 275;
import(path : "onshape/std/geometry.fs", version : "275.0");
THING::import(path : "7cc954522dbc4ce38424d920", version : "");

annotation { "Feature Type Name" : "Add thing" }
export const addThing = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Mounting mate connector", "Filter" : BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 1 }
        definition.mountingConnector is Query;
    }
    {
        // Build a thing in a separate context
        var contextWithThing is Context = THING::build();

        // Delete its construction planes and origin
        opDeleteBodies(contextWithThing, id + "deleteDefaultGeometry", {
            "entities" : qUnion([
                qConstructionFilter(qBodyType(qEverything(EntityType.BODY), BodyType.SHEET), ConstructionObject.YES),
                qCreatedBy(makeId("Origin"))
            ])
        });
        
        // Add thing's context to the current Part Studio
        opMergeContexts(context, id + "addThing", {
            "contextFrom" : contextWithThing
        });

        // Move the thing to the mate connector
        var transform is Transform = toWorld(evMateConnector(context, {
               "mateConnector" : definition.mountingConnector
        }));
        opTransform(context, id + "transformThing", {
               "bodies" : qCreatedBy(id + "addThing", EntityType.BODY),
               "transform" : transform
        });
    }, { /* default parameters */ });
This feature adds the bodies of a Part Studio, and transforms them. You could imagine adding other steps in the feature too (boolean, modify, create more geometry, etc.).

Imports collectively open up some powerful methods of defining reusable geometry. Our hope is for FeatureScript users to find a style of working where geometry best defined with traditional, interactive modeling lives inside a Part Studio, and geometry best defined with code and logic lives in a Feature Studio.

Comments

  • jacob_kingeryjacob_kingery Member Posts: 39 EDU
    Great post, thanks Kevin!

    If you want to isolate something from the imported Part Studio, say a specific sketch, how would you go about doing that? I tried looking in the imported Part Studio's code and copying the sketch's id (which looked like id + 'autogeneratedcharacters') and putting it in a qSketchRegion, but that didn't seem to work. This makes sense since the id variable is different between the imported Part Studio and the Part Studio where the feature is doing the importing. Is it possible to target something by id from the imported Part Studio?
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 455
    Great question.

    The opMergeContexts performs an operation in the context. All geometry created by the merge is a child of the merge operation.

    In your case, the id pointing to the sketch in question will be id + 'idOfMergeOperation' + 'autogeneratedIdOfSketchInPartStudio'  
  • jacob_kingeryjacob_kingery Member Posts: 39 EDU
    That did it, thanks Kevin!
Sign In or Register to comment.