Export profile sketch points to CSV

There is a Python script part of the Fusion 360 installation that can import points from a CSV file into a spline:
ImportSpline
We could use the source code of that as a starting point to implement the reverse: export the points of a spline.

In this case, however, I’m changing it to export the points of a profile instead: one consisting of many short sketch lines forming a closed loop.
Profile
Because it’s a closed loop, therefore it’s enough to just export the coordinates of the start sketch point of each line as we iterate through the sketch lines. 

import adsk.core, adsk.fusion, traceback
import io
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui  = app.userInterface
# Get all components in the active design.
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
title = 'Export Spline csv'
if not design:
ui.messageBox('No active Fusion design', title)
return
if app.userInterface.activeSelections.count != 1:
ui.messageBox('Select profile before running command', title)
return
profile = app.userInterface.activeSelections.item(0).entity
if not type(profile) is adsk.fusion.Profile:
ui.messageBox('Selected object needs to be a profile', title)
return
dlg = ui.createFileDialog()
dlg.title = 'Save CSV File'
dlg.filter = 'Comma Separated Values (*.csv)'
if dlg.showSave() != adsk.core.DialogResults.DialogOK :
return
filename = dlg.filename
with io.open(filename, 'w', encoding='utf-8-sig') as f:
profile = adsk.fusion.Profile.cast(profile)
loop = profile.profileLoops.item(0)
curves = loop.profileCurves
for curve in curves:
curve = adsk.fusion.ProfileCurve.cast(curve)
entity = curve.sketchEntity
point = entity.startSketchPoint
point = adsk.fusion.SketchPoint.cast(point)
geometry = point.geometry
f.write(str(geometry.x) + "," + str(geometry.y) + "n")
except:
if ui:
ui.messageBox('Failed:n{}'.format(traceback.format_exc()))

In order to use the above code, you can simply create a new Python Script in the “Scripts and Add-Ins” dialog and replace the code in the generated Python file with the one shown above – see Creating a Script or Add-In  

Here is the result produced by the script:

TestCsv

-Adam


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading