-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserInterface.py
176 lines (152 loc) · 7.16 KB
/
userInterface.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import clr
clr.AddReference('C:\Program Files\Siemens\Automation\Portal V18\PublicAPI\V18\Siemens.Engineering.dll')
import Siemens.Engineering.HW
import os
import ctypes
#from PIL import Image
import tkinter as tk
from tkinter import filedialog
from openness import TIAProject
from xmlHandler import extract_info
class ProjectGeneratorUI:
def __init__(self, master):
self.master = master
self.master.iconbitmap('.\\cora.ico')
#self.set_taskbar_icon(master, 'C:\\Users\\Cassioli\\Documents\\Automation\\Openess\\TIAprojectGenerator\\cora.png')
self.master.title("TIA Project Generator")
self.tia_project = TIAProject()
self.xml_path = ""
self.project_path = ""
self.project_name = ""
self.devices = []
self.file_label = tk.Label(master, text="1. Choose the HW file (TIA Selection Tool|*.tia):")
self.file_label.grid(row=0, column=0, sticky="w", padx=10, pady=5)
self.file_entry = tk.Entry(master, width=50)
self.file_entry.grid(row=0, column=1, padx=10, pady=5)
self.browse_button = tk.Button(master, text="Search", command=self.browse_xml)
self.browse_button.grid(row=0, column=2, padx=10, pady=5)
self.project_label = tk.Label(master, text="2. Choose how to save the project:")
self.project_label.grid(row=1, column=0, sticky="w", padx=10, pady=5)
self.project_entry = tk.Entry(master, width=50)
self.project_entry.grid(row=1, column=1, padx=10, pady=5)
self.browse_project_button = tk.Button(master, text="Search", command=self.browse_project)
self.browse_project_button.grid(row=1, column=2, padx=10, pady=5)
self.create_button = tk.Button(master, text="Create Project", command=self.create_project)
self.create_button.grid(row=3, columnspan=3, padx=10, pady=10)
self.output_label = tk.Label(master, text="Progress:")
self.output_label.grid(row=4, column=0, sticky="w", padx=10, pady=5)
self.output_text = tk.Text(master, width=50, height=10)
self.output_text.grid(row=5, columnspan=3, padx=10, pady=5)
# def set_taskbar_icon(self, window, icon_path):
# # Carregar a biblioteca de usuário do Windows
# user32 = ctypes.windll.user32
# # Carregar o ícone
# # Converter a imagem para um formato suportado (BMP)
# icon = Image.open(icon_path)
# bmp_path = "icon.bmp"
# icon.save(bmp_path, format="BMP")
# # Obter o identificador da janela
# hwnd = user32.GetParent(window.winfo_id())
# # Definir o ícone da janela na barra de tarefas
# user32.SendMessageW(hwnd, 0x80, 1, icon_path) # WM_SETICON
def browse_xml(self):
file_path = filedialog.askopenfilename(filetypes=[("TIA Selection Tools Files", "*.tia")])
self.xml_path = file_path
self.devices = extract_info(self.xml_path)
self.file_entry.delete(0, tk.END)
self.file_entry.insert(0, self.xml_path)
def browse_project(self):
project_path = filedialog.asksaveasfilename()
self.project_path = os.path.dirname(project_path)
self.project_name = os.path.basename(project_path)
self.project_entry.delete(0, tk.END)
self.project_entry.insert(0, project_path)
def create_project(self):
self.output_text.delete(1.0, tk.END) # Clear previous output
if not self.xml_path or not self.project_path or not self.project_name:
self.output_text.insert(tk.END, "Please, fill in all fields.\n")
return
#start tia
self.output_text.delete(1.0, tk.END)
self.output_text.insert(tk.END, "Inicializing TIA Portal without UI...\n")
self.output_text.see(tk.END)
self.master.update()
try:
self.tia_project.startTIA(False)
except Exception as e:
self.output_text.insert(tk.END, f"❌ Error to open TIA: {e}\n")
self.output_text.see(tk.END)
return
else:
self.output_text.insert(tk.END, f"✔ TIA started successfully\n")
self.output_text.see(tk.END)
self.master.update()
#create project
self.output_text.insert(tk.END, f"Creating the project {self.project_name}...\n")
self.output_text.see(tk.END)
self.master.update()
try:
self.tia_project.create_project(self.project_path, self.project_name)
except Exception as e:
self.output_text.insert(tk.END, f"❌ Error to create the project: {e}\n")
self.output_text.see(tk.END)
return
else:
self.output_text.insert(tk.END, f"✔ Project created successfully\n")
self.output_text.see(tk.END)
self.master.update()
#create devices
self.output_text.insert(tk.END, f"Creating devices...\n")
self.master.update()
for idx, device in enumerate(self.devices, start=1):
self.output_text.insert(tk.END, f"Creating device {idx}: {device.name}\n")
self.output_text.see(tk.END)
self.master.update()
try:
device.instance = self.tia_project.create_device(device.name, device.modules[0], 10.0)
except Exception as e:
self.output_text.insert(tk.END, f"❌ Error to create the device: {e}\n")
self.output_text.see(tk.END)
return
else:
self.output_text.insert(tk.END, f"✔ Device created successfully\n")
self.output_text.see(tk.END)
self.master.update()
#plug modules
self.output_text.insert(tk.END, f"Plugging modules to the devices...\n")
for idx, device in enumerate(self.devices, start=1):
self.output_text.insert(tk.END, f"Plugging modules at {device.name}:\n")
self.output_text.see(tk.END)
self.master.update()
for idx, module in enumerate(device.modules, start=1):
self.output_text.insert(tk.END, f"Plugging module {idx}: {module}\n")
self.output_text.see(tk.END)
self.master.update()
try:
self.tia_project.plug_device(device, idx)
except Exception as e:
self.output_text.insert(tk.END, f"❌ Error to plug module to device: {e}\n")
self.output_text.see(tk.END)
return
else:
self.output_text.insert(tk.END, f"✔ Module plugged successfully\n")
self.output_text.see(tk.END)
self.master.update()
# def output_animated_text(self, text):
# self.output_text.delete(1.0, tk.END)
# if count <= 3:
# dots = "." * count
# self.output_text.insert(tk.END, f"{text}{dots}")
# count += 1
# elif self.animateText:
# count = 0
# self.output_text.insert(tk.END, text)
# else:
# return
# self.master.after(500, lambda: self.output_animated_text(text))
def main():
root = tk.Tk()
app = ProjectGeneratorUI(root)
root.mainloop()
if __name__ == "__main__":
main()