Writing Special Values¶
Writing Special Values¶
1import datetime
2
3def var_type(value):
4 """VBScript VarType equivalent - returns type name string."""
5 if value is None:
6 return 'null'
7 elif isinstance(value, bool):
8 return 'boolean'
9 elif isinstance(value, int):
10 return 'integer'
11 elif isinstance(value, float):
12 return 'double'
13 elif isinstance(value, str):
14 return 'string'
15 elif isinstance(value, (datetime.date, datetime.datetime)):
16 return 'date'
17 else:
18 return 'object'
19
20
21def month_name(month, abbreviate=False):
22 """VBScript MonthName equivalent."""
23 names = ['January', 'February', 'March', 'April', 'May', 'June',
24 'July', 'August', 'September', 'October', 'November', 'December']
25 name = names[month - 1]
26 return name[:3] if abbreviate else name
27
28
29ot = impact.gui.output_toolbox
30
31folder_name = "2. Personal work"
32customer_code = "DEFAULT"
33
34
35def output_native_value(values, name):
36 v = values.load(name)
37
38 if (v is None or v == ""):
39 ot.add(str(name) + ": <EMPTY>")
40 elif isinstance(v, (datetime.date, datetime.datetime)): # Date
41 ot.add(str(name) + ": VarType=" + str(var_type(v)) + ", Value=" + str(v) + ", Year=" + str((v).year) + ", MonthName=" + str(month_name((v).month)) + ", Day=" + str((v).day))
42 else:
43 ot.add(str(name) + ": VarType=" + str(var_type(v)) + ", Value=" + str(v))
44
45
46
47ot.clear()
48
49db = impact.active_database
50
51folder = db.projects.find(folder_name)
52
53ot.add("Located folder '" + str(folder.folder_name) + "'")
54
55d = db.create_item(ipDrawingType.dtProject, "")
56
57if not d.isNone():
58 ot.add("Created new project")
59
60 d.customer = db.customers.find_by_code(customer_code)
61
62 ot.add("Set project customer")
63
64 db_values = d.database_values
65 us = impact.creator.unit_settings()
66
67 # override creation dates using VBScript date types
68 db_values.save("D_CREDATE", datetime.datetime(2008, 1, 31, 0, 0, 0))
69 db_values.save("D_CRETIME", datetime.datetime.combine(datetime.date.today(), datetime.time(11, 10, 30)))
70
71 ot.add("D_CREDATE: " + str(db_values.load("D_CREDATE")))
72 ot.add("D_CRETIME: " + str(db_values.load("D_CRETIME")))
73
74 # override modification dates using strings
75 db_values.save("D_MODDATE", "31/01/2008")
76 db_values.save("D_MODTIME", "11:10:30")
77
78 # output date/time using current Windows locale
79 ot.add("Date/Time using current Windows locale")
80 ot.add("D_MODDATE: " + str(db_values.load("D_MODDATE")))
81 ot.add("D_MODTIME: " + str(db_values.load("D_MODTIME")))
82
83 # load date/time using a specific locale
84 us.locale = "en-US"
85 us.distance_type = ipDistanceFormat.dfInches
86
87 db_values.unit_settings = us
88
89 ot.add("Date/Time using en-US locale")
90 ot.add("D_MODDATE: " + str(db_values.load("D_MODDATE")))
91 ot.add("D_MODTIME: " + str(db_values.load("D_MODTIME")))
92
93 # overide impact.creator, modifier and version
94 d.database_values.save("D_CREATOR", db.users.item("SUPPORT").key)
95 d.database_values.save("D_MODBY", db.users.item("DESIGNER").key)
96 d.database_values.save("D_VERSION", 10)
97
98 a = d.active_layer
99 db_values_l = a.database_values
100
101 # assign values using strings
102 db_values_l.save("LENGTH", "100.00mm")
103 db_values_l.save("WIDTH", "1in")
104
105 # assign values using variant (native types)
106 db_values_l.save("HEIGHT", 25.4) # mm
107 db_values_l.save("GLUED", True)
108 db_values_l.save("OU_ANGLE", 90) # degrees
109 db_values_l.save("OU_REAL", 99999.5555)
110 db_values_l.save("OU_INT", 100000)
111
112 # load values as strings using current Windows locale formatting
113 ot.add("load using current users formatting and Windows locale")
114 ot.add("LENGTH: " + str(db_values_l.load("LENGTH")))
115 ot.add("WIDTH: " + str(db_values_l.load("WIDTH")))
116 ot.add("HEIGHT: " + str(db_values_l.load("HEIGHT")))
117 ot.add("GLUED: " + str(db_values_l.load("GLUED")))
118 ot.add("OU_ANGLE: " + str(db_values_l.load("OU_ANGLE")))
119 ot.add("OU_REAL: " + str(db_values_l.load("OU_REAL")))
120 ot.add("OU_INT: " + str(db_values_l.load("OU_INT")))
121
122 # load same values using specific formatting and locale
123 us.locale = "de-DE"
124 us.distance_type = ipDistanceFormat.dfMetres
125 us.distance_decimals = 4
126 us.angle_type = ipAngleFormat.afRadians
127 us.double_decimals = 4
128
129 db_values_l.unit_settings = us
130
131 ot.add("load using IUnitSettings and Locale (de-DE)")
132 ot.add("LENGTH: " + str(db_values_l.load("LENGTH")))
133 ot.add("WIDTH: " + str(db_values_l.load("WIDTH")))
134 ot.add("HEIGHT: " + str(db_values_l.load("HEIGHT")))
135 ot.add("GLUED: " + str(db_values_l.load("GLUED")))
136 ot.add("OU_ANGLE: " + str(db_values_l.load("OU_ANGLE")))
137 ot.add("OU_REAL: " + str(db_values_l.load("OU_REAL")))
138 ot.add("OU_INT: " + str(db_values_l.load("OU_INT")))
139
140 # load values using variants (native data types)
141 db_values_l.unit_settings = None
142 db_values.values_as_variants = True
143 db_values_l.values_as_variants = True
144
145 ot.add("load using native types (Impacts internal units: mm, deg etc regardless of locale)")
146 output_native_value(db_values, "D_NAME")
147 output_native_value(db_values, "D_MODDATE")
148 output_native_value(db_values, "D_MODTIME")
149
150 output_native_value(db_values_l, "LENGTH")
151 output_native_value(db_values_l, "WIDTH")
152 output_native_value(db_values_l, "HEIGHT")
153 output_native_value(db_values_l, "GLUED")
154 output_native_value(db_values_l, "OU_ANGLE")
155 output_native_value(db_values_l, "OU_REAL")
156 output_native_value(db_values_l, "OU_INT")
157
158 us = impact.creator.unit_settings()
159
160 us.distance_type = ipDistanceFormat.dfInches
161 us.angle_type = ipAngleFormat.afGradients
162
163 db_values_l.unit_settings = us
164
165 # we want distances returned as inches, angles as gradients
166 ot.add("load using native types (specific units: inches, gradients")
167 output_native_value(db_values_l, "LENGTH")
168 output_native_value(db_values_l, "OU_ANGLE")
169
170 # assign values using native types using inches and gradients
171 db_values_l.save("LENGTH", 6) # inches
172 db_values_l.save("OU_ANGLE", 200) # gradients ~ 180deg
173
174 ot.add("load using native types (specific units: inches, gradients")
175 output_native_value(db_values_l, "LENGTH")
176 output_native_value(db_values_l, "OU_ANGLE")
177
178 us.distance_type = ipDistanceFormat.dfMillimetres
179 us.angle_type = ipAngleFormat.afDegrees
180
181 ot.add("load again using native types (specific units: mm, degrees")
182 output_native_value(db_values_l, "LENGTH")
183 output_native_value(db_values_l, "OU_ANGLE")
184
185 db_values_l.values_as_variants = False
186 db_values_l.unit_settings = None
187
188 # assign date/time using current locale to layer values
189 db_values_l.save("OU_SCOCRE", "31/1/2009")
190 db_values_l.save("OU_SCOTIM", "22:57:20.000")
191
192 ot.add("OU_SCOCRE: " + str(db_values_l.load("OU_SCOCRE")))
193 ot.add("OU_SCOTIM: " + str(db_values_l.load("OU_SCOTIM")))
194
195 # assign date using a built in VBScript date values to layer values
196 db_values_l.save("OU_SCOCRE", datetime.datetime(2009, 1, 31, 0, 0, 0))
197 db_values_l.save("OU_SCOTIM", datetime.datetime.combine(datetime.date.today(), datetime.time(23, 51, 20)))
198
199 ot.add("OU_SCOCRE: " + str(db_values_l.load("OU_SCOCRE")))
200 ot.add("OU_SCOTIM: " + str(db_values_l.load("OU_SCOTIM")))
201
202 if d.save_as("", "DB Date Test", "", folder, True):
203 ot.add("Successfully saved new project")
204
205 di = d.database_item
206
207 if di.close_and_release():
208 ot.add("Successfully closed and released")
209
210 # check saved values
211 ot.add("D_CREATED: " + str(di.created_date_time))
212 ot.add("D_MODIFIED: " + str(di.modified_date_time))
213 ot.add("D_CREATOR: " + str(di.created_by.full_name))
214 ot.add("D_MODBY: " + str(di.modified_by.full_name))
215 ot.add("D_VERSION: " + str(di.version))
216
217 # can also retrieve values directly
218 # set dv = di.values
219
220 # ot.add() "D_CREDATE: " + dv.load("D_CREDATE")
221 # ot.add() "D_CRETIME: " + dv.load("D_CRETIME")
222 # ot.add() "D_MODDATE: " + dv.load("D_CREDATE")
223 # ot.add() "D_MODTIME: " + dv.load("D_CRETIME")
224
225 # check saved layer values
226 ot.add("Layer Count: " + str(di.layers.count))
227
228 l = di.layers.item(1)
229
230 ot.add("Layer: " + l.name + " (" + str(l.type) + ")")
231
232 ot.add("OU_SCOCRE: " + str(l.values.load("OU_SCOCRE")))
233 ot.add("OU_SCOTIM: " + str(l.values.load("OU_SCOTIM")))
234
235 else:
236 ot.add("Unable to close and release project")
237
238 else:
239 ot.add("Unable to save new project")
240
241else:
242 ot.add("Unable to create new project")