After wrestling with this problem for an hour or two for a routine I am writing to load a .SKP file, change it and resave it, I came up with this solution:
Replace \ with / in the file name (save does not seem to like )
Save the current model with a temporary name (in the same folder as the desired final name)
Close the current model (You have to close the model so you can modify the .skp file already on disk)
Remove the current .skp file (if it exists)
Move the temporary file to the desired file (rename())
# routine to save a .SKP file with the same name as the current model
# need to save file with a temporary name, close model, then move the temporary name to the real name
def resave_file(sfile)
# save() does not seem to like back slashes
# replace them withforward slashes
sfile.gsub!(Regexp.new("\\\\"), '/')
printf("sfile; %s\n", sfile)
# save to a temporary location first
# this assumes the file name ends with .skp (or .SKP)
sfile2 = sfile[0, sfile.length - 3] + 'TMP'
printf("sfile2; %s\n", sfile2)
bret = Sketchup.active_model.save(sfile2)
printf("Save file; bret; %s\n", bret)
return(bret) if !bret
# now move sfile2 to sfile
# remove existing file
# need to close existing model first
Sketchup.file_new
if File.exists?(sfile)
begin
File.unlink(sfile)
rescue
warn "could not delete file; " + sfile
return(false)
end
end#if
bret2 = File.rename(sfile2, sfile)
printf("Returning bret2; %s2\n", bret2)
return(bret2)
end#def
If you need to file to still be open after the save, you could open it again.
Let me know if it works for you