[solved] Problem with FreeCADGui

Need help, or want to share a macro? Post here!
adonari
Posts: 3
Joined: Sun Dec 15, 2019 11:06 am

[solved] Problem with FreeCADGui

Postby adonari » Sun Dec 15, 2019 11:28 am

Hello,
I want to use FreeCAD with python without launching the graphical interface.
Here is a piece of code (which doesn't work in my case...)

Code: Select all

import sys
FREECADPATH = "C:/Program Files/FreeCAD 0.16/bin/"
sys.path.append(FREECADPATH)

import FreeCAD as App
import FreeCADGui as Gui

doc = App.ActiveDocument    
gui_doc = Gui.ActiveDocument
gui_doc.activateWorkbench("myWorkbench")
which generates an error message:

gui_doc = Gui.ActiveDocument
AttributeError: 'module' object has no attribute 'ActiveDocument'


And indeed, when I look at what FreeCADGui offers me :

Code: Select all

print(dir(Gui))
['__doc__', '__file__', '__name__', '__package__', 'embedToWindow', 'exec_loop',
 'setupWithoutGUI', 'showMainWindow']
I also tried with FreeCAD version 0.18, but I have the same problem.

What do I need to do to be able to use FreeCADGui as from the FreeCAD GUI?

Environment tested:
OS: Windows 7 64-bit
FreeCAD Version: 0.16
Python version: 2.7.16
---------------------------
OS: Windows 10 64-bit
FreeCAD Version: 0.18
Python version: 3.7

Thank you in advance.
Last edited by adonari on Tue Dec 17, 2019 7:31 pm, edited 1 time in total.
openBrain
Posts: 4617
Joined: Fri Nov 09, 2018 5:38 pm

Re: Problem with FreeCADGui

Postby openBrain » Sun Dec 15, 2019 12:01 pm

That's a bit weird. You want to use FreeCAD without GUI but try to import and use GUI module... :? Typically, how could there be a GUI active document while there is no GUI???
What are you trying to achieve?
adonari
Posts: 3
Joined: Sun Dec 15, 2019 11:06 am

Re: Problem with FreeCADGui

Postby adonari » Sun Dec 15, 2019 1:28 pm

Yes, I'm probably wrong about how to proceed...
I would like to be able to execute my code without running FreeCAD and still visualize the geometries I create. I thought, probably wrongly, that geometry visualization was possible with FreeCADGui...
So, how with a simple code like:

Code: Select all

import Part
cube = Part.makeBox(2,2,2)
visualize the cube ?

Thanks!
vocx
Posts: 5206
Joined: Thu Oct 18, 2018 9:18 pm

Re: Problem with FreeCADGui

Postby vocx » Sun Dec 15, 2019 7:42 pm

adonari wrote:
Sun Dec 15, 2019 11:28 am
...
gui_doc = Gui.ActiveDocument
AttributeError: 'module' object has no attribute 'ActiveDocument'
The problem is that you haven't created a new document. This is the first thing you must do as objects are children of the document.

Code: Select all

import FreeCAD as App

doc = App.newDocument("my_doc")
mybox = doc.addObject("Part::Box", "Box")
mybox.Length = 2
mybox.Width = 2
mybox.Height = 2

doc.FileName = "/home/vocx/Pictures/my_doc.FCStd"
doc.save()
I wouldn't use the Part module directly. It creates internal "Shapes", but it does not create "objects". Objects are what are saved in the document, so for this you'd typically use the addObject method of the document.

Certain workbenches like Draft, Arch, BIM, do create objects using their commands, for example, makeLine, makeStructure, makeWindow, etc., but in the case of Part or PartDesign, you first add the object, and then modify the properties like in the example above.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
wmayer
Site Admin
Posts: 16456
Joined: Thu Feb 19, 2009 10:32 am

Re: Problem with FreeCADGui

Postby wmayer » Mon Dec 16, 2019 3:18 pm

I would like to be able to execute my code without running FreeCAD and still visualize the geometries I create.
When loading the FreeCADGui module you must invoke its showMainWindow() method which then internally initializes the module by adding the missing methods.

Now depending on the environment and the way Qt was built it's sufficient that the FreeCAD GUI shows up. There is/was a mechanism in Qt to hook into the event loop of the host application to fetch the events from there and update the FreeCAD GUI. For environments where this doesn't work you must invoke the event_loop() method but this blocks your terminal window, then.
wmayer
Site Admin
Posts: 16456
Joined: Thu Feb 19, 2009 10:32 am

Re: Problem with FreeCADGui

Postby wmayer » Mon Dec 16, 2019 3:42 pm

In our repository under https://github.com/FreeCAD/FreeCAD/tree ... s/embedded some solutions are provided how to integrate the FreeCAD GUI into other tool kits.

Based on the PySide approach you can achieve what you want:

Code: Select all

import sys
from PySide2 import QtCore, QtGui, QtWidgets
import FreeCAD, FreeCADGui

class MainWindow(QtWidgets.QMainWindow):
    def showEvent(self, event):
        FreeCADGui.showMainWindow()
        self.setCentralWidget(FreeCADGui.getMainWindow())

app=QtWidgets.QApplication(sys.argv)
mw=MainWindow()
mw.resize(1200,800)
mw.show()

# must be done a few times to update the GUI
app.processEvents()
app.processEvents()
app.processEvents()

import Part
cube = Part.makeBox(2,2,2)
# creates a document and a Part feature with the cube
Part.show(cube)
app.processEvents()
app.processEvents()
But this way you cannot interact with the GUI, you can only process pending events from time to time to update the GUI.
If you want to interact with FreeCAD then you must call app.exec_(). And inside the FreeCAD Python console you can call app.quit() to get control back to the native Python console.
adonari
Posts: 3
Joined: Sun Dec 15, 2019 11:06 am

Re: Problem with FreeCADGui

Postby adonari » Tue Dec 17, 2019 7:30 pm

Thanks a lot for your answers !