0

I am using a script in fusion360 called importsplinecsv I was wondering if it was possible to modify the script so that it would import one row every 10th row? as the amount of rows that are being imported are very large and bloating.

if I could get some help that would be awesome.

here is the text

Author-Autodesk Inc.

Description-Import spline from csv file

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 = 'Import Spline csv' if not design: ui.messageBox('No active Fusion design', title) return

    dlg = ui.createFileDialog()
    dlg.title = 'Open CSV File'
    dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
    if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
        return

    filename = dlg.filename
    with io.open(filename, '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', title)            

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

1 Answers1

0

I have not used this library before but try:

for i, line in enumerate(f):
    if i%10==0:
        then your import command here

f is your filepointer i will be the linenumber and line will be your line

 dlg = ui.createFileDialog()
    dlg.title = 'Open CSV File'
    dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
    if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
        return

    filename = dlg.filename
    with io.open(filename, 'r', encoding='utf-8-sig') as f:
        points = adsk.core.ObjectCollection.create()
        for i, line in enumerate(f):
            if i%10==0:
                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', title)            

except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
anarchy
  • 3,709
  • 2
  • 16
  • 48