Files
create/src/Mod/Part/CompoundTools/Explode.py
luz paz 147542aef0 Part: Part.CompoundTools.Explode.explodeCompound access a property of...
...a sometimes null object with an error

Python code, file `src/Mod/Part/CompoundTools/Explode.py`
The `explodeCompound` function can be called in a context without assigning the `ViewObject` property, for example from a command line script.

The error that is fixed by this patch. 
```
Traceback (most recent call last):
  ...
  File "/opt/freecad/Mod/Part/CompoundTools/Explode.py", line 23, in explodeCompound
    cf.ViewObject.DontUnhideOnDelete = True
AttributeError: 'NoneType' object has no attribute 'DontUnhideOnDelete']
```

Patch submitted by marioamb.  
Fix #004421  
https://tracker.freecadweb.org/view.php?id=4421
2021-02-21 11:40:20 +01:00

28 lines
1.3 KiB
Python

from .CompoundFilter import makeCompoundFilter
def explodeCompound(compound_obj, b_group = None):
"""explodeCompound(compound_obj, b_group = None): creates a bunch of compound filters, to extract every child of a compound into a separate object.
group: if True, Group is always made. If False, group is never made. If None, group is made if there is more than one child.
returns: (group_object, list_of_child_objects)"""
sh = compound_obj.Shape
n = len(sh.childShapes(False,False))
if b_group is None:
b_group = n > 1
if b_group:
group = compound_obj.Document.addObject('App::DocumentObjectGroup','GrExplode_'+compound_obj.Name)
group.Label = 'Exploded {obj.Label}'.format(obj = compound_obj)
else:
group = compound_obj.Document
features_created = []
for i in range(0, n):
cf = makeCompoundFilter('{obj.Name}_child{child_num}'.format(obj = compound_obj, child_num = i), group)
cf.Label = '{obj.Label}.{child_num}'.format(obj = compound_obj, child_num = i)
cf.Base = compound_obj
cf.FilterType = 'specific items'
cf.items = str(i)
if cf.ViewObject is not None:
cf.ViewObject.DontUnhideOnDelete = True
features_created.append(cf)
return (group, features_created)