How to Write a SketchUp Script for Select Outer Edges.
Thank you for viewing my tutorial! My name is Rafael Rivera and I am a developer who loves SketchUp and I hope you can learn some basics in scripting so you can accelerate your workflow. Enjoy!
Copy Code
model = Sketchup.active_model
selection = model.selection
edges = selection.grep(Sketchup::Edge)
outerEdges = []
edges.each do |edge|
faces = edge.faces
outerEdges << edge if faces.length <= 1
end # do
selection.clear
selection.add outerEdges
Coding can be intimidating, but my goal is to provide bite-sized examples that are easy to digest & write.
The scope of this tutorial will not include how to register the script as a SketchUp Extension because that will make the code more complicated than it needs to be.
The code above is a short script that will help you select the outer edges of your model with just a single click. There are less than 12 lines of code to accomplish our goal.
First, let us review the tools and resources that will help us get started.
-
SketchUp - The primary tool used is, of course, Trimble’s SketchUp. If you don’t have SketchUp Pro, then you can use SketchUp Make 2017 for free.
-
SketchUp API - Developers at Trimble’s SketchUp have already created a library of reusable code for us to access functions that will give us control over different elements within the SketchUp model.
-
Ruby Code Editor - This code editor plugin by @alexschreyer will allow us to write and modify Ruby scripts directly within SketchUp. ( Other Extensions by Author )
-
Ruby Console+ - This code editor plugin by @Aerilius is another option to write & run Ruby code inside SketchUp. ( Other Extensions by Author )
-
Sublime Text Editor - Even though you can use the ‘Ruby Code Editor’ or ‘Ruby Console+’ as mentioned above, I first write my code with Sublime Text Editor and then load the saved .rb file in ‘Ruby Code Editor’ or ‘Ruby Console+’ so I can then run it inside SketchUp.
We are equipped to start coding!
The first line of code creates a variable with the SketchUp model from where we can extract further information.
SketchUp API used: Model
The second line is taking this model variable and passing a function to make an array of entities that are part of a selection.
SketchUp API used: Selection
The third line is taking this selection variable which may include different entity types, but with the grep function we can filter out all the entities that are not edges.
Ruby Method used: Grep
This line creates an empty array that will store all the outer edges, and in the next block of code, we are going to evaluate which SketchUp edges get stored or not.
Data Structure used: Array
We are using our edges array and iterating through each SketchUp edge because we want to know how many faces connect to that particular edge. With the SketchUp API, we get an array of SketchUp faces attached to the edge using the ‘edge.faces’ method.
Now we can pass the edges to the empty array that we created earlier named ‘outer edges’ by passing an if statement with the condition to add to the array only if the edge has one or fewer faces connected to it.
The thought behind this is that only outer edges will have less than two faces connected to it.
Ruby Methods used: if statement, iterator
SketchUp API used: #face
Before we select the outerEdge array we need to deselect our previous selection.
SketchUp API used: #clear
To finish our script, we now select the edges on the outerEdge array.
SketchUp API used: #add
Let’s load our .rb file in the Ruby Code Editor.
This tutorial will assume you know how to install and open the Ruby Code Editor inside SketchUp, but if you need help don’t be afraid to ask.
If you decide to write the code in Sublime Text then once you saved the script as a .rb file you will need to load it in the Ruby Code Editor Plugin.
- Click File menu.
- Click Open.
- Choose the .rb file and click Open.
- Now you can click the Play button to run the code.
If all went according to plan you should be able to select many edges or faces and when you hit play all edges will be deselected except the outer ones.
Congratulations!
You have finished the tutorial.
If you found this tutorial useful, consider visiting my website compotools.com and become a member. Also, check out my SketchUp Extensions and 3d models.
Best wishes,
Rafael Rivera