Creating Relationships

Creating Relationships
 1ot = impact.gui.output_toolbox
 2db = impact.active_database
 3
 4ot.clear()
 5
 6document_id = "781f9860-8966-4d2d-844c-fc5e9b94b979"
 7
 8# Find the document
 9document = db.find_document_by_id(document_id)
10
11if document is not None:
12    ot.add("Document " + document.name + " Relationships Count: " + str(document.series_relationships.count))
13
14    # Find a customer
15    cust_code = "ACME CORP"
16    cust = db.customers.find_by_code(cust_code)
17
18    if cust is not None:
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 " + str(cust_code))
29
30    # Find a project
31    proj_reference = "Documents when saving"  # "ACME12345"
32    db_item = db.find_item_by_name(ipDrawingType.dtProject, proj_reference)
33
34    if db_item is not None:
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, db_item.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 " + str(proj_reference))
45
46    # Need to refresh SeriesRelationships as adding a document relationship won't update this collection
47    document.series_relationships.refresh()
48
49    # Display relationships
50    ot.add("Document " + document.name + " Relationships Count (After): " + str(document.series_relationships.count))
51
52    for rel in document.series_relationships:
53        if rel.document_latest_version:
54            ot.add("Latest version " + str(rel.relationship_as_string) + " Relationship to " + str(rel.object_id_as_string))
55        else:
56            ot.add("version " + str(rel.document.version.version_as_string) + " " + str(rel.relationship_as_string) + " Relationship to " + str(rel.object_id_as_string))
57
58else:
59    ot.add("Unable to find the document with ID '" + str(document_id) + "'")