Nesting multiple components inside a component

I’m new to Ruby and SketchUp so pardon my ignorance and apparent clumsy coding.

I am trying to nest components within one group or component after these sub components are already defined. Here is a code snippet:

        current_time = Time.now
	formatted_time = current_time.strftime("%Y%m%d%H%M%S")
@truss_component = Sketchup.active_model.definitions.add "KING_POST_TRUSS_#{@Span.round(0)}_#{formatted_time}"
transformation = Geom::Transformation.new([0,0,0])
 	componentinstance = Sketchup.active_model.active_entities.add_instance(@truss_component, transformation)
	componentinstance.name = "KING_POST_TRUSS_GABLE_END_#{@Span.round(0)}_#{formatted_time}"
	
    offsetyvalue = @Arraylength - @Ply
	new_transformation = Geom::Transformation.new([0,offsetyvalue,0])
	new_instance = Sketchup.active_model.active_entities.add_instance(@truss_component, new_transformation)
	
	



	model=Sketchup.active_model
   	entities = model.entities
    definitions=model.definitions
   	compdefinition=definitions.add "TRUSS_ASSEMBLY_#{formatted_time}"

    	
    group = compdefinition.new_instance.add_group

	transformation = Geom::Transformation.new([0,0,0])
	
    component = group.to_component
	componentinstance2 = entities.add_instance(component.definition, transformation)

I would like to nest componentinstance and new_instance within componentinstance2. This parent component could just as well be a group and not a component if that makes more sense.

Alternatively I would just like to add a component to a group, maybe this is easier:

maingroup = Sketchup.active_model.entities.add_group
maingroup.entities.new_instance

However, this does not work either. I’m sure I’m missing something very simple here.

Group: maingroup
Component Instance: new_instance

Where did you get the “.new_instance” method from ?

In simple terms…

You have the truss_definition.
Make a new ‘container’ definition, into which you will place instances of the truss.
Iterate through the Y-spacings required.
Make a transformation [as a point], incrementing its Y value as needed.
Use:
container.entities.add-Instance(truss_definition, transformation)
Make a_transformation for where the container will be place.
Now use:
model.active_entities.add_instance(container, a_transformation)

Done.

Alternatively, a Group is just a special kind of Component definition.
You still add things to its entities-context.
You can change a Group into a Component-instance when you are done, you can get the definition from the instance if you need to…

Let’s face it I don’t understand the way this works very well.

However, I did get this to work:

@all_trusses =

@all_trusses << Sketchup.active_model.active_entities[-1]

@maingroup = Sketchup.active_model.entities.add_group(@all_trusses)
@maingroup.name = “TRUSS_ASSEMBLY_#{formatted_time}”

Each time I create a new component instance I add it to the array with the second line of code and then at the very end I create the group using the array. I don’t know if this is the best way to do it but it actually works.

That is very fragile, assuming it’ll be at the end of the active_entities collection. When you use entities.add_instance it will return the instance entity - capturing this return value is a more reliable way to keep track your entities.

Agreed this could be problematic

I now have a similar situation where I am trying to add a group to the array but the previous method does not work and what I have below is not working either:

# Left Rimboard

		riml_pt1 = [0,0,0]
		riml_pt2 = [@Rimboardthk,0,0]
		riml_pt3 = [@Rimboardthk,0,@Jdepth]
		riml_pt4 = [0,0,@Jdepth]

		group2 = Sketchup.active_model.active_entities.add_group
		entities2 = group2.entities
		group2.description = "RIMBOARD_LEFT"
		group2.name = "RIMBOARD_LEFT"

		new_face2 = entities2.add_face riml_pt1, riml_pt2, riml_pt3, riml_pt4
		new_face2.pushpull -@Arraylength
		@all_trusses << Sketchup.active_model.new_face2

When you pushpull a face, then afterwards that face no longer exists.
New side faces are added and two new main faces are added, one coplanar with the original and the other offset by the given distance, and reversed.
When you push [<<] the new_face2 into the array @all_trusses it’s not a valid thing.
Sketchup.active_model.new_face2 is just weird - a Sketchup model has no method named .new_face2 ?
And anyway your new_face2 is not a valid? reference either as it no longer exists ?
Why not simply add group2 to the collection of trusses, as that is the ‘truss’ array?