Ruby how to explode component, then remake component


ruby how to explode component, then remake component


Consider something like


then later [assuming that the ‘compo’ exploded into the model.active_entities context ! if not it might BugSplat]


entities = component.explode.find_all{|entity| entity if entity.respond_to?(:bounds)}.uniq
entities = entities.dup.find_all{|entity| entity.valid?}

new_component = Sketchup.active_model.entities.add_group(entities).to_component

the new component has a parent group but not the child of the Sketchup.active_model.

why doesn’t it work? you know i can manually copy the component and explode the copy component and then make exploded component, the new component become the child entity of the sketchup active mode directly.

i use the method, first i copy the component. and the component will be the child entity of the active model. then i explode the copy component and make the copy component. the component’s parent is the origin component’s parent group.

# copy the component
new_component = Sketchup.active_model.entities.add_instance(component.definition, component.transformation)
new_component.material = component.material

# explode the new component
entities = new_component.explode.find_all{|entity| entity if entity.respond_to?(:bounds)}.uniq
entities = entities.dup.find_all{|entity| entity.valid?}

# make group then convert to component.    you know  in sketchup there is an operator "make component" that the sketchup firstly add group and then convert to component?
new_component = Sketchup.active_model.entities.add_group(entities).to_component


Firstly, use model.active_entities NOT model.entities.
Adding a group directly from the exploded entities will fail unless they are exploded into the active_entities context - this might be the model.entities, but that is NOT guaranteed.

Secondly, your testing of exploded entities for suitability for grouping is a good idea, however they should all be ‘valid’ ?

Finally, if you have a nested component-instance inside a group then you cannot explode it and reform it into a new group since add_group(ents) will fail if te ‘ents’ are not in the active_entities context.

Consider this approach…

# assuming we already have a reference to 'component'
ents = component.parent.entities # might be nested etc, so find its parent's entities
group = ents.add_group() # initially empty
gents = group.entities
new_instance = gents.add_instance(component.definition, component.transformation)
new_component = group.to_component = # auto increments

This makes a duplicate of the original component-instance in the same context as that component-instance.
It overlays it exactly.
Its definition has an auto-incremented name - of course you can choose any name you want.

If you want it in another context make ‘group’ in that entities, add the new_instance and explode it etc…
If you no longer want ‘component’ then use component.erase! when you no longer need to refer to it.

PS. there is no need to use .make_unique when you have just one instance of a definition - or are about to explode a duplicate…


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.