How to use Onshape feature script for UI Automation & custom shape generation?

How to use Onshape feature script for UI Automation & custom shape generation?

What is Onshape Feature Script?
Feature Script is a scripting language which allows automation in Onshape environment. It is introduced by Onshape for creating and working 3D parametric models. This script provides foundation for modeling, powerful parametric tools, robust geometric references, and a type system with types built for math in three dimensions

FeatureScript looks like JavaScript and runs into Feature Studio, which is incorporated in Onshape. FeatureScript allows access to Onshape standard library which provides the number of CAD operations like extrude, revolve, fillet, as well as the number of geometry functions to deploy/create custom features.

Advantages of Feature Script:

  • It is lightweight, executes fast
  • Feature Script runs in Onshape Feature Studio in the browser. No external setup is required
  • You can create new features with customized modeling tasks
  • Building specialized patterns with unique per-instance behavior
  • Share Feature Script with other users
  • Onshape is having an iOS and Android apps. You can demonstrate your model whenever you want with manipulating its parameters runtime

 

What are the limitations of Feature Script?
We can not export model through Feature Script to various type such as STL, OBJ, etc.
Unable to debug Feature Script.

Will be helpful in?
You can create the geometric shape by using Onshape standard library.
Ex. Car designing with parametric changes. It will give user better visual experience about a model.

How to use Onshape Feature Script for UI Automation and custom shape generation?
To write your own Feature Script, you need to define parameters in preconditions parenthesis and just below parenthesis write functions.

Let’s have a simple demo for creating cylinder and cone.

  • Open FeatureScript studio
  • Copy following code snippets to studio and commit the script, it will add your script to side of “+” icon on part studio

Code:

export enum ProtoTechGeometry
{
annotation { “Name” :”cylinder”}
_cylinder,
annotation { “Name”:”Conical”}
_Conical,
}

annotation { “Feature Type Name” : “ProtoTech Featurescript” }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
//Define the parameters of the feature type
annotation { “Name” : “Choose type” }
definition._ProtoTechGeometry is ProtoTechGeometry;

annotation { “Name” : “Radius” }
isInteger(definition._Radius, POSITIVE_COUNT_BOUNDS);

annotation { “Name” : “Length” }
isInteger(definition._Length, POSITIVE_COUNT_BOUNDS);
}
{
//Define function action
if(definition._ProtoTechGeometry==ProtoTechGeometry._cylinder)
{
fCylinder(context, id + “cylinder1”, {
“topCenter” : vector(5, 0, 0) * inch,
“bottomCenter” : vector(5,definition._Length, 0) * inch,
“radius” : 1 *definition._Radius* inch
});
}
else if (definition._ProtoTechGeometry==ProtoTechGeometry._Conical)
{
var sketch1 = newSketch(context, id + “sketch1”, {
“sketchPlane” : qCreatedBy(makeId(“Top”), EntityType.FACE)
});

skPolyline(sketch1, “triangle1”, {
“points” : [
vector( -5, 0) * inch,
vector( -5 -definition._Radius,0) * inch,
vector( -5, definition._Length) * inch,
vector( -5, 0) * inch
]
});

// Create sketch entities here
skSolve(sketch1);

opRevolve(context, id + “revolve1”, {
“entities” : qSketchRegion(id + “sketch1”),
“axis” : line(vector( -5, 0,0)* inch, vector(0, definition._Length, 0) * inch),
“angleForward” : 360 * degree
});
}
});

  • Go to part studio and click on script. It will run your script.
  • You can change the type of shape as cylinder or cone and its length and radius and see the magic.

Here’s a video demonstrating that, How to use Onshape Feature Script for UI Automation and custom shape generation?

 

Author: Dhole S.
Contact us:
info@prototechsolutions.com
ProtoTech Solutions and Services Pvt. Ltd.