Create python class from PD 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
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Create python class from PD workbench

Post by Jee-Bee »

I am reading through the Wiki about FC and python.
I know that PD workbench is a C++ WB, but there are basically no python bindings available

i am wondering if it is somehow possible to (kind of) Inherit from PD objects.

for example:

Code: Select all

import PartDesign as pd

class extended_body(pd.body):
    __ini__(self):
        self.new_property = property(Value)
        self.another_property = property(Value2)
        
So is this currently possible? (If yes how, If no is it usefull to extend the Python bindings in PD?)
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Create python class from PD workbench

Post by onekk »

What is the purpose of extending a PD object?

PartDesign is 'Festure Based" so you coukd not add anything to a feature as there are code underlying that use parameters to "make the feature".

However PartDesign on some extent could be scripted to automate some operation to create things and as example Assembly4 WB use something similar to create assemblies.

the point as usual is what do you want to achieve?

In FreeCAD there are many ways to do things so probably there is way to do what you want, maybe with a FeaturePython object.

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/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Create python class from PD workbench

Post by Roy_043 »

Not the answer to the question, but you can add properties to objects in FreeCAD:
FeaturePython_Custom_Properties
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Create python class from PD workbench

Post by Jee-Bee »

onekk wrote: Sun Jan 29, 2023 9:56 am What is the purpose of extending a PD object?

PartDesign is 'Festure Based" so you coukd not add anything to a feature as there are code underlying that use parameters to "make the feature".
I understand that at least most of it... But if i'm able to inherit it, it is directly clear what parameters are available, where i can make use of and also what parameters names i can't use.
onekk wrote: Sun Jan 29, 2023 9:56 am the point as usual is what do you want to achieve?
I can't directly say jet, because i'm mainly in research phase. But in general i want to try it Object Oriented and as pythonic aas possible.
The general setup is as follows

Code: Select all

Group_of_objects(Group has for example the outer dimensions as properties and these values are via calculation forwarded to some of the lower level bodies)
    |- Body1
    |- Body2
    |- Body3
    |- Body4
    |- Body5
    |- Body6
    |- Body7
    |- Body8 (initial optional maybe later on default)
    |- Body9 (initial optional maybe later on default)
    |- Optional-Body1
    |- Optional-Body2-5
    |- Optional-Body...
All Bodies have some properties and some specific features(configuration and body depended).

But if you want to have an example for a change: I want at least give it a custom logo.

Roy_043 wrote: Sun Jan 29, 2023 11:21 am FeaturePython_Custom_Properties
I saw that one already, but still thank you
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Create python class from PD workbench

Post by onekk »

Jee-Bee wrote: Sun Jan 29, 2023 1:17 pm ...
You can do object oriented things if the object is meant to be subclassed.

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/
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Create python class from PD workbench

Post by Jee-Bee »

So the short conclusion is PD is not meant to be subclassed.
Maybe even worse(at least from my point of view) almost none of the PD features is accessible directly

what do i meant with directly: after importing PartDesign there are only a few features available:
Screenshot 2023-01-29 at 13.00.23.png
Screenshot 2023-01-29 at 13.00.23.png (52.55 KiB) Viewed 917 times
Screenshot 2023-01-29 at 19.21.19.png
Screenshot 2023-01-29 at 19.21.19.png (25.99 KiB) Viewed 917 times
Currently i use (an older )stable

Code: Select all

OS: macOS 10.15
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: (HEAD detached at 0.20)
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.9.13, Qt 5.12.9, Coin 4.0.0, Vtk 9.1.0, OCC 7.5.3
Locale: C/Default (C)
Installed mods: 
  * A2plus 0.4.56
  * fasteners 0.3.41
  * Assembly4 0.11.12
I was thinking does it make sense to create a feature request to extend the current PD python interface.
If found one but that one (issue #5859)only talk about Body orgin and not about the general case.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Create python class from PD workbench

Post by onekk »

See how these posts:

[Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed
https://forum.freecadweb.org/viewtopic.php?f=22&t=72207

Asm4 tutorial scripted.
https://forum.freecadweb.org/viewtopic. ... 24#p628824

You could create things based on PD and even Asm4.

For Asm4 that is coded in Python but have no API you could even write "helpers methods" to make things, using sources.

For PD, see maybe even:

https://github.com/FreeCAD/FreeCAD/tree ... esignTests

or even

https://github.com/FreeCAD/FreeCAD/tree ... eatureHole

Where a new object is created in Python, probably similar to what you want to do.

Hope it helps.

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/
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Create python class from PD workbench

Post by Jee-Bee »

Thanks for your answers even if they are not what i was hoping for!

I have created a FR(issue #8345). maybe some time in the feature there are more option to script from PD
Post Reply