-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacrorun.py
130 lines (98 loc) · 3.84 KB
/
macrorun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Author: Manuel Rosa
Description: Starts solidworks and runs a macro
This is a way of creating a connection between solidworks and python by starting up solidworks and running a macro.
The macro OPEN_FILE.swp will then run all solidworks operations and run all python executables.
"""
import subprocess
import os
import psutil
import tkinter as tk
import sys
from tkinter import ttk
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
parent_dir = os.path.dirname(script_dir)
options = os.path.join("op.txt")
solidworksPath = "/YourSolidworksPath"
def open_solidworks_macro():
macro_path = os.path.join(parent_dir, "OPEN_FILE.swp")
solidworks_executable = (
solidworksPath
)
print(solidworks_executable)
try:
if not os.path.exists(solidworks_executable):
raise Exception("SolidWorks executable not found.")
cmd = [solidworks_executable, "/m", macro_path]
subprocess.Popen(cmd, shell=True)
print(f'SolidWorks macro "{macro_path}" has been opened.')
SystemExit
except Exception as e:
error_message = f"An error occurred: {str(e)}"
print(error_message)
SystemExit
def check_sldworks_running():
for proc in psutil.process_iter():
try:
if proc.name() == "SLDWORKS.exe":
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
if __name__ == "__main__":
sld_bool = check_sldworks_running()
if sld_bool:
root = tk.Tk()
root.title("Be careful!")
window_width = 230
window_height = 100
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
label = tk.Label(
root,
text="O Solidworks will close,\n Save files before closing!",
)
label.grid(row=0, column=0, columnspan=2, pady=10)
def exit_program():
root.destroy()
subprocess.call(["taskkill", "/f", "/im", "SLDWORKS.exe"])
open_solidworks_macro()
wait_root = tk.Tk()
wait_root.overrideredirect(True)
wait_root.geometry(
"+{}+{}".format(
(wait_root.winfo_screenwidth() - 250) // 2,
(wait_root.winfo_screenheight() - 100) // 2,
)
)
wait_root.configure(bg="#34495E") # Set background color
border_frame = ttk.Frame(wait_root, style="White.TFrame")
border_frame.pack(padx=10, pady=10, fill="both", expand=True)
wait_label = tk.Label(
border_frame,
text="Wait,\nSolidworks initializing",
fg="white",
bg="#34495E",
)
wait_label.pack(pady=10)
def close_wait_window():
wait_root.destroy()
wait_root.after(5000, close_wait_window)
wait_root.mainloop()
raise SystemExit
yes_button = tk.Button(root, text="Ok", command=exit_program, width=10)
yes_button.grid(row=1, column=0, padx=5, pady=10)
def close_window():
root.destroy()
raise SystemExit
root.protocol("WM_DELETE_WINDOW", close_window)
no_button = tk.Button(root, text="Cancel", command=close_window, width=10)
no_button.grid(row=1, column=1, padx=5, pady=10)
root.mainloop()
open_solidworks_macro()
SystemExit