Execution Environment

isDevMode

Each script runs in a sandbox. The sandbox exposes following objects and functions as global variables. They are very similar to 'window' object in the Javascript.

e.g, you can directly use

cadlib.<some_fn>(<parameters>);

or

getPartFaceByName(<parameters>) in any script.

Following objects and functions are avaialbe in the global namespace. These are case sensitive.

params
All the script parameters are accessible from params object. You define script parameters from Parameters dialog. These same parameters will be exposed during custom shape editing in the part designer.
e.g., params.width, where width is defined via parameter dialog.

This lets you design Parametric Shapes
cadapi
This object exposes several objects and utility methods to create and manipulate a shape. Refer to Modeling Operations and Core Objects sections of this guide
mathlib, EPSILON, Mat33, Mat44, Vec2, Vec3, Vec4, Quaternion. Transform
Please refer to this documentation
console
console.log provides a convenient wrapper to log debug statements to be used during development


Important - Read this
Many of the methods below expect Part Id as one of the parameter.

Click Here to read more about how to use Face or Edge parameter to extract crucial pices of information (faceId, partId, regionId, faceName)


Additional global functions

isDevMode

isDevMode() function will return true when script is executing in the development mode, i.e, in the custom shape editor.

When the same script executes during part build process, isDevMode() returns false. This gives you greater control over responding to user selections.

Syntax

isDevMode()

Parameters

No parameters

Returns

Boolean - True, if running in custom shape editor, else False

Example


//psesudo code 

function build() {

 if(isDevMode()) {

     //create a solid
     //extract a face
     //offset the face

 } else {

     // face = params.face (this assumes you have defined a Face type parameter. In the part designer, user will be able to choose a face)
     // offset the face
 }

 //return solid;


}

resetFeatureTransform

This function will reset current shape feature’s transform.

Syntax

resetFeatureTransform();

Parameters

No parameteres

Return Value

This function does not return anything


getPartParams

This function will return parameters, if any defined, of the current part. Remember, each custom script is ‘feature’ in a part.

This lets you take proper actions depending on part level parameters. You could, for example, throw an exception if a certain required parameter this script needs is not defined at part level.

Use this function when not in the development mode (isDevMode() === false)

Syntax

getPartParams();

Parameters

No parameteres

Return Value

{Object} - An object with key/value pair, where key is the name of the part level parameter.


getPartFaceByName

This operation is used to extrat face information based on name.

Syntax

getPartFaceByName(partId, regionId, faceName);

Parameters

Name Type Description
partId String partId
regionId String regionId of the region this feature belongs to
faceName String Name of the face is more reliable way

Return Value

{Object} - An object that describes a face.

Example

Please refer to an example here to understand how to use this function.


getRegionSolid

This operation is used to extrat region information.

Syntax

getRegionSolid(partId, regionId);

Parameters

Name Type Description
partId String partId
regionId String regionId of the region this feature belongs to

Return Value

{Object} - An object that describes a face.

Example

Please refer to an example here to understand how to extract face information.


updateFeatureTransform

This function is used to update current shape’s transform.

Syntax

updateFeatureTransform(xform, concatTransform);

Parameters

Name Type Description
xform Transform {position, quanternion, scale: {factor}}.
concatTransform Boolean If True, will replace current transform, else applies to existing transformation

Return Value

No return value



setVariable

Adds a variable to internal variable collection. Subsequent custom feature can retreive this value using getVariable call.

Thus, this mechanism enables multiple custom shapes in a part to pass on to each other result of their execution.

Syntax

setVariable(name, value);

Parameters

Name Type Description
name String Variable name
value Any Variable value

Return Value

No return value


getVariable

Retreives variable based on the name. Null if variable was not found.

Syntax

getVariable(name);

Parameters

Name Type Description
name String Variable name

Return Value

[Any] - Return value


resetVariables

Resets variables collection.

Syntax

resetVariables();

Parameters

No parameters

Return Value

No return value


getPartUnitValue

Returns Part Id of which this feature is a member of.

Syntax

getPartUnitValue(partId);

Parameters

Name Type Description
partId String PartId can be acquired by querying a face

Return Value

String - Returns part unit as string value. e.g., “mm”, “cm” etc