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: 

Intersection Curve from two splines

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
0xHexdec
351 Views, 4 Replies

Intersection Curve from two splines

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

4 REPLIES 4
Message 2 of 5
kandennti
in reply to: 0xHexdec

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.

Message 3 of 5
0xHexdec
in reply to: 0xHexdec

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()))

 

Message 4 of 5
MichaelT_123
in reply to: 0xHexdec

Have you tried:

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

Regards

MichaelT

MichaelT
Message 5 of 5
0xHexdec
in reply to: MichaelT_123

@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.

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