sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Convert Layer Pointer to Layer Name, How?

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 608 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.
    • M Offline
      Marthmatica
      last edited by

      Anyone know how to check a layer name when the return looks like this?

      I do this in Ruby console:

      fs=Sketchup.active_model.selection.each { |entity| puts entity.layer}
      
      

      Ruby Console return data:

      #Sketchup::Layer:0x00000000447780
      #Sketchup::Layer:0x0000000b356ed8
      #Sketchup::Layer:0x0000000b356ed8

      Question is, if I wanted to do an "if" layer name check, with that pointer "0x00000000447780", how is it done?

      When I do this:

      if entity.layer = '0x00000000447780'; entity.material = 'red' end };
      

      I get this:

      @unknownuser said:

      Error: #<ArgumentError: Cannot find layer named "0x00000000447780">

      When I do this:

      fs=Sketchup.active_model.selection.each { |entity| if entity.layer =0x00000000447780; entity.material = 'red' end };
      

      I get this error:

      @unknownuser said:

      Error: #<ArgumentError: No implicit conversion to layer>
      <main>:in layer=' <main>:in block in <main>'
      <main>:in each' <main>:in <main>'
      SketchUp:1:in `eval'

      So how do you convert a layer pointer, so Ruby knows what you're looking for?

      Thanks

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        You're so close.

        
        Sketchup.active_model.selection.each do |entity| 
          if entity.layer.name == "Some Layer name"
            entity.material = "Red"
          end
        end
        

        What you were seeing is the representation of a Layer object. Layer objects have these methods.

        Also note the comparison operator in Ruby is == and not =

        Hi

        1 Reply Last reply Reply Quote 0
        • M Offline
          Marthmatica
          last edited by

          Thanks. But in the grep iterator, it only returns that layer pointer, and won't recognize the name of the layer, or this code has an error. The problem is, it turns all the faces red, even if they are in a layer other than "Bozworth".

          fs=Sketchup.active_model.selection.each { 
          |entity| if entity.layer == 'Bozworth'; entity.material = 'red' end };
          
          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            Your code is not correct - look at my example more carefully. Layer objects have names. A Layer's name is represented as a String.

            Hi

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

              As @Jim says, you need to access the layer by its ' .name'
              So you need something like:
              Sketchup.active_model.selection.each{|e|e.material="red" if e.layer**.name**=="Bozworth"}

              TIG

              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