Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Intersection Curve from two splines

0xHexdec
Contributor Contributor
803 Views
4 Replies
Message 1 of 5

Intersection Curve from two splines

0xHexdec
Contributor
Contributor

Hey all,

I am trying to get the intersection curve from two splines via API. 

I just want to reproduce the result I am getting when I use the "Intersection Curve" Command in the Sketch workspace, but I can't find it anywhere.

cheers

Reply
Reply
0 Likes
Accepted solutions (1)
804 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor
Accepted solution

Hi @0xHexdec .

 

Since the API did not seem to provide the functionality, we used a text command.

Please make the attached f3d file active and run the following script.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        crv1: adsk.fusion.SketchEntity = root.sketches[0].sketchCurves[0]
        crv2: adsk.fusion.SketchEntity = root.sketches[1].sketchCurves[0]

        intersectionCurve: adsk.fusion.SketchEntity = execIntersectionCurve(crv1, crv2)

        ui.messageBox('Done')

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


def execIntersectionCurve(
    crv1: adsk.fusion.SketchEntity,
    crv2: adsk.fusion.SketchEntity,
    skt: adsk.fusion.Sketch = None,) -> adsk.fusion.SketchEntity:

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

    if not skt:
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent
        skt = root.sketches.add(root.xYConstructionPlane)

    sktEntsCount = skt.sketchCurves

    sels.add(skt)
    app.executeTextCommand(u'Commands.Start SketchActivate')

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

    sels.add(crv1)
    sels.add(crv2)

    try:
        app.executeTextCommand(u'NuCommands.CommitCmd')
    except:
        pass
    finally:
        app.executeTextCommand(u'NaFusionUI.SketchStopCmd')

    if sktEntsCount == skt.sketchCurves.count:
        return None
    else:
        return skt.sketchCurves[-1]

 

If you select a curve for which the command succeeds in the GUI, it is OK, but if you select a curve for which the command fails, you will get an error.
I need some logic to determine if it will fail beforehand, but I haven't come up with it.

Reply
Reply
2 Likes
Message 3 of 5

0xHexdec
Contributor
Contributor

As the solution above by @kandennti was a good, valid, and very clean but only works in scripts and not in Add-Ins due to the weirdness of TextCommands (see: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/creating-a-custom-command-dialog-then-runn...) I implemented a workaround by projecting to a surface. The result is almost the same except the project gets polluted by surface bodys. But at least this works reliably under all circumstances.

Open the attached .f3d file and run the script.

 

import traceback

import adsk.core
import adsk.fusion


def run(context):
    ui = adsk.core.UserInterface.cast(None)
    app = adsk.core.Application.get()
    try:
        rootComponent = adsk.fusion.Design.cast(app.activeProduct).rootComponent
        # finding Top Spline
        top_spline_sketch = rootComponent.sketches.itemByName("Top_Spline")
        top_spline = top_spline_sketch.sketchCurves.sketchControlPointSplines.item(0)
        # finding Side Spline
        side_spline_sketch = rootComponent.sketches.itemByName("Side_Spline")
        side_spline = side_spline_sketch.sketchCurves.sketchControlPointSplines.item(0)

        extrudes = rootComponent.features.extrudeFeatures

        # define a path from the sketch curves
        path = rootComponent.features.createPath(top_spline, False)

        # set parameters to extrude surfaces according to the profileCollection
        extrudeInput = extrudes.createInput(path, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extrudeInput.setSymmetricExtent(adsk.core.ValueInput.createByReal(50), False, adsk.core.ValueInput.createByReal(0))

        # indiciate that the extrude should be a surface, not a solid
        extrudeInput.isSolid = False

        # extrude and get the created body
        body = extrudes.add(extrudeInput).bodies.item(0)
        newSketch: adsk.fusion.Sketch = rootComponent.sketches.add(rootComponent.xYConstructionPlane)
        newSketch.name = "Intersection Curve"
        ret = newSketch.projectToSurface([body.faces.item(0)], [side_spline], adsk.fusion.SurfaceProjectTypes.AlongVectorSurfaceProjectType, rootComponent.xConstructionAxis)
        body.isLightBulbOn = False

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

 

Reply
Reply
1 Like
Message 4 of 5

MichaelT_123
Advisor
Advisor

Have you tried:

(returnValue, intersectingCurves, intersectionPoints) = sketchCurve_var.intersections(sketchCurves)

Regards

MichaelT

MichaelT
Reply
Reply
0 Likes
Message 5 of 5

0xHexdec
Contributor
Contributor

@MichaelT_123 

Thanks for the reply, but your solution does not create an intersection curve, it checks for curve intersections

The "Intersection Curves" Tool in Fusion's Sketch Environment creates a 3-Dimensional line based on 2 orthogonal 2D sketchlines.

Reply
Reply
1 Like