Google SketchUp - a program for quickly creating and editing three-dimensional graphics. The convenience and simplicity of SketchUp will be appreciated by both beginners with three-dimensional modeling and professionals.
But not everyone knows that SketchUp has a powerful API, with which you can create modules by adding new functionality to the program. In this post I will try to explain the general principles of the SketchUp architecture and the plug-in development process. Before writing a new plug-in
bike, it’s worth looking at the
Sketchucation website for ready-made implementations with the functionality you need. Having found a suitable open source plugin, you can implement the required functionality, leaving the bulk of the code intact. For example, a plugin does some calculations and constructions, and you only change their use or visualization.
SketchUp plugins are written in
Ruby .
Google Code has official development documentation. It consists of 3 sections: Introduction, Quick Reference and Object Reference.
')
1) Introduction - introductory section, which shows an example of creating a simple plug-in.
2) Quick Reference - reference section on classes, methods.
3) Object Reference - a reference section on the SketchUp object model. The object hierarchy is very conveniently divided into groups, which allows you to quickly search for the classes necessary for writing code.
I will take a plugin development example from my own practice. For convenience, required additional functionality that is missing in SketchUp. The task was to quickly and easily determine the size of the object (width, height, thickness). A ready-made plug-in with this functionality was found —
GetDimensions , but it had a big disadvantage: it showed dimensions in a MessageBox, which had to be closed constantly, which created a certain inconvenience. I decided to investigate its code and change the result output.
GetDimensions plugin code:require 'sketchup.rb'
def get_dimensions
model = Sketchup.active_model
mname = model.title
Sketchup::set_status_text(( "GET COMPONENT DIMENSIONS..." ), SB_PROMPT)
Sketchup::set_status_text( " " , SB_VCB_LABEL)
Sketchup::set_status_text( " " , SB_VCB_VALUE)
boundingBox = model.selection[0].bounds
dims = [ boundingBox.height,
boundingBox.width,
boundingBox.depth ]
dims.sort!
UI.messagebox( "Thickness: " + dims[0].to_s + "\nWidth: " + dims[1].to_s + "\nLength: " + dims[2].to_s)
end
if ( not file_loaded?( "GetDimensions.rb" ) )
add_separator_to_menu( "Plugins" )
UI.menu( "Plugins" ).add_item( "Get Dimensions" ) { get_dimensions }
end
file_loaded( "GetDimensions.rb" )
* This source code was highlighted with Source Code Highlighter .
The code consists of plug-in logic (
get_dimensions
), adding a menu item (
Plugins -> Get Dimensions ) and loading the plug-in file itself into the system (
GetDimensions.rb ).
To install, the plugin must be copied to the “
C: \ Program Files \ Google \ Google SketchUp \ Plugins \ ” directory, and the program will automatically load all the scripts from this folder at startup.
The main object that holds the structure of the picture is
model
.
In this plugin, the first selected object and its dimensions are taken. The dimensions are sorted in ascending order and shown in the MessageBox, and the name of the plug-in is displayed in the status bar.
I was immediately interested in the status panel, and I decided to transfer the output of the obtained dimensions to it.
After a small modification of the plugin, I managed to achieve this:
def get_dimensions
model = Sketchup.active_model
entities = model.entities
boundingBox = model.selection[0].bounds
dims = [ boundingBox.height,
boundingBox.width,
boundingBox.depth ]
dims.sort!
Sketchup::set_status_text(( "Thickness: " + dims[0].to_s + ". Width: " + dims[1].to_s + ". Length: " + dims[2].to_s ), SB_PROMPT)
end
* This source code was highlighted with Source Code Highlighter .
After selecting an item using the
Select tool, select the
Get Dimensions command from the menu. As a result, the size of the selected item will be displayed on the status bar. To more conveniently invoke a command, assign a hotkey.

The next step was to make the dimensions automatically appear when the object was selected. Two options came to mind: make your own tool that would select elements as the
Select tool, but show dimensions below, or modify the
Select tool to show the dimensions of the object when selected.
After searching through the
Object Reference , the idea of ​​implementing the second method was born.
As it turned out, using the
Observer Classes -> SelectionObserver you can subscribe to the events of the
Select tool.
After modification, the plugin logic was split into two files:
Dimensions_load.rbrequire 'sketchup.rb'
require 'Dimensions/GetDimensions.rb'
$PluginMenuName = "Tools"
$DimensionsMenuName = "Dimensions Tool"
$GetDimensionsMenuItem = "Get Dimensions"
$AutoDisplayMenuItem = "Auto Display Dimensions"
if (not file_loaded?( "dimensions_load.rb" ))
pluginMenu = UI.menu($PluginMenuName)
dimensions = Dimensions. new
pluginMenu.add_separator
getDimensionsSubMenu = pluginMenu.add_submenu($DimensionsMenuName){}
getDimensionsSubMenu.add_item($GetDimensionsMenuItem){dimensions.get_selection_dimensions}
autoDisplayItem = getDimensionsSubMenu.add_item($AutoDisplayMenuItem){dimensions.connect_observer}
getDimensionsSubMenu.set_validation_proc(autoDisplayItem){dimensions.menu_checked}
end
file_loaded( "dimensions_load.rb" )
* This source code was highlighted with Source Code Highlighter .
GetDimensions.rbrequire 'sketchup.rb'
class Dimensions < Sketchup::SelectionObserver
def initialize()
@usedObserver = false
end
def onSelectionBulkChange(selection)
get_dimensions(selection)
end
def get_selection_dimensions
get_dimensions(Sketchup.active_model.selection)
end
def get_dimensions(selection)
boundingBox = selection[0].bounds
dims = [ boundingBox.height,
boundingBox.width,
boundingBox.depth ]
dims.sort!
Sketchup::set_status_text(( "Thickness: " + dims[0].to_s + ". Width: " + dims[1].to_s + ". Length: " + dims[2].to_s ), SB_PROMPT)
end
def connect_observer
if (@usedObserver) then
return remove_observer
else
return add_observer
end
end
def add_observer
@usedObserver = true
Sketchup.active_model.selection.add_observer self
return MF_CHECKED
end
def remove_observer
@usedObserver = false
Sketchup.active_model.selection.remove_observer self
return MF_UNCHECKED
end
def menu_checked
if (@usedObserver) then
return MF_CHECKED
else
return MF_UNCHECKED
end
end
end
file_loaded( "GetDimensions.rb" )
* This source code was highlighted with Source Code Highlighter .
Consider the code in more detail.
In order to be able to intercept the events of the
Select tool, you must inherit from the
SelectionObserver
class, override the
onSelectionBulkChange(selection)
method that will be called when selecting objects, and subscribe to events using
Sketchup.active_model.selection.add_observer
.
The plugin has been moved to the
Tool -> Dimensions Tool menu, which contains two subitems:
Get Dimensions and
Auto Display Dimensions .
As I said earlier, the task of the plugin was reduced to displaying the parameters of the object automatically when it was selected. Because additional functionality is not always needed, it was decided to make it disabled.
Auto Display Dimensions - allows you to turn it on at the right time, and
Get Dimensions - a call to a plug-in on demand - was left for greater flexibility of use.

Source code of the
plugin .
As you can see, upgrading an existing plug-in is much easier than writing it from scratch. By the way, I first wrote Ruby code, but thanks to my great programming experience, it was easy to understand the syntax.
Good luck to everyone writing their own plugins for SketchUp.