Conversion with unit settings

Conversion with unit settings
 1ot = impact.gui.output_toolbox
 2
 3ot.clear()
 4
 5# define some string containing a length value, optionally with impact.units
 6val_str = "266.7mm"
 7
 8ot.add("Original Value: " + str(val_str))
 9
10# the Convertor object will be used to perform the unit conversions
11conv = impact.system.convertor
12
13# convert the string to a "double" value - this assumes current windows locale decimal separator is '.'
14value = conv.string_as_length(val_str)
15
16# output the value using the user's current unit settings and current locale
17ot.add("Converted Value (Current Locale): " + str(conv.length_as_string(value)))
18
19# retrieve the users current unit settings and locale
20unit_options = impact.active_database.unit_settings()
21
22ot.add("Users Locale: " + str(unit_options.locale))
23
24# set IConvertor to use these IUnitSettings
25conv.unit_settings = unit_options
26
27# define a length value using ',' decimal separator
28val_str = "266,7mm"
29
30ot.add("Original Value: " + str(val_str))
31
32# change the locale to german
33unit_options.locale = "de-DE"
34ot.add("New Locale: " + str(unit_options.locale))
35
36value = conv.string_as_length(val_str)
37
38ot.add("Converted Value (Specific Locale 'de-DE'): " + str(conv.length_as_string(value)))
39
40# now create some other unit settings from scratch
41unit_options = impact.creator.unit_settings()
42
43# With unit_options
44unit_options.distance_decimals = 4
45unit_options.distance_type = ipDistanceFormat.dfInches
46unit_options.distance_suffix = " inches"
47unit_options.distance_fractions = True
48unit_options.distance_denominator = 16
49
50# End With
51
52# new IUnitSettings uses current Windows locale
53ot.add("New IUnitSettings Locale: " + str(unit_options.locale))
54
55# output the value using the alternative unit settings
56conv.unit_settings = unit_options
57
58ot.add("Converted Value (Imperial with fractions): " + str(conv.length_as_string(value)))
59
60# output the same value using a different locale
61unit_options.distance_fractions = False
62unit_options.locale = "es"
63
64ot.add("Locale: " + str(unit_options.locale))
65
66ot.add("Converted Value (Imperial Specific Locale 'es-ES'): " + str(conv.length_as_string(value)))
67
68# output again using the 'C' locale
69unit_options.locale = "C"
70
71ot.add("Locale: " + str(unit_options.locale))
72ot.add("Converted Value (Imperial Specific Locale 'C'): " + str(conv.length_as_string(value)))
73
74# output again using the current Windows locale
75unit_options.locale = ""
76ot.add("Locale: " + str(unit_options.locale))
77ot.add("Converted Value (Imperial Current Windows Locale): " + str(conv.length_as_string(value)))
78
79
80
81
82
83
84