Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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_1
Onshape Employees, Developers, HDM Posts: 565
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:
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:
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.
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:
- Get the element id of the imported Feature Studio by copying the 24-character string following "/e/" from your URL (e.g.
a4b15a1a4bba13206e42cd64
). - 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 : "");
- 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");
- 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(...
- 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" });
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.
2
Comments
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 aqSketchRegion
, but that didn't seem to work. This makes sense since theid
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?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'
first of all, thanks a lot for the information, and it is great as information,
one thing I wanted to point out (seen that you make part of Onshape staff), is it would be great to add the path and version (the 1. up to 3. steps) in the documentation when we are reading https://cad.onshape.com/FsDoc/imports.html#importing-external-data I spent a lot of time trying to find how to do this for a custom icon and at least I have not seen it (for the path and version), thanks to god i found this post.
either way, thank you a lot!
You can also use a reference parameter to allow users to choose whatever geometry they wish in a manner akin to Onshape's derive feature or Ilya Baran's super derive feature. However, since you have a single sketch in mind, this probably isn't necessary for your purpose.
I am trying the instantiator method and have successfully brought in sketches. I'm not sure of a good way to filter for the one I want. I thought I could just use qCreatedBy(), but that doesn't seem to work.
The easiest way to bring in a specific sketch is to configure your part studio such that there's only one unsuppressed sketch, then use the configuration options of the instantiator to select the correct configuration when importing (configuration values can be viewed using the change FeatureScript Id option of the configuration menu). Depending on how your part studio is set up, this could be fairly straightforward. qCreatedBy and similar id based query methods are tricky to get working since the way ids work when importing a context isn't very clear - plus those methods tend to be not very robust.
The error message would be something like below:
Called value must be function, value is Transform (map)
@jake_r@Jake_Rosenfeld mentions the bug in this post from 2019:
https://forum.onshape.com/discussion/11462/transform-help-request
- Morteza