[SOLVED] Customizing a spline object...

Need help, or want to share a macro? Post here!
Joel_graff
Posts: 823
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

[SOLVED] Customizing a spline object...

Postby Joel_graff » Thu Feb 14, 2019 3:44 am

I've been trying to sort out the best way to customize a Draft.BSpline. The idea is that I want to create a custom Part::Part2DObjectPython class that is a BSpline with additional properties / methods. At a bare minimum, I need to add properties to it and process property changes.

So Draft.makeBSpline builds the BSpline using this bit of code:

Code: Select all

    obj = FreeCAD.ActiveDocument.addObject("Part::Part2DObjectPython",fname)
    _BSpline(obj)
    obj.Closed = closed
    obj.Points = pointslist
    obj.Support = support
    ...
    if gui:
        _ViewProviderWire(obj.ViewObject)
I want to add properties to it and, when the properties change, rebuild the spline points and reassign them to regenerate the object. I would rather do it this way than manage it externally and destroy / recreate the object entirely, because then I need to manage two objects - the BSpline and a separate spline "manager" object.

The problem is, if I try to write my own custom Part::Part2DObjectPython class, then use it as the basis for the BSpline object, the BSpline class methods override my methods. As a very abbreviated example:

Code: Select all

def create(points):
	obj = FreeCAD.ActiveDocument.addObject('Part::2DObjectPython', 'myObject')
	_MyCustomClass(obj, points)
	
class _MyCustomClass:

	def __init__(self, obj, points):
	
            obj.Proxy = self
            self.Type = 'MyCustomClass'
            self.Object = obj
            
            _BSpline(obj)
            obj.Points = points
This will work. It will generate the spline and do what I need. Any properties I create, of course, will exist. But, I can't handle them because the spline's onChanged() event apparently overrides mine. It makes sense, of course, but I don't know how to extend a Part::Part2DObjectPython with my own event-handling code, if that's even possible...
Last edited by Joel_graff on Thu Feb 14, 2019 12:45 pm, edited 1 time in total.
User avatar
Chris_G
Posts: 927
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Customizing a spline object...

Postby Chris_G » Thu Feb 14, 2019 8:53 am

Hi,
2 comments in the code below :

Code: Select all

def create(points):
	obj = FreeCAD.ActiveDocument.addObject('Part::2DObjectPython', 'myObject')
	_MyCustomClass(obj, points)
	
class _MyCustomClass:

	def __init__(self, obj, points):
	
            obj.Proxy = self             <--- obj.Proxy is a _MyCustomClass() instance
            self.Type = 'MyCustomClass'
            self.Object = obj
            
            _BSpline(obj)                <--- obj.Proxy is probably overwritten here with a  _BSpline() instance
            obj.Points = points
I don't know if this is possible, but maybe you can try to make _MyCustomClass inherit from _BSpline :

Code: Select all

class _MyCustomClass(_BSpline):
	def __init__(self, obj, points):
		super(_MyCustomClass, self).__init__(obj)
		obj.Proxy = self
Joel_graff
Posts: 823
Joined: Fri Apr 28, 2017 4:23 pm
Contact:

[SOLVED] Re: Customizing a spline object...

Postby Joel_graff » Thu Feb 14, 2019 12:45 pm

Chris_G wrote:
Thu Feb 14, 2019 8:53 am
I don't know if this is possible, but maybe you can try to make _MyCustomClass inherit from _BSpline :
That seems to have fixed it. Thanks!