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: 

Ray Intersection Function in Fusion

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
ebunn3
363 Views, 4 Replies

Ray Intersection Function in Fusion

Hi

 

I am looking for a function I used in other softwares like Solidworks that will return points of intersection between the ray and any entity that is in its path.  Does something like this exist in Fusion?   I want to do it inside of a sketch to check intersection points with sketch geometry.  I saw something in a Google search called FindNearest but could not find it in the api.   

Alternatively, I could create a line in the sketch and check the points of intersection between it and other sketch geometry?

Eric

4 REPLIES 4
Message 2 of 5
kandennti
in reply to: ebunn3

Hi @ebunn3 .

 

I first thought it was possible with the findBRepUsingRay method, but looking at the documentation, I felt that sketch entities were not supported.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7CE5A6D9-BB79-4A6C-9634-587BFF5FE077 

 

Since I am not good at English and cannot explain it well, I created a sample.

Select a sketch line, check its intersection with all curve entities in the same sketch, and exit with the closest entity selected.

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

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

        msg: str = 'Select SketchLine'
        selFiltter: str = 'SketchLines'
        sel: adsk.core.Selection = selectEnt(msg, selFiltter)
        if not sel:
            return

        ent: adsk.fusion.SketchEntity = findNearest(sel)
        if not ent:
            return

        sels: adsk.core.Selections = ui.activeSelections
        forcedClear(sels)
        sels.add(ent)

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


def findNearest(
        sel: adsk.core.Selection):

    # -- support function --
    def getSketchEntities(
            sktCrvs: adsk.fusion.SketchCurves) -> list:

        sktCrvGroups = [
            sktCrvs.sketchArcs,
            sktCrvs.sketchCircles,
            sktCrvs.sketchConicCurves,
            sktCrvs.sketchEllipses,
            sktCrvs.sketchEllipticalArcs,
            sktCrvs.sketchFittedSplines,
            sktCrvs.sketchFixedSplines,
            sktCrvs.sketchLines,
        ]

        sktEnts = []
        for group in sktCrvGroups:
            sktEnts.extend([e for e in group])

        return sktEnts

    # return [[SketchEntity, Point3D],]
    def getIntersectingEntities(
            line: adsk.fusion.SketchLine,
            sktEnts: list) -> list:

        ray: adsk.core.InfiniteLine3D = line.worldGeometry.asInfiniteLine()
        ent: adsk.fusion.SketchEntity
        intersects = []
        for ent in sktEnts:
            geo = ent.worldGeometry
            res: adsk.core.ObjectCollection = ray.intersectWithCurve(geo)
            if res.count < 1:
                continue

            intersects.append([ent, res[0]])

        return intersects

    # param Point3D,[[SketchEntity, Point3D],]
    def getNearestEntity(
            pnt: adsk.core.Point3D,
            intersects: list) -> adsk.fusion.SketchEntity:

        lst = [[e, pnt.distanceTo(p)] for e, p in intersects]

        def key_func(v):
            return v[1]

        res = min(lst, key=key_func)
        return res[0]

    # ----

    # get sketchLine, sketch
    line: adsk.fusion.SketchLine = sel.entity
    skt: adsk.fusion.Sketch = line.parentSketch

    # get all Sketch Entities
    sktEnts = getSketchEntities(skt.sketchCurves)
    sktEnts.remove(line)
    if len(sktEnts) < 1:
        return None

    # get intersection Entities & point3d
    intersects = getIntersectingEntities(line, sktEnts)
    if len(intersects) < 1:
        return None

    # get click point
    clickPnt: adsk.core.Point3D = sel.point

    # get Nearest Entity
    return getNearestEntity(clickPnt, intersects)


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


def forcedClear(self: adsk.core.Selections):
    app = adsk.core.Application.get()
    self.clear()

    if self.count > 0:
        ents = self.all
        app.executeTextCommand(u'Commands.Start SelectabilityToggleCmd')
        [self.add(ent) for ent in ents]
        app.executeTextCommand(u'Commands.Start SelectabilityToggleCmd')
        self.clear()

We have only done a simple test, so there may be errors.

 

I am looking forward to your add-ins.

Message 3 of 5
ebunn3
in reply to: kandennti

@kandennti 

 

Thank you again!!  I'll check it out.  

 

Eric

Message 4 of 5
ebunn3
in reply to: kandennti

@kandennti 

 

This will definitely work for me.  I can get the coordinates of the intersections from here and this is what I am after.  Thanks again.

 

Eric

Message 5 of 5
ebunn3
in reply to: ebunn3

@kandennti 

 

Took me a while to reverse engineer your code but I got it to work finally.  I am posting the code thus far.  Thanks for all the help with this one.

 

Eric

#Author-Bunn
#Description-

increment = .1

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        radius1 = 2
        radius2 = 0.156

        #get the application
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct

        # #get the root component
        # rootComp = design.rootComponent
        #Cast the root component (casting versus setting will get access to more intellisense)
        rootComp = adsk.fusion.Component.cast(design.rootComponent)

        #create a new sketch on the xy plane
        sketches = rootComp.sketches
        Plane = rootComp.yZConstructionPlane
        sketch = sketches.add(Plane)
        sketch.name = 'YZ Plane'

        #get the id of the cushion body
        prodBody = adsk.core.ObjectCollection.create()
        prodBody = rootComp.bRepBodies.itemByName('Pump')

        # Get sketch points
        projCrv = sketch.project(prodBody)
        sketchCurves = sketch.sketchCurves
        
        BB = sketch.boundingBox
        minBB = BB.minPoint
        maxBB = BB.maxPoint
        
        minBB = sketch.modelToSketchSpace(minBB)
        minX = minBB.x
        minY = minBB.y
        minZ = minBB.z

        maxBB = sketch.modelToSketchSpace(maxBB)
        maxX = maxBB.x
        maxY = maxBB.y
        maxZ = maxBB.z
        

        #Create Base Line on Sketch
        lines = sketch.sketchCurves.sketchLines
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(maxZ-increment , minY , 0), adsk.core.Point3D.create(maxZ-increment , maxY ,0))
        infiniteLine=line1.worldGeometry.asInfiniteLine()
        # infiniteLine : adsk.core.InfiniteLine3D = line1.worldGeometry.asInfiniteLine()

        intSects = []
        for ent in sketchCurves:
            geo = ent.worldGeometry
            res: adsk.core.ObjectCollection = infiniteLine.intersectWithCurve(geo)
            if res.count > 0: 
                intSects.append(res[0])

        for i in range(0,len(intSects)):
            print(intSects[i].x,intSects[i].y,intSects[i].z)



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

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