Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
π« Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download
[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.
Advertisement