Still trying to figure it out. Tested it by putting the code directly into the Ruby Console and putting it into a rb-file.
Try with a freshly started Sketchup 2014 and delete 'Sophie' (but leave her in the component definitions). First I executed in the Ruby Console:
@model = Sketchup.active_model
@ents = @model.entities
@defs = @model.definitions
inst = @defs["Sophie"]
@model.place_component inst,false
Place her anywhere but not on the origin 0,0,0
Second run this:
@ents.each {|e|
next unless e.is_a?(Sketchup;;ComponentInstance)
puts e.transformation.origin
}
You get the proper coordinates of Sophie.
Now I delete all Sophies but leave her in the component definitions.
I wrap the above code into a plugin. I need to add an observer so the code can pick it up again (place_component breaks the code). I load it and start it with: MB_place_instance.activate
The first placed instance always returns 0,0,0 as the origin. Add another one (activate again) and it returns the origin of the first added instance etc... Looks like its returning the definition and not the placed instance.
There must be some logic in it but I fail to see it...
require 'sketchup.rb'
module MB_place_instance
class MyDefObserver < Sketchup;;DefinitionObserver ### create an observer (place_component breaks the code and returns to SU) ###
def onComponentInstanceAdded(definition, instance)
puts "observer has fired; " + instance.to_s
MB_place_instance.refire(instance) # go back to the tool
end
end
def self.activate
@model = Sketchup.active_model
@ents = @model.entities
@defs = @model.definitions
inst = @defs["Sophie"]
@observer = MyDefObserver.new
@model.definitions[0].add_observer(@observer)
@model.place_component inst,false
end #def
def self.refire(instance)
@defs[0].remove_observer(@observer) # delete the observer
@observer = nil # delete the observer in case previous method fails
GC.start # collect garbage
@ents.each {|e|
next unless e.is_a?(Sketchup;;ComponentInstance)
puts "looking with each; #{e.transformation.origin}"
}
puts "directly adressing passed instance; #{instance.transformation.origin}"
end #def
end #module
### start in RubyConsole with; MB_place_instance.activate