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

    Ruby to open a sequence of files - run ruby

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 403 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.
    • T Offline
      tallbridgeguy
      last edited by

      Is it possible to make a ruby that opens a sequence of files and then runs a ruby?

      For example, I am in a sketchup file called bob_000001.skp. (Starting file)

      Run ruby in this file

      It opens bob_000002.skp and runs a ruby. (all files in same directory)

      Then it loops to the next file, opens bob_000003.skp and runs ruby, etc

      This would continue until it runs out of files.
      something like this (sorry I'm not proficient in ruby)

      require 'sketchup.rb'
      model = Sketchup.active_model
      title = model.title

      open title + 1
      run ruby
      repeat

      Thanks!

      TBG

      Nielsen
      http://www.tallbridgeguy.com/

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

        Yes.
        There are 'batch tool' methods that can do this.
        Often used for batch exporting/converting, batch unit-changing etc...
        I've made a few... you just need to change parts of the ruby to do what you want then decide if you save changes etc before opening the next one etc, depending on what want the batch process to do...
        Search the Plugins at SCF and see what suits your needs best...

        TIG

        1 Reply Last reply Reply Quote 0
        • T Offline
          tallbridgeguy
          last edited by

          Thanks! i will take a look. (now that I know it will work)

          Edit

          So I want to open a file like
          open bob_0000001.skp
          run a ruby
          open bob_000002.skp
          run a ruby
          etc

          Since I am just learning ruby, this is what I have so far ( I will update as I learn)


          require 'sketchup.rb'
          model = Sketchup.active_model
          model_filename = File.basename(model.path) #current open filename bob_000001.skp to start

          Loop
          name = model_filename.slice!(/\d+/) #splits out 000001 as a string? need a number?

          #add a number - doesn't work but thats the goal
          bob = name + 1

          run ruby.rb here # other ruby in plugin directory

          #open next file i.e. bob_000002.skp

          Sketchup.open_file "bob_" + bob ".Skp"

          repeat until I run out of files...
          goto loop

          Nielsen
          http://www.tallbridgeguy.com/

          1 Reply Last reply Reply Quote 0
          • T Offline
            tallbridgeguy
            last edited by

            would this code run a .rb file for every sketchup file in a directory?

            Dir.glob('/path/to/dir/*.skp') do |rb_file|

            do work on files ending in .skp in the desired directory

            end

            This is kinda what I would like but I can't seem to get it to work.

            s

            Nielsen
            http://www.tallbridgeguy.com/

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

              Dir.getwd() returns the current working directory

              Dir.chdir( dirpath ) changes the working directory to the argument.[list]can be an absolute path, or a relative path from the current working directory.

              SketchUp usually starts with the User's home directory set as the WD. BUT DON'T count on that, ... any script can change it.

              RULES:
              (1) Don't assume the WD is what you need it to be.
              (2) Save the current WD before your code changes it.
              (3) Change it just before you need it.
              (4) Restore the old working directory, when done.

              💭

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • T Offline
                tallbridgeguy
                last edited by

                Thanks Dan, I appreciate the help.

                One of the things I can't figure out is how to say "no to saving file" in ruby code.

                I open a file, run a ruby, and when I want to go to the next file it asks "do you want to save your file".

                How do you bypass this?

                Thanks!

                Stuart

                Nielsen
                http://www.tallbridgeguy.com/

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

                  @tallbridgeguy said:

                  I open a file, run a ruby, and when I want to go to the next file it asks "do you want to save your file". How do you bypass this?

                  Don't modify the model !

                  🤣

                  Well.. seriously this is true.

                  But you can save out the model to a temp file (in the working directory:
                  Sketchup.active_model.save("Temp_#{model.name}.skp")

                  Or if you wrap what you've done in a UNDO operation, then you can undo it.
                  Sketchup.undo()

                  Or to just return to an unmodified state:
                  Sketchup.undo() while Sketchup.active_model.modified?

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    tallbridgeguy
                    last edited by

                    😄 Thanks Dan, this worked great!

                    Sketchup.undo() while Sketchup.active_model.modified?

                    Stuart

                    Nielsen
                    http://www.tallbridgeguy.com/

                    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