-
-
Save engina/9a2736914f2c7486ad7c9ca2a18654cb to your computer and use it in GitHub Desktop.
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
PRECISION = 6 | |
module Xenakis | |
def sync | |
modelexport = { | |
"version" => 3, | |
"signature" => "XAAT", | |
"surfaces" => [], | |
"receivers" => [], | |
"sources" => [], | |
} | |
Sketchup.active_model.entities.grep(Sketchup::Face) | |
.find_all {|f| f.hidden? === false}.each do |face| | |
material = face.layer.name | |
mesh = face.mesh | |
surface = { | |
"material" => material, | |
"vertices" => [], | |
"faces" => [], | |
} | |
mesh.points.each do |point| | |
surface["vertices"] << [point.x.to_m.round(PRECISION), point.y.to_m.round(PRECISION), point.z.to_m.round(PRECISION)] | |
end | |
mesh.polygons.each do |poly| | |
surface["faces"] << [poly[0].abs - 1, poly[1].abs - 1, poly[2].abs - 1] | |
end | |
modelexport["surfaces"] << surface | |
end | |
# NAME SHOULD BE CHECKED FOR RECEIVER AND SOURCES? | |
Sketchup.active_model.entities.grep(Sketchup::ComponentInstance) | |
.find_all {|e| e.definition.name === "Receiver" and e.hidden? === false}.each do |r| | |
receiver = { | |
"name" => r.name, | |
"position" => [], | |
"attributes" => {} | |
} | |
c = r.bounds.center | |
receiver["position"] = [c.x.to_m.round(PRECISION), c.y.to_m.round(PRECISION), c.z.to_m.round(PRECISION)] | |
if r.attribute_dictionaries["dynamic_attributes"] | |
r.attribute_dictionaries["dynamic_attributes"].each_pair do |key, val| | |
if !key.start_with?("_") | |
# if this is a length, we should convert it from inches | |
receiver["attributes"][key] = val | |
end | |
end | |
end | |
modelexport["receivers"] << receiver | |
end | |
Sketchup.active_model.entities.grep(Sketchup::ComponentInstance) | |
.find_all {|e| e.definition.name === "Source" and e.hidden? === false}.each do |s| | |
source = { | |
"name" => s.name, | |
"position" => [], | |
"attributes" => {} | |
} | |
c = s.bounds.center | |
source["position"] = [c.x.to_m.round(PRECISION), c.y.to_m.round(PRECISION), c.z.to_m.round(PRECISION)] | |
if s.attribute_dictionaries["dynamic_attributes"] | |
s.attribute_dictionaries["dynamic_attributes"].each_pair do |key, val| | |
if !key.start_with?("_") | |
# if this is a length, we should convert it from inches | |
source["attributes"][key] = val | |
end | |
end | |
end | |
modelexport["sources"] << source | |
end | |
self.cp2clip('###EAAS$$$' + DateTime.now.strftime('%Q').to_s + JSON.generate(modelexport)) | |
end | |
def cp2clip(input) | |
cmd = '' | |
if RUBY_PLATFORM.include? 'darwin' | |
cmd = 'pbcopy' | |
elsif RUBY_PLATFORM.include? 'mingw' | |
cmd = 'clip' | |
end | |
str = input.to_s | |
IO.popen(cmd, 'w') { |f| f << str } | |
str | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment