Back to Home

Writing a plugin for Google SketchUp

google · sketchup · plugin · ruby · get dimensions

Writing a plugin for Google SketchUp

Google SketchUp is a program for quickly creating and editing three-dimensional graphics. Convenience and simplicity SketchUp will be appreciated by both beginners with 3D 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 bike, the plugin should be searched on the Sketchucation website .ready-made implementations with the functionality you need. Having found a suitable open source plugin, you can implement the required functionality, leaving the main part of the code untouched. For example, a plug-in does some calculations and constructions, and you only change their application or visualization.

Plugins for SketchUp are written in Ruby .

At Google Code provided official development documentation. It consists of 3 sections: Introduction, Quick Reference and Object Reference.

1) Introduction - an introductory section, which shows an example of creating a simple plug-in.
2) Quick Reference - a 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 classes necessary for writing code.

I’ll take an example of plug-in development from my own practice. For convenience, additional functionality was required that was not available in SketchUp. The task was to quickly and conveniently determine the size of the object (width, height, thickness). A ready-made plugin with this functionality was found - GetDimensions , but it had a big minus: it showed the dimensions in a MessageBox, which had to be constantly closed, which created a certain inconvenience. I decided to examine his code and change the output of the result.

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 plugin logic ( get_dimensions), adding a menu item ( Plugins -> Get Dimensions ) and loading the plugin file itself into the system ( GetDimensions.rb ).

To install, the plug-in must be copied to the directory “ C: \ Program Files \ Google \ Google SketchUp \ Plugins \ ”, and the program will automatically load all the scripts from this folder at startup.

The main object that stores the structure of the picture is model.

In this plugin, the first selected object and its dimensions are taken. Sizes are sorted in ascending order and shown in the MessageBox, and the name of the plugin is displayed in the status bar.

The status bar immediately interested me, and I decided to transfer the output of the received sizes 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 element, using the Select tool , select the Get Dimensions command from the menu . As a result, the status bar will display the dimensions of the selected item. For a more convenient invocation of a command, a hot key should be assigned.

Get Dimensions Result

The next step was to make the dimensions automatically show when you select an object. Two options came to my mind: to make my own tool, which would select elements as a Select tool , but at the same time show the dimensions below, or modify the Select tool so that it would show the size of the object when it was selected .

After searching the Object Reference , the idea of ​​implementing the second method was born.
As it turned out, usingObserver Classes -> SelectionObserver can subscribe to the events of the Select tool .

After the modification, the plugin logic was split into two files:

Dimensions_load.rb

require '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.rb

require '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 , it is necessary to inherit from the class SelectionObserver, redefine the method onSelectionBulkChange(selection)that will be called when objects are selected, 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 sub-items: Get Dimensions and Auto Display Dimensions .

As I said earlier, the task of the plugin was to display the parameters of the object automatically when it is selected. Because additional functionality is not always needed, it was decided to make it disabled. Auto Display Dimensions- allows you to include it at the right time, and Get Dimensions - calling the plugin on demand - was left for greater flexibility of use.

Get Dimensions Menu

The source code of the plugin .

As you can see, upgrading an existing plugin is much easier than writing it from scratch. By the way, I first wrote code in Ruby, but thanks to my great programming experience, understanding the syntax was not difficult.

I wish you all good luck writing your own plugins for SketchUp.

Read Next