Create Mesh Section Sketch API calls.

Create Mesh Section Sketch API calls.

mpieper
Contributor Contributor
1,631 Views
11 Replies
Message 1 of 12

Create Mesh Section Sketch API calls.

mpieper
Contributor
Contributor

Hello,

 

The create mesh section sketch feature is extremely useful and I'd like to be able to take advantage of it from some of my scripts.

 

Any pointers on the calls to make to enact the same features that "Create Mesh Section Sketch" implements?  I was hoping it was just a simple function call, but I can't seem to find it.

 

Alternatively, seeing the function calls that fusion360 makes internally while you're working with an object would provide the insight I'm looking for.

 

Thanks

Matt

0 Likes
Accepted solutions (1)
1,632 Views
11 Replies
Replies (11)
Message 2 of 12

kandennti
Mentor
Mentor
Accepted solution

Hi @mpieper .

 

Since the API does not provide "Create Mesh Section Sketch", we have created a sample using the Text Commands.

 

After the script is executed, multiple Mesh Section Sketches will be created along the guide curve by specifying the mesh body and the guide curve (the curve of the sketch).

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

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

        # mesh body
        msg :str = 'Select MeshBody'
        selFiltter :str = 'MeshBodies'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return
        mesh :adsk.fusion.MeshBody = sel.entity

        # guide curve
        msg :str = 'Select Guide Curve'
        selFiltter :str = 'SketchCurves,SketchLines,SketchCircles'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return
        guideCrv :adsk.fusion.SketchEntity = sel.entity

        # mesh sections
        createMeshPlanarSections(mesh, guideCrv)

        # finish
        ui.messageBox('Done')

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


def createMeshPlanarSections(
    mesh :adsk.fusion.MeshBody,
    guideCrv :adsk.fusion.SketchEntity,
    count :int = 10):

    # --Support function--
    def createMeshSection(
        mesh :adsk.fusion.MeshBody,
        plane :adsk.fusion.ConstructionPlane):

        app = adsk.core.Application.get()
        ui = app.userInterface
        sels = ui.activeSelections

        app.executeTextCommand(u'Commands.Start MeshPlanarSectionCommand')

        app.executeTextCommand(u'UI.EnableCommandInput MeshSectionBodyInput')
        sels.add(mesh)

        app.executeTextCommand(u'UI.EnableCommandInput MeshSectionPlaneInput')
        sels.add(plane)

        app.executeTextCommand(u'NuCommands.CommitCmd')
    # ----

    comp :adsk.fusion.Component = guideCrv.parentSketch.parentComponent

    # create ValueInput
    ValIpt = adsk.core.ValueInput
    valIpts = [ValIpt.createByReal(v/count) for v in range(count+1)]

    # create Section
    planes :adsk.fusion.ConstructionPlanes = comp.constructionPlanes
    planeIpt :adsk.fusion.ConstructionPlaneInput = planes.createInput()

    for retio in valIpts:
        # Plane
        planeIpt.setByDistanceOnPath(guideCrv, retio)
        plane :adsk.fusion.ConstructionPlane = planes.add(planeIpt)
        plane.isLightBulbOn = False

        # Mesh Section
        createMeshSection(mesh, plane)

        adsk.doEvents()

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

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

1.png

 

Message 3 of 12

mpieper
Contributor
Contributor
sel :adsk.core.Selection = selectEnt(msg ,selFiltter) should be:
self :adsk.core.Selection = selectEntity(msg, selFilter)
0 Likes
Message 4 of 12

mpieper
Contributor
Contributor

Thank you very much for pointing me in the right direction / showing the use of text commands.  They are very poorly documented (not at all!).  Do you know of anywhere where there's more documentation on them?

 

Getting further down the rabbit hole:

In order to make the intersections useful I need a way of generating fitted splines from the mesh intersections and then getting their control points.  There's a tool "Fit Curves to Mesh Section" that performs what I'm looking for, but once again an API call is lacking.

 

If I manually add a closed fitted spine on the intersecting curve, getting the control points is done by:

 

control_points = sketch.sketchCurves.sketchFittedSplines[0].controlPoints

for point in control_points:

    point.x

    point.y

    point.z

 

and from there you can make a subset / generate new splines etc.

 

If you could point me in the right direction for generating fittedSplines from the same text-driven interface it would be a boon.

 

Many thanks

0 Likes
Message 5 of 12

fheidinga
Enthusiast
Enthusiast

Hi,

I came across this script you wrote because this is exactly the functionality I am looking for to analyse a complicate mesh body. However, when I run the script the following error message (see below). Would you have any idea how to fix this?
Thanks!

Failed:

Traceback (most recent call last):

File "/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Mesh section/Mesh section.py", line 25, in run

createMeshPlanarSections(mesh, guideCrv)

File "/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Mesh section/Mesh section.py", line 77, in createMeshPlanarSections

createMeshSection(mesh, plane)

File "/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Mesh section/Mesh section.py", line 49, in createMeshSection

app.executeTextCommand(u'Commands.Start MeshPlanarSectionCommand')

File "/Application Support/Autodesk/webdeploy/production/6a4ee1e135d539f7735a031beb7a5c1c01c7a76f/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/core.py", line 3690, in executeTextCommand

return _core.Application_executeTextCommand(self, command)

RuntimeError: 3 : There is no command MeshPlanarSectionCommand. Use ? to get help on available commands

 

0 Likes
Message 6 of 12

kandennti
Mentor
Mentor

@fheidinga .

 

Mesh changed its command ID when it switched from a preview function to an official function. Therefore, old command IDs will result in an error.

Please change the createMeshSection function to the following

    # --Support function--
    def createMeshSection(
        mesh :adsk.fusion.MeshBody,
        plane :adsk.fusion.ConstructionPlane):

        app = adsk.core.Application.get()
        ui = app.userInterface
        sels = ui.activeSelections

        app.executeTextCommand(u'Commands.Start ParaMeshPlanarSectionCommand')

        app.executeTextCommand(u'UI.EnableCommandInput infoBodyToModify')
        sels.add(mesh)

        app.executeTextCommand(u'UI.EnableCommandInput planeSelectionInfo')
        sels.add(plane)

        app.executeTextCommand(u'NuCommands.CommitCmd')
    # ----
Message 7 of 12

fheidinga
Enthusiast
Enthusiast

Works beautifully now! Thanks for the fast reply!

Message 8 of 12

fheidinga
Enthusiast
Enthusiast

I noticed the section planes are not stacked according to the 'plane along path' command (for instance with a spline curve of 10mm lengt, 10 planes along the path of the curve every 1mm). Is there any way to do this?

0 Likes
Message 9 of 12

kandennti
Mentor
Mentor

@fheidinga .

 

Currently, the API only supports ratios for the setByDistanceOnPath method, which creates a plane.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-46dd5f0a-e384-4707-b431-37c0e596f328 

 

It may be possible to create a plane by using a text command, but for now, try adjusting it by the number of planes to create.

・・・
        # mesh sections
        count = int(guideCrv.length // 0.1)
        createMeshPlanarSections(mesh, guideCrv, count)

        # finish
        ui.messageBox('Done')
・・・
Message 10 of 12

fheidinga
Enthusiast
Enthusiast

It works with a simple mesh shape (a cilinder that I 'tesselated' into a mesh) but with a 3D scan the planes do not follow that path, strange... I will play with it a little bit further....Scherm­afbeelding 2023-02-21 om 10.29.33.png

0 Likes
Message 11 of 12

kandennti
Mentor
Mentor

@fheidinga .

 

Since we did not have the 3D scan data at hand, we borrowed this data to try it out.

https://grabcad.com/library/human-femur-1 

 

1.png

I didn't notice any problems, and I feel that the 3D scan data and the CAD-generated mesh data are the same.

 

Message 12 of 12

fheidinga
Enthusiast
Enthusiast

Thanks for checking! I see what's going on, I have been working for quite some time on this scan/model in the Front view orientation and assumed that the slices were taken in this perpendicular to this plane/direction too. However, they were made perpendicular to the Top view plane. Turning the scan 90 degrees solved the problem! Pretty silly I had not checked this before 😉 Thanks for the effort!Scherm­afbeelding 2023-02-22 om 07.44.15.pngScherm­afbeelding 2023-02-22 om 07.44.23.png

0 Likes