Page 1 of 1

[Solved]:How is data exchanged between FreeCAD and Workbench

Posted: Thu Jun 30, 2022 11:17 am
by xianyu
At present, I am developing Workbench and can realize some simple functions.
However,I don't know how FreeCAD and Workbench exchange data, the interface between them,or how does FreeCAD recognize user actions in the Workbench?
Is it APP and Gui?Does anyone know, thanks.

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24267 +148 (Git)
Build type: Release
Branch: Branch_0.19.4
Hash: 476ecf091941bead59b14e44afa6064d5a66afa3
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Thu Jun 30, 2022 3:04 pm
by Kunda1
Does Workbench_creation help at all ?

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Fri Jul 01, 2022 2:12 am
by xianyu
Thanks for your reply.
My workbench creation learned this Workbench creation → FreeCAD commands →Python command definition ,but I still don't know how they exchange data.
For example, in the Part workbench,i want to add optical properties to a surface of the cube, such as smooth, rough, etc. How does FreeCAD know which surface I have selected?

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Fri Jul 01, 2022 9:56 am
by xianyu
xianyu wrote: Fri Jul 01, 2022 2:12 am For example, in the Part workbench,i want to add optical properties to a surface of the cube, such as smooth, rough, etc. How does FreeCAD know which surface I have selected?
Is using FreeCAD. and FreeCADGui.?

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Fri Jul 01, 2022 11:59 am
by Kunda1
@Chris_G can you offer any good explanations/tutorials for @xianyu to follow?

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Fri Jul 01, 2022 12:04 pm
by onekk
xianyu wrote: Fri Jul 01, 2022 9:56 am
xianyu wrote: Fri Jul 01, 2022 2:12 am For example, in the Part workbench,i want to add optical properties to a surface of the cube, such as smooth, rough, etc. How does FreeCAD know which surface I have selected?
Is using FreeCAD. and FreeCADGui.?
First of all, Please put your FC info using the instruction done in:

http://forum.freecadweb.org/viewtopic.php?f=3&t=2264


Without them answers could not be correct as some thing change between versions, and sometimes even with "Python version" hat could be different depending on the way FC is compiled and what OS you use.

Without some code is difficult to tell you how things works!

It is difficult to put together a code that is rather complex.

Some rough and not technically correct explanation:

Rough explanation FC is composed by two parts:

1) FreeCAD that is also called an referred as App that is the "modelling engine" part
2) FreeCADGui that is also called Gui that is the "visualization Part" i.e the GUI but not only.

These part are related, and are working together when you use the FreeCAD program.

Roughly the "modelling engine" Part is realted to the "Data" Tab on the ComboView and the "visualization Part" is in the "View Tab".

What you select is managed usually by the Gui part and you could obtain it using:

Code: Select all

selection = FreeCADGui.Selection.getSelectionEx()

l_sen = len(selection)

if l_sen != 1:
    msg = f"You have selected <b>{l_sen}</b> shape(s)<br>"
    msg += "You must select only 1 shape <br>"
    print(msg)
else:
    do_something(selection)

selection could be composed by many things, so you have to choose what you want, in the case above using selection[0] will gave you the selected solid as if you select more than 1 solid it usually print an error in the "Report View" (It could depend on some setting in preferences, but usually it will work on stock FC)


Hope it helps

Carlo D.

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Mon Jul 04, 2022 6:15 am
by xianyu
onekk wrote: Fri Jul 01, 2022 12:04 pm

Code: Select all

selection = FreeCADGui.Selection.getSelectionEx()

l_sen = len(selection)

if l_sen != 1:
    msg = f"You have selected <b>{l_sen}</b> shape(s)<br>"
    msg += "You must select only 1 shape <br>"
    print(msg)
else:
    do_something(selection)

I'm so sorry, I ignored FC information.
Yes, I have read about APP and Gui on wiki.
This code is FreeCAD recognizes the user's selection operation? What if I want to select a face in the cube

Code: Select all

selection = FreeCADGui.Selection.getSelectionEx()
I know that when I click on an object, the Python Console will show addSelection and clearSelection.
Does this still apply?
https://wiki.freecadweb.org/Selection_API/de

Re: Question:How is data exchanged between FreeCAD and Workbench

Posted: Mon Jul 04, 2022 11:07 am
by onekk
xianyu wrote: Mon Jul 04, 2022 6:15 am I'm so sorry, I ignored FC information.
Yes, I have read about APP and Gui on wiki.
This code is FreeCAD recognizes the user's selection operation? What if I want to select a face in the cube
once you have the shape you could takecwhat is into the shape.

The page you linked is correct.

Sekection should return even the subshape, try to search for getSelection in the forum and you will probably find some hints on usage

Regards.

Carlo D