Updating Project values from IDatabaseItem

Updating Project values from IDatabaseItem
  1import win32com.client
  2
  3# This script demonstrates updating project database values using an IDatabaseItem object
  4# directly, rather than opening/updating an IDrawing. You can only update database values
  5# in this way if no user has the IDatabaseItem checked out.
  6
  7# Updating values using the IDatabaseValues objects retrieved from an IDatabaseItem object
  8# will automatically generate a new IDatabaseRevision with a comment showing all database
  9# values that were updated. It will also update the modification date/time and user for the
 10# IDatabaseItem.
 11
 12ad_type_binary = 1
 13project_code_to_find = "P061932"
 14customer_code_to_find = "H029"
 15picture_file = "D:\andy.jpg"
 16
 17db = active_database
 18ot = impact.gui.output_toolbox
 19
 20# NOTE: This will only have an effect using n_server
 21db.application_name = "WEBcnx"
 22
 23
 24def read_binary_file(file_name):
 25
 26    # create stream object
 27    stream = win32com.client.Dispatch("ADODB.Stream")
 28
 29    # load the file data from disk to stream object
 30    stream.open()
 31    stream.type = ad_type_binary
 32    stream.load_from_file(file_name)
 33
 34    # get binary data from the object
 35    _result = stream.read
 36    return _result
 37
 38def show_modification_and_revisions():
 39
 40    # need to ensure we refresh modification info from database
 41    item.refresh()
 42
 43    ot.add("  Modified: " + str(item.modified_date_time))
 44    ot.add("  Modified By: " + str(item.modified_by.full_name))
 45    ot.add("  Revision Count: " + str(item.revisions.count))
 46    for r in item.revisions:
 47        ot.add("   Revision " + str(r.created_date_time))
 48        ot.add("   Comments: " + str(r.comments))
 49
 50
 51ot.clear()
 52
 53item = db.find_item_by_code(ipDrawingType.dtProject, project_code_to_find)
 54
 55if not item.isNone():
 56    ot.add("Code: " + str(item.code))
 57    ot.add("reference: " + str(item.reference))
 58    ot.add("customer: " + str(item.customer.name))
 59    ot.add("Modified: " + str(item.modified_date_time))
 60    ot.add("Modified By: " + str(item.modified_by.full_name))
 61    ot.add("")
 62
 63    show_modification_and_revisions()
 64
 65    ot.add("==== Updating customer ====")
 66    item.change_customer(db.customers.find_by_code(customer_code_to_find), "Corporate Rename")
 67    show_modification_and_revisions()
 68
 69    ot.add("==== Updating LAYERS/ONE_UP with no changes ====")
 70    db_values = item.layers.item(1).values
 71
 72    # no REVISION should be created
 73    db_values.do_update()
 74    show_modification_and_revisions()
 75
 76    ot.add("==== Updating LAYERS/ONE_UP values ====")
 77
 78    db_values.save("LAYERS.L_CODE", "ABCD")
 79    db_values.save("OU_INT", 10)
 80    db_values.save("OU_REAL", 25.4)
 81    db_values.save("OU_CUTAREA", "1000 sqmm")
 82
 83    # REVISION containing changes should be created
 84    db_values.do_update()
 85    show_modification_and_revisions()
 86
 87    ot.add("==== Updating DRAWINGS with no changes ====")
 88    db_values = item.values
 89
 90    # no REVISION should be created
 91    db_values.do_update()
 92    show_modification_and_revisions()
 93
 94    ot.add("==== Updating DRAWINGS values ====")
 95    db_values = item.values
 96
 97    db_values.save("D_INK1", 20)
 98    db_values.save("D_ORDERNUM", "#CS564723")
 99    db_values.save("D_NOTES", "A multi-line comment" + "\r\n" + "where only the first line should be included")
100    db_values.save("D_MYPIC", read_binary_file(picture_file))
101
102    # REVISION containing changes should be created - incl memo first line onlt and BLOB size
103    db_values.do_update()
104    show_modification_and_revisions()
105
106    ot.add("==== Updating DRAWINGS values With Text Tokens ====")
107    db_values = item.values
108
109    # D_CREATOR is a coded (key) value - look up the current and new friendly names first
110    old_creator = item.created_by
111    new_creator = db.users.item("SUPPORT")
112
113    db_values.save_with_text_tokens("DRAWINGS", "D_CREATOR", new_creator.key, old_creator.full_name, new_creator.full_name)
114
115    # REVISION comment will show the old/new user's full_name rather than the raw d_creator keys
116    db_values.do_update()
117    show_modification_and_revisions()
118
119    ot.add("==== Updating D_MODDATE/D_MODTIME/D_MODBY ====")
120
121    # you can explicitly set these values and they won't be updated
122    db_values.save("D_MODDATE", "2010-01-31")
123    db_values.save("D_MODTIME", "11:10:30")
124    db_values.save("D_MODBY", 5)
125    db_values.do_update()
126    show_modification_and_revisions()
127
128else:
129    ot.add("Unable to locate project " + str(project_code_to_find))