" />

ProtoTech's Blog

CAD Customization, CAD Plugin development, QA, 3D development and Services, Augmented and Virtual Reality applications

How to use Onshape FeatureScript for UI Automation and custom shape generation?

What is Onshape FeatureScript?
FeatureScript 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 FeatureScript:

  • It is lightweight, executes fast.
  • FeatureScript 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 FeatureScript 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 FeatureScript?
We can not export model through FeatureScript to various type such as STL, OBJ, etc.
Unable to debug FeatureScript.


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 FeatureScript for UI Automation and custom shape generation?
To write your own FeatureScript, 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.
Steps to create FeatureScript as follows:

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

  • Goto 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 FeatureScript for UI Automation and custom shape generation?

 

 

Contact Us:

Tags:

About

ProtoTech Solutions is a custom software development company with skills in engineering graphics, specifically focused in the areas of CAD/CAM/CAE and Graphics. My role in the company is to look after everything, and specially the work which is most important or critical for that day.

View all posts by

POST A COMMENT

You must be logged in to post a comment.