sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Entities Edge color apply

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 209 Views 4 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
      krisjaniszakis
      last edited by

      Can some one say why this is working: (where 's2' is Sketchup::Edge entity)

      material_name=e2.material
      if mat
      cname=material_name.name
      else
      cname="<Default>"
      end#if
      

      but this is not working:

      material_name=e2.material.name
      

      After this code Ruby returns this error:
      Error: #<NoMethodError: undefined method `name' for nil:NilClass>

      Why i cannot strait forward access material name.

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

        Because the material() method returns a Sketchup::Material object, not a String object, But ONLY IF the entity has been assigned a material. If the entity has no material, then the material() method returns nil.

        This is standard operating procedure in Ruby. It is common to test using if, because in Ruby only false and nil evaluate logically false. Everything else, including 0, an empty Array and empty Strings, evaluate as logically true.

        So you do:

        if mat = e2.material()
          mat_name = mat.name()
        else
          mat_name = "<Default>"
        end
        

        You cannot call .name() on the singleton NilClass object ( nil,) because it does NOT have a instance method called ' name'.)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • K Offline
          krisjaniszakis
          last edited by

          Thanks it helped

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

            mat=e2.material
            if mat
              cname=mat.name ### OR .display_name ?
            else ### NO MATERIAL
              cname="<Default>"
            end#if
            

            test for the existence of the material and then get it's name if it exists, if the material is ' nil'then there's no ' name' to get.

            TIG

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

              Late to the party πŸ˜‰

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • K Offline
                krisjaniszakis
                last edited by

                Thanks TIG there can be made changes in my code even more later πŸ˜„

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

                  @tig said:

                  mat=e2.material
                  > if mat
                  >   cname=mat.name ### OR .display_name ?
                  > else ### NO MATERIAL
                  >   cname="<Default>"
                  > end#if
                  

                  test for the existence of the material and then get it's name if it exists, if the material is ' nil'then there's no ' name' to get.

                  #display_name should only be used to output the name of a material to the UI - not as an id.
                  http://www.thomthom.net/thoughts/2012/03/the-secrets-of-sketchups-materials/#speaking-of-names8230

                  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