Skip to content

Instantly share code, notes, and snippets.

@JetStarBlues
Last active August 27, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JetStarBlues/76b6250457beb173ab15c2b7e7f771ae to your computer and use it in GitHub Desktop.
Save JetStarBlues/76b6250457beb173ab15c2b7e7f771ae to your computer and use it in GitHub Desktop.
Modified version of TIG's "Export Vertices to CSV" script for SketchUp. The modifications make the script export vertices from all faces and edges present in the model (nested, unselected, or otherwise) with the relevant group and component level transformations applied. See http://sketchucation.com/forums/viewtopic.php?t=33956
=begin
(c) TIG 2011
http://sketchucation.com/forums/viewtopic.php?t=33956
Type exportvertices2csv in the Ruby Console.
Exports all Vertices is a Selection to a X,Y,Z 'CSV' file.
Edit sep="," if something other than a separating comma is desired e.g. ';'
Make sep="\t" if a TSV file is desired and change ext="csv" to ext="tsv".
It uses the current Model Units/accuracy with the approximate '~ ' and
unit suffix [if any] removed; e.g. change Model Units to 'meters' 3dp to
get exported csv in meters 1.234 - don't use 'fraction' 1' 2 1/2" formats,
always use a 'decimal' format.
=end
=begin
JK 2016
- Added support for faces/edges nested in components and groups including
application of relevant transform matrices
=end
# Dependencies ------------------------------
require 'sketchup.rb'
# Add to menu -------------------------------
unless file_loaded?( File.basename( __FILE__ ) )
UI.menu("Plugins").add_item( "Export2Vertices" ){ exportvertices2csv() }
end
file_loaded( File.basename( __FILE__ ) )
# Helper functions ---------------------------
$verts = []
def getVerts( ents, transforms )
# === By JK ===
for e in ents
# Base case
if e.class == Sketchup::Face or e.class == Sketchup::Edge
# Apply transforms
if transforms.length > 0
e.vertices.each do |vert|
v = vert.position
transforms.reverse_each do |transform|
v = transform * v
end#do
$verts << v
end#do
# Else pass as is
else
e.vertices.each{ |vert| $verts << vert.position }
end#if
# Recursion case ... keep exploding until class is face/edge
elsif e.class == Sketchup::Group
puts "Found a group: " + e.name + "\n"
t = transforms.dup
t << e.transformation
puts "And it has this many transforms: " + t.length.to_s
getVerts( e.entities, t )
elsif e.class == Sketchup::ComponentInstance
puts "Found a component: " + e.name + "\n"
t = transforms.dup
t << e.transformation
puts "And it has this many transforms: " + t.length.to_s
getVerts( e.definition.entities, t )
end#if
end
end
# Main --------------------------------------
def exportvertices2csv()
sep = ","
ext = ".csv"
model = Sketchup.active_model
# Original code ===
# ss = model.selection
# verts = []
# ss.each{ |e| verts << e.vertices if e.class == Sketchup::Face or e.class == Sketchup::Edge }
# Begin modified code, JK ===
getVerts( model.entities, [] )
verts = $verts
# End modified code ===
if not verts
UI.messagebox( "No vertices were selected.\nExiting.")
return nil
end#if
# Original code ===
# verts.flatten!.uniq!
# pts = []
# verts.each{ |v| pts << v.position.x.to_s.gsub( /^~ /, '' ).to_f.to_s + sep +
# v.position.y.to_s.gsub( /^~ /, '' ).to_f.to_s + sep +
# v.position.z.to_s.gsub( /^~ /, '' ).to_f.to_s
# }
# Begin modified code, JK ===
pts = []
verts.each{ |v| pts << v.x.to_s.gsub( /^~ /, '' ).to_f.to_s + sep +
v.y.to_s.gsub( /^~ /, '' ).to_f.to_s + sep +
v.z.to_s.gsub( /^~ /, '' ).to_f.to_s
}
pts.uniq!
# End modified code ===
path = model.path
if not path or path == ""
path = Dir.pwd
title = "Untitled"
else
path = File.dirname( path )
title = model.title
end#if
ofile = File.join( path, title + '_verts' + ext ).tr( "\\", "/" )
begin
file = File.new( ofile, "w" )
rescue # trap if open
UI.messagebox( "Vertices file:\n\n\ " + ofile + "\n\nCannot be written - it's probably already open.\nClose it and try making it again...\n\n\Exiting..." )
return nil
end#rescue
pts.each{ |pt| file.puts( pt ) }
file.close
puts ( pts.length.to_s ) + " vertices written to \n" + ofile
begin
UI.openURL( "file:///" + ofile )
rescue
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment