[Code] encapsulate instance into group
-
This snippet meant to be wrapped inside your own module namespace (or in one of it's sub-namespace classes or submodules.) Please do not run it at the toplevel, except for testing.
def encapsulate( instance, into_group ) if instance.is_a?(Sketchup;;Group) itype = 'group' elsif instance.is_a?(Sketchup;;ComponentInstance) itype = 'component' else raise(TypeError,"group or component instance argument(#1) required",caller) end unless into_group.is_a?(Sketchup;;Group) || ( into_group.is_a?(Sketchup;;ComponentDefinition) && into_group.group? ) raise(TypeError,"group argument(#2) required",caller) end model = instance.model if model.active_path != nil && model.active_entities != instance.parent.entities model.close_active until model.active_path == nil || model.active_entities == instance.parent.entities end if model.active_entities != instance.parent.entities msg ="Current editing context & instance's parent context\n"<< msg<<"do not match! Cannot encapsulate #{itype} instance." puts(msg) if $VERBOSE UI.messagebox(msg) else begin ### model.start_operation("Encapsulate #{itype.capitalize}",true) # if instance.is_a?(Sketchup;;Group) definition = instance.entities.parent elsif instance.is_a?(Sketchup;;ComponentInstance) definition = instance.definition end transform = instance.transformation newbie = into_group.entities.add_instance( definition, transform ) if newbie # copy the old instance's properties; unless instance.material.nil? newbie.material = instance.material end unless instance.name.nil? || instance.name.empty? newbie.name = instance.name end unless instance.description.nil? || instance.description.empty? newbie.description = instance.description end newbie.casts_shadows= instance.casts_shadows? newbie.receives_shadows= instance.receives_shadows? newbie.hidden= instance.hidden? newbie.locked= instance.locked? newbie.layer= instance.layer unless instance.attribute_dictionaries.nil? dset = instance.attribute_dictionaries dset.each {|d1| d2 = newbie.attribute_dictionary(d1.name,true) if d1.size > 0 d1.each {|key,val| d2[key]= val } end } end # delete old instance; instance.locked= false instance.parent.entities.erase_entities(instance) end # model.commit_operation() ### rescue => e model.abort_operation() msg ="Warning... the #{itype} instance could not be copied!\n\n" msg<<"Modeling operation was aborted due to error;\n" msg<<"#{e.class.name}; '#{e.message}'" puts(msg) if $VERBOSE UI.messagebox(msg) return false else # Caution, the original instance reference is now invalid !! msg ="Success. The #{itype} instance was copied into\n" msg<<"the group, and the original instance was erased." puts(msg) if $VERBOSE UI.messagebox(msg) return newbie end end end # encapsulate()Updated (2014-03-16) to copy properties of old instance to the new one.
Updated (2014-03-17) to fix line 57,dsetwas misspelleddest.
-
Updated (2014-03-16) to copy properties of old instance to the new one.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement