Import the necessary SOLIDWORKS PDM API libraries
import Dispatch
import time
import subprocess
import os
Create a SOLIDWORKS PDM API application object
sw_pdm_app = Dispatch(“SldWorksPDM.Application”)
Log in to the PDM vault
vault = sw_pdm_app.GetVault(“3D Printing Automation”)
vault.Login(“Admin”, “123456”)
Define the root folder in PDM where you want to start the search
root_folder = vault.GetFolderFromPath(r"$/Users\ADMIN\Desktop\3D Printing Automation")
Replace with the path to your Ultimaker Cura executable
cura_executable_path = “C:\Program Files\Ultimaker Cura 5.4.0\UltiMaker-Cura.exe”
Specify the Cura profile name you want to use
cura_profile_name = “Default”
Function to search for STL files and generate G-code with the specified profile
def search_and_generate_gcode(folder):
for file in folder.GetFiles():
if file.Type == Dispatch.PDMFILESType.PDMFILE_3D:
if file.Extension.lower() == “.stl”:
stl_file_path = file.GetLocalCopy()
print(f"Processing {stl_file_path}…“)
gcode_output_path = os.path.splitext(stl_file_path)[0] + “.gcode”
# Use Ultimaker Cura to generate G-code with the specified profile
subprocess.run([cura_executable_path, stl_file_path, “-p”, cura_profile_name, “-o”, gcode_output_path], check=True)
print(f"G-code generated for {stl_file_path} with profile ‘{cura_profile_name}’.”)
for subfolder in folder.GetFolders():
search_and_generate_gcode(subfolder)
Loop with a 10-second refresh rate
while True:
search_and_generate_gcode(root_folder)
time.sleep(10) # Sleep for 10 seconds before the next search
The script will not log out of PDM here, allowing the user to continue using the vault.
the Error prompt: ModuleNotFoundError: No module named ‘Dispatch’