Reading Related Table Values for IDatabaseItem¶
Reading Related Table Values for IDatabaseItem¶
1import datetime
2def var_type(value):
3 """VBScript VarType equivalent - returns type name string."""
4 if value is None:
5 return 'null'
6 elif isinstance(value, bool):
7 return 'boolean'
8 elif isinstance(value, int):
9 return 'integer'
10 elif isinstance(value, float):
11 return 'double'
12 elif isinstance(value, str):
13 return 'string'
14 elif isinstance(value, (datetime.date, datetime.datetime)):
15 return 'date'
16 else:
17 return 'object'
18
19# This script shows how to access addition data related to a project. This includes data in
20# child tables linked to the main drawing or layer tables.
21
22
23ui = impact.gui
24ot = ui.output_toolbox
25
26db = impact.active_database
27ad = impact.active_drawing
28
29proj_code = "P061893"
30
31
32_DESCRIBE_STATUS_MAP = {
33 ipDatabaseTableRowStatus.dtrsUnmodified: "Unmodified",
34 ipDatabaseTableRowStatus.dtrsAdded: "Added",
35 ipDatabaseTableRowStatus.dtrsModified: "Modified",
36 ipDatabaseTableRowStatus.dtrsDeleted: "Deleted",
37}
38
39def describe_status(status):
40 return _DESCRIBE_STATUS_MAP.get(status, None)
41
42
43def output_native_value(row, name):
44 v = row.load(name)
45
46 if (v is None or v == ""):
47 ot.add(" " + str(name) + ":")
48 else:
49 ot.add(" " + str(name) + ": VarType=" + str(var_type(v)) + ", Value=" + str(v))
50
51def describe_row(r_index, row):
52
53 # each row can be identified by a GUID - but it transient and is not streamed with the drawing
54 # opening the same drawing again with stored related table data WILL produce different GUID's
55 ot.add(" Row " + str(r_index) + ", GUID: " + str(row.guid) + ", Status: " + str(describe_status(row.status)))
56
57 row.values_as_variants = True
58
59 for f_index in range(1, row.field_count + 1):
60 output_native_value(row, row.field_name(f_index))
61
62def describe_table(table):
63 relationship = table.table_relationship
64
65 ot.add("Relationship: " + str(relationship.formatted_name))
66 ot.add(" Table: " + str(table.table.name))
67 ot.add(" Row Count: " + str(table.count))
68 ot.add(" Rows Needing Update: " + str(table.needs_updating_count))
69 ot.add(" Deleted Rows: " + str(table.deleted_count))
70
71 for r_index in range(1, table.count + 1):
72 describe_row(r_index, table.item(r_index))
73
74 # alternative syntax
75 # r_index = 1
76 # for each row in table
77 # DescribeRow r_index, row
78 # r_index = r_index + 1
79 # next
80
81def describe_related_tables(data):
82 if data is None or (hasattr(data, "isNone") and data.isNone()):
83 ot.add("No related data exists")
84 return
85
86 ot.add("Related Tables Data Count: " + str(data.count))
87 ot.add(" Tables Needing Update: " + str(data.needs_updating_count))
88
89 for t_index in range(1, data.count + 1):
90 describe_table(data.item(t_index))
91
92
93ot.clear()
94
95 # alternative syntax
96 # for each table in data
97 # DescribeTable table
98 # next
99
100db_item = db.find_item_by_code(ipDrawingType.dtProject, proj_code)
101
102if not db_item.isNone():
103 ot.add("Related Tables for Project")
104 describe_related_tables(db_item.values.related_table_values)
105
106 for layer in db_item.layers:
107 ot.add("Related Tables for " + str(layer.type) + " Layer '" + layer.name + "'")
108 describe_related_tables(layer.values.related_table_values)
109
110else:
111 ot.add("Unable to locate IDatabaseItem with code " + str(proj_code))