Skip to content

Instantly share code, notes, and snippets.

@DanRathbun
Forked from Tener/plytkowanie.rb
Created March 4, 2012 05:43
Show Gist options
  • Save DanRathbun/1970846 to your computer and use it in GitHub Desktop.
Save DanRathbun/1970846 to your computer and use it in GitHub Desktop.
Plugin for sketch up - fixed up somewhat by Dan Rathbun
###
###
##### SAVE IN FORMAT: "UTF-8 WITHOUT BOM" OR "ANSI AS UTF-8"
###
###
# Wtyczka: "P-ytkuj"
# Autor: Krzysztof Skrzrtnicki
# Licencja: GPLv3
# Wersja: 2.0
# FILE DEPENDANCIES / ZALEŻNOŚCI PLIKÓW
require('sketchup.rb')
# Show the Ruby Console at startup so we can
# see any programming errors we may make.
Sketchup.send_action( "showRubyPanel:" )
# EN: make sure toplevel module namespace is defined:
# PL: upewnij się, że szczytowe poziomy kodu przestrzeń nazw modułu jest zdefiniowana:
module Krzysztof; end
# EN: define this plugin's submodule namespace:
# PL: określenie tej wtyczki przestrzeni nazw modułem:
module Krzysztof::BathTiler
#{# RUN ONCE / WYKONAĆ RAZ
#
#unless file_loaded?( "#{Module.nesting[0].name} - module definition" )
#{# CONSTANTS / STAŁE
#
#
#}#
#{# MODULE VARIABLES / ZMIENNE MODUŁU
#
@@currmat = nil
@@currmat_name = ''
@@apply_mat = true
if Sketchup.get_locale == 'pl'
#
@@menu_text = 'Plytkuj'
@@tile_word = 'Płytek'
#
@@input_title = " Podaj parametry kafelkow"
# defaults should be metric
@@defaults = ["10","10","40","25","0.2","0.2","1","1","0","tak"]
@@paramlist = ["","","","","","","","","","tak|nie"]
@@prompts = ["Liczba X", "Liczba Y", "Szerokosc X", "Wysokosc Y",
"Przerwa X", "Przerwa Y", "Grubosc","Podzielnik","Kąt obrotu",
"Aplikuj aktualny material ('#{@@currmat_name}')"]
@@op_name_group = 'Dodać do płytek Grupa'
@@op_name_tiler = 'Połóż Płytki'
#
else
# EN: assume Sketchup.get_locale == 'en-US'
# PL: zakładamy lokalnego języka == angielski
#
@@menu_text = 'Tiler'
@@tile_word = 'Tile'
#
@@input_title = " Enter tile parameters"
# defaults should be inches
@@defaults = ["10","10","6","6","0.2","0.2","1","1","0","true"]
@@paramlist = ["","","","","","","","","","true|false"]
@@prompts = ["Count X", "Count Y", "Width X", "Height Y",
"Gap X", "Gap Y", "Thickness ","Divisor", "Rotation Angle",
"Apply current material ('#{@@currmat_name}')"]
@@op_name_group = 'Make Tile Group'
@@op_name_tiler = 'Lay Tiles'
#
end
#
#}#
#end
#
#}# RUN ONCE / WYKONAĆ RAZ
#{# MODULE FUNCTIONS / FUNKCJE MODUŁU
#
# These can be run from outside the module.
public # publiczny
# Krzysztof::BathTiler::tiler()
#
def self.tiler
plytkuj()
end
#
#
#}#
#{# PROXY CLASS / KLASA PEŁNOMOCNIK
#
class << self
private # prywatny
# apply_current_material_prompt()
#
def apply_current_material_prompt()
if Sketchup.get_locale == 'pl'
"Aplikuj aktualny material ('#{@@currmat_name}')"
else
# EN: assume Sketchup.get_locale == 'en-US'
# PL: zakładamy lokalnego języka == angielski
"Apply current material ('#{@@currmat_name}')"
end
end
# get_input()
#
def get_input()
# EN: insert current material name into last prompt string:
# PL: wstaw nazwę bieżącego materiału w ostatnim wierszu ciąg:
@@prompts[-1]= apply_current_material_prompt()
# EN: query for parameters
# PL: zapytaj o parametry
begin
vals = UI.inputbox( @@prompts, @@defaults, @@paramlist, @@input_title )
rescue ArgumentError => e
#
msg = e.message.strip << " \n\n"
msg[0,1]= msg[0,1].upcase
#
UI.messagebox( "\n" << msg )
#
retry
#
rescue Exception
raise()
end
#
return false unless vals
#
# EN: validate proper input values here ?
# PL: potwierdzić prawidłowe wartości wejściowe tutaj?
#
@@apply_mat =( vals[9] == @@paramlist[9].split('|')[0] )
@@defaults = vals
#
end
# make_tile_group( in_entities, size_x, size_y, size_z )
#
def make_tile_group( ents, size_x, size_y, size_z )
#
grp = ents.add_group()
#
pt1 = Geom::Point3d.new( 0.0, 0.0, 0.0 )
pt2 = Geom::Point3d.new( size_x, 0.0, 0.0 )
pt3 = Geom::Point3d.new( size_x, size_y, 0.0 )
pt4 = Geom::Point3d.new( 0.0, size_y, 0.0 )
#
new_face = grp.entities.add_face( pt1, pt2, pt3, pt4 )
new_face.pushpull( -size_z )
#
return grp
#
end
# plytkuj()
#
def plytkuj
# Get handles to our model and the Entities collection it contains.
mdl = Sketchup.active_model
### use active_entties, not entities, in case the user wants
# to add a tile group inside another group or component.
ents = mdl.active_entities
# get handle to Materials collection
matls = mdl.materials
@@currmat = matls.current #
if (@@currmat.nil?)
if matls["BRAK"]
@@currmat_name = "BRAK"
@@currmat = matls["BRAK"]
else
# assign some other material ??
end
else
# material may not be loaded
@@currmat_name = @@currmat.display_name
if matls[@@currmat_name].nil?
# matl is not loaded yet (only selected in browser)
current = matls.add(@@currmat_name)
@@currmat = matls.current if current
end
end
# zapytaj o parametry
input = get_input()
return false unless input
# input is really referencing @@defaults
podzielnik = input[7].to_f
count_x = input[0].to_i
count_y = input[1].to_i
size_x = units( input[2].to_f / podzielnik )
size_y = units( input[3].to_f / podzielnik )
gap_x = units( input[4].to_f / podzielnik )
gap_y = units( input[5].to_f / podzielnik )
size_z = units( input[6].to_f / podzielnik )
angle = input[8].to_f
angle_rad = angle / 1.radians
# @@apply_mat is set to boolean in get_input()
begin
#
mdl.start_operation(@@op_name_tiler)
###
#
# make the temporary group
grp = make_tile_group(ents,size_x,size_y,size_z)
# get it's definition:
gdefn = grp.entities.parent
#
if @@apply_mat && (not @@currmat.nil?)
gdefn.material = @@currmat
gdefn.name = "#{@@tile_word}: #{@@currmat.display_name}"
else
gdefn.name = @@tile_word
end
#
# EN: add instances of the group, to the model:
for step_x in 0..(count_x-1)
for step_y in 0..(count_y-1)
# calc the insertion point
vec = Geom::Vector3d.new( step_x * (size_x+gap_x), step_y * (size_y+gap_y), 0.0 )
# placement vector transform
putv = Geom::Transformation.new( vec )
# rotational transform
#rot = Geom::Transformation.rotation( [0,0,0], [0,0,1], angle_rad )
new_tile = ents.add_instance( gdefn, putv )
#new_tile.transformation= rot
new_tile.name = gdefn.name
if @@apply_mat && (not @@currmat.nil?)
new_tile.material = gdefn.material
end
end # for step_y
end # for step_x
#
grp.erase!
#
###
mdl.commit_operation
#
rescue => e
puts(e.message)
puts(e.backtrace)
mdl.abort_operation
#
end # operation
end # def plytkuj()
# units( number )
#
def units( number )
units = Sketchup.active_model.options['UnitsOptions']
ui = units['LengthUnit'] # unit index
return number.inch if ui==0
return number.feet if ui==1
return number.mm if ui==2
return number.cm if ui==3
return number.m if ui==4
# otherwise:
return number.inch # default
#
end # def
end # PROXY CLASS / KLASA PEŁNOMOCNIK
#
#}#
#{# RUN ONCE / WYKONAĆ RAZ
#
unless file_loaded?( "#{Module.nesting[0].name} - module definition" )
# EN: Add a menu item to launch our plugin.
# PL: Dodaj element menu, aby uruchomić nasz wtyczki:
UI.menu('Plugins').add_item(@@menu_text) { plytkuj() }
# EN: register this file as loaded:
# PL: rejestr ten plik jako załadowany:
file_loaded( "#{Module.nesting[0].name} - module definition" )
end
#
#}#
end # module Krzysztof::BathTiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment