Creating Relationships (Python)

Creating Relationships (Python)
 1ot = impact.gui.output_toolbox
 2db = impact.active_database
 3
 4ot.clear()
 5documentID = "781f9860-8966-4d2d-844c-fc5e9b94b979"
 6
 7# Find the document
 8document = db.find_document_by_id( documentID )
 9
10if not document.isNone():
11
12    ot.add("Document " + document.name + " Relationships Count: " + str(document.series_relationships.count))
13
14    # Find a customer
15    custCode = "ACME CORP"
16    cust = db.customers.find_by_code(custCode)
17
18    if not cust.isNone():
19
20        # Create a relationship from this specific document version (using Relationships)
21        # to the customer
22        if document.relationships.add(ipDocumentRelationship.drCustomer, None, cust.key, None):
23            ot.add("Successfully added relationship to customer")
24        else:
25            ot.add("Unable to create relationship to customer")
26
27    else:
28        ot.add("Unable to locate a customer with code " + custCode)
29
30    # Find a project
31    projReference = "Documents when saving"  # "ACME12345"
32    dbItem = db.find_item_by_name(ipDrawingType.dtProject, projReference)
33
34    if not dbItem.isNone():
35
36        # Create a relationship from the latest document version (using SeriesRelationships)
37        # to the project
38        if document.series_relationships.add(ipDocumentRelationship.drProject, None, dbItem.key, None):
39            ot.add("Successfully added series relationship to project")
40        else:
41            ot.add("Unable to create series relationship to project")
42
43    else:
44        ot.add("Unable to locate the project " + projReference)
45
46    # Need to refresh SeriesRelationships as adding a document relationship won't update this collection
47    document.series_relationships.refresh()  # Display relationships
48    ot.add("Document " + document.name + " Relationships Count (After): " + str(document.series_relationships.count))
49
50    for rel in document.series_relationships :
51
52        if rel.document_latest_version:
53            ot.add("Latest version " + rel.relationship_as_string + " Relationship to " + rel.object_id_as_string)
54        else:
55            ot.add("version " + rel.document.version.version_as_string + " " + rel.relationship_as_string + " Relationship to " + rel.object_id_as_string)
56else:
57
58    ot.add("Unable to find the document with ID '" + documentID + "'")