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: 

Mesh Merge-Feature in API?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
k.trucksees
629 Views, 3 Replies

Mesh Merge-Feature in API?

Hi all,

I want to use the MeshMerge-Command in a Script, but can't find anything about it.Feature.PNG

Can someone tell me where I find it in the API and how it is used?

Thanks

Labels (5)
3 REPLIES 3
Message 2 of 4
kandennti
in reply to: k.trucksees

Hi @k.trucksees .

 

I don't think the API provides MeshWorkspace functionality.
The only way left is to use TextCommands.

 

Go into MeshWorkspace, prepare two or more mesh bodies and run the following script
(I opened the script in Edit beforehand and ran it from VSCode, because MeshWorkspace doesn't allow you to call the script.)

 _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        # Workspace check
        if _ui.activeWorkspace.id != 'MeshEnvironment':
            _ui.messageBox('Please, Switch to MeshWorkspace')
            return

        # MeshBody Count check
        des :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent
        if root.meshBodies.count < 2:
            _ui.messageBox('A minimum of 2 MeshBody is required')
            return

        # select MeshBodies
        sels :adsk.core.Selections = _ui.activeSelections
        sels.clear()
        for mesh in root.meshBodies:
            sels.add(mesh)

        # exec textcommands
        txtCmds = [
            u'Commands.Start MeshCombineCommand',
            u'NuCommands.CommitCmd'
        ]
        for cmd in txtCmds:
            _app.executeTextCommand(cmd)


        _ui.messageBox('Done')

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

 

2.png

 

 

Learn more about TextCommands.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/m-p/9645688 

 

Not related to Mesh, but @thomasa88  provided some useful information.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/access-section-analysis/m-p/9700491#M11052 

 

It is very difficult to develop with TextCommands because of the lack of information.
There is no guarantee that you will reach the goal, and even if you do, there is no guarantee that it will continue to work in the future.

 

If you have something that works well, we hope that you will share the information with us.

Message 3 of 4
k.trucksees
in reply to: kandennti

here is the merge-function I made.

The strange is, that it only works if the comp==root, because I found no way to put meshBodies in any other component else root.

An other issue is if the meshes have more than 50k faces fusion will stop merging the bodies.

 

def fmesh_merge(comp:adsk.fusion.Component):
    """executes a TextCommand to merge the Fusion-meshBodies in the given Component"""
    #todo: check if meshbody.faces.count > 50000, if so reduce faces!
    sel = ui.activeSelections
    sel.clear()
    for mesh in comp.meshBodies:
        sel.add(mesh)
    if len(sel)==0:
        for mesh in root.meshBodies:
            sel.add(mesh)
    txtCmds = [u'Commands.Start MeshCombineCommand', u'NuCommands.CommitCmd']
    
    for cmd in txtCmds:
        ui.palettes.itemById('TextCommands').writeText(
            'Merging '+str(haz_comp.meshBodies.count)
            +' Bodies: '+app.executeTextCommand(cmd)
        )
    

 

Message 4 of 4
kandennti
in reply to: k.trucksees

@k.trucksees .

 

In order to select the Occurrence element, we need to get the proxy. (There is an explanation at the very end.)

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A 

 

For this reason, we have created this sample.

#Fusion360API Python script
import adsk.core, adsk.fusion, adsk.cam, 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

        # root
        execMeshbodiesMerge(root)

        # occ
        execMeshbodiesMerge(root.allOccurrences[0])

        des.activateRootComponent()

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

def execMeshbodiesMerge(
    root_occ :adsk.core.Base
    ):

    app :adsk.core.Application = adsk.core.Application.get()
    ui :adsk.core.UserInterface = app.userInterface
    des :adsk.fusion.Design = _app.activeProduct
    root :adsk.fusion.Component = des.rootComponent

    # selections
    sels :adsk.core.Selections = ui.activeSelections
    sels.clear()

    # get meshbodies
    meshBodyList = []
    if root_occ == root:
        # root
        root_occ.parentDesign.activateRootComponent()
        meshBodyList = [mb for mb in root_occ.meshBodies]
    else:
        # occ -proxy
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A
        root_occ.activate()
        meshBodyList = [
            mb.createForAssemblyContext(root_occ) for mb in root_occ.component.meshBodies]

    # select meshbodies
    [sels.add(mb) for mb in meshBodyList]

    # exec textcommands
    txtCmds = [u'Commands.Start MeshCombineCommand', u'NuCommands.CommitCmd']
    [app.executeTextCommand(cmd) for cmd in txtCmds]

1.png

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