Message 1 of 9

Not applicable
03-07-2018
10:14 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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()))
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
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.
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?
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
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!
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.
# 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)
・・・