sketchucation logo sketchucation
    • Login
    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

    Ruby Help for Components & Layers

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 317 Views 2 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.
    • K Offline
      ktkoh
      last edited by

      I am trying to move a component created in a rb file to a new layer also created by that file. This code moves the enities so they are only visible on the new layer but when I check the Info the component is still on layer0. Is there a way to define the component layer knowing the name? Or is there a way to select the component knowing the name? (As I am able to change the layer of a selected component so I could make that work.)

      model = Sketchup.active_model
      model.start_operation "CreateBox"
      name = "WebTest"
      layerno = "01"
      
      # Create new layer for Part Assembly
            layers = model.layers
            newname = "A" + layerno + "-" + name
            if layers[newname]
      	 UI.messagebox("Layer \"#{newname}\" exists, exiting.")
      	 return
            end
            layer = layers.add newname
            pb = layer.page_behavior = LAYER_IS_HIDDEN_ON_NEW_PAGES
            model.pages.each { |p| p.set_visibility( layer, false ) }    
          entities = model.active_entities
      
          group = entities.add_group
          entities = group.entities
            
          pts = []
          pts[0] = [0, 0, 0]
          pts[1] = [10, 0, 0]
          pts[2] = [10, 1, 0]
          pts[3] = [0, 1, 0]
          base = entities.add_face pts
          
          width = -4if( base.normal.dot(Z_AXIS) < 0 )
          
          # Now we can do the pushpull
          base.pushpull width
          
          # Put entities on layer named "newname"
          entities.each{|e|e.layer=newname}
      
          # Now we are done and we can end the operation
          model.commit_operation
          
          # If you want the group to be a Component try the following.
          component_instance=group.to_component  
          component_instance.definition.name=name
          
          model.commit_operation
      

      Keith

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        You never set the layer property for your group.

        at where you got this line:
        group = entities.add_group

        add:
        group.layer = layer

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          And if you want to create components, then you use model.definitions.add "newCompName"
          http://code.google.com/apis/sketchup/docs/ourdoc/definitionlist.html#add

          And then place an instance of the definition. http://code.google.com/apis/sketchup/docs/ourdoc/entities.html#add_instance

          Instead of making a groups and then converting the group.

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          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