Using the IDocumentCreator Advanced

Using the IDocumentCreator Advanced
  1ot = impact.gui.output_toolbox
  2db = impact.active_database
  3
  4ot.clear()
  5
  6# This is an advanced script showing many of the new interfaces for IDocumentCreator
  7docs = db.documents
  8
  9if docs is None:
 10    ot.add("No document support in the database")
 11else:
 12    ot.add("Document support is enabled in this database")
 13
 14    doc_creator = docs.creator
 15
 16    # Add a local document and assign properties
 17    doc1 = doc_creator.add_local_file(r"C:\\Documents\\ExampleDocument1.docx")
 18
 19    if doc1 is not None:
 20        ot.add("Successfully added document " + str(doc1.local_file_path))
 21
 22        doc1.name = "My First Document"
 23        doc1.description = "A description of my document"
 24        doc1.tags.add("TagOne")
 25        doc1.tags.add("TagTwo")
 26
 27    # Add a second local document and assign properties
 28    doc2 = doc_creator.add_local_file(r"C:\\Documents\\ExampleDocument2.docx")
 29
 30    if doc2 is not None:
 31        ot.add("Successfully added document " + str(doc2.local_file_path))
 32
 33        doc2.group = db.settings.document_settings.groups.item("GroupA")
 34        doc2.tags.add("TagTwo")
 35
 36    # Add a common relationship to an existing project
 37    db_item = db.find_item_by_name(ipDrawingType.dtProject, "COM Update Test")  # "ACME12345")
 38
 39    if db_item is not None:
 40        rel1 = doc_creator.add_relationship(True, ipDocumentRelationship.drProject, None, db_item.key)
 41
 42        if rel1 is not None:
 43            ot.add("Successfully added " + str(rel1.relationship_as_string) + " relationship to " + str(rel1.object_id_as_string) + " for all documents")
 44
 45    else:
 46        ot.add("Unable to locate project")
 47
 48    # Add a relationship just for the second document to an existing customer
 49    cust = db.customers.find_by_code("ACME CORP")
 50
 51    if cust is not None:
 52        doc2rel = doc2.add_relationship(True, ipDocumentRelationship.drCustomer, None, cust.key)
 53
 54        if doc2rel is not None:
 55            ot.add("Successfully added " + str(doc2rel.relationship_as_string) + " relationship to " + str(doc2rel.object_id_as_string) + " for " + str(doc2.display_id))
 56
 57    else:
 58        ot.add("Unable to locate customer")
 59
 60    # Add an existing document (which will also be assigned the common relationship)
 61    existing_doc = db.find_document_by_id("e1b1ad95-f9de-40c0-9d08-0fee87e8a011")
 62
 63    if existing_doc is not None:
 64        doc3 = doc_creator.add_existing_document(existing_doc)
 65
 66        if doc3 is not None:
 67            ot.add("Successfully added existing document")
 68
 69    else:
 70        ot.add("Unable to locate existing document")
 71
 72    # Add the contents of a zip file
 73    zip_file = r"C:\\Documents\\ExampleDocuments.zip"
 74    if doc_creator.files_can_be_extracted_from_local_file(zip_file):
 75        zip_docs = doc_creator.add_extracted_files_from_local_file(zip_file)
 76
 77        # Wrap variant return items with Impact.py wrapper
 78        if zip_docs is not None:
 79            zip_docs = [IDocumentCreatorDocument(item) for item in zip_docs]
 80
 81        if zip_docs:
 82            for index in range(0, len(zip_docs) - 1  + 1):
 83                zip_doc = zip_docs[index]
 84
 85                if zip_doc is not None:
 86                    ot.add("Successfully added " + str(zip_doc.display_id))
 87
 88    # Set a comment for the add history item
 89    doc_creator.comments = "Adding documents from COM script"
 90
 91    # Optionally show the 'Add Documents' dialog to the user, or skip ShowDialog and use Perform
 92    doc_creator.allow_documents_to_be_changed = True
 93
 94    if doc_creator.show_dialog():
 95        perform_add = True
 96
 97    else:
 98        ot.add("Cancelled by user")
 99        perform_add = False
100
101    # Finally actually add these documents and relationships
102    if perform_add:
103        if doc_creator.perform():
104            ot.add("Processed " + str(doc_creator.count) + " documents")
105
106            ot.add("Added " + str(doc_creator.created_count) + " new documents")
107            ot.add("Updated " + str(doc_creator.existing_count) + " existing documents")
108
109            # Iterate documents that were added or updated
110            for index in range(1, doc_creator.count  + 1):
111                if doc_creator.item(index).was_created:
112                    ot.add("Added document " + str(doc_creator.item(index).document.name))
113                elif not doc_creator.item(index).document is None:
114                    ot.add("Updated document " + str(doc_creator.item(index).document.name))
115                else:
116                    ot.add("Failed to add document " + str(doc_creator.item(index).name))
117
118        else:
119            ot.add("No documents added or updated")
120