Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble with batch import and Loft of csv files

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
mychinaorders
436 Views, 2 Replies

Trouble with batch import and Loft of csv files

Hello everybody,

 

as the title says I try to import around 200 csv Files in to fusion and then Automatically loft them. As I have not found any script that does a batch import and loft I tried to write my own with limited succes. I managed to import all csv files with modding the importCSV script but I struggle to get them in to a Loft. Here is My Code so far:

 

#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)
        rootComp = design.rootComponent
        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


        for i in range(1,202):#Loop for csv import

         filename = 'C:/*privacy*/layer{}.csv'.format(i)

         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:
            design.rootComponent.sketches.add(design.rootComponent.xYConstructionPlane).sketchCurves.sketchFittedSplines.add(points)
         else:
            ui.messageBox('No valid points', title)            
        

        loftFeats = rootComp.features.loftFeatures
        loftInput = loftFeats.createInput(adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        loftSectionsObj = loftInput.loftSections

        for j in range(0,100):#Loop for selecting the scetch profiles for the loft
           
         profile = design.rootComponent.sketches.add(rootComp.xYConstructionPlane).profiles.item(j)
         loftSectionsObj.add(profile)
          
     

        loftInput.isSolid = False

        # Create loft feature
        loftFeats.add(loftInput)



#design.rootComponent.sketches.add(rootComp.xZConstructionPlane).profiles.item(0)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


 I get the Error message Bad Index parameter at line 63. Does annybody know why that is or knows a script that does what I try to implement?

 

Have a nice Day and Thanks for any help!

Labels (4)
2 REPLIES 2
Message 2 of 3
BrianEkins
in reply to: mychinaorders

Is it successfully importing the points and creating the sketches with the splines?  If it is getting that far, the problem is in your where you're getting the profile.  You're creating new sketches rather that getting the profile from the sketches you just made.

 

I made some modifications to your code but with the csv file am unable to test it.  At the top I'm creating a new list called "sketches".  Later where you were creating the splines, I create the sketch as a separate step step, create the spline, and then add the sketch to the list.  And then a big change to where you're populating the loft sections where I iterate over the sketches that were created and get the first profile from each sketch.

        sketches = []
        for i in range(1,202):#Loop for csv import
            filename = 'C:/*privacy*/layer{}.csv'.format(i)

            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:
                sketch = design.rootComponent.sketches.add(design.rootComponent.xYConstructionPlane)
                sketch.sketchCurves.sketchFittedSplines.add(points)
                sketches.append(sketch)
            else:
                ui.messageBox('No valid points', title)            
        
        loftFeats = rootComp.features.loftFeatures
        loftInput = loftFeats.createInput(adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        loftSectionsObj = loftInput.loftSections

        for sketch in skethes:
            profile = sketch.profiles.item(0)
            loftSectionsObj.add(profile)

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3
mychinaorders
in reply to: BrianEkins

Thank you it works like a charm!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report