Catching document close event
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Catching document close event
Is there a way to get some Python code executed when a FreeCAD document is closed?
I have a macro which creates a document and a model in that document and also adds a menu to the menu bar to do some actions on the model. If I close the document, the menu should get removed.
I tried creating an invisible widget and catching its close event, but it doesn't get one when the document is closed even though the widget does get destroyed.
Currently the macro prevents duplicate menus by checking to see if its menu is already there in case I have previously run the macro and closed its document, but this is not really the right solution: the menu should go away as soon as I close the document.
I have a macro which creates a document and a model in that document and also adds a menu to the menu bar to do some actions on the model. If I close the document, the menu should get removed.
I tried creating an invisible widget and catching its close event, but it doesn't get one when the document is closed even though the widget does get destroyed.
Currently the macro prevents duplicate menus by checking to see if its menu is already there in case I have previously run the macro and closed its document, but this is not really the right solution: the menu should go away as soon as I close the document.
Re: Catching document close event
You can use a document observer :
The other methods that can be used in a document observer are here :
https://github.com/FreeCAD/FreeCAD/blob ... erPython.h
Code: Select all
import FreeCAD
import FreeCADGui
class myObserver(object):
def __init__(self):
self.target_doc = None
def slotDeletedDocument(self, doc):
if doc == self.target_doc:
FreeCAD.Console.PrintMessage("%s has been deleted\n"%doc.Label)
obs = myObserver()
App.addDocumentObserver(obs)
doc = FreeCAD.newDocument("i_am_observed")
obs.target_doc = doc
https://github.com/FreeCAD/FreeCAD/blob ... erPython.h
Re: Catching document close event
Hi,
is it possible to abort the document close
Context : my workbench lets user edit an object with an external app (and save them back in FC document). I'd like to disallow document close until the external is closed.
is it possible to abort the document close
Context : my workbench lets user edit an object with an external app (and save them back in FC document). I'd like to disallow document close until the external is closed.
Re: Catching document close event
Not really possible to abort document close with the observer. However as the observer is run in the main thread, if you take care of closing your external inside its callback it will lock further processing until you're done.
Re: Catching document close event
In C++ the document class has the methods setClosable(bool)/isClosable() that have been added for this purpose. However, they are not yet exposed to Python.
Re: Catching document close event
The problem is the object has to get it's state from the external before the document is saved. When does the close event occur, before or after user is asked to save?
So even if they are exposed soon my workbench won't be retro-compatible
Re: Catching document close event
Just give a try to @Chris_G code. As it prints in the Console, you'll see if it happens before or after save.
Re: Catching document close event
slotDeletedDocument occurs after ask-to-save
Re: Catching document close event
Code: Select all
/** Called when a document is about to be saved*/
void slotStartSaveDocument(const App::Document&, const std::string&);