Yes, partly. If the model’s modified flag is set (true
) then SketchUp will ask the user if they wish to save the work. (The modified flag can be cleared by saving the model via the API to a temporary file in the TEMP
folder for example.)
But it can also be cleared by returning the model to the “start state” by wrapping all your drawing within an undo operation (which you should be doing as it is best practice anyway.)
But, the “safe” in shutdown, really comes from the fact that batch coders used to purposely crash SketchUp in an unsafe manner, by purposely crashing Ruby. This means that SketchUp cannot go through it’s normal shutdown and saving of it’s environment settings (in the registry on Windows, or a .plist
file on Mac,) and perhaps some .dat
files.
SketchUp will save some settings during runtime, and others only during shutdown. If it is shutdown unsafely, there exists the chance that settings get into a bad state, which means the user needs to manually delete settings files, or repair using the installer.
A few examples of doing what you wish. Each assume you have a method named draw_geometry()
to do the work.
Using an abort operation:
def draw_op(
op_name, # also used for the SKP filename
dirname= File.join(ENV['Home'],"Documents/SketchUp/"),
model=Sketchup::active_model
)
###
model.start_operation(op_name,true)
draw_geometry(model)
model.commit_operation
rescue => err
puts err.inspect
puts err.backtrace
else # no errors
model.save( File.join(dirname,op_name) )
ensure
model.abort_operation
end
But after the overhaul of observers, they have suggested everyone attach their operations to the previous one, so you may wish to switch off as many extensions as possible.
Or, just draw within a group, and reuse that group:
@grp = model.entities.add_group
# Immediately add a cpt to prevent GC of group
@grp.entities.add_cpoint([0,0,0])
draw_using_group(@grp,"GearBox")
# Reuse @grp for other drawing loops,
# then when done with @grp
@grp.clear!
model.definitions.purge_unused
model.materials.purge_unused
model.layers.purge_unused
def draw_using_group(
grp, # a reference to a valid group
op_name, # also used for the SKP filename
dirname = File.join(ENV['Home'],"Documents/SketchUp/"),
model = Sketchup::active_model
)
###
model.start_operation(op_name)
draw_geometry(model,grp)
model.commit_operation
rescue => err
puts err.inspect
puts err.backtrace
else # no errors
model.save( File.join(dirname,op_name) )
ensure
grp.clear!
# Immediately add a cpt to prevent GC of group
grp.entities.add_cpoint([0,0,0])
GC.start
end