Loading a file

In the newest version of SketchUp I’m seeing this error:

Error:

#<RuntimeError: Model is of a newer model version that can be opened, but some information might be lost. Use allow_newer: true option to load the file.>

That sounds like you are attempting to open a 2023 file using 2021 or 2022. The file format is now extendable so that all versions after 2021 can open the files, but as that error reports, not without the risk that a newer file may contain things the older version of SketchUp can’t handle. You should set the optional parameter as suggested.

It does seem to me that raising an exception rather than posting a warning was a bit of an extreme measure, though.

1 Like

According to the documentation, the method is supposed to return false. But some “wires have gotten crossed” in the implementation of the override.

EDIT:

Never mind. Apparently, Natthaniel is talking about DefinitionList#load().
Please be specific about what method the issue is with in future topics, so we are not wasting our time.

Since this method returns a reference to the loaded definition, the various exceptions are the only way to notify code of behavior so that code branching might occur in rescue clauses.


EDIT: Does not exactly apply, but same concept could be used for conditional use of the method …

To deal with the deprecation with old versions:

if Sketchup.method(:open_file).arity == 1
  # Old version expecting 1 path argument only:
  status = Sketchup.open_file(filepath)
else
  # Would be -1 meaning variable number of arguments:
  status = Sketchup.open_file(filepath, with_status: true)
end

Interesting that the documentation does not indicate an :allow_newer hash option, …
and says that :with_status defaults to true which should mean that the option can be omitted ?

1 Like

Sorry, I’ve been out for the last few days, I should have posted more details.

Here is the exact line(s) of code that threw that error:


def load_component_door_casing (casing)

	model = Sketchup.active_model()
	definitions = model.definitions

	filename1 = File.join(THIS_DIR,"library/casing/#{casing}.skp")		
  	casing_compdef = definitions.load(filename1)

	return casing_compdef

end

Yes, we figured this out. (Hint: You can edit the original post and topic title.)


Review the method documentation. You need to use a beginrescue structure because this method raises exceptions in order to give your code explanations of what went wrong (when things go “sideways”.)

You can use a #arity test (similar to as I showed) or just trap for RuntimeError and test the exception message for a "allow_newer:" substring. (But this would need a nested beginrescue which might not be so elegant.)

1 Like

So what is the best way of handling this?

It’s not that difficult.

Something like … (The response assignments are the same, but where you need to translate text for the end user, they’d create localized response text. The localized text would likely be displayed in a message box.)

def load_component(filepath)
  definitions = Sketchup.active_model.definitions
  if definitions.method(:load).arity == 1
    # Old version expecting 1 path argument only:
    comp = definitions.load(filepath)
  else
    # Would be -1 meaning variable number of arguments:
    comp = definitions.load(filepath, allow_newer: true)
  end
rescue IOError => error
  msg = error.message
  if msg.include?('Invalid') # "Invalid component file"
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  elsif msg.include?('empty') # "Can't insert empty component."
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  else
    # some other IOError
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  end
  puts response
  return nil
rescue RuntimeError => error
  msg = error.message
  if msg.include?('itself')
    # "You cannot insert a component or model into itself."
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  elsif msg.include?('just screen text')
    # Can't insert a component with just screen text.
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  elsif msg.include?('newer model version') 
    # Model is of a newer model version that can be opened, but some information
    #  might be lost. Use allow_newer: true option to load the file.
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  else
    # some other Runtime error
    response = "#{__method__}: #{msg}:\n  #{filepath}"
  end
  puts response
  return nil
rescue
  raise # any other exceptions
else # no errors
  return comp
ensure
  # ... Optional, stuff to always do ...
end
1 Like