Skip to content

Commit 7db50e0

Browse files
committed
Updated python file
Now asks if you want to read or write to a file. It has two new functions readTheParameters and writeTheParameters.
1 parent 1c58150 commit 7db50e0

File tree

2 files changed

+83
-39
lines changed

2 files changed

+83
-39
lines changed

ReadWriteParametersFromFile_Python.py

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def notify(self, args):
124124
workspaces_ = ui.workspaces
125125
modelingWorkspace_ = workspaces_.itemById('FusionSolidEnvironment')
126126
toolbarPanels_ = modelingWorkspace_.toolbarPanels
127-
toolbarPanel_ = toolbarPanels_.item(0) # add the new command under the first panel
127+
toolbarPanel_ = toolbarPanels_.item(1) # add the new command under the secind panel
128128
toolbarControlsPanel_ = toolbarPanel_.controls
129129
toolbarControlPanel_ = toolbarControlsPanel_.itemById(commandIdOnPanel)
130130
if not toolbarControlPanel_:
@@ -179,49 +179,95 @@ def stop(context):
179179
ui.messageBox('AddIn Stop Failed:\n{}'.format(traceback.format_exc()))
180180

181181
def updateParamsFromCSV():
182-
ui = None
182+
app = adsk.core.Application.get()
183+
ui = app.userInterface
184+
185+
try:
186+
#Ask if reading or writing parameters
187+
dialogResult = ui.messageBox('Reading or writing parameters? Read = Yes, Write = No', 'Read or Write Parameters', adsk.core.MessageBoxButtonTypes.YesNoCancelButtonType, adsk.core.MessageBoxIconTypes.QuestionIconType)
188+
if dialogResult == adsk.core.DialogResults.DialogYes:
189+
readParameters = True
190+
elif dialogResult == adsk.core.DialogResults.DialogNo:
191+
readParameters = False
192+
else:
193+
return
194+
195+
fileDialog = ui.createFileDialog()
196+
fileDialog.isMultiSelectEnabled = False
197+
fileDialog.title = "Get the file to read from or the file to save the parameters to"
198+
fileDialog.filter = 'Text files (*.csv)'
199+
fileDialog.filterIndex = 0
200+
if readParameters:
201+
dialogResult = fileDialog.showOpen()
202+
else:
203+
dialogResult = fileDialog.showSave()
204+
205+
if dialogResult == adsk.core.DialogResults.DialogOK:
206+
filename = fileDialog.filename
207+
else:
208+
return
209+
210+
#if readParameters is true read the parameters from a file
211+
if readParameters:
212+
readTheParameters(filename)
213+
else:
214+
writeTheParameters(filename)
215+
216+
except:
217+
if ui:
218+
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
219+
220+
221+
def writeTheParameters(theFileName):
222+
app = adsk.core.Application.get()
223+
design = app.activeProduct
224+
225+
result = ""
226+
for _param in design.userParameters:
227+
result = result + _param.name + "," + _param.unit + "," + _param.expression + "," + _param.comment + "\n"
228+
229+
outputFile = open(theFileName, 'w')
230+
outputFile.writelines(result)
231+
outputFile.close()
232+
233+
#get the name of the file without the path
234+
pathsInTheFileName = theFileName.split("/")
235+
ui = app.userInterface
236+
ui.messageBox('Parameters written to ' + pathsInTheFileName[-1])
237+
238+
def readTheParameters(theFileName):
239+
app = adsk.core.Application.get()
240+
design = app.activeProduct
183241

184242
try:
185-
app = adsk.core.Application.get()
186-
design = app.activeProduct
187-
ui = app.userInterface
188-
189-
fileDialog = ui.createFileDialog()
190-
fileDialog.isMultiSelectEnabled = False
191-
fileDialog.title = "Select CSV file the with parameters"
192-
fileDialog.filter = 'Text files (*.csv)'
193-
fileDialog.filterIndex = 0
194-
dialogResult = fileDialog.showOpen()
195-
if dialogResult == adsk.core.DialogResults.DialogOK:
196-
filename = fileDialog.filename
197-
else:
198-
return
199-
200-
201-
# get the names of current parameters and put them in a list
202243
paramsList = []
203244
for oParam in design.allParameters:
204-
paramsList.append(oParam.name)
245+
paramsList.append(oParam.name)
205246

206247
# Read the csv file.
207-
csvFile = open(filename)
248+
csvFile = open(theFileName)
208249
for line in csvFile:
209250
# Get the values from the csv file.
210251
valsInTheLine = line.split(',')
211252
nameOfParam = valsInTheLine[0]
212-
valueOfParam = valsInTheLine[1]
213-
# if the name of the paremeter is not an existing parameter
253+
unitOfParam = valsInTheLine[1]
254+
expressionOfParam = valsInTheLine[2]
255+
comentOfParamFromFile = valsInTheLine[3]
256+
#need to remove the return character from the comment
257+
commentOfParamList = comentOfParamFromFile.split("\n")
258+
commentOfParam = commentOfParamList[0]
259+
# if the name of the paremeter is not an existing parameter add it
214260
if nameOfParam not in paramsList:
215-
valInput_Param = adsk.core.ValueInput.createByString(valueOfParam)
216-
design.userParameters.add(nameOfParam, valInput_Param, 'mm', 'Comment')
217-
#create a new parameter
261+
valInput_Param = adsk.core.ValueInput.createByString(expressionOfParam)
262+
design.userParameters.add(nameOfParam, valInput_Param, unitOfParam, commentOfParam)
263+
#update the values of existing parameters
218264
else:
219265
paramInModel = design.userParameters.itemByName(nameOfParam)
220-
paramInModel.expression = valueOfParam
221-
222-
ui.messageBox('Finished adding parameters')
223-
224-
except Exception as error:
266+
paramInModel.unit = unitOfParam
267+
paramInModel.expression = expressionOfParam
268+
paramInModel.comment = commentOfParam
269+
ui = app.userInterface
270+
ui.messageBox('Finished reading and updating parameters')
271+
except:
225272
if ui:
226-
ui.messageBox('Failed : ' + str(error))
227-
#ui.messageBox('Failed:\n{}'.format(traceback.forma​t_exc()))
273+
ui.messageBox('AddIn Stop Failed:\n{}'.format(traceback.format_exc()))
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Height,1.1
2-
Length,2.2
3-
Width,3.3
4-
Depth,4.11
5-
Size,5.1
6-
WingSpan,6.1
1+
Width,mm,5.1 mm,My Width
2+
Height,mm,7.2 mm,My Height
3+
length,mm,4.3 mm,My Length
4+
WallThickness,mm,124 mm,My wall thickness

0 commit comments

Comments
 (0)