I provide shop drawings to structural steel fabricators with SDS/2 software (http://sds2.com) by Design Data (DD). I am not a programmer by education or trade and started writing scripts about 5 years ago. DD has a script interface with Python in the 3D model which is very useful in production. Part of the Python interface includes a dialog box class:
Method dlg1.done() returns a dictionary. I am exporting the dictionary or list of dictionaries to disc with Pickler and importing with Unpickler to update the variables initialized in the scripts. I have been dumping everything into the global dictionary with globals().updat e(dd). All variables in the scripts are accessed directly - not through a dictionary. In an effort to evolve towards local variables, I reworked one script this way:
This new way seems to work well. It would be a nightmare to rework all my scripts to access values from dictionaries or from dialog box attributes. Is there a potential problem with updating the local dictionary this way or a better way to do it? I will appreciate any suggestions.
Code:
dlg1 = Dialog('Dialog Box Title')
Code:
def run_script():
from math import sqrt, cos, ..........
from param import ............
import macrolib.pickle
...............
# Define default values for variables
...............
# Define functions and classes not imported
...............
# Import defaults file and update local dictionary
if enable_default_import_export == "Enable":
dd0 = import_data(default_file_path + def_file)
if dd0:
if isinstance(dd0, types.ListType):
for dd in dd0:
for key, value in dd.items():
exec "%s = %s" % (key, repr(value)) in None
elif isinstance(dd0, types.DictType):
for key, value in dd0.items():
exec "%s = %s" % (key, repr(value)) in None
else:
Warning("Invalid data - Reverting to original defaults")
## Main program loop
while 1:
ClearSelection()
# Select members
# do other stuff as necessary
######################################################################################################################################
## DIALOG BOX 1 ---------------------------------------------------------------------------------------------------------------------#
######################################################################################################################################
dlg1 = Dialog("Add stiffener plates to beam members")
dlg1.menu("print_doc", ("Yes", "No"), "No", "Print parametric script documentation only")
dlg1.tabset_begin()
dlg1.tab("General Information")
dlg1.column_group_begin()
dlg1.column(0)
if mem2 == "pick point":
dlg1.group_title("Stiffener Location Selection Method")
dlg1.menu("location_method", ("Pick Points", "Fixed Spacing"), location_method, "Method of locating stiffeners ")
dlg1.entry("points_x_offset", dim_print(points_x_offset), "'Pick Points' member 'X' offset distance")
dlg1.group_title_end
dlg1.group_title("Single/Double, Side, Color, Grade")
dlg1.menu("no_pairs", ("Single", "Double"), no_pairs, "Single or Double Stiffeners ")
# more dialog box stuff
try:
dd1 = dlg1.done()
except ResponseNotOK:
break
for key, value in dd1.items():
exec "%s = %s" % (key, repr(value)) in None
........................................
# Do what needs to be done
........................................
Add_pls = yes_or_no("Add more full or partial depth stiffener plates to beams?")
if Add_pls == 0:
break
#### END run_script() xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if __name__ == '__main__':
try:
run_script()
finally:
del run_script
Comment