Coding a Cube

by tinkercad-support in Workshop > 3D Printing

5917 Views, 3 Favorites, 0 Comments

Coding a Cube

_1440179445506-outer_mesh UCS.png

The following information is a single lesson in a larger Tinkercad project. Check out this and more projects on Tinkercad.

Return to Previous Lesson: Overview

Lesson Overview:

Now we're going to make a cube.

Application Programming Interface (API) Documentation

_1440179445506-outer_mesh UCS.png

The image below provides the information to create our first 3D object…the cube. We have discussed all of the building blocks like points, faces, and the order of point definition.

The Code

As you prepare to begin coding your own Shape Generator it is important to review the application programming interface (API) documentation at: http://api.tinkercad.com. This documentation will provide additional information and examples that can be quite helpful when starting to code in Tinkercad.

Take a few moments to review that information and remember the documentation may be helpful from time to time to learn more about programming in Tinkercad.

Instructions

  1. Continue to the next step.

Shape Generator Creation

1440180015332-New Shape Generator.png
1440180154761-Shape Generator Code Window.png

To begin, a shape generator must be created to hold the code.

Instructions

  1. Navigate to the Shape Generators section of the browser on the right side of the Tinkercad interface.
  2. Expand the Shape Generators section, and then expand the “Your Shape Generators” section.
  3. Select “New Shape Generator” and pick the Empty option from the list.
  4. A shape generator code window will appear in the lower left corner of the Tinkercad interface.
  5. Continue to the next step.

Preparing to Code

1440180281534-Shape Generator Code Window.png
1440180421412-The Right Hand Rule.png
1440180642696-Cube quad code.png
1440181019163-Cube Coding Window.png

After creating the empty Shape Generator, some code will be pre-populated for you. At the top of the code window you will see a set of Libraries pre-populated into the code.

Next you will see several lines of commented code seen in an orange font. This code shows an example of how to create parameters, but we won’t be doing that just yet; parameters will be covered in a later lesson.

Finally, you will see the main function of the shape generator. This is where we will place the code to create our first cube. The only change we must make is to replace the line that says “Plugin.warning(“This shape script is empty.”);” with code that will generate a cube.

<

Instructions

  1. Within the function you will see that a new Mesh3D variable called mesh has already been defined. To create the cube we must create the 6 faces that make us the sides of the cube, this is done by adding quads (or Faces) to the mesh variable.
  2. While creating the quads that make up cube, the order in which they are created is not important, but the order in which the points are define for each quad (or face) is critical to direction of the surface normal and the overall success of the shape generator.
  3. Each of the quads that are added to the mesh are defined by an array of four points each defined by their X, Y, and Z locations. In the next step, I have provided the code that can be copied and pasted in to the generator.
  4. mesh.quad([0 , 0 , 0 ], [0 , 10, 0 ], [15, 10, 0 ], [15, 0 , 0 ]); //box bottom mesh.quad([0 , 0 , 0 ], [15, 0 , 0 ], [15, 0 , 10], [0 , 0 , 10]); //box front mesh.quad([15, 0 , 0 ], [15, 10, 0 ], [15, 10, 10], [15, 0 , 10]); //box right mesh.quad([0 , 0 , 0 ], [0 , 0 , 10], [0 , 10, 10], [0 , 10, 0 ]); //box left mesh.quad([0 , 10, 0 ], [0 , 10, 10], [15, 10, 10], [15, 10, 0 ]); //box back mesh.quad([0 , 0 , 10], [15, 0 , 10], [15, 10, 10], [0 , 10, 10]); //box top
  5. Replace the “Plugin.warning(“This shape script is empty.”);” line of code from the default code generator with the six rows of code shown above. Each line of code above will generate one of the faces that make up the cube.
  6. When complete the code will look like the image below. After adding the code to the shape generator, click save in the upper right corner of the coding window.
  7. Continue to the next step.

The Results

1440181222598-Final Cube.png

After saving the shape generator an image of what the code generates will be shown in the “Your Shape Generators” section of the browser. If no image is generated, this is an indication that something is incorrect in the code.

If you do see the shape then you have successfully created your first shape generator and you can simply drag and drop that onto the Tinkercad workplane to begin using it in a design.

Instructions

  1. The color of the cube in your shape generator window may be different from the yellow shown in the image above. This is normal and color can of the block can be changed after the shape is added to the Tinkercad workplane.
  2. Remember, if you don't see an image like the one above, this is an indication that something is incorrect in the code. Double check your code and try again.
  3. Continue to the next lesson.

In the next lesson you will learn manual versus programatic design.

Next Lesson: Manual Design vs. Program Design