Tello3 / NL2SplineTrackExtractor

A tool to extract individual track rails from No Limits 2 Spline Data
MIT License
1 stars 0 forks source link

A script to open all CSV files in a selected folder within Fusion360 #2

Open DishItDash opened 9 months ago

DishItDash commented 9 months ago

I was annoyed with how Fusion made me run the script a bunch. This was generated with ChatGPT and let me import the extracted splines super fast.

Author-Autodesk Inc.

Modified to import multiple CSV files from a user-selected folder

Description-Import splines from multiple csv files in a selected folder

import adsk.core, adsk.fusion, traceback import io, os

def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface product = app.activeProduct design = adsk.fusion.Design.cast(product) title = 'Import Spline csv' if not design: ui.messageBox('No active Fusion design', title) return

    # Dialog to select folder
    folderDlg = ui.createFolderDialog()
    folderDlg.title = 'Select CSV Folder'

    if folderDlg.showDialog() != adsk.core.DialogResults.DialogOK:
        return

    folderPath = folderDlg.folder

    files = os.listdir(folderPath)
    csv_files = [f for f in files if f.endswith('.csv')]

    for file in csv_files:
        filePath = os.path.join(folderPath, file)
        with io.open(filePath, 'r', encoding='utf-8-sig') as f:
            points = adsk.core.ObjectCollection.create()
            line = f.readline()
            data = []
            while line:
                pntStrArr = line.split(',')
                for pntStr in pntStrArr:
                    try:
                        data.append(float(pntStr))
                    except:
                        break

                if len(data) >= 3:
                    point = adsk.core.Point3D.create(data[0], data[1], data[2])
                    points.add(point)
                line = f.readline()
                data.clear()
        if points.count:
            root = design.rootComponent
            sketch = root.sketches.add(root.xYConstructionPlane)
            sketch.sketchCurves.sketchFittedSplines.add(points)
        else:
            ui.messageBox('No valid points in ' + file, title)

except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
DishItDash commented 9 months ago

I'm not sure how to force a code block so the code is incomplete in the posting. Sorry about that, I'm totally new to everything.