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

    Copying files using "ftools.rb" library

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 2 Posters 1.4k Views 2 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
      mgate
      last edited by

      I aim to copy a SketchUp file from one folder to another. I'm using the following method from the "ftools.rb" library:

      def copy(from, to, verbose = false)
          $stderr.print from, " -> ", catname(from, to), "\n" if verbose
          syscopy from, to
        end
      
        alias cp copy
      

      when running my plugin, the file is copied correctly from the original folder to the destination folder but SketchUp crashes afterwards.

      @unknownuser said:

      SketchUp Application has stopped working
      A problem caused the program to stop working correctly.
      Windows will close the program and notify you if a solution is available.

      I tried different options, i.e. keeping the same "to" file name as the "from", also changing it but SketchUp always crashes.
      However, when using the move method of the "ftools.rb", it works perfectly with no problem even changing the "to" name:

        def move(from, to, verbose = false)
          to = catname(from, to)
          $stderr.print from, " -> ", to, "\n" if verbose
      
          if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
            unlink to
          end
          fstat = stat(from)
          begin
            rename from, to
          rescue
            begin
              symlink readlink(from), to and unlink from
            rescue
      	from_stat = stat(from)
      	syscopy from, to and unlink from
      	utime(from_stat.atime, from_stat.mtime, to)
      	begin
      	  chown(fstat.uid, fstat.gid, to)
      	rescue
      	end
            end
          end
        end
      
        alias mv move
      

      Any idea where the problem is? Thanks for your time.
      Note: I use SU v8

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

        (1) SU v8 uses Ruby 1.8.x which does not have Unicode character support on Windows. So any unicode characters in the pathnames could cause issues.

        (2) Symbolic links require being logged into an administrator account on newer Windows version (I believe v6.0 [Vista] and higher.)

        Lastly, you haven't said what kind of file you are copying. ?

        Is the file currently in use by SketchUp or your extension ?

        I'm not here much anymore.

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

          The goal is to create a copy of two existing template files (not opened) from "Resource" folder to a different path, the target files should have new names too (previously defined in a inputbox).
          templates:
          ...\Resources\XXX AT Template.skp
          ...\Resources\XXX AT Template.xlsx

          Here it is part of the script of my plugin, at the moment just trying to copy the skp file though:

          require 'ftools.rb'
          ...
          ...
          folder		= File.dirname( __FILE__ )
          resources	= "Resources"
          resource_name= "XXXXXX AT Template.skp"
          resource_file	= File.join(folder, resources, resource_name).tr("\\","/") # the script will be modified to select the directory via a UI.savepanel dialogue
          file_name	= "123456 AT aa.bb.skp"  # the real name will be picked via an inputbox
          desti_file	= File.join(folder, file_name).tr("\\","/")
          
          if File.exist?(resource_file)
           File.copy(resource_file, desti_file, verbose = true)
          else
           puts "SKETCHUP TEMPLATE FILE NOT FOUND!"
           return nil
          end
          ...
          ...
          
          

          The above script certainly creates the copy in the correct folder but SU crashes afterwards.

          As the move() doesn't crash, I tried to modify it as follow:

            def move(from, to, verbose = false)
              to = catname(from, to)
              $stderr.print from, " -> ", to, "\n" if verbose
          
          #    if RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ and file? to
          #      unlink to
          #    end
              fstat = stat(from)
              begin
          #     rename from, to
          #  rescue
                begin
                ln from, to and unlink from
                rescue
          	from_stat = stat(from)
          	syscopy from, to  and unlink from
          	utime(from_stat.atime, from_stat.mtime, to)
          	begin
          	  chown(fstat.uid, fstat.gid, to)
          	rescue
          	end
                end
              end
            end
          
            alias mv move
          

          by using above move(), the resource_file is kept and the desti_file is created but SU crashes too.

          I tried to use Fileutils.rb instead but there are lots of errors when open SU v.8 (I'm using !AdditionalPluginFolders.rb) and below doesn't work:

          FileUtils.copy_file(resource_file, desti_file, preserve = false, dereference = true)
          
          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            Is the above code within a method? Using local variablesin the TOPLEVEL_BINDING of a script will not work. (They are removed from memory when the script ends.) They should be instance or class variables instead.

            @mgate said:

            require 'ftools.rb'

            Where did "ftools.rb" come from ?

            @mgate said:

            I tried to use Fileutils.rb instead but there are lots of errors when open SU v.8

            Where did you get the "FileUtils.rb" file from ?

            I have a properly versioned Ruby 1.8.6-p287 standard Ruby library packaged as an RBZ extension for SketchUp v8..13 here:

            Link Preview Image
            Release SketchUp Extension RBZ archive of Ruby 1.8.6 p287 Standard Library · DanRathbun/sketchup-ruby186-stdlib-extension

            Ruby 1.8.6 p287 Standard Library packaged as a SketchUp extension. - Release SketchUp Extension RBZ archive of Ruby 1.8.6 p287 Standard Library · DanRathbun/sketchup-ruby186-stdlib-extension

            favicon

            GitHub (github.com)

            @mgate said:

            (I'm using "!AdditionalPluginFolders.rb") and below doesn't work:
            FileUtils.copy_file(resource_file, desti_file, preserve = false, dereference = true)

            Using this just adds complexity to the issue at hand. I'd suggest removing it from the equation. And fix the underlying issue.

            I'm not here much anymore.

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

              @mgate said:

              require 'ftools.rb'
              > ...
              > folder		= File.dirname( __FILE__ )
              > resources	= "Resources"
              > resource_name= "XXXXXX AT Template.skp"
              > resource_file	= File.join(folder, resources, resource_name).tr("\\","/")
              > ...
              > 
              

              Problem here.

              The "Resources" folder is not in a sub-folder of the "Plugins" folder, nor in a sub-folder of YOUR extension sub-folder (which should be in a sub-folder of "Plugins".)

              This should return the correct path:
              resource_path = Sketchup.find_support_file("Resources")

              The above script certainly creates the copy in the correct folder but SU crashes afterwards.[/quote]
              Do you get a BugSplat!, or is a Windows Error Report (WER) generated (ie, check Event Viewer) ?

              Secondly, your rescue clauses are not telling you what error is happening. Do something like this:

              
              begin
                # file operation
              rescue => e
                puts e.inspect # or UI.messagebox(e.inspect)
              else # change to more meaningful message;
                puts "file operation success!"
              end
              
              

              Thirdly, we used to tell users to run SketchUp as administrator because all users needed read and write permissions on folder in the SketchUp application's program path. (This can cause file drag and drop to stop working for SketchUp.)
              The alternative is to set permissions for SketchUp and all it's program sub-folders to allow all users full permissions.

              I'm not here much anymore.

              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