I patched this code together as described below.
I select a single component and run the following.
I’d like to be able to select multiple components.
Too many failed attempts - need help
#-----------------------------------------------------------------------------
# Copyright 2005, CptanPanic
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#-----------------------------------------------------------------------------
# Name : GetDimensions.rb
# Type : Tool
# Description : Displays Dimensions of Component.
# Menu Item : Plugins -> Get Component Dimensions.
# Context-Menu: None
# Author : CptanPanic
# Usage : Select Component, and call script.
# Date : December 2005
# Version : 1.0 Initial Release.
# : 2010-06 <jim.foltz@gmail.com>
# : returns "true" dimensions for rotated and scaled Group and Instance.
#-----------------------------------------------------------------------------
# The above is from the original plugin "JF Get_Dimensions"
# This plugin was subsequently modified by KP,
# with permission from the original author
# I changed the plugin name so as not to get it confused with the original
# KP modifications are annotated in the Ruby code
#-----------------------------------------------------------------------------
# This plugin is designed with woodworkers in mind
# ASSUMES EACH COMPONENT IS AN INDIVIDUAL BOARD
# Handy when you want to dimension an exploded view without cluttered dimensions
# (1) Select a component
# (2) Run the plugin
# It will append the COMPONENT "Definition" field in "Entity Info" window
# with part thickness, width and length (inches)
# EXAMPLE:
# Component Name before [Center Panel]
# Component Name after [Center Panel: 3/4" x 11 7/8" x 75 1/4"]
# (3) It will also update the associated TEXT LABEL if one exists
# 'and' has the same name as the component.
# (4) It's easier if you add a shortcut to run this plugin (I use Ctrl-D)
#
#-------------#######IMPORTANT########----------------------------------------
# I AM NOT A PROGRAMMER SO CODE MODIFICATIONS MAY NOT THE BEST
# I JUST FOUND CODE SNIPPETS FROM THE INTERNET
# AND FIGURED OUT SOMETHING THAT WORKED
#
# Version : 1.1 Modified from Initial Release
# : 2016-03-25 <kpstuff@swbell.net>
#-----------------------------------------------------------------------------
require 'sketchup.rb'
module KP
module GetDimensions
module_function
def get_dimensions
model = Sketchup.active_model
selection = model.selection
### show VCB and status info...
Sketchup::set_status_text(("DIMENSIONS..." ), SB_PROMPT)
Sketchup::set_status_text(" ", SB_VCB_LABEL)
Sketchup::set_status_text(" ", SB_VCB_VALUE)
### Get Selected Entities.
return unless selection.length == 1
e = selection[0]
return unless e.respond_to?(:transformation)
scale_x = ((Geom::Vector3d.new 1,0,0).transform! e.transformation).length
scale_y = ((Geom::Vector3d.new 0,1,0).transform! e.transformation).length
scale_z = ((Geom::Vector3d.new 0,0,1).transform! e.transformation).length
bb = nil
if e.is_a? Sketchup::Group
bb = Geom::BoundingBox.new
e.entities.each {|en| bb.add(en.bounds) }
elsif e.is_a? Sketchup::ComponentInstance
bb = e.definition.bounds
end
if bb
dims = [
width = bb.width * scale_x,
height = bb.height * scale_y,
depth = bb.depth * scale_z
]
end
# New code - added by kp
# UI.messagebox("Thickness:\t#{dims.sort[0].to_l}\nWidth:\t#{dims.sort[1].to_l}\nHeight:\t#{dims.sort[2].to_l}")
# Retrieve current Component Name for processing
# Create new Component Name
# Verify name has changed
# If changed, strip off previous modification, add current modification
# Modify component name (if changed)
# Modify associated Text Label, if exists
name = Sketchup.active_model.selection[0].definition.name
from = name
vpos = name.index(";")
if vpos.nil?
vpos = 0
elsif vpos>0
tempname = name.partition(';').first
name = tempname
end
newname = name + "; #{dims.sort[0].to_l} x #{dims.sort[1].to_l} x #{dims.sort[2].to_l}"
if from != newname
name = Sketchup.active_model.selection[0].definition.name=newname
to = newname
model.entities.grep(Sketchup::Text) { |text|
self.replace(text, from, to)
}
end
end
def self.replace(text_entity, from, to)
text_entity.text = text_entity.text.gsub(from, to)
end
# End of new code - by kp
end
end
### do menu
if( not file_loaded?("kp_get_dimensions.rb") )
add_separator_to_menu("Plugins")
menu_name = "[kp] Get Dimensions"
UI.menu("Plugins").add_item(menu_name) { KP::GetDimensions.get_dimensions }
end
file_loaded("kp_get_dimensions.rb")