|
1 | | -#! /usr/bin/python3.6 |
| 1 | +#! /usr/bin/python3.9 |
2 | 2 |
|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | Example - Assembly Convertor - 001 |
6 | 6 |
|
7 | | - Print the BOM of a product to XLS using the inbuilt AssemblyConvertor. You |
8 | | - must already have excel installed. |
| 7 | + Description: |
| 8 | + Print the BOM of a product to XLS using the inbuilt AssemblyConvertor. |
| 9 | + This can also be used to create TXT and HTML files. |
9 | 10 |
|
10 | | - This can also be used to create TXT and HTML files. |
| 11 | + Requirements: |
| 12 | + - An open product document with parts inside. |
| 13 | + - MS EXCEL must be installed. |
11 | 14 |
|
12 | | - See github issue https://github.com/evereux/pycatia/issues/110 with regards |
13 | | - to file paths and saying "No" to overwriting existing files and file paths |
14 | | - when using excel. These issues are mitigated using the code below by |
15 | | - checking for an existing excel file and removing it and also using pythons |
16 | | - pathlib.Path module. |
| 15 | + Further information: |
| 16 | + See github issue https://github.com/evereux/pycatia/issues/110 with regards |
| 17 | + to file paths and saying "No" to overwriting existing files and file paths |
| 18 | + when using excel. These issues are mitigated using the code below by |
| 19 | + checking for an existing excel file and removing it and also using pythons |
| 20 | + pathlib.Path module. |
17 | 21 |
|
18 | 22 | """ |
19 | 23 |
|
|
34 | 38 |
|
35 | 39 | # file_type can be "TXT", "HTML" or "XLS". |
36 | 40 | file_type = "XLS" |
37 | | -# full path to excel file. |
38 | | -# use pathlib.Path to prevent path related errors. See github link above. |
39 | | -excel_file = Path("C:\\Users\\evereux\\Desktop\\my_bom.xls") |
40 | 41 |
|
41 | | -# check that the parent folder exists |
42 | | -if not excel_file.parent.is_dir(): |
43 | | - raise NotADirectoryError(f'Directory "{excel_file.parent}" doesn\'t exist') |
| 42 | +caa = catia() |
| 43 | +document = ProductDocument(caa.active_document.com_object) |
| 44 | +product = Product(document.product.com_object) |
| 45 | +# Note: It's not necessary to explicitly use the ProductDocument or the Product class |
| 46 | +# with the com_object. It's perfectly fine to write it like this: |
| 47 | +# document = caa.active_document |
| 48 | +# product = document.product |
| 49 | +# But declaring 'document' and 'product' this way, your linter can't resolve the |
| 50 | +# product reference, see https://github.com/evereux/pycatia/issues/107#issuecomment-1336195688 |
| 51 | + |
| 52 | +bom = product.get_item("BillOfMaterial") |
| 53 | +assembly_convertor = AssemblyConvertor(bom.com_object) |
| 54 | + |
| 55 | +details_top = ("Quantity", "Part Number", "Type", "Nomenclature", "Revision") |
| 56 | +details_recap = ("Quantity", "Part Number") |
| 57 | +# Note: Keep in mind that CATIA is language based when it comes to the bill of material format. |
| 58 | +# So 'Part Number' in english will become 'Teilenummer' in german, and so on. |
| 59 | +assembly_convertor.set_current_format(details_top) |
| 60 | +assembly_convertor.set_secondary_format(details_recap) |
| 61 | + |
| 62 | +# Define the absolute path for the excel file. |
| 63 | +# In this example the excel file will be saved to the same folder as the product, |
| 64 | +# from which the bill of material is created. The excel file will also have the |
| 65 | +# same name as the product. |
| 66 | +excel_path = Path(product.path().parent, product.name + ".xls") |
44 | 67 |
|
45 | 68 | # check if the file already exists. I recommend doing this in python for two |
46 | 69 | # reasons .... |
|
50 | 73 | # errors until CATIA is restarted. |
51 | 74 | # see github link above for more information. |
52 | 75 | # !! this will delete excel file if it exists !! |
53 | | -if excel_file.is_file(): |
54 | | - os.remove(excel_file) |
| 76 | +if excel_path.is_file(): |
| 77 | + os.remove(excel_path) |
55 | 78 |
|
56 | 79 |
|
57 | | -caa = catia() |
58 | | -document = caa.active_document |
59 | | -product = ProductDocument(document.com_object).product |
60 | | -# not neccessary but will provide autocompletion in IDEs. |
61 | | -product = Product(product.com_object) |
62 | | - |
63 | | -bom = product.get_item("BillOfMaterial") |
64 | | -assembly_convertor = AssemblyConvertor(bom.com_object) |
65 | | - |
66 | | -details_top = ("Quantity", "Part Number", "Type", "Nomenclature", "Revision") |
67 | | -details_recap = ("Quantity", "Part Number") |
68 | | -assembly_convertor.set_current_format(details_top) |
69 | | -assembly_convertor.set_secondary_format(details_recap) |
70 | | - |
71 | | -assembly_convertor.print(file_type, excel_file, product) |
| 80 | +assembly_convertor.print(file_type, excel_path, product) |
72 | 81 | # Important note: |
73 | 82 | # The print-method will fail if you try to export the bill-of-material-xls file |
74 | 83 | # to a location, where another process will access this file. Such process is |
|
0 commit comments