Well now that explains a lot of why you are having so much trouble.
If you look at my Tutorials list in the Resources wikis, you’ll see I strongly discourage it’s use in big red typeface.
Martin was a Java programmer, that just did not really understand Ruby’s paradigms.
He often proposed that SketchUp should abandon Ruby and use Java in a long debate topic where the Rubyists had to defend it’s use from others who proposed various other scripting languages.
Anyway, several times he posted and proposed modifying the global ObjectSpace
for specific utility, not understanding his code was modifying everything. I called him on this several times. As far as I know he never corrected his online tutorial to “play nice” with SketchUp’s shared Ruby process. And this is why I added the warning.
I think that now is the time to remove it from the tutorial resource list.
Listen, I am moving on. I don’t want any more to do with this or any edition of that codebase.
I have arthritis in my hands and all this typing is about problems that would not occur if ya fully read a good book on Ruby like the ol’ Pick Axe book, and get a grasp of the basic fundamentals of Ruby.
Unfortunately, this is not one of the first books you should read. It is similar to Martin’s work in that Matthew does not teach coding that is correct for a shared Ruby environment. It really needs an update.
The module concept is the 2nd thing I teach after describing how Ruby is object oriented.
It is just the way the Ruby interpreter is coded to work.
Also I do not use them myself and many other do not.
I use blocks of #
lines. Most good code editors can collapse contiguous comment lines.
You are not selecting it. That is something for the end user to do manually.
With the API all you need to do is reference an object. The visible selection that the user sees is not needed for manipulating model objects with the API.
So, again, if you wish to find (and reference) an object by a string name, use the searching statements I gave above.
def m(*args)
model = Sketchup.active_model
ents = model.active_entities
if args[0].is_a?(String)
instset = ents.grep(ComponentInstance)
# search for the 1st instance whose name is args[0]
inst = instset.find {|i| i.name == args[0] }
if inst # an instance was found
# use this inst
else
# search for the 1st instance whose definition name is args[0]
inst = instset.find {|i| i.definition.name == args[0] }
if inst # an instance was found
# use this inst
end
end
else # args[0] is some other type
# ... etc ...
end
# ... etc ...
end # method m()
ADD: You can add in a conditional test if you still want to use the visible selection,
whether the inst
reference found is actually selected in the viewport …
if model.selection.include?(inst)