[Solved] Python object with OriginGroupExtension
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved] Python object with OriginGroupExtension
Sorry for the probably silly question, but: is it possible to have a Python object with App::OriginGroupExtension?
Or a python scriptable App::Part?
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
Re: Python object with OriginGroupExtension
Moved to Python forum.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Re: Python object with OriginGroupExtension
Create an Origin, and a general object that can handle placements and hold geometrical shapes.
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.
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.
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
origin = App.ActiveDocument.addObject("App::Origin")
obj = App.ActiveDocument.addObject("App::GeometryPython")
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
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
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: Python object with OriginGroupExtension
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
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
Re: Python object with OriginGroupExtension
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>;
//}
Sorry Werner to always disturb you with my doubts.wmayer wrote: ping
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
Re: Python object with OriginGroupExtension
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.
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Python object with OriginGroupExtension
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?
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
Re: Python object with OriginGroupExtension
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.
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: Python object with OriginGroupExtension
Not completely. You still can add or remove custom properties but you cannot define a Python proxy class.
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.
Re: Python object with OriginGroupExtension
Thx for replying @wmayer.
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.
That's the point , 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