how find point on face

how find point on face

Anonymous
1,323 Views
8 Replies
Message 1 of 9

how find point on face

Anonymous
Not applicable

sk.PNG

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

0 Likes
Accepted solutions (1)
1,324 Views
8 Replies
Replies (8)
Message 2 of 9

marshaltu
Autodesk
Autodesk
Accepted solution

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 Developer
>
Message 3 of 9

amargett
Observer
Observer

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

0 Likes
Message 4 of 9

BrianEkins
Mentor
Mentor

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
0 Likes
Message 5 of 9

amargett
Observer
Observer

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? 

0 Likes
Message 6 of 9

kandennti
Mentor
Mentor

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

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!

0 Likes
Message 8 of 9

BrianEkins
Mentor
Mentor

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

@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)
・・・
0 Likes