Like any programming language, the most basic thing to do is to print out "Hello World!" somehow. In our case, we are not only going to print it, but also create a primitive shape - a cylinder!
To do it in the Editor, just type in or copy the following code:
make cylinder() echo("Hello World!")
Then press Compile (the leftmost button at the Toolbar, wich resembles a Play Button). You should see a Hello World! printed in the Output section and a cylinder rendered in the Results section. Congrats! This is your first trCAD script! If you want to try different shapes, just change cylinder
to sphere
, box
, cone
or torus
. To see all available shapes, check out the Primitive Solids section of the trCAD documentation.
As you can observe, to render solids in trCAD you need to use the make
operator. To print out text output you can use the echo
function.
Now let's improve the first example a little bit by using some variables. This way it is easier to generate solids with parameters that are easy to change programmatically. Check out the code snippet below.
int height = 30 int radius = 5 solid my_cylinder = cylinder(height, radius) make my_cylinder
With this code, you can create a cylinder with the dimensions defined by the height
and radius
variables.
Creating some geometries with hard-coded params was simple, wasn't it? How about adding some user interaction now? With this example we will show you how to add some parameters that the user can interact with in an easy way. These are the so called Open Params. Check out the Open Paramaters trCAD documentation to learn more.
In this example we will change the cylinder dimensions using Open Params. First, let's change its height.
open int height { name = "Height of Cylinder" descr = "Height of my first cylinder" value = 30 } float radius = 5 solid my_cylinder = cylinder(height, radius) make my_cylinder
By running the previous code, you can see that the same cylinder as the one before is created. But, with this code, not only the cylinder is created - just below the cylinder visualization at the Results section there will be an input field. With this field you can change the height of the cylinder. You can change the value and press Enter to update the visualization. We will leave as an exercise the implementation of the Open Param for the radius value.
Now let's add some automatic generation of geometries. Let's make a loop that creates multiple cylinders in a row. To learn more about loops in trCAD, check out Arrays and Loops section of the trCAD documentation.
open int height { name = "Height of Cylinder" descr = "Height of my first cylinder" value = 30 } float radius = 5 solid my_cylinder = cylinder(height, radius) for (int i = 0; i < 5; i++) { make translation(<[radius * 3 * i, 0, 0]>) >> my_cylinder }
With this code, you will see 5 cylinders which are copies of my_cylinder
, but displaced (translated) in the x-direction. The translation
function is a especial kind of function of trCAD called modifier. This is a set of different transformations used to manipulate any 3D geometry. Check out the Transformations documentation and the Modifiers documentation to learn more.
Now that you are familiar with some basic functionalities, let's try something more complex but also essential in CAD modelling.
Boolean operations are a powerful feature used in 3D modelling. trCAD have the main boolean operations for solids: union, difference and intersection. You can learn more at Solid Boolean Operations documentation. What we will create in this example is a cylinder with many holes. The holes will be generated by the same cylinder used in the previous examples.
open int height { name = "Height of Cylinder" descr = "Height of my first cylinder" value = 30 } int number_of_cylinders = 12 float radius = 5 float main_cylinder_radius = radius * 10 // Main cylinder has 10 times the radius of my_cylinder solid my_cylinder = cylinder(height, radius) solid main_cylinder = cylinder(height, main_cylinder_radius) solid holes // holes is initialized as an empty solid for (int i = 0; i < number_of_cylinders; i++) { // At each iteration, a copy of my_cylinder displaced around the main cylinder is added to holes holes += rotation(<[0, 0, 1]>, rad(360 / number_of_cylinders) * i) >> translation(<[main_cylinder_radius - (radius * 2), 0, 0]>) >> my_cylinder } // Subtract the sum of all cylinders (the holes solid) from the main_cylinder make (main_cylinder - holes)
This example is a little bit more complex. You can try and see the results of the parts by running make
for each solid. Try out make holes
and make main_cylinder
instead of the last make
to see what these solids are.
Getting the dimensions of a solids axis aligned bounding box assumed that one trCAD unit is one mm. The default unit of meassure are trCAD units or millimeters. You can get centimeters or inches using the strings 'cm' or 'inch' as the second parameter.
function getBboxDimensions( solid s, string uom ) { selectbox sBbox = s.min_bbox() vector v1 = <[ sBbox.minx, sBbox.miny, sBbox.minz ]> vector v2 = <[ sBbox.maxx, sBbox.miny, sBbox.minz ]> vector v3 = <[ sBbox.minx, sBbox.maxy, sBbox.minz ]> vector v4 = <[ sBbox.minx, sBbox.miny, sBbox.maxz ]> vector connectingVectorV1V2 = v2 - v1 vector connectingVectorV1V3 = v3 - v1 vector connectingVectorV1V4 = v4 - v1 float sDimensions[3] sDimensions[0] = | connectingVectorV1V2 | sDimensions[1] = | connectingVectorV1V3 | sDimensions[2] = | connectingVectorV1V4 | if ( uom == 'cm' ) { for( int idx = 0; idx < 3; idx++ ) { sDimensions[ idx ] *= 10 } } if ( uom == 'inch' ) { for( int idx = 0; idx < 3; idx++ ) { sDimensions[ idx ] *= 25.4 } } return sDimensions }
The return value sDimensions is an array of floats containing length, width and height of the solids bounding box.
Getting the center of a solids axis aligned bounding box.
function getBboxCenter( solid s ) { selectbox sBbox = s.min_bbox() vector v1 = <[ sBbox.minx, sBbox.miny, sBbox.minz ]> vector v2 = <[ sBbox.maxx, sBbox.maxy, sBbox.maxz ]> vector center = <[ (v1.x + v2.x ) / 2, (v1.y + v2.y ) / 2, (v1.z + v2.z ) / 2 ]> return center }
The return value center is a vector describing the bounding box center.
Getting the distance between two points in space.
function getDistance( vector v1, vector v2 ) { vector connectionVector = v2 - v1 float distance = |connectionVector| return distance }
The function returns a float value describing the distance between the two input vectors in trCAD units.
STL files can be imported into trCAD for further processing.
// add a file object that contains a relative path to the stl file file stlFile = "box.stl" // use the file variable to generate a mesh object mesh stlMesh( stlFile ) // create solid objects for further processing solid stlSolid = stlMesh solid sphereSolid = sphere( 1.1 ) // apply modifications and transformations stlSolid -= sphereSolid stlSolid = scaling( 1.5 ) >> stlSolid make stlSolid
The box.stl file can be downloaded here.
File objects can also be declared as an open parameter. This way of declaring a file object makes it possible to upload STL files on the client side.
open file userStlFile { name = "stl file" descr = "an stl file uploaded by the client" value = "default.stl" } mesh stlMesh = mesh(userStlFile) make stlMesh