Insert Obj. file

Insert Obj. file

Anonymous
Not applicable
2,579 Views
13 Replies
Message 1 of 14

Insert Obj. file

Anonymous
Not applicable

Hi,

 

I am trying to import a triangular mesh obj. file (screenshot below) into Fusion and convert it to BRep using the API. I have validated the workflow manually in Fusion UI, now trying to recreate the whole workflow through a script to achieve automation. However, I couldn't find sample script or forum thread that shows how to do this in API. Please, can anyone help to provide a sample Python code for obj. file import and conversion to BRep?

 

Many thanks.

Capture.PNG

2,580 Views
13 Replies
Replies (13)
Message 2 of 14

amichaudJ6E3R
Contributor
Contributor

I want to do the exact same thing. If anyone read this, a starting point would be greatly helpful 🙂

0 Likes
Message 3 of 14

kandennti
Mentor
Mentor

Hi @amichaudJ6E3R .

 

I made a sample that imports an OBJ file and converts it to BRepBody.
Not checked, but the mesh must be Quad mesh.

# Fusion360API Python script  by kantoku
# Mesh2BRep sample
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

        # select files
        paths = selectFiles('select OBJ file')
        if not paths: return

        # new doc
        _app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        des  :adsk.fusion.Design = _app.activeProduct
        des.designType = adsk.fusion.DesignTypes.ParametricDesignType
        root :adsk.fusion.Component = des.rootComponent

        # baseFeature
        baseFeatures = root.features.baseFeatures
        baseFeature = baseFeatures.add()

        baseFeature.startEdit()
        
        # import obj files
        meshs = importMesh(paths ,root.meshBodies, baseFeature)

        # Mesh2BRep
        execMesh2BRep(meshs)

        baseFeature.finishEdit()

        # finish
        _ui.messageBox('Done')

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

def execMesh2BRep(
    meshBodies):

    # select meshBody
    for quadMesh in meshBodies:
        _ui.activeSelections.add(quadMesh)

    # show Mesh2BRep Command dialog
    _app.executeTextCommand(u'Commands.Start Mesh2BRepCommand')

    # push OK button
    _app.executeTextCommand(u'NuCommands.CommitCmd')


def importMesh(
    paths :list,
    meshBodies :adsk.fusion.MeshBodies,
    baseFeature :adsk.fusion.BaseFeature) -> list:

    unitCm = adsk.fusion.MeshUnits.CentimeterMeshUnit
    bodies = []

    for path in paths:
        meshLst = meshBodies.add(path, unitCm, baseFeature)
        [bodies.append(mesh) for mesh in meshLst]

    return bodies


def selectFiles(
    msg :str):

    fileDlg = _ui.createFileDialog()
    fileDlg.isMultiSelectEnabled = True
    fileDlg.title = msg
    fileDlg.filter = '*.obj'
    
    dlgResult = fileDlg.showOpen()
    if dlgResult == adsk.core.DialogResults.DialogOK:
        return fileDlg.filenames
Message 4 of 14

amichaudJ6E3R
Contributor
Contributor

That's great!

 

I got a few questions for you though,

 

1. I thought that to transform a mesh to B-Rep, we had to deactivate the history recording?

 

2. Is it Mesh2Rep that needs to be under 10,000 faces, or Quad to T-Splines

 

3. If I wanted to had a convert "Quad to T-Spline" before the Mesh to B-Rep, I'd create a Def and call it in between these two?

 

        # import obj files
        meshs = importMesh(paths ,root.meshBodies, baseFeature)

        # Mesh2BRep
        execMesh2BRep(meshs)

 

 

Thank you so much for your help!

0 Likes
Message 5 of 14

amichaudJ6E3R
Contributor
Contributor

Sorry,

 

my last question was unclear. Since I first posted my first message, I discovered that I get way better results by converting my mesh to T-Spline first (Quad to T-Splines), then T-Splines to B-Rep.

 

So ultimately, that is what I'm aiming for 🙂

0 Likes
Message 6 of 14

BrianEkins
Mentor
Mentor

The API doesn't support any of the current mesh functionality besides the ability to import a mesh.  Some of the code above attempts to work around this by directly calling the mesh related commands.  In this case, that's the best you can do but it's limited in functionality.

 

I guess I'm just saying if you're waiting for someone to post a sample program that does what you described in your initial post, you'll be waiting a long time.  Hopefully in the future there will be some mesh functionality exposed through the API but it's not there today.

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

kandennti
Mentor
Mentor

I have summarized the method of using TextCommands.

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

 

Although not all, things that can be operated with a GUI increase the possibility of processing.
However, development is very difficult because there is almost no information.

0 Likes
Message 8 of 14

amichaudJ6E3R
Contributor
Contributor

Hi,

 

Thank you for your help.

 

So I've read through the page you linked and spent the night trying to make sense of it all. It's  a little bit beyond my understand of the whole thing. I'm like a dog chasing his own tail

 

So using the Text Commands inside Fusion, I found that the...thing (for lack of better words) I am looking for would be

TSpline2BRepCommand

 

So in the code above, if I replace the Mesh2BRep by TSpline2BRepCommand, I load up the menu that I want. Now the problem I have is to access the drop down menus. Basically, I open the menu "Convert" that I need, but nothing happens.

 

I tried stuff like Commands.Select, Commands.Set, Commands.SetGroup

 

But because I don't really know what I'm doing, I just end up running in circles

0 Likes
Message 9 of 14

amichaudJ6E3R
Contributor
Contributor

Hi Brian,

 

thank you for replying.

 

Well I am not necessarily looking for someone to give me the final solution.

 

While I was looking into my problem, I seemed to recall one of your videos a long time ago where you mentioned that Forms weren't supported by the API, but that was a long time ago.

 

I guess I was looking for the confirmation it was still the case. Nowhere I've looked online was I able to actually get a clear answer. Maybe I don't type the proper search words but I find it hard to get any kind of answers, or help regarding Fusion's API

0 Likes
Message 10 of 14

kandennti
Mentor
Mentor

I tried a little.
I haven't found a way to switch "DropDownCommandInput" yet.
Therefore, I cannot reach the goal.

 

I'm sorry I can't help you.

0 Likes
Message 11 of 14

kandennti
Mentor
Mentor

I tried a little more. I think this is done.
Of course, the condition is Quad mesh.

# Fusion360API python script  by kantoku
# Quad2TSpline sample
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

        # select files
        paths = selectFiles('select OBJ file')
        if not paths: return

        # new doc
        _app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        des  :adsk.fusion.Design = _app.activeProduct
        des.designType = adsk.fusion.DesignTypes.ParametricDesignType
        root :adsk.fusion.Component = des.rootComponent

        # import obj files
        meshs = importMesh(paths ,root.meshBodies)

        # form start
        formFeature = root.features.formFeatures.add()
        formFeature.startEdit()

        # Quad2TSpline
        execQuad2TSpline(meshs)

         # form finish
        formFeature.finishEdit()

        # finish
        _ui.messageBox('Done')

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


def execQuad2TSpline(
    meshBodies):

    # select meshBody
    for quadMesh in meshBodies:
        _ui.activeSelections.add(quadMesh)

    # show TSpline2BRep Command dialog
    _app.executeTextCommand(u'Commands.Start TSpline2BRepCommand')

    # push OK button
    _app.executeTextCommand(u'NuCommands.CommitCmd')


def importMesh(
    paths :list,
    meshBodies :adsk.fusion.MeshBodies) -> list:

    unitCm = adsk.fusion.MeshUnits.CentimeterMeshUnit
    bodies = []

    for path in paths:
        meshLst = meshBodies.add(path, unitCm)
        [bodies.append(mesh) for mesh in meshLst]

    return bodies


def selectFiles(
    msg :str):

    fileDlg = _ui.createFileDialog()
    fileDlg.isMultiSelectEnabled = True
    fileDlg.title = msg
    fileDlg.filter = '*.obj'
    
    dlgResult = fileDlg.showOpen()
    if dlgResult == adsk.core.DialogResults.DialogOK:
        return fileDlg.filenames
Message 12 of 14

amichaudJ6E3R
Contributor
Contributor

This is amazing!

 

So just to know if I understand correctly. You first import the object, then open a new "form object", put the mesh in, which automatically transforms it into TSpline, then convert those splines into a BRep, is that it?

 

This is pretty cool. Thanks a lot

0 Likes
Message 13 of 14

amichaudJ6E3R
Contributor
Contributor

So I just saw this previous post from Brian Ekins saying that Fusion automatically transforms Splines to BRep once you leave Form mode. So you basically had transform the mesh to Tspline and the second conversation was done by itself. Right?

0 Likes
Message 14 of 14

kandennti
Mentor
Mentor

That's right. It is processed in this way.


QuadMesh → TSpline : TSpline2BRepCommand
TSpline → BRepBody : formFeature.finishEdit()