Last active
November 15, 2015 10:58
-
-
Save jimfoltz/c728bf26b50d3dfda432 to your computer and use it in GitHub Desktop.
amf-ex.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#UI.menu.add_item("Export AMF") { | |
#JF::AMF.export | |
#} | |
require 'rexml/document' | |
module JF | |
module AMF | |
module Exporter | |
DEBUG = false | |
PREF_KEY = self.to_s.split('::').join('\\') | |
METADATA = {'generator' => 'SketchUp AMF Exporter'} | |
def self.get_entities(object) | |
if object.class == Sketchup::Group | |
object.entities | |
elsif object.class == Sketchup::ComponentInstance | |
object.definition.entities | |
end | |
end | |
# Top-level Groups and Components to AMF Objects | |
def self.export | |
model = Sketchup.active_model | |
sel = model.selection | |
if sel.size > 0 | |
entities = sel.to_a | |
else | |
entities = model.entities.grep(Sketchup::Group) | |
entities.concat(model.entities.grep(Sketchup::ComponentInstance)) | |
end | |
t0 = Time.now | |
xml = '' | |
xml = File.open("#{ENV['USERPROFILE']}/Dropbox/Plugins/work/amf/su-exporter-test.amf", 'w') | |
xml << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" | |
xml << "<amf unit=\"inch\">\n" | |
write_metadata(xml) | |
entities.each do |entity| | |
material = entity.material | |
color = material.nil? ? nil : material.color | |
verts = {} | |
tris = [] | |
find_triangles(get_entities(entity), entity.transformation, verts, tris, color) | |
verts = verts.sort_by{|k, v| v} | |
xml << "<object id=\"#{entity.entityID}\">\n" | |
xml << "<metadata type=\"name\">#{entity.entityID}</metadata>\n" | |
xml << "<mesh>\n" | |
xml << "<vertices>\n" | |
verts.each do |v| | |
xml << sprintf("<vertex><coordinates><x>%f</x><y>%f</y><z>%f</z></coordinates></vertex>\n", *v[0]) | |
end | |
xml << "</vertices>\n" | |
# Append "volume" element. | |
xml << "<volume>\n" | |
tris.each do |tri| # [{:verts => [0, 1, 2], :color => Sketchup::Color}] | |
verts = tri[:verts] | |
color = tri[:color] | |
xml << "<triangle>\n" | |
if color | |
xml << "<color>" | |
color = color.to_a.map{|e| e / 255.0} | |
xml << sprintf("<r>%f</r><g>%f</g><b>%f</b><a>%f</a>", *color) | |
xml << "</color>\n" | |
end | |
str = sprintf("<v1>%i</v1><v2>%i</v2><v3>%i</v3>\n", *verts) | |
xml << str | |
xml << "</triangle>\n" | |
end | |
xml << "</volume>\n" | |
xml << "</mesh>\n" | |
xml << "</object>\n" | |
if DEBUG | |
puts "vertex count: #{verts.size}" | |
puts "triangle count: #{tris.size}" | |
puts "Completed at #{Time.now - t0}" | |
puts " ==== " | |
end | |
end # each entity | |
xml << "</amf>\n" | |
xml.close if xml.respond_to?(:close) | |
puts "total: #{Time.now - t0} seconds." if DEBUG | |
end # export | |
def self.find_triangles(entities, tform, verts, tris, color, max_depth = 0, _depth = 0) | |
tmp = [] | |
_depth += 1 | |
for e in entities.grep(Sketchup::Face) | |
face_color = e.material.nil? ? color : e.material.color | |
mesh = e.mesh(0) # A triangulated representation of the Face | |
mesh.count_polygons.times do |i| | |
points = mesh.polygon_points_at(i + 1) | |
tmp.clear | |
for pt in mesh.polygon_points_at(i + 1) do | |
pt = pt.transform!(tform).to_a | |
#verts.push(pt) if !verts.include?(pt) | |
if verts[pt].nil? | |
verts[pt] = verts.length | |
end | |
tmp << verts[pt] | |
end | |
tris << {:verts=>tmp.dup, :color=>face_color} | |
end | |
end | |
if(max_depth > 0 && _depth >= max_depth) | |
return | |
end | |
for e in entities.grep(Sketchup::Group) | |
find_triangles(e.entities, tform * e.transformation, verts, tris, face_color, max_depth, _depth) | |
end | |
for e in entities.grep(Sketchup::ComponentInstance) | |
find_triangles(e.definition.entities, tform * e.transformation, verts, tris, face_color, max_depth, _depth) | |
end | |
end | |
def self.write_metadata(file, metadata = {}) | |
# Write default metadata | |
METADATA.each_pair do |attr, value| | |
file << "<metadata type=\"#{attr}\">#{value}</metadata>\n" | |
end | |
# Write user metadata | |
metadata.each_pair do |attr, value| | |
file << "<metadata type=\"#{attr}\">#{value}</metadata>\n" | |
end | |
end | |
end # module Exporter | |
end # module AMF | |
end # module JF | |
JF::AMF::Exporter.export | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment