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: 

'Generate Face Groups' via python script?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
ferdlhons
380 Views, 8 Replies

'Generate Face Groups' via python script?

I have imported a simple 3d-mesh (obj-File) and I'm wondering if it possible to do a 'Generate Face Groups' via script?

In the end I need the edges of each FG afterwards... (seems to me similar to the BRepEdges - stuff, but I don't know how to generate them). I'm new to the fusion API, so many thnx for any help!!

BR gerhard

8 REPLIES 8
Message 2 of 9
ferdlhons
in reply to: ferdlhons

Seems my question cannot be answered, maybe the API function which I asked for is not available?

Is there another way to create a BRep from a Mesh and then access the BRepEdges?

Thnx for any help...

Message 3 of 9

Hi I'm sure there is a solution that at least comes close. Not all features need to be accessible through the Fusion API but that doesn't have to stop you right away.

A first option would be to use 3rd party libraries (for example PyAutoGui) and simulate the user clicking on the "Generate Face Groups" button.

The second option would be to find the command directly through adsk.core.Application.userInterface and run it with the execute.

Message 4 of 9

Hi Tomas,

many thnx for your reply. For me it is not the big problem not having the 'Generate Face Groups' function (your PyAutoGui is a great idea anyway). Sorry my missleading info:

 

My problem is: I need the DATA of the face groups itself (or BRepEdge data). I don't know how to access it via python script... I tried to acess via 

design.rootComponent.bRepBodies
but those seem to be empty. How do I create them from a mesh?
Message 5 of 9

Mesh body cannot be found among BRepBodies because Mesh are not BRep from the definition of these objects.. You need to convert Mesh to BRep first and then the converted body will be available from BRepBodies.


tomasbangoA2EPZ_0-1716803189952.png

 

Message 6 of 9

Thank you very much - I think this helps 🙂

Message 7 of 9
kandennti
in reply to: ferdlhons

Hi @ferdlhons -San.

 

We have created a sample script that executes 'Generate Face Groups' using the text command.

After running the script, select MeshBody.

# Fusion360API Python script

import traceback
import adsk.core as core
import math

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

        msg: str = "Select MeshBody"
        selFilter: str = "MeshBodies"
        sel: core.Selection = select_ent(msg, selFilter)
        if not sel:
            return

        exec_mesh_calculate_faceGroups_Command(5.0, 0.1)

        ui.messageBox("Done")

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


def exec_mesh_calculate_faceGroups_Command(
        infoOpeningAngle: float = 25.0,
        infoFastSegmentArea: float = 0.2,
):
    app: core.Application = core.Application.get()

    cmds = [
        u"Commands.Start ParaMeshCalculateFaceGroupsCommand",
        u"Commands.SetString infoSegmentationMethod {}".format(
            "infoSegmentationMethodFast"
        ),
        u"Commands.SetDouble infoOpeningAngle {}".format(
            math.radians(infoOpeningAngle)
        ),
        u"Commands.SetDouble infoFastSegmentArea {}".format(
            infoFastSegmentArea
        ),
        u"NuCommands.CommitCmd",
    ]

    [app.executeTextCommand(cmd) for cmd in cmds]


def select_ent(
    msg: str,
    filter: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filter)
        return sel
    except:
        return None

1.png

Message 8 of 9
kandennti
in reply to: kandennti

@ferdlhons -San.

 

I have created a script to create a sketch of the FaceGroup boundary.

Note that this is a very slow process.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion
import math

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

        des: fusion.Design = app.activeProduct
        des.designType = fusion.DesignTypes.ParametricDesignType

        # select mesh
        msg: str = "Select MeshBody"
        selFilter: str = "MeshBodies"
        sel: core.Selection = select_ent(msg, selFilter)
        if not sel: return

        baseMesh: fusion.MeshBody = sel.entity

        # face group
        exec_mesh_calculate_faceGroups_Command(baseMesh, 5.0, 0.1)

        # mesh extract
        meshes = exec_mesh_extract(baseMesh)

        # mesh to brep
        breps = meshes_to_brepBodies(meshes)
        
        # open edges
        openEdgesLst = get_open_edges_by_bodies(breps)

        # draw edges - sketch
        comp: fusion.Component = baseMesh.parentComponent
        draw_edges_by_edgesList(comp, openEdgesLst)

        # remove
        [x.deleteMe() for x in meshes]
        breps[0].deleteMe()

        baseMesh.isLightBulbOn = True

        ui.messageBox("Done")

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


def draw_edges_by_edgesList(
        comp: fusion.Component,
        edgesLst: list[list[fusion.BRepEdge]],
) -> list[fusion.Sketch]:

    sktLst = []
    for edges in edgesLst:
        sktLst.append(
            draw_edges(comp, edges)
        )
        adsk.doEvents()

    return sktLst


def draw_edges(
        comp: fusion.Component,
        edges: list[fusion.BRepEdge],
) -> fusion.Sketch:
    
    if len(edges) < 1: return

    skt: fusion.Sketch = comp.sketches.add(
        comp.xYConstructionPlane
    )
    skt.name = "Mesh Group Open Edge"

    objs: core.ObjectCollection = core.ObjectCollection.createWithArray(
        edges
    )

    for x in edges:
        if not x.classType() == fusion.SketchLine.classType():
            print("K") 
        else:
            print("V")

    skt.isComputeDeferred = True
    skt.include(objs)
    skt.isComputeDeferred = False

    skt.isComputeDeferred = True
    for crv in skt.sketchCurves:
        crv.isReference = False
        crv.isFixed = True
    skt.isComputeDeferred = False


def get_open_edges_by_bodies(
        bodies: list[fusion.BRepBody],
) -> list[list[fusion.BRepEdge]]:

    openEdgesLst = []
    for body in bodies:
        openEdgesLst.append(
            [e for e in body.edges if e.coEdges.count < 2]
        )

    return openEdgesLst


def meshes_to_brepBodies(
        meshes: list[fusion.MeshBody],
) -> list[fusion.BRepBody]:

    if not meshes: return
    if len(meshes) < 1: return

    app: core.Application = core.Application.get()

    comp: fusion.Component = meshes[0].parentComponent
    beforeLst = list(comp.bRepBodies)

    sels: core.Selections = app.userInterface.activeSelections
    sels.clear()
    [sels.add(m) for m in meshes]

    cmds = [
        u"Commands.Start ParaMeshConvertCommand",
        u"Commands.SetString ConvertMeshInfoOperation {}".format(
            "infoOperationNewBaseFeature"
        ),
        u"Commands.SetString MeshToBREPAlgorithmInput {}".format(
            "infoMethodTriangleBased"
        ),
        u"NuCommands.CommitCmd",
    ]

    [app.executeTextCommand(cmd) for cmd in cmds]

    return [m for m in comp.bRepBodies if not m in beforeLst]


def exec_mesh_extract(
        mesh: fusion.MeshBody, 
) -> list[fusion.MeshBody]:

    if not mesh: return

    app: core.Application = core.Application.get()

    comp: fusion.Component = mesh.parentComponent
    beforeLst = list(comp.meshBodies)

    sels: core.Selections = app.userInterface.activeSelections
    sels.clear()
    sels.add(mesh)

    cmds = [
        u"Commands.Start ParaMeshExtractCommand",
        u"Commands.SetString infoType {}".format(
            "infoTypeAllFaceGroups"
        ),
        u"Commands.SetBool infoPreserveSourceBody 1",
        u"NuCommands.CommitCmd",
    ]

    [app.executeTextCommand(cmd) for cmd in cmds]

    return [m for m in comp.meshBodies if not m in beforeLst]


def exec_mesh_calculate_faceGroups_Command(
        mesh: fusion.MeshBody, 
        infoOpeningAngle: float = 25.0,
        infoFastSegmentArea: float = 0.2,
):

    app: core.Application = core.Application.get()
    sels: core.Selections = app.userInterface.activeSelections
    sels.clear()
    sels.add(mesh)

    cmds = [
        u"Commands.Start ParaMeshCalculateFaceGroupsCommand",
        u"Commands.SetString infoSegmentationMethod {}".format(
            "infoSegmentationMethodFast"
        ),
        u"Commands.SetDouble infoOpeningAngle {}".format(
            math.radians(infoOpeningAngle)
        ),
        u"Commands.SetDouble infoFastSegmentArea {}".format(
            infoFastSegmentArea
        ),
        u"NuCommands.CommitCmd",
    ]

    [app.executeTextCommand(cmd) for cmd in cmds]


def select_ent(
    msg: str,
    filter: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filter)
        return sel
    except:
        return None

1.png

Message 9 of 9
ferdlhons
in reply to: ferdlhons

Hello Kandennti-San,

this is amazing - for me as newby this is very helpful lession how to get access to the data! Thank you so much for this, I will try this today evening...

BR Gerhard

 

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

Post to forums  

Technology Administrators


Autodesk Design & Make Report