Modify and save the model when SketchUp is closed

My plugin creates some temporary entities that I would like to hide when the user closes the application.

I tried using the Sketchup::AppObserver onQuit method like this

def onQuit(mod = Sketchup.active_model)
  RG::SUHelpers.select_by_layer(mod, ["cfd_domain"]).each { |e| e.hidden = true }
  mod.save
end
def self.select_by_layer(mod, layer_name)
  ent = mod.active_entities # Error here
......
end

(The temporary entities are hidden and then the model is saved.)

The problem is that the active model does not seem to be available while quitting. It is probably closed before the observer is triggered. The Ruby console shows the error just before the window disappears.

It is not a major issue, but it is quite annoying to see these entities in the preview panel when the user opens SketchUp again.

I’m not an “observer” expert, but as I recall when I studied them, I didn’t really able catch the “onQuit” call. I guess it is happening when a modell is already saved and everything else is “done”.

When Sketchup quits (e.g. user select File>>Exit) , it will check if the model have changes, if yes it will show the save panel, but the “onQuit” call will hapen only after the save, so I do think the “onQuit” at least poorly documented or buggy… but I gueess - currently you cant use it to interrupt the quit and to make changes at the model.
As you also realised.

You can check also:
Tracking major events in the lifetime of a Sketchup session · Issue #682 · SketchUp/api-issue-tracker · GitHub
(and further related links there)
__

Anyway, to hid an entities without user interaction, in my opinion is a bad habit, you should inform the user about it… E.g. You should have a menu like: “Hide my temporally entities…”
__

:bulb: Alternatively, perhaps, you can use the “onOpenModel” , “onActivateModel”… call next time the user open the model, and your extension is still installed… :wink: >> Popup: “Do you want to Hide my temporally entities…?”

1 Like

Thanks @dezmo

I agree in principle, but this is a special case. These entities are generated on the fly only when a specific tab of the plugin is selected. They are used more like visual aids and they are not part of the model in the sense of objects, in my case buildings in a masterplan. They are also hidden when the user navigates to a different tab.

Yes, I had thought about this :slight_smile: However, they would still be shown in the preview panel.

1 Like

Perhaps you need to leverage the new Overlay class.

By the way, your snippets do not take into account the multiple models that can be open on the Mac.

Yep, that looks interesting. I was looking at it this morning. I need to update the plugin for SU 2023 first.