Model.name not working?


#1

Hi,

I wanted to retrieve the saved file name but the following is not working…

model = Sketchup.active_model
name = model.name

The workaround for this was the following…

model = Sketchup.active_model
path = model.path[0...-4]
name = path.split('\\')
name = name.last

Does the API have an easier method to get the file name?

Thanks in advance!


#2

YES. It is Sketchup::Model#title(), but it returns the filename minus the ".skp".

Sketchup::Model#name() just returns the name attribute that is set manually on the Model Info dialog’s File panel, or via code by the Sketchup::Model#name=() method.


And BTW, getting the filename from a path string in Ruby is this simple …

filename = File.basename(Sketchup.active_model.path)
# or minus the extension ...
filename = File.basename(Sketchup.active_model.path,".*")

#3

Thank you very much!

I wanted to save all SketchUp versions from 8 to 2018 with a single action. If anyone else need this here is a simple script…

model = Sketchup.active_model
path = model.path[0...-4]
title = model.title

versions = {
"8": Sketchup::Model::VERSION_8,
"2013": Sketchup::Model::VERSION_2013,
"2014": Sketchup::Model::VERSION_2014,
"2015": Sketchup::Model::VERSION_2015,
"2016": Sketchup::Model::VERSION_2016,
"2017": Sketchup::Model::VERSION_2017,
"2018": Sketchup::Model::VERSION_2018
}

versions.each do |key, value|
  filename = File.join(path, "#{title}_v#{key}.skp")
  status = model.save_copy(filename, value)
end

Enjoy!


#4
  1. There is a permissions bug in #save_copy
    (I filed the report last week and Thomas logged it. See the tracker.)

  2. The “title” is still on the end of your path, and File::join will create a filepath with the title as a subdirectory name for the new files, but you don’t have the code to make sure this directory exists and create it if it doesn’t.