Tag (layer) changed to Untagged when copying w/ Ruby script

Hello all,

I use Ruby script to copy and move multiple dynamic components.
It works great but I noticed that the tag (layer) is changed to Untagged from the tag they were in.
Is this default behavior?

To fix this, in the loop of making copies, I can add to check the tag of original, save it, and assign it to the copy. But is there a better way?

Like what? Saying “Abracadabra” and waving a magic wand ? :roll_eyes:
:magic_wand: :mage:
Nope, doesn’t work. :wink:

Come on, dude. It will not happen by itself. Coding is all about paying attention to the details and making it happen.

The originals were assigned to certain tags (layers) because someone set them to manually, or they were inserted into the model when the current tag (layer) was set to what was desired for the instance tag assignment.
(The latter is usually the only common reason to ever change the current tag to one other than “Untagged”. But when done inserting components, the current tag should be reset immediately. There are extensions that can remind you to do this.)

2 Likes

haha of course not. I was just reading the API documentation to write something to test my idea.

# Make copies of the selected components
copied_components = []
sel.each do |comp|
  # Get the layer/tag of the component
  comp_layer = comp.layer
  # Make a copy of the component
  copied_comp = comp.copy
  # Assign the same layer/tag to the copied component
  copied_comp.layer = comp_layer
  # Add the copied component to the array
  copied_components << copied_comp
end

I found the solution!

2 Likes

Except, … the ComponentInstance class does not have a “copy” method.

It has been requested.

2 Likes

oh okay, I see. I guess my code works because the object is treated as entity from selection.

No that is not it.

The reason is that the Dynamic Components extension itself has done a “no-no” and monkey-patched the Sketchup::ComponentInstance class.

With a “Bench” DC component instance inserted and selected, viz:

model = Sketchup.active_model
#=> #<Sketchup::Model:0x00000240006887f0>
sel = model.selection
#=> #<Sketchup::Selection:0x0000024000687af8>
c = sel[0]
#=> #<Sketchup::ComponentInstance:0x0000024000685b68>
c.respond_to?(:copy)
#=> true
c.method(:copy).owner
#=> Sketchup::ComponentInstance
c.method(:copy).source_location
#=> ["c:/users/dan/appdata/roaming/sketchup/sketchup 2024/sketchup/plugins
#=>  /su_dynamiccomponents/ruby/dcutils.rbe", 289]

Sorry, I forgot that the DC extension did this several places to several classes.