Wednesday, September 30, 2009

Scripting in Sketchup - part 2

I succeed in building the wall up the y-axis. One issue that I still need to address is that the wall height must be divisible by the stone height. If it isn't the wall height will be cut short of the specified height.

to use the script, use the same directions from the previous post.

...sleepy time

++++++++++++++++++Copy Code Below+++++++++++++++++++++++

# Build Stone Wall - Andrew Kimball
# ANTH-258
# 9/29/09
#-----------------------------------------------------------------------------

require 'sketchup.rb'

#-----------------------------------------------------------------------------
# This script will build a stone wall

def create_stone_wall
prompts = [$exStrings.GetString("Wall Length"), $exStrings.GetString("Wall Height"), $exStrings.GetString("Stone Height"), $exStrings.GetString("Wall Depth"), "Grouped?"]
values = [5.feet, 5.feet, 1.feet, 1.feet, "Yes"]
grouped_yn = ["", "", "", "", "Yes|No"]
results = inputbox prompts, values, grouped_yn, $exStrings.GetString("Stone Dimensions")

return if not results

width, wheight, sheight, depth, grouped = results

model = Sketchup.active_model
model.start_operation $exStrings.GetString("Create Stone")
entities = model.active_entities

if grouped == "Yes"
group = entities.add_group
entities = group.entities
end

wall_loop = (wheight/sheight)
initial_stone = 0
total_width = 0

#random stone lengths array
a = [9.inch,10.inch,11.inch,12.inch,13.inch,14.inch,15.inch,16.inch,17.inch,18.inch]
for i in 0..wall_loop
x_place = (i*sheight).to_inch
if i == 1
else
until total_width == width
block_width = a[rand(a.length)]
if total_width+block_width < width

ptsa = []
ptsa[0] = [total_width, 0, x_place]
ptsa[1] = [total_width+block_width, 0, x_place]
ptsa[2] = [total_width+block_width, depth, x_place]
ptsa[3] = [total_width, depth, x_place]
face = entities.add_face ptsa

height = -sheight if(face.normal.dot(Z_AXIS) < 0)
face.pushpull height
total_width=total_width+block_width
ptsa = 0
else
final_width = width - total_width
ptsf = []
ptsf[0] = [total_width, 0, x_place]
ptsf[1] = [total_width+final_width, 0, x_place]
ptsf[2] = [total_width+final_width, depth, x_place]
ptsf[3] = [total_width, depth, x_place]
face = entities.add_face ptsf

height = -sheight if(face.normal.dot(Z_AXIS) < 0)
face.pushpull height
break
end
end
end
x_place = 0
total_width = 0
block_width = 0
end
model.commit_operation

end

if( not file_loaded?("stone_wall.rb") )

# Add seperator to the Menu
add_separator_to_menu("Draw")

# Add the "Stone"
UI.menu("Draw").add_item($exStrings.GetString("Stone Wall")) { create_stone_wall }

end

#-----------------------------------------------------------------------------
file_loaded("stone_wall.rb")


++++++++++++++++++Copy Code Above+++++++++++++++++++++++

No comments:

Post a Comment