Skip to content

Instantly share code, notes, and snippets.

@maxhawkins
Created July 1, 2010 23:19
Show Gist options
  • Save maxhawkins/460708 to your computer and use it in GitHub Desktop.
Save maxhawkins/460708 to your computer and use it in GitHub Desktop.
Demo code for Google Sketchup - pghrb resentation
# Sketchup & Ruby
#
# Pittsburgh.rb Presentation
# 7/1/10
# Draw rectangle
model = Sketchup.active_model
entities = model.entities
model.selection.add entities.to_a.last
# Making shapes
entities.add_face([0,0,0], [0,1,0], [0,1,1])
50.times do
triangle = Array.new(3)
triangle.fill { Array.new(3) {rand(50)} }
entities.add_face(triangle)
end
#Setting styles
faces = entities.select { |e| e.is_a? Sketchup::Face }
faces.each do |face|
face.back_material = face.material = Array.new(3) { rand() }
end
#pushpull
def make_box(width, depth, height)
group = Sketchup.active_model.entities.add_group
entities = group.entities
points = []
points[0] = [0, 0, 0]
points[1] = [width, 0, 0]
points[2] = [width, depth, 0]
points[3] = [0, depth, 0]
base = entities.add_face points
base.pushpull height
return group
end
#Transforms
def make_staircase
width = 25
depth = 100
height = 10
stair_count = 5
stair_count.times do |n|
box = make_box(width, depth, height)
rotate = Geom::Transformation.rotation(ORIGIN, Z_AXIS, (Math::PI/12) * n)
translate = Geom::Transformation.translation([0, 0, height*n])
box.entities.transform_entities rotate*translate, box
end
end
# Prompts
def make_staircase
width = 25
depth = 100
height = 10
prompts = ["# Stairs"]
defaults = [15]
results = inputbox prompts, defaults, "Staircase dimensions"
return if not results
stair_count = results[0]
stair_count.times do |n|
box = make_box(width, depth, height)
rotate = Geom::Transformation.rotation(ORIGIN, Z_AXIS, -(Math::PI/12) * n)
translate = Geom::Transformation.translation([0, 0, height*n])
box.entities.transform_entities rotate*translate, box
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment