Python-Code zum Öffnen einer .sldprt- und .sldasm-Datei mit SolidWorks, deren Verknüpfung sich an einem anderen Ort befiPython

Python-Programme
Guest
 Python-Code zum Öffnen einer .sldprt- und .sldasm-Datei mit SolidWorks, deren Verknüpfung sich an einem anderen Ort befi

Post by Guest »

Code: Select all

def open_file():
"""
Open a file dialog to select a file and launch SolidWorks for supported files.
"""
# Specify the SolidWorks shortcut location (modify this path accordingly)
solidworks_exe = r"C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\SLDWORKS.exe"

file_path = filedialog.askopenfilename(
title="Open SolidWorks File",
filetypes=[
("SolidWorks Files", "*.SLDPRT *.SLDASM"),  # SolidWorks part and assembly files
("Python Files", "*.py"),                   # Optionally include Python files
("All Files", "*.*")                        # Catch-all for all file types
]
)

if file_path:
# Ensure the file path is absolute and properly formatted
formatted_file_path = os.path.abspath(file_path).replace("\\", "\\\\")
print(f"Selected file (formatted): {formatted_file_path}")

# Check for SolidWorks file extensions
if formatted_file_path.lower().endswith((".sldprt", ".sldasm")):
try:
# Ensure SolidWorks is triggered with the selected file
subprocess.run([solidworks_exe, file_path], shell=False)
print(f"Opened {file_path} in SolidWorks.")
# Use subprocess to open SolidWorks with the selected file
command = f'"{solidworks_exe}" "{formatted_file_path}"'
print(f"Executing command: {command}")
subprocess.run(command, shell=True)
print(f"Opened {formatted_file_path} in SolidWorks.")

except FileNotFoundError:
messagebox.showerror(
"Error",
f"SolidWorks executable not found at {solidworks_exe}. Please check the path."
)
except Exception as e:
messagebox.showerror(
"Error",
f"An error occurred while opening the file in SolidWorks: {e}"
)
elif formatted_file_path.endswith(".py"):  # For Python files, optionally execute them
try:
subprocess.run(["python", formatted_file_path], check=True)
except Exception as e:
messagebox.showerror(
"Error",
f"An error occurred while executing the Python file: {e}"
)
else:
messagebox.showwarning(
"Unsupported File",
f"The selected file format '{os.path.splitext(formatted_file_path)[1]}' is not supported for this operation."
)
else:
print("No file selected.")
Das Obige ist Teil meines Codes. Ich versuche, ein Programm mit Python zu erstellen. Wenn ich im Menü des Programms „Datei“ >> „Öffnen“ auswähle und dann eine . sldprt oder .sldasm-Datei, ich möchte, dass das Programm den Ordnerspeicherort der Datei kopiert und an die SolidWorks-Software übergibt, wo es die Datei öffnen würde.
Die Fehlermeldung, die ich erhalte, ist nach dem Die SolidWorks-Software wird automatisch geöffnet (was ich möchte). der zu tunde Code), heißt es: C:\"Dateiname" nicht gefunden. Daher wird der Standort nicht ordnungsgemäß an die SolidWorks-Software übertragen.
Wie kann ich das richtig machen?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post