Document added in script does not show up in GUI

Need help, or want to share a macro? Post here!
francoisv2
Posts: 2
Joined: Mon Mar 13, 2017 12:37 pm

Document added in script does not show up in GUI

Postby francoisv2 » Mon Mar 13, 2017 1:07 pm

I am trying to dynamically create a document, add some objects to it, and then launch the GUI. Example script below:

Code: Select all

from PySide import QtGui

def main(*args, **kwargs):
    import sys
    from csg.visualization import FreeCADModelViewer

    import FreeCAD as App
    import FreeCADGui as Gui
    app = QtGui.QApplication(sys.argv)

    new_document = App.newDocument("Test")

    App.ActiveDocument = new_document

    # add some data to the active document   
    model = FreeCADModelViewer(new_document)
    model.attach('regulator_plate.asm')
 

    Gui.ActiveDocument = new_document
    # check that objects were added
    print(new_document.Objects)

    Gui.showMainWindow()
    Gui.ActiveDocument = new_document

    sys.exit(app.exec_())


if __name__ == '__main__':
        main()


The problem is that the document does not appear in the GUI, although (in the console) it is correctly set as App.ActiveDocument. This remains the case even when the document is empty (contain no objects).

Am I missing something?
DeepSOIC
Posts: 4462
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Document added in script does not show up in GUI

Postby DeepSOIC » Mon Mar 13, 2017 1:36 pm

Hi!
I think that you should never assign to App.ActiveDocument, because that doesn't inform FreeCAD that active document has changed. Use App.setActiveDocument() instead. (EDIT: and it shouldn't be necessary either, because any new document automatically becomes active)

Assigning App.ActiveDocument to Gui.ActiveDocument is probably even worse. They are objects of different types.

Code: Select all

>>> Gui.ActiveDocument.TypeId
'Base::Persistence' # <-- that's a pretty strange type, IMO. I expected Gui::Document
>>> App.ActiveDocument.TypeId
'App::Document'
>>>
francoisv2
Posts: 2
Joined: Mon Mar 13, 2017 12:37 pm

Re: Document added in script does not show up in GUI

Postby francoisv2 » Mon Mar 13, 2017 2:04 pm

Thanks for pointing that out.

I've removed any assigning of active documents, but my document still does not show up in the GUI. However, in the console,

Code: Select all

>>> App.listDocuments()
{'Test': <Document object at 0x22fe2a0>}

so it is indeed there.
ulrich1a
Posts: 1374
Joined: Sun Jul 07, 2013 12:08 pm

Re: Document added in script does not show up in GUI

Postby ulrich1a » Mon Mar 13, 2017 4:14 pm

At typing in the FreeCAD-Python console the command "new_document = App.newDocument("Test")" the following happens:

Code: Select all

>>> new_document = App.newDocument("Test")
>>> App.setActiveDocument("Test")
>>> App.ActiveDocument=App.getDocument("Test")
>>> Gui.ActiveDocument=Gui.getDocument("Test")
And the new document shows up.

You may have to add one of the commands to your script.

Ulrich