1+ #Author-Wayne Brill
2+ #Description-Allows you to select a CSV (comma seperated values) file and then edits existing Attributes. Also allows you to write parameters to a file
3+
4+ import adsk .core , adsk .fusion , traceback
5+
6+ commandIdOnQAT = 'ParamsFromCSVOnQAT'
7+ commandIdOnPanel = 'ParamsFromCSVOnPanel'
8+
9+ # global set of event handlers to keep them referenced for the duration of the command
10+ handlers = []
11+
12+ def commandDefinitionById (id ):
13+ app = adsk .core .Application .get ()
14+ ui = app .userInterface
15+ if not id :
16+ ui .messageBox ('commandDefinition id is not specified' )
17+ return None
18+ commandDefinitions_ = ui .commandDefinitions
19+ commandDefinition_ = commandDefinitions_ .itemById (id )
20+ return commandDefinition_
21+
22+ def commandControlByIdForQAT (id ):
23+ app = adsk .core .Application .get ()
24+ ui = app .userInterface
25+ if not id :
26+ ui .messageBox ('commandControl id is not specified' )
27+ return None
28+ toolbars_ = ui .toolbars
29+ toolbarQAT_ = toolbars_ .itemById ('QAT' )
30+ toolbarControls_ = toolbarQAT_ .controls
31+ toolbarControl_ = toolbarControls_ .itemById (id )
32+ return toolbarControl_
33+
34+ def commandControlByIdForPanel (id ):
35+ app = adsk .core .Application .get ()
36+ ui = app .userInterface
37+ if not id :
38+ ui .messageBox ('commandControl id is not specified' )
39+ return None
40+ workspaces_ = ui .workspaces
41+ modelingWorkspace_ = workspaces_ .itemById ('FusionSolidEnvironment' )
42+ toolbarPanels_ = modelingWorkspace_ .toolbarPanels
43+ toolbarPanel_ = toolbarPanels_ .item (0 )
44+ toolbarControls_ = toolbarPanel_ .controls
45+ toolbarControl_ = toolbarControls_ .itemById (id )
46+ return toolbarControl_
47+
48+ def destroyObject (uiObj , tobeDeleteObj ):
49+ if uiObj and tobeDeleteObj :
50+ if tobeDeleteObj .isValid :
51+ tobeDeleteObj .deleteMe ()
52+ else :
53+ uiObj .messageBox ('tobeDeleteObj is not a valid object' )
54+
55+ def run (context ):
56+ ui = None
57+ try :
58+ commandName = 'CSV Parameters'
59+ commandDescription = 'Parameters from CSV File'
60+ commandResources = './resources'
61+
62+ app = adsk .core .Application .get ()
63+ ui = app .userInterface
64+
65+ class CommandExecuteHandler (adsk .core .CommandEventHandler ):
66+ def __init__ (self ):
67+ super ().__init__ ()
68+ def notify (self , args ):
69+ try :
70+ updateParamsFromCSV ()
71+ except :
72+ if ui :
73+ ui .messageBox ('command executed failed:\n {}' .format (traceback .format_exc ()))
74+
75+ class CommandCreatedEventHandlerPanel (adsk .core .CommandCreatedEventHandler ):
76+ def __init__ (self ):
77+ super ().__init__ ()
78+ def notify (self , args ):
79+ try :
80+ cmd = args .command
81+ onExecute = CommandExecuteHandler ()
82+ cmd .execute .add (onExecute )
83+ # keep the handler referenced beyond this function
84+ handlers .append (onExecute )
85+
86+ except :
87+ if ui :
88+ ui .messageBox ('Panel command created failed:\n {}' .format (traceback .format_exc ()))
89+
90+ class CommandCreatedEventHandlerQAT (adsk .core .CommandCreatedEventHandler ):
91+ def __init__ (self ):
92+ super ().__init__ ()
93+ def notify (self , args ):
94+ try :
95+ command = args .command
96+ onExecute = CommandExecuteHandler ()
97+ command .execute .add (onExecute )
98+ # keep the handler referenced beyond this function
99+ handlers .append (onExecute )
100+
101+ except :
102+ ui .messageBox ('QAT command created failed:\n {}' .format (traceback .format_exc ()))
103+
104+ commandDefinitions_ = ui .commandDefinitions
105+
106+ # add a command button on Quick Access Toolbar
107+ toolbars_ = ui .toolbars
108+ toolbarQAT_ = toolbars_ .itemById ('QAT' )
109+ toolbarControlsQAT_ = toolbarQAT_ .controls
110+ toolbarControlQAT_ = toolbarControlsQAT_ .itemById (commandIdOnQAT )
111+ if not toolbarControlQAT_ :
112+ commandDefinitionQAT_ = commandDefinitions_ .itemById (commandIdOnQAT )
113+ if not commandDefinitionQAT_ :
114+ commandDefinitionQAT_ = commandDefinitions_ .addButtonDefinition (commandIdOnQAT , commandName , commandDescription , commandResources )
115+ onCommandCreated = CommandCreatedEventHandlerQAT ()
116+ commandDefinitionQAT_ .commandCreated .add (onCommandCreated )
117+ # keep the handler referenced beyond this function
118+ handlers .append (onCommandCreated )
119+ toolbarControlQAT_ = toolbarControlsQAT_ .addCommand (commandDefinitionQAT_ , commandIdOnQAT )
120+ toolbarControlQAT_ .isVisible = True
121+ ui .messageBox ('A CSV command button is successfully added to the Quick Access Toolbar' )
122+
123+ # add a command on create panel in modeling workspace
124+ workspaces_ = ui .workspaces
125+ modelingWorkspace_ = workspaces_ .itemById ('FusionSolidEnvironment' )
126+ toolbarPanels_ = modelingWorkspace_ .toolbarPanels
127+ toolbarPanel_ = toolbarPanels_ .item (0 ) # add the new command under the first panel
128+ toolbarControlsPanel_ = toolbarPanel_ .controls
129+ toolbarControlPanel_ = toolbarControlsPanel_ .itemById (commandIdOnPanel )
130+ if not toolbarControlPanel_ :
131+ commandDefinitionPanel_ = commandDefinitions_ .itemById (commandIdOnPanel )
132+ if not commandDefinitionPanel_ :
133+ commandDefinitionPanel_ = commandDefinitions_ .addButtonDefinition (commandIdOnPanel , commandName , commandDescription , commandResources )
134+ onCommandCreated = CommandCreatedEventHandlerPanel ()
135+ commandDefinitionPanel_ .commandCreated .add (onCommandCreated )
136+ # keep the handler referenced beyond this function
137+ handlers .append (onCommandCreated )
138+ toolbarControlPanel_ = toolbarControlsPanel_ .addCommand (commandDefinitionPanel_ , commandIdOnPanel )
139+ toolbarControlPanel_ .isVisible = True
140+ ui .messageBox ('A CSV command is successfully added to the create panel in modeling workspace' )
141+
142+ except :
143+ if ui :
144+ ui .messageBox ('AddIn Start Failed:\n {}' .format (traceback .format_exc ()))
145+
146+
147+ def stop (context ):
148+ ui = None
149+ try :
150+ app = adsk .core .Application .get ()
151+ ui = app .userInterface
152+ objArrayQAT = []
153+ objArrayPanel = []
154+
155+ commandControlQAT_ = commandControlByIdForQAT (commandIdOnQAT )
156+ if commandControlQAT_ :
157+ objArrayQAT .append (commandControlQAT_ )
158+
159+ commandDefinitionQAT_ = commandDefinitionById (commandIdOnQAT )
160+ if commandDefinitionQAT_ :
161+ objArrayQAT .append (commandDefinitionQAT_ )
162+
163+ commandControlPanel_ = commandControlByIdForPanel (commandIdOnPanel )
164+ if commandControlPanel_ :
165+ objArrayPanel .append (commandControlPanel_ )
166+
167+ commandDefinitionPanel_ = commandDefinitionById (commandIdOnPanel )
168+ if commandDefinitionPanel_ :
169+ objArrayPanel .append (commandDefinitionPanel_ )
170+
171+ for obj in objArrayQAT :
172+ destroyObject (ui , obj )
173+
174+ for obj in objArrayPanel :
175+ destroyObject (ui , obj )
176+
177+ except :
178+ if ui :
179+ ui .messageBox ('AddIn Stop Failed:\n {}' .format (traceback .format_exc ()))
180+
181+ def updateParamsFromCSV ():
182+ ui = None
183+
184+ 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
202+ paramsList = []
203+ for oParam in design .allParameters :
204+ paramsList .append (oParam .name )
205+
206+ # Read the csv file.
207+ csvFile = open (filename )
208+ for line in csvFile :
209+ # Get the values from the csv file.
210+ valsInTheLine = line .split (',' )
211+ nameOfParam = valsInTheLine [0 ]
212+ valueOfParam = valsInTheLine [1 ]
213+ # if the name of the paremeter is not an existing parameter
214+ 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
218+ else :
219+ paramInModel = design .userParameters .itemByName (nameOfParam )
220+ paramInModel .expression = valueOfParam
221+
222+ ui .messageBox ('Finished adding parameters' )
223+
224+ except Exception as error :
225+ if ui :
226+ ui .messageBox ('Failed : ' + str (error ))
227+ #ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 commit comments