Tutorial: How to Write a Short Script for Selecting SketchUp Outer Edges. (v0.0.3)

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.

  1. 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.

  2. 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.

  3. 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 )

  4. Ruby Console+ - This code editor plugin by @Aerilius is another option to write & run Ruby code inside SketchUp. ( Other Extensions by Author )

  5. 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

a27d24_fd793fbad1854bde98139512290eb2a8_mv2

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

a27d24_8c07c5469645482698263ae0163fe377_mv2

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.

  1. Click File menu.
  2. Click Open.
  3. Choose the .rb file and click Open.
  4. 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

12 Likes

This seems like a good code examples for future developers! I hope we’ll see some new faces in the future writing extensions for SketchUp.

I have some additional steps to make the code shorter, and in my view easier to read. Code readibility is important for easier finding bugs and be able to maintain the project.

model = Sketchup.active_model
selection = model.selection

can be changed into:

selection = Sketchup.atcive_model.selection

Since we only use the reference to model once, we don’t need to define it, but can use method chaining instead to call #selection directly. This should be avoided if it would make long lines, but in a case like this when the line length is low I like to have one less line of code.

Rather than defining an empty Array outerEdges, iterate over (loop) edges and populate the Array one value at the time, we can use Ruby’s nifty select method create a new Array with the values from our old array meating a certain condition.

outerEdges = selection.grep(Sketchup::Edge).select { |e| e.faces.length < 2 }

We could include the type check in the select block as so:

outerEdges = selection.select { |e| e.is_a?(Sketchup::Edge) && e.faces.length < 2 }

but I don’t know if one would perform better than the other (be faster). Personally I think the top one reads better and it is actually slightly shorter code.

Lastly I would recommend following Rubocop’s styleguide, and use snake_case, not camelCase, for variable names. The computer don’t care if you write outerEdges or outer_edges, but the convention throughout the Ruby community is to use the latter, and consistency makes it easier to read each others code. Using parenthesis around arguments is another one of these guidelines.

This gives you the following code:

selection = Sketchup.active_model.selection
outer_edges = selection.grep(Sketchup::Edge).select { |e| e.faces.length < 2 }
selection.clear
selection.add(outer_edges)

Hope someone finds this useful :slight_smile: .

10 Likes

Thank you!!

2 Likes

Such small tutorials would fit well to Try Ruby. What would you think if users can directly run them in SketchUp?

3 Likes