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

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

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

Post 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
Last edited by xianyu on Thu Jul 07, 2022 7:01 am, edited 2 times in total.
Freecad novice, A Python enthusiast
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

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

Post by Kunda1 »

Does Workbench_creation help at all ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

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

Post 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?
Freecad novice, A Python enthusiast
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

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

Post 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.?
Freecad novice, A Python enthusiast
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

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

Post by Kunda1 »

@Chris_G can you offer any good explanations/tutorials for @xianyu to follow?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post 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.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

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

Post 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
Freecad novice, A Python enthusiast
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post 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
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply