sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    [Code] encapsulate instance into group

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 1 Posters 317 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      (By request)

      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, dset was misspelled dest.

      πŸ’­


      encapsulate.rb

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Updated (2014-03-16) to copy properties of old instance to the new one.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Buy SketchPlus
        Buy SUbD
        Buy WrapR
        Buy eBook
        Buy Modelur
        Buy Vertex Tools
        Buy SketchCuisine
        Buy FormFonts

        Advertisement