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: 

Object: attributes

3 REPLIES 3
Reply
Message 1 of 4
datanasovte
217 Views, 3 Replies

Object: attributes

Hi!

 

In my script I used attributes.add to assign attributes to a BRepBody. When this body is used for a pattern or mirror feature the bodies created doesn't share the assigned attributes. I was wondering is this the desired effect?

 

Other features, for example, Copy creates a body with the same attributes as the original one.

 

I am looking forward to your reply.

Regards,

Dimitar

3 REPLIES 3
Message 2 of 4
kandennti
in reply to: datanasovte

Hi @datanasovte .

 

We have created a sample that copies the Attributes of the original Body to the body of the pattern when the PatternFeature is selected in the timeline.

# Fusion360API Python script
# clone Attributes
import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # select PatternFeature
        msg :str = 'Select Pattern Feature'
        selFiltter :str = 'Features'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        feat :adsk.fusion.Feature = sel.entity

        # Record marker Position
        timeState = des.timeline.markerPosition
        feat.timelineObject.rollTo(True)

        # is Pattern Feature
        if not hasattr(feat, 'inputEntities'):
            # Return the marker Position
            des.timeline.markerPosition = timeState

            msg = 'PatternFeature only.'
            _ui.messageBox(msg)
            return

        # get inputEntitie
        refBody :adsk.fusion.BRepBody = feat.inputEntities[0]

        # Return the marker Position
        des.timeline.markerPosition = timeState

        # clone Attributes
        for targetBody in feat.bodies:
            cloneAttributes(refBody.attributes, targetBody.attributes)


        _ui.messageBox('Done')

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


def cloneAttributes(
    baseAttrs :adsk.core.Attributes,
    targetAttrs :adsk.core.Attributes):

    for groupName in baseAttrs.groupNames:
        attrLst :list = baseAttrs.itemsByGroup(groupName)
        for attr in attrLst:
            targetAttrs.add(groupName, attr.name, attr.value)


def selectEnt(
    msg :str, 
    filtterStr :str
    ) -> adsk.core.Selection :

    try:
        sel = _ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None

 

Since Attributes are difficult to check, we also created this sample.

# Fusion360API Python script
# Dump BrepBody Attributes
import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

# check
def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        msg :str = 'Select BrepBody'
        selFiltter :str = 'Bodies'
        while True:
            sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
            if not sel: return

            body :adsk.fusion.BRepBody = sel.entity
            dumpAttributes(body)

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

def dumpAttributes(
    body: adsk.fusion.BRepBody):

    dumpMsg(f'-- {body.name} --')
    attrs :adsk.core.Attributes = body.attributes

    for groupName in attrs.groupNames:
        attrLst :list = attrs.itemsByGroup(groupName)
        for attr in attrLst:
            dumpMsg(f'{groupName} : {attr.name} : {attr.value}')

def selectEnt(
    msg :str, 
    filtterStr :str
    ) -> adsk.core.Selection :

    try:
        sel = _ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None

def dumpMsg(msg :str):
    adsk.core.Application.get().userInterface.palettes.itemById('TextCommands').writeText(str(msg))

1.png

Message 3 of 4
datanasovte
in reply to: kandennti

Hi @kandennti ,

 

Thank you for sharing the way you deal with that issue. I really appreciate it.

Your solution is fine but you have to remember to execute it for every pattern and that might lead to error and also it is an additional step, which according to me should be automatic that is why I was hoping to get an answer from someone from Fusion 360 development.

Message 4 of 4
kandennti
in reply to: datanasovte

@datanasovte .

 

Currently, the only way to do this would be for a human to start the script and process it in order from the top of the timeline.

(Perhaps this could be done if you have a CustomEvent that monitors periodically.)


PatternFeature is probably not supported at the moment, but when CustomFeature becomes available, I feel that I will be able to create the command I want.

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