Model.save does not save
-
The doc here http://groups.google.com/group/SketchUp-Plugins-Dev/web/Ruby-Model.html#save
@unknownuser said:
model = SketchUp.active_model
status = model.save “mysketchup.skp”Wouldn't a save need a filename with full path?
model = Sketchup.active_model skpdirname = File.dirname(model.path) skpbasename = File.basename(model.path, ".skp") #Here I import a DWG with skpbasename+".dwg" savename = skpbasename+".skp" status = model.save savename
It returns 'true' but nothing is saved back to disk.
So how can I save my SKP from ruby? -
Have you searched for the file name. Seeing that you give the method a relative filename, do you know where it's saved?
What if you change one if your line to:
savename = File.join(Fileskpdirname, skpbasename+".skp")
? -
@unknownuser said:
Have you searched for the file name. Seeing that you give the method a relative filename, do you know where it's saved?
Yes it is there. When I press the File/Save it gets written with it's new size.
@unknownuser said:
What if you change one if your line to: savename = File.join(Fileskpdirname, skpbasename+".skp")
It gives a invalid filename-error.
Removing the path it does not complain anymore and return a true. But file is not updated.Maybe it is important that I do this from a ruby starting on SU-startup.
-
@hpw said:
@unknownuser said:
Have you searched for the file name. Seeing that you give the method a relative filename, do you know where it's saved?
Yes it is there. When I press the File/Save it gets written with it's new size.
? Aren't we talking about saving from Ruby?
@hpw said:
It gives a invalid filename-error.
Can you give an example of a filename with the path that doesn't work? Maybe you got some non-ASCII characters that causes problems.
-
@unknownuser said:
? Aren't we talking about saving from Ruby?
Yes, I copy a empty SKP to my work-directory and start SU with the filename in the commandline-parameter.
So I stand in my SKP-file yet.
Now the startup-ruby does import my autocad-file and does some processing on it.
Now I want to save and leave SU.
The ruby save does not work. But when I choose File/save or CTRL-S then it gets saved.@unknownuser said:
Can you give an example of a filename with the path that doesn't work? Maybe you got some non-ASCII characters that causes problems.
No. My filesnames are as following:
20090708_080921161_samplecust@sampleurl_com_{E62CA959-8615-40C9-926E-4883168F8FEC}.skp
Date_time_email_GUID.skp
I also tried Hello.skp with no luck.
And why does the command only take a filename without path?
-
@hpw said:
Yes, I copy a empty SKP to my work-directory and start SU with the filename in the commandline-parameter.
So I stand in my SKP-file yet.
Now the startup-ruby does import my autocad-file and does some processing on it.
Now I want to save and leave SU.
The ruby save does not work. But when I choose File/save or CTRL-S then it gets saved.I'm not sure if SU will use the path that you opened the file from. It could be saved to a completely different location. Which is why I suggest you search your harddrive for it.
Not sure what the deal with full path is though. Have to look into that.
-
I did a quick test myself.
File.dirname('test.skp')
This saved a file in my Sketchup folder. I'm guessing it's where your files are saved as well.And I had no problems saving files using full path. But I wonder if you haven't used forward slashes (/) as Ruby require due to it's cross-platform nature.
'C:\test.skp'
fails, but'C:/test.skp'
works.You can use File.expand_path to convert relative paths to full paths and also sort out your slashes.
` File.expand_path('.')C:/Programfiler/Google/Google SketchUp 7`
-
Thanks for the hint with the forward-slashes.
And I find some test-files in my 'Google Sketchup 7\Plugins' folder.But there remains a problem.
I can not save to the filename of my currently open file.
That does not work.
When I modify the filename and save to a new file then it works. -
That's probably because SU has that file open and the Ruby script isn't allowed access to it.
-
@unknownuser said:
That's probably because SU has that file open and the Ruby script isn't allowed access to it.
But then it shouldn't return 'true' for success.
I would call it a bug.
Also the doc could be more improved with a sample with path and a hint to the forward-slash problem. -
It return true when you save over the currently open file?
And if you reopen that file - what do you get? -
@unknownuser said:
It return true when you save over the currently open file?
And if you reopen that file - what do you get?It return true when I try to save to the currently open file!
But it stays the same size and when I reopen it I get the same as before. -
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
Advertisement