Note: This issue has been fixed in SketchUp 2014
This file is not needed, but v0.3.2 should do nothing if loaded under SketchUp 2014.
A full ruby install is needed.
Testing, Thoughts etc welcome.
One interesting thing is that instances of both classes return Set for obj.class()
(Not sure if this is desired.)
set_fix.rb
- Code: Select all
# ----------------------------------------------------------------
# File : "set_fix.rb"
#
# Version : 0.3.2
# Date : 2014-03-01
#
# Author : Dan Rathbun, Palm Bay, FL, USA
# ----------------------------------------------------------------
if defined?(Sketchup)
unless defined?(Sketchup::Set)
begin
# Move the API's Set class into the Sketchup module namespace:
Sketchup::Set = Object.class_eval('remove_const(:Set)')
# Paths to standard Ruby libs must first be pushed into
# the $LOAD_PATH (aka $:,) array:
if RUBY_PLATFORM =~ /(darwin)/ # Mac / OSX
# require some other script that pushes Ruby lib paths into $:
else # MS Windows
['C:/Ruby186/lib/ruby/1.8',
'C:/Ruby186/lib/ruby/1.8/i386-mswin32'].each do |lib|
$: << lib unless ($:.include?(lib) || $:.include?(lib.downcase))
end
end
$:.uniq! # remove duplicate entries
# Now require the standard Ruby Set class via 'set.rb'
require('set')
rescue LoadError
Set = Sketchup::Set
raise
else
# No errors occured, so make Ruby's Set class have
# the two methods, that the Sketchup::Set class has:
Set.class_eval('alias_method(:contains?,:include?)')
Set.class_eval %q{
def insert(*args)
errors = {}
args.each_with_index{|a,i|
begin
add(a)
rescue Exception => e
errors[i]= e
end
}
unless errors.empty?
stack = caller(0)
called_from =( stack.size>1 ? stack[1] : stack[0] )
msg = "#{errors.size} errors occured during Set.insert() from #{called_from}:\n"
errors.each {|i,e|
msg<< "arg #{i} (#{args[i].class.name}): #<#{e.class.name}: #{e.message}>\n"
}
raise(RuntimeError,msg)
end
end # def insert()
}
end
end # unless Sketchup::Set class is defined.
end # if Sketchup namespace is defined
EDIT:
0.1.0 : first post.
0.2.0 : Added a class alias in the rescue block, to alias a toplevel Set constant, if the "set.rb" file cannot be found.
0.3.0 : Changed the insert method from a simple alias of add, to an iteration that can handle multiple arguments like the API insert method can.
0.3.1 : $0 returns a String, so comparison must be: $0 == 'SketchUp' (The Argument was not quoted.)
0.3.2 : In Ruby 2.0 under SketchUp 2014, $0 returns a "-e", instead of "SketchUp" so that test was removed. We now just test for the Sketchup module namespace.
The SketchUp API v14 moves the API Set class into the Sketchup namespace so it does not conflict with the Ruby standard library Set class.
-