How do I add 2 existing components to a new component?


So I have a question that seams really easy (probably a brain fart on my end) but I just cant find the answer and its driving me crazy! I’m new to Ruby coding and still haven’t figured out all the details.

So I have created 2 components with ruby from 2 groups using the .to_component method. This works fine. So now I want to create a new component and add the 2 existing components into the new one, the new component should have no entities inside it, only the 2 existing components. I cant seam to figure out how to create a component without using the .to_component method. Any ideas?

This has been discussed several times in this category:

Sketchup.active_model.selection[0].definition.entities.add_instance(definition, transform)

You can nest the components in one component using the SketchUp::Entities Class.
#add_instance(definition, transform) ⇒ Sketchup::ComponentInstance

#add_instance(definition, transform) ⇒ Sketchup::ComponentInstance

#add_instance(definition, transform) ⇒ Sketchup::ComponentInstance

#add_instance(definition, transform) ⇒ Sketchup::ComponentInstance

@nnijmeijer We really cannot add an instance of a definition to it’s own entities.
That would be circular reference.

You snippet does not show that the definition arg must be a new empty definition and transformation arg should be the transform of selection[0].

You do not need to do this.

You can create new empty component definitions with Sketchup::DefinitionList#add and then add geometry directly into it’s entities collection. Lastly add an instance of that new definition to whatever entities collection you desire (the model’s or some other definition’s entities collection.)

So if the 2 instances you are creating will always be your code’s creations then you’ve been doing it butt-backward. First create the empty parent definition. Then the child definitions. Add instances of the children to the parent’s entities collection, then add an instance of the parent to the model at some level.

That’s right @DanRathbun.
You explained it well and completely.

With my response I tried to offer a possible programming direction.

I am aware that the snippit is not complete. But when I first started using Ruby, I actually liked figuring things out instead of copy-paste.

@DanRathbun thank you for your input! I’ll try it out next week when I’'m back home. Have I understod it correctly that I would have to add a new definition to the model.definitions (new_comp) and then add the other components (comp_A & comp_B) to the new components entities and lastly add a instance off the newly created component (new_comp) to the model? Wouldn’t that create 2 copies of comp_A & comp_B? The original ones and the new ones in the new_comp?

@nnijmeijer appreciate your input as well. Currently enjoying some time in the southern part of your wonderful country!

Yes to question 1, and …

Well I had said that IF you were creating instance A and instance B … then you might as well just create them as component definitions (instead of going through the group paradigm,) in which case no there would not be 2 copies of each.

But IF you are wanting to encapsulate existing instances in a component, then as I said in the 2nd post above, this has been discussed multiple times with multiple code snippets already posted. (See the category search link in that post.)