Skip to content

Commit

Permalink
Merge pull request #10 from DankoCaboski/abas
Browse files Browse the repository at this point in the history
Abas
  • Loading branch information
DankoCaboski authored Jul 5, 2024
2 parents 43ea857 + f5a1dc1 commit 1bc34fc
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 170 deletions.
Binary file modified requirements.txt
Binary file not shown.
15 changes: 10 additions & 5 deletions src/openness/controllers/OpennessController.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ def create_project(
error_creating = self.openness_service.tia.create_project(proj_name, proj_path)
if error_creating:
raise Exception(error_creating)
self.openness_service.tia.add_hardware(hardwware)
self.openness_service.tia.wire_profinet()
self.openness_service.tia.import_blocks(blocks_to_import)
hardwware_count = hardwware.__len__()
if hardwware_count > 0:
self.openness_service.tia.add_hardware(hardwware)
if hardwware_count > 1:
self.openness_service.tia.wire_profinet()
self.openness_service.tia.import_blocks(blocks_to_import)
self.openness_service.tia.save_project()
return "Projeto criado com sucesso!"
status = "Projeto criado com sucesso!"
return status
except Exception as e:
return "Erro ao gerar projeto: " + str(e)
error = "Erro ao gerar projeto: " + str(e)
return error


def open_project(self, project_path):
Expand Down
1 change: 1 addition & 0 deletions src/openness/services/LanguageService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from openness.services.Utils import Utils


class LanguageService:
def __init__(self):
self.language_composition = None
Expand Down
65 changes: 65 additions & 0 deletions src/openness/services/RobotService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from openness.services.UDTService import UDTService
from openness.services.Utils import Utils


class RobotService:
def __init__(self, tia_service) -> None:
self.tia_service = tia_service
self.dependencies = r"\\AXIS-SERVER\Users\Axis Server\Documents\xmls\dependence"


def manege_robot(self, robots_association: dict):
try:
brands = list(robots_association.keys())
for robot_brand in brands:
qtd = robots_association.get(robot_brand)
if qtd is None or str(qtd) == '':
continue
qtd = int(qtd)
if qtd > 1:
print("qtd > 1")
for i in range(qtd):
self.create_robot_structure(robot_brand + "_" + str(i), robot_brand)
else:
self.create_robot_structure(robot_brand, robot_brand)
except Exception as e:
print("Error manege_robot: ", e)


def create_robot_structure(self, robot_name, robot_brand):
op_gp = self.tia_service.recursive_folder_search(None, "03_Blocos Operacionais")
if not op_gp:
op_gp = self.tia_service.create_group(None, "03_Blocos Operacionais", None)

rb_gp = self.tia_service.recursive_folder_search(op_gp.Groups, "03.5_Robos")
if not rb_gp:
rb_gp = self.tia_service.create_group(None, "03.5_Robos", "03_Blocos Operacionais")

group_name = robot_name + '_group'
robot_group = self.tia_service.create_group(None, group_name, "03.5_Robos")
self.import_robot_bk(robot_group, robot_brand)


def import_robot_bk(self, robot_group, robot_robot_brand : str):
try:
robot_robot_brand = robot_robot_brand.upper()

generated_block_name = Utils().get_attibutes(["Name"],robot_group)
print(f'Importing {robot_robot_brand} robot block to {generated_block_name[0]}...')
bk_path:str = ""
if robot_robot_brand == 'ABB':
bk_path = r"\\AXIS-SERVER\Users\Axis Server\Documents\xmls\03_Blocos Operacionais\robots\bk_abb.xml"
elif robot_robot_brand == 'FANUC':
bk_path = r"\\AXIS-SERVER\Users\Axis Server\Documents\xmls\03_Blocos Operacionais\robots\bk_fanuc.xml"

udts = UDTService().list_udt_from_bk(bk_path)

for udt in udts:
udt_path = self.dependencies + '\\' + udt + '.xml'
device = self.tia_service.get_device_by_index(0)
self.tia_service.import_data_type(device, udt_path)

self.tia_service.import_block(robot_group.Blocks, bk_path)

except Exception as e:
print("Error importing robot block: ", e)
77 changes: 53 additions & 24 deletions src/openness/services/TiaService.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from openness.services.CompilerService import CompilerService
from openness.services.LanguageService import LanguageService
from openness.services.XmlService import XmlService
from openness.services.RobotService import RobotService


class TiaService:
def __init__(self, tia, hwf, comp):
Expand All @@ -25,6 +27,11 @@ def save_project(self):
print(RPA_status)
return RPA_status

def get_project(self):
if self.myproject is None:
raise "No project open"
return self.myproject

def open_tia_ui(self):
# Create an instance of Tia Portal
self.tia_instance = self.tia.TiaPortal(self.tia.TiaPortalMode.WithUserInterface)
Expand Down Expand Up @@ -67,7 +74,7 @@ def get_all_devices(self):
print('Error getting all devices:', e)

def get_device_by_index(self, index):
cpu_list = self.get_all_devices(self.myproject)
cpu_list = self.get_all_devices()
cpu = cpu_list[index]
return cpu

Expand All @@ -85,8 +92,9 @@ def add_hardware(self, hardwware: list):
if self.myproject != None:
if deviceType == "PLC":
print('Creating CPU: ', deviceName)
config_Plc = "OrderNumber:"+deviceMlfb+"/"+FirmVersion
deviceCPU = self.myproject.Devices.CreateWithItem(config_Plc, deviceName, deviceName)
config_Plc = "OrderNumber:6ES7 512-1SK01-0AB0/V2.5"
# config_Plc = "OrderNumber:"+deviceMlfb+"/"+FirmVersion #
deviceCPU = self.myproject.Devices.CreateWithItem(config_Plc, deviceName, deviceName)
self.my_devices.append(deviceCPU)

elif deviceType == "IHM":
Expand Down Expand Up @@ -237,28 +245,42 @@ def get_types(self, cpu):
type_group = plc_software.TypeGroup
return type_group.Types

def recursive_folder_search(self, groups, group_name):
def recursive_folder_search(self, group, group_name: str):
try:
found = groups.Find(group_name)
if group == None:
for device in self.my_devices:
plc_software = self.hwf.get_software(device)
group = plc_software.BlockGroup.Groups

found = group.Find(group_name)
if found:
return found

for group in groups.GetEnumerator():
for group in group.GetEnumerator():
found = self.recursive_folder_search(group.Groups, group_name)
if found:
return found
return

return False

except Exception as e:
print('Error searching group:', e)


def create_group(self, device, group_name, parent_group):
def create_group(self, device, group_name: str, parent_group: str):
try:
if device is None:
device = self.my_devices[0]
plc_software = self.hwf.get_software(device)
groups = plc_software.BlockGroup.Groups
if not parent_group:
return groups.Create(group_name)
else:
return self.recursive_folder_search(groups, parent_group).Groups.Create(group_name)
mygroup = self.recursive_folder_search(groups, parent_group)
if mygroup:
return mygroup.Groups.Create(group_name)
else:
raise Exception(f"Parent {parent_group} group not found")

except Exception as e:
print('Error creating group:', e)
Expand All @@ -268,7 +290,7 @@ def import_data_type(self, cpu, data_type_path):
udts_dependentes = XmlService().list_udt_from_xml(data_type_path)
for udt in udts_dependentes:
udt_path = data_type_path.rsplit(".xml", 1)[0] + "\\" + udt + ".xml"
self.import_data_type(self.myproject, cpu, udt_path)
self.import_data_type(cpu, udt_path)

types = self.get_types(cpu)
if type(data_type_path) == str:
Expand All @@ -279,7 +301,7 @@ def import_data_type(self, cpu, data_type_path):
except Exception as e:
if str(e).__contains__("culture"):
LanguageService().add_language(self.myproject, "pt-BR")
self.import_data_type(self.myproject, cpu, data_type_path)
self.import_data_type(cpu, data_type_path)

else:
print('Error importing data type from: ', data_type_path)
Expand Down Expand Up @@ -317,26 +339,33 @@ def export_data_type(self, device, data_type_name : str, data_type_path : str):
print('Error exporting data type while in service:', e)


def import_blocks(self, block_list: list):
return
for block in block_list:
self.import_block(block['device'], block['file_path'])
def import_blocks(self, block_list: dict):
robos = block_list['robots']
robos = RobotService(self).manege_robot(robos)
turntables = block_list['turntables']
grippers = block_list['grippers']


def import_block(self, object, file_path):
try:
import_options = self.tia.ImportOptions.Override
xml_file_info = Utils().get_file_info(file_path)

if str(object.GetType()) == "Siemens.Engineering.HW.DeviceImpl":
object = object.DeviceItems[1]
print(f"Importing block to CPU: {object}")
plc_software = self.hwf.get_software(object)
plc_software.BlockGroup.Blocks.Import(xml_file_info, import_options)

elif str(object.GetType()) == "Siemens.Engineering.SW.Blocks.PlcBlockComposition":
print(f"Importing block to group: {object}")
object.Import(xml_file_info, import_options)
object_type = str(object.GetType())

if object_type != "Siemens.Engineering.HW.DeviceImpl" and object_type != "Siemens.Engineering.SW.Blocks.PlcBlockComposition":
raise Exception("Invalid object type: ", object_type)

else:
if object_type == "Siemens.Engineering.HW.DeviceImpl":
object = object.DeviceItems[1]
print(f"Importing block to CPU: {object}")
plc_software = self.hwf.get_software(object)
plc_software.BlockGroup.Blocks.Import(xml_file_info, import_options)

elif object_type == "Siemens.Engineering.SW.Blocks.PlcBlockComposition":
print(f"Importing block to group: {object}")
object.Import(xml_file_info, import_options)

return True

Expand Down
16 changes: 16 additions & 0 deletions src/openness/services/UDTService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re

class UDTService:
def __init__(self) -> None:
pass

def list_udt_from_bk(self, bk_path):
udts = []
with open(bk_path, 'r', encoding='utf-8') as file:
conteudo = file.read()
pattern = r'<Member[^>]+Datatype="&quot;([^&]+)&quot;"[^>]*>'

matches = re.findall(pattern, conteudo)
for match in matches:
udts.append(match)
return udts
7 changes: 6 additions & 1 deletion src/openness/services/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def get_tia_versions(self):
mylist.append(str(version[0]))
return mylist

def get_attibutes(self, attribute_names, item):
def get_attibutes(
self,
attribute_names: list[str],
item
):

cs_attribute_names = List[str]()
for i in attribute_names:
cs_attribute_names.Add(i)
Expand Down
2 changes: 1 addition & 1 deletion src/view/components/CustomButton.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_button(self) -> customtkinter.CTkButton:
if self.image is not None:
button = customtkinter.CTkButton(self.frame, image=self.image, text="", width=24, height=24, command=self.command)
elif self.texto is not None:
button = customtkinter.CTkButton(self.frame, text=self.texto, command=self.command)
button = customtkinter.CTkButton(self.frame, text=self.texto, height=24, command=self.command)
else:
raise ValueError("Both 'texto' and 'image' cannot be None.")

Expand Down
5 changes: 2 additions & 3 deletions src/view/components/FakeTab.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from CustomTkinter import customtkinter

import tkinter as tk

class FakeTab:
def __init__(self, frame, texto=None, command=None):
self.frame = frame
Expand All @@ -10,7 +8,8 @@ def __init__(self, frame, texto=None, command=None):

def get_button(self) -> customtkinter.CTkButton:
button = customtkinter.CTkButton(
self.frame, text=self.texto,
self.frame,
text=self.texto,
command=self.command,
fg_color="transparent",
bg_color="transparent",
Expand Down
30 changes: 30 additions & 0 deletions src/view/components/InputConveyor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from CustomTkinter import customtkinter
from tkinter import BooleanVar

class InputConveyor:
def __init__(self, frame):
self.frame: customtkinter.CTkFrame = frame

self.inversor = BooleanVar()
self.sensor_presenca = BooleanVar()
self.sensor_posicao = BooleanVar()

self.main_content()


def main_content(self):

lb_inversor = customtkinter.CTkLabel(self.frame, text="Intversor?")
lb_inversor.grid(row=0, column=0, padx=3, pady=3, sticky='ws')
inversor = customtkinter.CTkCheckBox(self.frame, variable=self.inversor)
inversor.grid(row=1, column=0, padx=3, pady=3, sticky='wn')

lb_s_presenca = customtkinter.CTkLabel(self.frame, text="Sensor de presença?")
lb_s_presenca.grid(row=0, column=1, padx=3, pady=3, sticky='ws')
s_presenca = customtkinter.CTkCheckBox(self.frame, variable=self.sensor_presenca)
s_presenca.grid(row=1, column=1, padx=3, pady=3, sticky='wn')

lb_s_pos = customtkinter.CTkLabel(self.frame, text="Sensor de posição?")
lb_s_pos.grid(row=0, column=2, padx=3, pady=3, sticky='ws')
s_pos = customtkinter.CTkCheckBox(self.frame, variable=self.sensor_posicao)
s_pos.grid(row=1, column=2, padx=3, pady=3, sticky='wn')
Loading

0 comments on commit 1bc34fc

Please sign in to comment.