Last active
October 10, 2015 13:57
-
-
Save jimfoltz/a084ee4604fc32553e9a to your computer and use it in GitHub Desktop.
cli.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
# require 'win32api' | |
module CLI | |
module_function | |
class Cmd | |
attr_accessor :name, :help, :proc | |
def initialize(name, help, prc) | |
@name = name | |
@help = help | |
@proc = prc | |
end | |
end | |
Command = Struct.new(:name, :help, :proc) | |
Commands = {} | |
Commands[:unhide] = Cmd.new(:unhide, "unhide help", proc { }) | |
#def self.toggleRollUp(name = 'Outliner') | |
# findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N') | |
# pw=findWindow.call(0,name) | |
# sendMessage = Win32API.new("user32.dll","SendMessage",['N','N','N','P'],'N') | |
# sendMessage.call(pw,0x00a1,2,"")#WM_NCLBUTTONDOWN | |
# sendMessage.call(pw,0x0202,0,"")#WM_LBUTTONUP | |
#end | |
#def self.isRolledUp?(name = 'Outliner') | |
# findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N') | |
# getWindowRect = Win32API.new("user32.dll","GetWindowRect",['P','P'],'N') | |
# pw = findWindow.call(0, name) | |
# data = Array.new.fill("\000" ,0..4*4).join | |
# p data | |
# getWindowRect.call(pw, data) | |
# p data | |
# rect=data.unpack("i*") | |
# #if window height is less than 90 then the window is rolledup | |
# return (rect[3]-rect[1]) < 90 | |
#end | |
#class Entities | |
# include Enumerable | |
# def initialize(entities = Sketchup.active_model.entities, dig = true) | |
# @entities = entities | |
# @dig = dig | |
# end | |
# def each(&pr) | |
# @entities.each {|e| | |
# pr.call(e) | |
# if @dig && e.is_a?(Sketchup::Group) | |
# CLI::Entities.new(e.entities, @dig).each{|c| pr.call(c)} | |
# end | |
# if @dig && e.is_a?(Sketchup::ComponentInstance) | |
# CLI::Entities.new(e.definition.entities).each{|c| pr.call(c)} | |
# end | |
# } | |
# end | |
#end | |
#def method_missing(m, *args, &blk) | |
#p m | |
#p args | |
#super | |
#end | |
#@commands << Command.new('help', &help) | |
def help(cmd = nil) | |
cmd = cmd.to_s | |
case cmd | |
when "" | |
puts "help [:model, :select, :entities, :invert, :hide, :sys]\n" | |
when "sel" | |
puts "sel [:cpoints, :edges, :faces: instances, :groups, :cpoints]" | |
end | |
end | |
def h2(cmd = nil) | |
if cmd.nil? | |
puts "Usage:" | |
Commands.each do |n, o| | |
puts " help :#{n}" | |
end | |
return | |
end | |
puts Commands[cmd].help | |
nil | |
end | |
def ans | |
@ans | |
end | |
def model | |
@ans = Sketchup.active_model | |
end | |
def active_path() | |
@ans = model().active_path | |
end | |
def rendering_options | |
model.rendering_options | |
end | |
alias :ro :rendering_options | |
def selection() | |
@ans = model().selection | |
end | |
def edges | |
#CLI::Entities.new.grep(Sketchup::Edge) | |
@ans = model().active_entities.grep(Sketchup::Edge) | |
end | |
def faces | |
#@ans = CLI::Entities.new.grep(Sketchup::Face) | |
@ans = model().active_entities.grep(Sketchup::Face) | |
end | |
def ss | |
model().selection | |
end | |
def layers(cmd=nil) | |
case cmd | |
when nil | |
model().layers#.map{|layer| layer.name} | |
when :hide_all | |
layers().each{|l| l.visible = false} | |
when :show_all | |
layers().each{|l| l.visible = true} | |
when :invert | |
layers().each do |layer| | |
if layer.visible? | |
layer.visible = false | |
else | |
layer.visible = true | |
end | |
end | |
when :names | |
model().layers.each{|l| puts l.name} | |
end | |
end | |
def layer(arg) | |
if arg.class == Symbol | |
arg = arg.to_s | |
end | |
layers()[arg] | |
end | |
def pages(cmd=nil) | |
case cmd | |
when :help | |
puts "pages:" | |
puts " :purge - delete all pages." | |
when nil | |
model().pages | |
when :purge | |
pages().to_a.each{|page| pages().erase(page)} | |
nil | |
end | |
end | |
alias :scenes :pages | |
def materials | |
model().materials | |
end | |
alias :mats :materials | |
def m(o) | |
p o.class | |
o.class.instance_methods(false).sort | |
end | |
def entities | |
model().active_entities | |
end | |
alias :ents :entities | |
def rot() | |
end | |
def entlast | |
ents[-1] | |
end | |
def invert | |
selection().toggle(entities().to_a) | |
end | |
def hide_unselected | |
model().start_operation("Hide Unselected", true) | |
to_hide = entities().to_a - selection().to_a | |
to_hide.each { |e| | |
e.hidden = true if e.respond_to?(:hidden=) | |
} | |
model().commit_operation | |
nil | |
end | |
def hide(type = nil) | |
case type | |
when :selected, nil | |
selection().each{|e| e.respond_to?(:visible=) && e.visible = false} | |
when :unselected | |
ents = entities() - selection() | |
ents.each{|e| e.respond_to?(:visible=) && e.visible = false} | |
when :solids | |
entities().each {|e| e.visible = false if e.respond_to?(:manifold?) && e.manifold?} | |
end | |
end | |
def unhide(type = nil, recurse = false) | |
case type | |
when :everything, :all | |
# unhide :layers | |
CLI::Entities.new.each {|e| e.hidden = false if e.respond_to?(:hidden?)} | |
when :layers | |
layers(:show_all) | |
end | |
end | |
def test(a = 'a', b = 'b') | |
p a | |
p b | |
end | |
def iterate_entities | |
for e in entities | |
yield e | |
if e.is_a?(Sketchup::Group) | |
iterate_entities(e.entities) | |
end | |
if e.is_a?(Sketchup::ComponentInstance) | |
iterate_entities(e.definition.entities) | |
end | |
end | |
end | |
def select(*args) | |
p args | |
if args[0].class == Hash | |
k = args[0].keys[0] | |
v = args[0].values[0] | |
selection().add(entities().select{|e| e.send(k) == v}) | |
return | |
end | |
type = args[0] | |
case type | |
when :all, 1 | |
@ans = selection().add(entities().to_a) | |
when :none, :clear, 0 | |
selection().clear | |
when :last | |
selection().clear; selection.add(entities()[-1]) | |
when :edges | |
selection().add(edges()) | |
#selection().add(entities().grep(Sketchup::Edge)) | |
when :faces | |
#entities().collect{|e| selection().add(e) if e.is_a? Sketchup::Face } | |
selection().add(faces()) | |
when :groups | |
entities().each {|e| selection().add(e) if e.is_a? Sketchup::Group } | |
when :instances | |
entities().each {|e| selection().add(e) if e.is_a? Sketchup::ComponentInstance } | |
when :clines | |
entities().each {|e| selection().add(e) if e.is_a? Sketchup::ConstructionLine } | |
when :cpoints | |
entities().each {|e| selection().add(e) if e.is_a? Sketchup::ConstructionPoint } | |
when :cons | |
selection().add(entities().grep(Sketchup::ConstructionPoint)) | |
selection().add(entities().grep(Sketchup::ConstructionLine)) | |
when :text | |
entities().each {|e| selection().add(e) if e.is_a? Sketchup::Text } | |
when :invert | |
invert() | |
when :solids | |
entities().each{|e| selection().add(e) if e.respond_to?(:manifold?) && e.manifold?} | |
when :bylayer | |
layer = model().layers[args[1].to_s] | |
p layer | |
puts "invalid layer" unless layer.valid? | |
model().active_entities.each {|e| selection().add(e) if e.respond_to?(:layer) && e.layer == layer} | |
end | |
@ans = model().selection.to_a | |
end | |
alias :sel :select | |
def resel | |
a = @ans.dup | |
model().selection.clear | |
selection().toggle(a) | |
@ans = a | |
end | |
def make(arg = nil) | |
case arg | |
when :group | |
end | |
end | |
def reload(file=nil) | |
load file | |
@last_file = file | |
end | |
def debug(flag) | |
$DEBUG = flag | |
end | |
def verbose(lvl = nil) | |
if lvl == :help | |
puts "verbose [nil, false, true]" | |
return | |
end | |
$VERBOSE = lvl | |
end | |
def prefs | |
UI.show_preferences("") | |
end | |
def keys | |
UI.show_preferences('Accelerator') | |
end | |
def sys(cmd=nil) | |
case cmd | |
when :help, nil | |
puts "sys [:reload :help]\n" | |
puts " :reload - reload cli.rb" | |
when :reload | |
load(__FILE__) | |
end | |
end | |
def r | |
sys(:reload) | |
end | |
def units | |
end | |
def api(klass) | |
base = "http://www.sketchup.com/intl/en/developer/docs/ourdoc/#{klass.to_s.downcase}" | |
UI.openURL(base) | |
end | |
def flippedX?() | |
tr = selection()[0].transformation | |
return true if tr.to_a[0] < 0 | |
return false | |
end | |
def flippedY?() | |
tr = selection()[0].transformation | |
return true if tr.to_a[5] < 0 | |
return false | |
end | |
def flippedZ?() | |
tr = selection()[0].transformation | |
return true if tr.to_a[10] < 0 | |
return false | |
end | |
def flipped? | |
p flippedX? | |
p flippedY? | |
p flippedZ? | |
nil | |
end | |
def find_by_entity_id(id, entities) | |
for e in entities | |
end | |
end | |
def attach(m) | |
s = selection()[0] | |
end | |
# Draw | |
def line(pt1, pt2) | |
entities().add_line(pt1, pt2) | |
end | |
end # module CLI | |
include CLI | |
#puts "CLI included" | |
# | |
#def map(lst, meth) | |
# lst.map(&meth) | |
#end | |
def cli | |
include CLI | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment