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

    Editing string variables

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 3 Posters 230 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      If I have a variable "mat" that is a string "FileName.Dxf", how do I edit the variable to remove ".Dxf"

                 
      unless mat.nil?
         if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then
            #String editing to remove ".Dxf" here
            e.material = mat
         end
      end
      
      

      Does my question make sense?

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

        For files there's a separate method for that: File.basename

        
        unless mat.nil?
           if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then
              #String editing to remove ".Dxf" here
              mat = File.basename(mat, ".Dxf")
              e.material = mat
           end
        end
        
        

        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

          Alternatively, if the file type isn't always known:

          
          unless mat.nil?
             if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then
                #String editing to remove ".Dxf" here
                mat = mat.split('.').first
                e.material = mat
             end
          end
          
          

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

          1 Reply Last reply Reply Quote 0
          • honoluludesktopH Offline
            honoluludesktop
            last edited by

            Thanks, I am coding a plugin that allows me to texture imported components (dxf files) by applying a texture of a similar name to the imported component's name. In other words a imported component named "roof.dxf", will be textured by a material named "roof".

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

              Does it work to set entity.material = "stringName" ?

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

              1 Reply Last reply Reply Quote 0
              • honoluludesktopH Offline
                honoluludesktop
                last edited by

                Thomas, This is my first real attempt to understand ruby, so far its not working. Thanks for the help, I'll get it eventually.

                1 Reply Last reply Reply Quote 0
                • honoluludesktopH Offline
                  honoluludesktop
                  last edited by

                  WooPee!! Got it. Ruby is case sensitive, and I was attempting the string conversion in the wrong place.

                  # Loop through entities & apply material based on layer name
                   Sketchup.active_model.entities.each do |e|
                       begin#if here found
                          #10/30/09 ignore file extention name start
                          stuff=e.definition.name
                          stuff=stuff.split('.').first
                          mat = su_materials[stuff]
                          #mat = su_materials[e.definition.name]#original
                          #end
                       rescue#else if here not found
                           mat = su_materials[e.name]
                       end#end if
                  

                  Can't get over not having to declare variables. Dangerous for guys like me.

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

                    The general way to do string fiddling in Ruby is via RegEx and it's not that hard:

                    
                    x = 'asdf.wer'
                    
                    # match string to regex; string =~ regex
                    # regex; '/'...'/'
                    # match into $1 and $2; string =~ /(...)(...)/
                    
                    x =~ /(.*)\.(.*)/ # match (any number of any char)a period(more chars)
                    
                    puts $1 # 'asdf'
                    puts $2 # 'wer'
                    
                    

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

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

                      Plus, regex can handle the Unicode data from Sketchup, which SU's Ruby doesn't.

                      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