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: 

how find point on face

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
raterWBBQB
867 Views, 8 Replies

how find point on face

sk.PNG

how find point(A) on select face (TRUE/FALSE)

8 REPLIES 8
Message 2 of 9
marshaltu
in reply to: raterWBBQB

Hello,

 

Please refer to the following sample for how to judge if a point is on face or not.

 

Thanks,

Marshal

 

import adsk.core, adsk.fusion, traceback

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

    face = adsk.fusion.BRepFace.cast(ui.activeSelections.item(0).entity)
    pointA = adsk.core.Point3D.create(0, -0, 2)
    
    res, paramA = face.evaluator.getParameterAtPoint(pointA)
    res, newPoint = face.evaluator.getPointAtParameter(paramA)
    
    if pointA.isEqualToByTolerance(newPoint, 0.01):
        ui.messageBox('Point A is on the face.')
    else:
        ui.messageBox('Point A is not on the face.')

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


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 9
amargett
in reply to: marshaltu

I am trying to find a way to take a body and find a set of x,y,z coordinates on that body. I tried this method, but Fusion runs forever once I run the script and I have to restart Fusion every time I try to edit the code. 

Is there a better way to find x,y,z point coordinates on a body? 

Also, how would I get the script to quit itself once I know it will be running forever? 

Thanks, 

Ashley

Message 4 of 9
BrianEkins
in reply to: amargett

What are your requirements for the point on the face, since there are an infinite number of points on a face? If you only care that the point is on the face, I would recommend using the BRepFace.pointOnFace property.

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

This is helpful to generate one point on a face, but is there a way to use this function to generate, say, 200 different points on the same face? 

Message 6 of 9
kandennti
in reply to: amargett

Hi @amargett .

 

We have created a sample that uses SurfaceEvaluator to randomly create 200 sketch points.

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import random

POINT_COUNT = 200

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

        msg: str = 'Select Face'
        selFilter: str = 'Faces'
        sel: adsk.core.Selection = selectEnt(msg, selFilter)
        if not sel:
            return

        createPointsOnFace(
            sel.entity,
            POINT_COUNT
        )

        ui.messageBox('Done')

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


def createPointsOnFace(
    face: adsk.fusion.BRepFace,
    count: int) -> adsk.fusion.Sketch:

    # Evaluator
    eva: adsk.core.SurfaceEvaluator = face.evaluator

    # Parameter Range
    prmRange: adsk.core.BoundingBox2D = eva.parametricRange()
    prmMin: adsk.core.Point2D = prmRange.minPoint
    prmMax: adsk.core.Point2D = prmRange.maxPoint

    # Parameters On Face
    prmsOnFace = []
    while len(prmsOnFace) < count + 1:

        prmLst = [
            (random.uniform(prmMin.x, prmMax.x), random.uniform(prmMin.y, prmMax.y))
            for _ in range(count)
        ]

        prmLst = list(set(prmLst) - set(prmsOnFace))
        for x, y in prmLst:
            prm: adsk.core.Point2D = adsk.core.Point2D.create(x, y)
            if eva.isParameterOnFace(prm):
                prmsOnFace.append((x, y))

    # Points on Face
    prms = [adsk.core.Point2D.create(x, y) for x, y in prmsOnFace]
    _, pnts = eva.getPointsAtParameters(prms)

    # Draw Points
    comp: adsk.fusion.Component = face.body.parentComponent
    skt: adsk.fusion.Sketch = comp.sketches.add(comp.xYConstructionPlane)
    skt.isComputeDeferred = True
    for pnt in pnts:
        skt.sketchPoints.add(pnt)
    skt.isComputeDeferred = False

    return skt


def selectEnt(
        msg: str,
        filterStr: str) -> adsk.core.Selection:

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        sel = ui.selectEntity(msg, filterStr)
        return sel
    except:
        return None

 

1.png

Message 7 of 9
amargett
in reply to: kandennti

This was really helpful. Thank you so much! 

I was able to generate the points, but I want to save the position of these shapes for use in a different software. I am able to display them through a UI message box, but the box isn't big enough to return all the points. Is there a way to open and write txt files through the API? I tried but got the error: '[Errno 30] Read-only file system: 'pointvals.txt' fusion 360'

 

Thanks again!

Message 8 of 9
BrianEkins
in reply to: amargett

The API doesn't support writing to files but Python does. Just do a quick Google search of writing to a file with Python and you should see a lot of results.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 9 of 9
kandennti
in reply to: amargett

@amargett .

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import random
import csv

EXPORT_PATH = r'C:\temp\points.csv'
・・・

def createPointsOnFace(
・・・
    # Points on Face
    prms = [adsk.core.Point2D.create(x, y) for x, y in prmsOnFace]
    _, pnts = eva.getPointsAtParameters(prms)

    # write file  Unit Cm
    pointsLst = [p.asArray() for p in pnts]
    with open(EXPORT_PATH, 'w') as f:
        writer = csv.writer(f)
        writer.writerows(pointsLst)
・・・

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