Writing Special Values

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