[Solved] Python object with OriginGroupExtension

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

[Solved] Python object with OriginGroupExtension

Post by carlopav »

Sorry for the probably silly question, but: is it possible to have a Python object with App::OriginGroupExtension?
Or a python scriptable App::Part?
Last edited by carlopav on Mon Apr 06, 2020 10:19 am, edited 1 time in total.
follow my experiments on BIM modelling for architecture design
chrisb
Veteran
Posts: 54213
Joined: Tue Mar 17, 2015 9:14 am

Re: Python object with OriginGroupExtension

Post by chrisb »

Moved to Python forum.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Python object with OriginGroupExtension

Post by vocx »

Create an Origin, and a general object that can handle placements and hold geometrical shapes.

Code: Select all

origin = App.ActiveDocument.addObject("App::Origin")
obj = App.ActiveDocument.addObject("App::GeometryPython")
New proxy class for an object. It defines the Type attribute, and adds the OriginGroupExtension to the class. The two __getstate__ and __setstate__ are mandatory.

Code: Select all

class MyNew:
    def __init__(self, obj, tp="Unknown"):
        if obj:
            obj.Proxy = self
        self.Type = tp
        obj.addExtension("App::OriginGroupExtensionPython", self)

    def __getstate__(self):
        return self.Type
    def __setstate__(self, state):
        if state:
            self.Type = state
Simple view provider for the object. The attach, __getstate__, and __setstate__ are mandatory. GetIcon is just needed to get an icon from a resource file, in this case, from Draft_rc.py.

The claimChildren method is implemented to absorb the list of objects returned by it. In this case, it is the Origin and Group properties of the base object that were added through the OriginGroupExtension.

Code: Select all

import Draft_rc

class ViewProviderNew:
    def __init__(self, vobj):
        vobj.Proxy = self
        self.Object = vobj.Object

    def attach(self, vobj):
        self.Object = vobj.Object
        return None
    def __getstate__(self):
        return None
    def __setstate__(self, state):
        return None

    def getIcon(self):
        return ":/icons/Draft_Draft.svg"
    def claimChildren(self):
        objs = []
        objs.append(self.Object.Origin)
        objs.extend(self.Object.Group)
        return objs
The new object is created, with a "Custom_Part" type, and the view provider is added. Also, the Origin property is set to the App::Origin previously created.

Code: Select all

MyNew(obj, "Custom_Part")
ViewProviderNew(obj.ViewObject)
obj.Origin = origin
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.
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

Wow, it works, but: i expected that the transformations of object placement would be automagically transferred to the view providers of the grouped object (as for App::Part), but it's not like this...
Any advice on how to achieve that? I'd like to achieve a scriptable object that behaves like and App::Part, moving its children
follow my experiments on BIM modelling for architecture design
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

vocx wrote: Sun Mar 29, 2020 7:26 pm Create an Origin, and a general object that can handle placements and hold geometrical shapes.
In the App::Part source code i found this commented part by Jurgen:

Code: Select all

// Python feature ---------------------------------------------------------

// Not quite sure yet making Part derivable in Python is good Idea!
// JR 2014

//namespace App {
///// @cond DOXERR
//PROPERTY_SOURCE_TEMPLATE(App::PartPython, App::Part)
//template<> const char* App::PartPython::getViewProviderName(void) const {
//    return "Gui::ViewProviderPartPython";
//}
//template<> PyObject* App::PartPython::getPyObject(void) {
//    if (PythonObject.is(Py::_None())) {
//        // ref counter is set to 1
//        PythonObject = Py::Object(new FeaturePythonPyT<App::PartPy>(this),true);
//    }
//    return Py::new_reference_to(PythonObject);
//}
///// @endcond
//
//// explicit template instantiation
//template class AppExport FeaturePythonT<App::Part>;
//}
wmayer wrote: ping
Sorry Werner to always disturb you with my doubts.
But does that mean that we can't have an App::Part object scriptable in python? If so, why?
follow my experiments on BIM modelling for architecture design
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Python object with OriginGroupExtension

Post by onekk »

I've not tried, but in the script documentation, there are some page about "Part::FaeturePython" that you can customize.

Maybe the reason for not having "App:PartPython" derivable is simply that there is another better thing.

My two cents, and if not relevant sorry for the inconvenience.

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/
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

thx carlo for trying to help. I dont' know if I can achieve what I want with a feature python object:
i need it to test an idea that continues to come up to my mind concerning architectural modelling, in an assembly mode (kind of Arch assembly where everything is an App::Part). So I wonder if we can have a kind of Part that - at the same time - owns a shape (controlled by python) and contains objects like an App::Part. So every part LCS is related to the parent. And everything can be moved hierarchically (building move floors, floors move walls, walls move windows). And extremely fast, because the placement change of the parent part automatically move the children viewprovider.
That is probably just vague enough ATM, but... why not experimenting on that?
follow my experiments on BIM modelling for architecture design
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Python object with OriginGroupExtension

Post by onekk »

I don't know the creation on objects, but using a script i think it could be achieved.

creating a object tree is not difficult, and with scripting, you could easily create some parametric builds, using maybe an external library (move most fo your recurring buildings block in an external python file).

Needing to investigate maube in the soruce code the object creations, and replicate them in your code. I suppose that the commented out part maybe could be copied, decommented in your code and then try to modify it, maybe deriving most of the original code and renaming the method.

Maybe I'm not been clear, sorry for that.

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/
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Python object with OriginGroupExtension

Post by wmayer »

carlopav wrote: Mon Mar 30, 2020 8:40 am But does that mean that we can't have an App::Part object scriptable in python?
Not completely. You still can add or remove custom properties but you cannot define a Python proxy class.
carlopav wrote: Mon Mar 30, 2020 8:40 amIf so, why?
I don't know the intention behind it. The App::Part class was implemented as part of the PartDesignNext and I was never involved in this process and thus don't know everything about the design decisions.

What do you want to achieve actually? Btw, when looking at the code of the App::Part class it doesn't implement anything sophisticated. So, with the code snippets offered by vocx you should be able to implement something very similar.
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: Python object with OriginGroupExtension

Post by carlopav »

Thx for replying @wmayer.
wmayer wrote: Thu Apr 02, 2020 3:58 pm What do you want to achieve actually?
That's the point :oops: , I don't have a precise answer. But, I'd like to have an object that it's own shape but can also contain other objects, and move them as the App::Part does. Imagine a wall object that contains it's wall trace, but also the windows, and move all of them accordingly, just like App::Part does in an assembly context.
Vocx suggestion is good, but Grouped objects do not move according to the container (also here maybe i did something wrong).
Maybe i'm just not looking in the right place?
Thanks in advance for every advice you can give me.
follow my experiments on BIM modelling for architecture design
Post Reply