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.