Python Forum
Export Utility for Inventor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code Review (https://python-forum.io/forum-46.html)
+--- Thread: Export Utility for Inventor (/thread-41620.html)



Export Utility for Inventor - cthull12 - Feb-18-2024

I am trying to create a utility for my position that will allow the user to input a list of parts that need exported to DXF files from Inventor, then have them placed in a created destination folder. Trying to automate the requests I get from sales so I don't have to stop for 10-20 minutes at a time when they need something. I am just learning Python, so I referenced a few examples online, consolidated them, then asked chatGPT if it would work, and it suggested some edits. Below is what I have come up with thus far. Any feedback is greatly appreciated. I want to make sure that I have a fully polished code before I even attempt to use this on my work network.
import os
import comtypes.client
import tkinter as tk
from tkinter import filedialog

# Function to export drawings as DXF
def export_drawings(file_names, export_folder_path):
    # Check if Inventor is already running
    try:
        inv_app = comtypes.client.GetActiveObject("Inventor.Application")
    except:
        # If Inventor is not running, create a new instance
        inv_app = comtypes.client.CreateObject("Inventor.Application")

    for file_name in file_names.split(","):
        # Open the Inventor file
        full_path = os.path.join(folder_path, file_name.strip())
        doc = inv_app.Documents.Open(full_path)

        # Export the drawing as DXF
        drawing_doc = doc.ComponentDefinition.Document
        export_path = os.path.join(export_folder_path, file_name.strip() + ".dxf")
        drawing_doc.SaveAs(export_path, FileFormat=8)  # FileFormat 8 is for DXF

        # Close the Inventor file
        doc.Close()

# Function to handle the export button click
def export_button_click():
    file_names = file_entry.get()
    export_folder_path = export_folder_path_entry.get()
    export_drawings(file_names, export_folder_path)

# Create the main window
root = tk.Tk()
root.title("Autodesk Inventor Drawing Exporter")

# Select folder containing Inventor files
folder_path = filedialog.askdirectory(title="Select Folder Containing Inventor Files")

# Create a new folder on the desktop to store the exported files
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
export_folder_path_entry = tk.Entry(root)
export_folder_path_entry.pack()

# File names entry
file_label = tk.Label(root, text="Enter File Names (comma-separated, without extension):")
file_label.pack()
file_entry = tk.Entry(root)
file_entry.pack()

# Export button
export_button = tk.Button(root, text="Export Drawings", command=export_button_click)
export_button.pack()

# Start the main loop
root.mainloop()