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: 

Trouble with intersectWithCurve on sketch circles

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
ebunn3
236 Views, 2 Replies

Trouble with intersectWithCurve on sketch circles

Hi,

 

I've posted code below.  When you use intersectWithCurve on a sketch circle it only returns one point since it is just a single closed curve.  I need the opposite point as well.  Is there a way to accomplish this?  I tried converting the circle to a nurbs curve and it returned no intersections?

 

ebunn3_0-1634578323839.png

 

Thanks in advance.

 

Eric

 

#Author-Autodesk Inc.
#Description-Demo command input examples
import adsk.core, adsk.fusion, traceback


def sketchLIntersect():
    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 None
        return ent
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

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

    # 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
    return intersects

intersects = sketchLIntersect()
print(intersects)

# Arc3DCurveType	1	Transient 3D arc.
# Circle3DCurveType	2	Transient 3D circle.
# Ellipse3DCurveType	3	Transient 3D ellipse.
# EllipticalArc3DCurveType	4	Transient 3D elliptical arc.
# InfiniteLine3DCurveType	5	Transient 3D infinite line.
# Line3DCurveType	0	Transient 3D line segment.
# NurbsCurve3DCurveType	6	Transient 3D NURBS curve.

 

 

2 REPLIES 2
Message 2 of 3
BrianEkins
in reply to: ebunn3

I created a small test to try and reproduce the problem but it's working correctly for me.

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

        # Have a sketch line selected and get the Line geometry from the sketch line.
        skLine = ui.selectEntity('Select a sketch line.', 'SketchLines').entity
        lineGeom: adsk.core.Line3D = skLine.worldGeometry
        lineGeom = lineGeom.asInfiniteLine()

        # Have a sketch circle selected.        
        skCircle = ui.selectEntity('Select a sketch circle.', 'SketchCircles').entity
        circleGeom: adsk.core.Circle3D = skCircle.worldGeometry

        # Intersect the circle with the line.
        intPoints = lineGeom.intersectWithCurve(circleGeom)

        # Draw a circle at each intersection point to validate the results.
        sk: adsk.fusion.Sketch = skLine.parentSketch
        circles = sk.sketchCurves.sketchCircles
        for intPoint in intPoints:
            circles.addByCenterRadius(intPoint, 0.5)            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

I think the problem is this line in your code:

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

 

You're just saving the first intersection point into the list.

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3
ebunn3
in reply to: BrianEkins

Brian,

 

Thank you!  Good catch.  That line was definitely the problem.  I pulled that code from another example whose goal, I believe, was to catch only the first intersection.  Quick fix thanks.

 

Eric

intersects.append([ent, res[0]])
#change to
intersects.append([ent, res])

 

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