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

    Name of component

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 267 Views 3 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.
    • F Offline
      fred2420
      last edited by

      Hi i am struggling with what i am sure is something really easy but after hours of search i can't work out how to get the name of the active component as a string. I can set the name in Component attributes.

      This is what I've tried
      model = Sketchup.active_model
      selection = model.selection.name
      but its no where near

      Cheers in advance

      Fred

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        model = Sketchup.active_model
        name = model.selection[0].name
        

        BUT of course that assumes that the first object in the model's selection has a 'name'. A ComponentInstance or a Group might have a name [BUT it might also be ""], AND many other things like a Face or Edge never will have a name, so you'll then get an error...
        If [as I suspect] you want the name of the component-definition of the first item [component-instance] in the current selection... AND you know it is definitely a ComponentInstance then you can use

        name = model.selection[0].definition.name
        

        which will return the unique string that is that component-definition's name.
        To stop errors you really ought to test for the selection's suitability, 'type' thus...

        model = Sketchup.active_model
        sel=model.selection
        name=nil
        if sel and sel[0] and sel[0].is_a?(Sketchup.ComponentInstance)
          name=sel[0].definition.name
        end
        

        This way name is either 'nil' or a string, so the next step in your code could be

        if name
          ### do something with 'name'
        else
          UI.messagebox("Please select a Component Instance to find its Name !")
          return nil
        end
        

        TIG

        1 Reply Last reply Reply Quote 0
        • F Offline
          fred2420
          last edited by

          Nice one that has worked well cheers for the help πŸ‘

          fred

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

            Note that there is component instance name and component name. Component instance name is the same as a group's name.

            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