Adding Geometry
In this lesson we'll show the basics of adding geometry to a model. By the end of this tutorial, you should be able to:
- Access the model object.
- Access the model's entities collection.
- Add an edge, face and construction point to the model.
Try it Now!
One of the most basic things you might want to do with Ruby code is to add geometry to a model. Lets dive right into this core feature of the Ruby API. In this snippet we will add an edge into the model.
Lets look at the code. First we define two variables as arrays with 3 values in them. Those arrays are interpereted by SketchUp as points in space. point1
is the model origin at 0,0,0 and point2
is at 100,100,100.
Then we declare the model
variable that references the current, or active SketchUp model. Next we make a variable called entities
and assign it a reference to the model's active_entity collection. Entities are things like edges, faces, images, components, construction geometry, etc. and an entity collection is like a Ruby Array, full of those entities. There can be many entity collections in a model. The last step is to add the edge to that entity collection with the Entities.add_edges
method.
This is the final reminder, I promise. Remember to look things up in the API docs! The more you read through the docs, the more you will understand what the Ruby API is capable of doing. Check out the documentation for these methods we used:
Moving On, Adding a Face!
Lets take the ideas we covered in the first example and keep going with it by adding a face to the model. We add a face to the model in a similar manner to adding an edge. We need three unique points in space. So lets create one additional array, and add a face with edges that go from point1 to point2, point2 to point3, and point3 back to point1. Here's an example.
Adding a Construction Point
Or adding a few construction points. Lets repurpose the previous example and add construction points to the end of all edges of the face we created. Instead of using the point1, point2, and point3 arrays to add a face, we'll use them to add construction points at each of those positions, one at a time using the Entities.add_cpoint
method.
One Step Further
Here's some ideas to help you take the provided examples and take them to the next level. See if you can find a way to do it without looking at the provided solutions.
Let's start with something that should be pretty straightfoward. Before you get started, delete everything that you have in the model. Now use the Ruby API to add a circle into the model.
How did you add the other geometry into the model? By accessing the active_entities object and calling a method that adds the geometry. Look at the documentation for the Entities class.
Was the circle task too easy? Lets build on it. Go ahead and delete the circle in the model. Now try to create a circle and add a construction point to each vertex on the perimiter of the circle. Bonus points if you only add one Construction Point per vertex!
Notice that when you create the circle, you get an array of edges returned. Use that array of edges and find all the edges that define the circle. Then find those edges' vertices. Use the position of those vertices to determine where to add the Construction Points.
Here's another test to try. Delete everything from the model and use the Ruby API to add 3D Text to the model. If you have made it this far, this should be pretty easy. Try playing with all the available parameters when you create the 3D Text.
Still need some help? Look in the Ruby API docs for the add_3d_text method in the Entities class. The example there should give you enogh to get this working.