Please could we have a facility added to FeaturePython objects, that when the object is deleted a function is called.
At present there are functions onChange and executed.
Bit like with a C++ object it would be useful to have an opportunity to tidy things up.
FeaturePython - Feature request,
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Veteran
- Posts: 2756
- Joined: Mon Feb 27, 2012 5:31 pm
-
- Veteran
- Posts: 5505
- Joined: Thu Apr 05, 2018 1:53 am
Re: FeaturePython - Feature request,
There is an onDelete(self, vobj, subelements) function in the view provider object.keithsloan52 wrote: ↑Wed Feb 08, 2023 8:22 am Please could we have a facility added to FeaturePython objects, that when the object is deleted a function is called.
At present there are functions onChange and executed.
Bit like with a C++ object it would be useful to have an opportunity to tidy things up.
I use it in PDWrapper macro to remove child objects from the body when the wrapper object is deleted.
Code: Select all
def onDelete(self, vobj, subelements):
#only remove children from the body if they are claimed and not native PD objects
doc = FreeCAD.ActiveDocument
body = doc.getObject(vobj.Object.Body)
if vobj.Object.LinkedObject and vobj.Object.ClaimChildren:
if vobj.Object.LinkedObject in body.Group:
grp = body.Group
if not vobj.Object.LinkedObject.isDerivedFrom("PartDesign::Feature") \
and not vobj.Object.LinkedObject.isDerivedFrom("Part::Part2DObject") \
and not vobj.Object.LinkedObject.isDerivedFrom("PartDesign::ShapeBinder"):
grp.remove(vobj.Object.LinkedObject)
body.Group = grp
vobj.Object.LinkedObject.ViewObject.Visibility = True
#need to ensure the next feature in the tree's BaseFeature property points to our BaseFeature
solids = [feat for feat in body.Group if feat.isDerivedFrom("PartDesign::Feature") and feat.BaseFeature == vobj.Object]
if len(solids) == 1: #found it
solids[0].BaseFeature = vobj.Object.BaseFeature
#need also to set the tip to our object's base feature if our object is the current tip
if hasattr(vobj.Object,"_Body") and vobj.Object._Body.Tip == vobj.Object and vobj.Object.BaseFeature:
vobj.Object._Body.Tip = vobj.Object.BaseFeature
vobj.Object.BaseFeature.ViewObject.Visibility = True
if vobj.Object.Proxy.editingMode:
vobj.Object.Proxy.exorciseGhosts()
FreeCADGui.Control.closeDialog()
return True