Function when a workbench opens a new file.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
Function when a workbench opens a new file.
I wish to create some FreeCAD objects when a new file is created. I only want to do this when a particular workbench is active.
Where in the implementation of a particular workbench could I add code to perform something when a new file is opened.
Where in the implementation of a particular workbench could I add code to perform something when a new file is opened.
Re: Function when a workbench opens a new file.
Use the C++ class DocumentObserver and override its slotCreatedDocument. But note that the Document is passed as a const reference. So you can try to use const_cast to directly add a new object there which should be safe to do. The only thing you have to take care of is that the Label of the document is not yet defined at that time.
Here is a minimum example in Python only for demonstrating purposes:
Here is a minimum example in Python only for demonstrating purposes:
Code: Select all
class DocObserver(object):
def slotCreatedDocument(self, doc):
doc.addObject("Part::Feature", "Shape")
doc=DocObserver()
App.addDocumentObserver(doc)
Re: Function when a workbench opens a new file.
About the current workbench you can use the class WorkbenchManager which has the method active() that returns a pointer of a Workbench.
In Python you can do:
In Python you can do:
Code: Select all
wb=Gui.activeWorkbench()
wb.GetClassName()
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
Re: Function when a workbench opens a new file.
So I do that in the workbenches InitGui.py ?wmayer wrote: ↑Thu May 30, 2019 7:39 am Use the C++ class DocumentObserver and override its slotCreatedDocument. But note that the Document is passed as a const reference. So you can try to use const_cast to directly add a new object there which should be safe to do. The only thing you have to take care of is that the Label of the document is not yet defined at that time.
Here is a minimum example in Python only for demonstrating purposes:Code: Select all
class DocObserver(object): def slotCreatedDocument(self, doc): doc.addObject("Part::Feature", "Shape") doc=DocObserver() App.addDocumentObserver(doc)
Re: Function when a workbench opens a new file.
For example.
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
Re: Function when a workbench opens a new file.
With the two Python snippets above you should be able to do it. I guess the active workbench you are talking about is the one of your InitGui.py file. So, the easiest would be to add the observer in the workbench's Activated method and remove it in its Deactivated method.
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
Re: Function when a workbench opens a new file.
Thanks Werner
What is the function to remove the Observer? App.removeDocumentObserver(doc) ?
I tried search for doc on App.addDocumentObserver() hoping to find how to remove but not much luck with my searches.
Thanks Keith
What is the function to remove the Observer? App.removeDocumentObserver(doc) ?
I tried search for doc on App.addDocumentObserver() hoping to find how to remove but not much luck with my searches.
Thanks Keith
Re: Function when a workbench opens a new file.
Yes, App.removeDocumentObserver() is your friend. Actually you should be able to see this method when using FreeCAD's Python console as it automatically lists all methods of an object when pressing "."
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
Re: Function when a workbench opens a new file.
Okay I am missing something as I get the follow error message
My code in InitGUI.py looks like
Will have to give the FreeCAD python console a try I am still on the old fashioned way of doing things
Code: Select all
name 'DocObserver' is not defined
Traceback (most recent call last):
File "<string>", line 65, in Activated
Code: Select all
import FreeCAD
class DocObserver(object):
def slotCreatedDocument(self, doc):
mySphere = doc.addObject("Part::Sphere", "Globe")
mySphere.Radius = 100
class Astro_Workbench ( Workbench ):
"Astro workbench object"
def __init__(self):
self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Astro/Resources/icons/AstroWorkbench.svg"
self.__class__.MenuText = "Astro"
self.__class__.ToolTip = "Astro workbench"
def Initialize(self):
def QT_TRANSLATE_NOOP(scope, text):
return text
import AstroCommands
commands=['AddRegionCommand' ]
toolbarcommands=['AddRegionCommand']
self.appendToolbar(QT_TRANSLATE_NOOP('Workbench','AstroTools'),toolbarcommands)
self.appendMenu('Astro',commands)
#FreeCADGui.addIconPath(":/icons")
FreeCADGui.addIconPath(FreeCAD.getResourceDir() + \
"Mod/Astro/Resources/icons")
FreeCADGui.addLanguagePath(":/translations")
#FreeCADGui.addPreferencePage(":/ui/openscadprefs-base.ui","OpenSCAD")
def Activated(self):
"This function is executed when the workbench is activated"
doc=DocObserver()
App.addDocumentObserver(doc)
return
def Deactivated(self):
"This function is executed when the workbench is deactivated"
App.removeDocumentObserver(doc)
return
def GetClassName(self):
"This is executed whenever the user right-clicks on screen"
return "Gui::PythonWorkbench"
"This function is executed when the workbench is deactivated"
return
def ContextMenu(self, recipient):
"This is executed whenever the user right-clicks on screen"
# "recipient" will be either "view" or "tree"
#self.appendContextMenu("My commands",self.list) # add commands to the context menu
Gui.addWorkbench(Astro_Workbench())