I get the following error at object creation, but not on object changes.
Code: Select all
15:13:42 Traceback (most recent call last):
File "C:/Users/Aleksander/AppData/Roaming/FreeCAD/Macro\uselessbox.py", line 27, in execute
link_sub = obj.LinkSub[0]
<class 'TypeError'>: 'NoneType' object is not subscriptable
Code: Select all
class UselessBox():
def __init__(self, obj):
"""
Default constructor
"""
self.Type = 'box'
obj.Proxy = self
obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = "Just a simple box"
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = 5.0
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = 10.0
obj.addProperty("App::PropertyLinkSub", "LinkSub", "Subsection", "Description for tooltip").LinkSub = None
def execute(self, obj):
"""
Called on document recompute
"""
# get the parent document object containing the subobject
link_sub = obj.LinkSub[0]
# get the list of sub object names from the LinkSub Property
sub_obj_name_list = obj.LinkSub[1]
# get the first name in the sub object name list
sub_obj_name = sub_obj_name_list[0]
# get the actual sub object by its name
vertex = link_sub.getSubObject(sub_obj_name)
vtx_vector = App.Vector(vertex.X - (obj.Length.Value * 0.5), vertex.Y - (obj.Width.Value * 0.5), vertex.Z - (obj.Height.Value * 0.5))
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height, vtx_vector)
def create():
"""
Object creation method
"""
obj = App.ActiveDocument.addObject('Part::FeaturePython', 'UselessBox')
UselessBox(obj)
obj.ViewObject.Proxy = 0 # just set it to something different from None (this assignment is needed to run an internal notification)
App.ActiveDocument.recompute()
return obj