-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv2stl.py
90 lines (63 loc) · 4.37 KB
/
csv2stl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#Author-Simon JAMAIN
#Description-
import adsk.core, adsk.fusion, adsk.cam, traceback, csv
def saveParameterExpressions(parameterList):
savedParameterExpressions = {}
for parameterToSave in parameterList:
savedParameterExpressions[parameterToSave.name] = parameterToSave.expression
return savedParameterExpressions
def countCSVRows(fileName):
with open(fileName) as fileName:
csvReader = csv.reader(fileName)
return sum(1 for row in csvReader)
def updateParameters(parametersListToUpdate, savedParameterExpressions):
for parameterToUpdate in parametersListToUpdate:
parameterToUpdate.expression = savedParameterExpressions[parameterToUpdate.name]
def run(context):
ui = None
try:
app = adsk.core.Application.get()
currentDesign = app.activeProduct
ui = app.userInterface
csvFileDialog = ui.createFileDialog()
csvFileDialog.isMultiSelectEnabled = False
csvFileDialog.title = "select the parameters file"
csvFileDialog.filter = 'Text files (*.csv)'
if csvFileDialog.showOpen() == adsk.core.DialogResults.DialogOK:
with open(csvFileDialog.filename) as csvFile:
csvReader = csv.reader(csvFile)
parametersNames = next(csvReader)
nbVariants = countCSVRows(csvFileDialog.filename) - 1
exportFolderDialog = ui.createFolderDialog()
exportFolderDialog.title = str(nbVariants) + " parameter sets have been found, select an export directory"
if exportFolderDialog.showDialog() == adsk.core.DialogResults.DialogOK:
exportManager = currentDesign.exportManager
previousParameterExpressions = saveParameterExpressions(currentDesign.userParameters)
exportedFiles = 0
try:
for parametersRow in csvReader:
fileName = None
for index,parameterCell in enumerate(parametersRow):
parameterName = parametersNames[index]
if index == 0 and parameterName == "fileName":
fileName = parameterCell
elif index == 0 and parameterName != "fileName":
raise Exception('Process Canceled !\nThe first column of your csv file must be a fileName parameter and the values should be a unique filename for each set of parameters.\nNote: you can also have another fileName attribute column and they won\'t interfear.')
elif currentDesign.userParameters.itemByName(parameterName) is None :
raise Exception('Process Canceled !\nParameter "' + parameterName + '" does not exists in fusion, please check the parameters names of the fusion project and match them to the csv file')
else:
currentDesignParameter = currentDesign.userParameters.itemByName(parameterName)
currentDesignParameter.expression = parameterCell
fullFileName = exportFolderDialog.folder + "/" + fileName
stlExportOptions = exportManager.createSTLExportOptions(currentDesign.rootComponent, fullFileName)
stlExportOptions.sendToPrintUtility = False
exportManager.execute(stlExportOptions)
exportedFiles = exportedFiles + 1
except Exception as e:
ui.messageBox(str(e))
finally:
updateParameters(currentDesign.userParameters,previousParameterExpressions) # restore initial parameters
ui.messageBox("Successfully exported " + str(exportedFiles) + " files in " + exportFolderDialog.folder)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))