• Login
sketchucation logo sketchucation
  • Login
⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

How to convert String to Path?

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 4 Posters 933 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.
  • D Offline
    derei
    last edited by 18 Jul 2011, 07:05

    Hy, I'm trying to "learn" by doing something and I need some help.
    I got the current model's path like this:

    
    model = Sketchup.active_model
    pth = model.path
    
    

    and pth is returned as string, so I cannot do this:

    
    pth = model.path.dirname
    
    

    My question: how I can convert the returned string to path, so I can get model's directory?

    Or is any easier way to get model's directory?

    Thanks.

    DESIGNER AND ARTIST [DEREI.UK](http://derei.uk/l)

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 18 Jul 2011, 07:44

      Use the 'File' methods...
      ` pth=model.path >> "C:/Temp/MySkip.skp"

      if the file has never been saved it '' ! so trap for that??

      folder=File.dirname(pth) >> "C:/Temp"
      file_name=File.basename(pth) >> "MySkip.skp"
      file=File.basename(pth, ".*") >> "MySkip"
      ext=File.extname(pth).downcase >> ".skp"

      OR

      ext=File.extname(pth).upcase >> ".SKP"
      file_path=File.join(folder, file+".txt") >> "C:/Temp/MySkip.txt"
      f=File.open(file_path, "w")

      opens/makes the file to 'w'rite to...

      or 'r'ead, 'a'ppend, 'b'inary etc

      f.puts("Hello word!")

      f.close

      important to close any open file as it's 'locked' by SUp/Ruby otherwise...`

      TIG

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 22 Jul 2011, 11:41

        pth = File.dirname( model.path )

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

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 23 Jul 2011, 12:19

          @thomthom said:

          pth = File.dirname( model.path )

          On new unsaved model, model.path returns an empty string.

          On an empty string, File.dirname("") returns ".", which usually means the current working directory (but is false really, as there is no actual path set for the model.)
          Dir.getwd() will return the path for the current working directory (which is likely to be the user's HOME directory, when Sketchup first starts, if no other script has changed it.)
          The HOME dir on Mac: ENV['HOME']
          The HOME dir on Win: ENV['USERPROFILE']

          So you test pth this way:

          pth =( model.path.empty? ? nil ; File.dirname( model.path ) )
          if pth
            # not nil, a valid path
            # do something here with the model path
          else
            # model was not saved
            # perhaps save the model here
            # then do something else
          end
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 23 Jul 2011, 15:13

            As I explained in my earlier post 😕
            pth=model.path will be '' [an empty string] if the SKP's never been saved NOT nil, so a test like if pth will always return true because an empty string is not equivalent to false or nil ! so you need a if pth != '' test etc...

            TIG

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 24 Jul 2011, 09:23

              @tig said:

              ... so a test like if pth will always return true

              NOT if you use the conditional statement on line 1 of my code example, which will set pth to nil if the model.path.empty? test is true, otherwise the valid path will be referenced by pth.

              There are many ways to do the same thing, here's another:

              if model.path.empty?
                # model was not saved
                # perhaps save the model here;
                pth = "some/new/path/filename.skp"
                valid = model.save(pth)
              else
                # it's a valid path
                pth = model.path
                valid = true
              end
              if valid
                # do some more stuff
              else
                puts("Model save was unsucessful to path; #{pth}.")
              end
              

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 24 Jul 2011, 09:49

                So didn't spot your initial .empty? test.
                Of course you could have avoided some more extra steps with a streamlined

                if pth=model.path.empty?
                  ### do stuff with an alternative path
                else
                  ### do stuff with 'pth' as it's not empty
                end
                

                TIG

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 24 Jul 2011, 10:00

                  Isn't Ruby great! So many ways to do the same thing... we can all be happy and flexible.

                  phrase for today: COBOL SUCKS!

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 24 Jul 2011, 12:36

                    @tig said:

                    So didn't spot your initial .empty? test.
                    Of course you could have avoided some more extra steps with a streamlined

                    if pth=model.path.empty?
                    >   ### do stuff with an alternative path
                    > else
                    >   ### do stuff with 'pth' as it's not empty
                    > end
                    

                    ### do stuff with 'pth' as it's not empty - incorrect. pth will have the result of .empty? - true/false.

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

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 24 Jul 2011, 16:56

                      Thomthom you are right!
                      Here is a fixed version

                          if (pth=model.path).empty?
                            ### do stuff with an alternative path
                          else
                            ### do stuff with 'pth' as it's not empty
                          end
                      

                      😒

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        4/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement