Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
JeromeBriot
256 Views, 2 Replies

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

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.

 

Labels (6)
2 REPLIES 2
Message 2 of 3
BrianEkins
in reply to: JeromeBriot

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
in reply to: BrianEkins

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. 🤣

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report