Problem adding instance of child component
-
My script is intended to add a instance of a child component outside of the parent component. The script starts as a context selection when a child component is selected. The user needs to open the parent for editing to select the component.
UI.add_context_menu_handler do |popup| sel = Sketchup.active_model.selection unless sel.empty? if sel.single_object? obj = sel[0] if obj.is_a?(Sketchup;;ComponentInstance) && obj.parent.is_a?(Sketchup;;ComponentDefinition) popup.add_item(NC2SCENE) { # nested_component_command(obj) # } end # component test end # single_object? end # unless empty? end # add_context_menu_handler
and I have not found a method to close the parent component and then when I add an instance of the child it is added inside the parent which is not what I need. The instance needs to be added to the model outside of the parent component.
Thanks
Keith -
Assuming 'obj' is an instance... using
obj.parent
will return the actual 'container' of 'obj' - which might be the model or a group/component-definition].
Use
definition=obj.definition model.entities.add_instance(definition, transformation)
to add an instance into the model itself using a 'transformation' - which you might get from the 'obj' and its 'container' instance... -
@ktkoh said:
... and I have not found a method to close the parent component ...
as in:
Sketchup.active_model.close_active()
-
I used the Sketchup.active_model.close_active() and it worked as advertised.
Thanks (I wish sometimes I could find these on my own)
Keith -
@ktkoh said:
(I wish sometimes I could find these on my own)
The API Method Index really helps find things.
-
@ktkoh said:
I used the Sketchup.active_model.close_active() and it worked as advertised.
Note that the method is bugged and doesn't add the operation to the undo-stack - so if the user undo operations you might see some unexpected results. No workarounds atm.
-
Ok I tried the other method
c2=@model.entities.add_instance(ent_def,insert_tran)
when I first looked at the suggestion I thought that was how I added the instance in my origional code but closer inspection I noted that I was using
c2=@model.active_entities.add_instance(ent_def,insert_tran)
Once I replaced active_entities with just entities the code worked also.
Thanks
Keith
Advertisement