[Bug] Attributes duplicated when a component is created from a body

[Bug] Attributes duplicated when a component is created from a body

JeromeBriot
Mentor Mentor
652 Views
2 Replies
Message 1 of 3

[Bug] Attributes duplicated when a component is created from a body

JeromeBriot
Mentor
Mentor

Hello,

 

It seems that attributes are duplicated when a component is created from a body. But the parent reference is lost.

 

Here is a small code:

import adsk.core, adsk.fusion, adsk.cam, traceback # pylint: disable=import-error

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        rootComp = design.rootComponent
        sketches = rootComp.sketches
        sketch1 = sketches.add(rootComp.yZConstructionPlane)
        sketchLines = sketch1.sketchCurves.sketchLines
        startPoint = adsk.core.Point3D.create(0, 0, 0)
        endPoint = adsk.core.Point3D.create(5.0, 5.0, 0)
        sketchLines.addTwoPointRectangle(startPoint, endPoint)
        prof = sketch1.profiles.item(0)
        extrudes = rootComp.features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        distance = adsk.core.ValueInput.createByReal(5.0)
        extInput.setDistanceExtent(False, distance)
        extInput.isSolid = True
        ext = extrudes.add(extInput)
        brepBody = ext.bodies.item(0)

        brepBody.attributes.add('BugAttributesComponentFromBody', 'test', 'test')

        attributes = design.findAttributes('BugAttributesComponentFromBody', 'test')

        msg = 'Before component\n'
        for idx, attribute in enumerate(attributes):
            if attribute.parent:
                msg += '{} - {} - OK\n'.format(idx, attribute.name)
            else:
                msg += '{} - {} - PB\n'.format(idx, attribute.name)

        brepBody.createComponent()

        attributes = design.findAttributes('BugAttributesComponentFromBody', 'test')

        msg += '\nAfter component\n'
        for idx, attribute in enumerate(attributes):
            if attribute.parent:
                msg += '{} - {} - OK\n'.format(idx, attribute.name)
            else:
                msg += '{} - {} - PB\n'.format(idx, attribute.name)

        ui.messageBox(msg)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Here is the result:

bug-api-attributes-body-component.png

Thank you.

 

0 Likes
Accepted solutions (1)
653 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor
Accepted solution

This is actually as expected.  Attributes are typically not automatically deleted when the entity they're associated with no longer exists.  In your case, you have a body and add an attribute to it.  The body is then moved into another component.  Essentially, a new body is created and the old one deleted.  The attribute to the old body still exists even though the body is gone.  This is because it's possible that the body will come back.  I can roll the timeline back to a time before the body was copied, then the attribute on the old body will have a parent and the newer attribute on the copied body will fail to get its parent.

 

This is briefly discussed in the User Manual in the topic on Attributes in the section titled "Getting the Associated Entity".

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

JeromeBriot
Mentor
Mentor

Thank you @BrianEkins 

 

After a good sleep, I remembered that I replied the exact same answer in a previous discussion: Orphan Attributes!

 

I guess this discussion deserves a high rank in the forum blooper. 🤣

 

0 Likes