onComponentTypeChanged not working for me


    def onComponentTypeChanged(defslist, cdef)
      puts "onComponentTypeChanged: #{cdef}"
    end

I have a definitions list observer attached, onComponetAdded method works fine. However, when I change a group to a component onComponentTypeChanged does not fire. Can anyone tell me why that might be?

Thanks for any help!

https://ruby.sketchup.com/Sketchup/DefinitionsObserver.html#onComponentTypeChanged-instance_method
Should spot the group changing into a component definition…

2 Likes

Ya but it isn’t… I copied and pasted the documented onComponentTypeChanged into my code right below my onComponentAdded method, but it does not fire, ie does not put “onComponentTypeChanged: #{cdef}” in the console, when I convert a group to a component. I am mentioning onComponentAdded simply to show that the definitions list observer is attached and working…

I don’t know if it is relevant, as I haven’t ever used onComponentTypeChanged, but in the past I have seen that output to the Ruby Console using puts does not work from some of the Tool protocol callbacks. Could that be happening here too?

1 Like

I don’t think so. I am trying to attach an observer to the newly created component because the group observer does not stay attached to the instance when the conversion happens, this attachment does not happen either.

The operations would not change the #group? flag to false it would instead create a whole new definition for the component instance (which likely also gets a new ID.)

So I would think that onComponentAdded would fire which should allow you to attach the observer watching each definition object.

But since this will be happening after the new instance is created, the observer attachment will miss the adding of the instance (via onComponentInstanceAdded,) so I think you’ll need to catch the new instance in the EntitiesObserver#onElementAdded callback and test the 2nd argument for is_a?(Sketchup::ComponentInstance) and attach any instance observer there.

1 Like

Okay I just tested simplified version and it does fire for me.

With a group object selected, I paste this into the console:

module Spy
    extend self
    def onComponentAdded(defslist, cdef)
      puts "onComponentAdded: #{cdef}"
    end
    def onComponentTypeChanged(defslist, cdef)
      puts "onComponentTypeChanged: #{cdef}"
    end
end
model = Sketchup.active_model
model.definitions.add_observer(Spy)
obj = model.selection[0]
comp = obj.to_component
onComponentTypeChanged: #<Sketchup::ComponentDefinition:0x0000025f21b481d8>
=> #<Sketchup::ComponentInstance:0x0000025f21ad85e0>

Testing with 1 unique group shows that the definition is reflagged as a #group? = false, ie no new definition. Following this the onComponentTypeChanged fires for the old definition.

But when the group definition has more than 1 instance, then onComponentAdded fires because a new definition is necessary. Following this the onComponentTypeChanged fires for the new definition.

In both cases the instance reference changes as the result is of a different class.

Testing in SketchUp 2022 on Win 10.


The possible reason may be that there is a Mac bug, or your code did not really attach the definition list observer.

If you can put together a small test code and model that reproduces this bug, please file an issue in the API issue tracker,

1 Like

Hey Dan, yes tested as you did and got the same result.

Then I tried

  1. make a new single group, onComponentAdded fires
  2. right click and make component, onComponentAdded fires again NOT onComponentTypeChanged

Same … and this is likely a bug.

The API’s Group#to_component method and the core context menu Make Component command should work the same (in my opinion.)

Again, pls open an issue if one has not yet been opened.

1 Like

OK, will do.

Here: onComponentAdded fires instead of onComponentTypeChanged · Issue #859 · SketchUp/api-issue-tracker · GitHub

2 Likes