天天看點

Ezcam 自動輸出Gerber274格式資料

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import ezcam
import os, sys
if sys.version_info[0] == 2:
    from Tkinter import *
    from tkFont import Font
    from ttk import *
    from tkMessageBox import *
    from tkinter import messagebox
else:  #Python 3.x
    from tkinter import *
    from tkinter.font import Font
    from tkinter.ttk import *
    from tkinter.messagebox import *
    from tkinter import messagebox

def step_open(job_name):
    '''定義每個job中打開的step'''
    matrix_info = ezcam.DO_INFO(args = '-t matrix -e {job}/matrix -m json'.format(job=job_name))
    for i in range(len(matrix_info['gCOLcol'])):
        step_type=matrix_info['gCOLtype'][i]
        if step_type != 'empty':
            open_step = matrix_info['gCOLstep_name'][i]
        break
    return open_step


class Application_ui(Frame):
    #這個類僅實作界面生成功能,具體事件處理代碼在子類Application中。
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('自動輸出Gerber274X(ezCAM平台:V1.0)')
        #獲得螢幕的寬高
        screenwidth = self.master.winfo_screenwidth()
        screenheight = self.master.winfo_screenheight()
        size = '%dx%d+%d+%d' % (400, 600, (screenwidth - 400) / 2, (screenheight - 600) / 2)
        #設定寬高并移動位置  參數格式:%dx%d+%d+%d   如:300x250+500+240
        self.master.geometry(size)
        #建立部件
        self.createWidgets()

    #設定預設的step
    def SetDefaultSetp(self,Steps):
        for stp in Steps:
            if stp == 'panel':
                #index():從清單中找出某個值第一個比對項的索引位置
                return Steps.index('panel')
        return 0

    def createWidgets(self):
        self.top = self.winfo_toplevel()

        self.style = Style()

        self.stp_list = []
        self.job_name = os.environ['JOB']
        self.stp_matrix = ezcam.DO_INFO(args = '-t matrix -e {job}/matrix -d COL -m json'.format(job=self.job_name))  
        for item in self.stp_matrix['gCOLstep_name']:
            if item != '':
                self.stp_list.append(item)
                #self.comboxlist.insert(END,item)  
                
        self.lb1 = Label(self.top, text = "請選取Step", font = "tahoma 12 normal")
        self.lb1.place(relx=0.05, rely=0.01, relwidth=0.3, relheight=0.06)
        self.ComboboxVar = StringVar()
        self.comboxlist=Combobox(self.top,textvariable=self.ComboboxVar) #初始化
        #self.comboxlist["values"]=("1","2","3","4")
        self.comboxlist["values"]=self.stp_list
        self.comboxlist.place(relx=0.3, rely=0.01, relwidth=0.65, relheight=0.06)
        self.comboxlist.current(self.SetDefaultSetp(self.stp_list))
                
        self.style.configure('TFrame3.TLabelframe', font=('宋體',12))
        self.style.configure('TFrame3.TLabelframe.Label', font=('宋體',12))
        self.Frame3 = LabelFrame(self.top, text='請選取層别', style='TFrame3.TLabelframe')
        self.Frame3.place(relx=0.02, rely=0.1, relwidth=0.95, relheight=0.78)

        ## 确認,采集資訊
        self.Command1Var = StringVar(value='确定')
        self.style.configure('TCommand1.TButton', font=('宋體',14))
        self.Command1 = Button(self.top, text='确定', textvariable=self.Command1Var, command=self.OK, style='TCommand1.TButton')
        self.Command1.setText = lambda x: self.Command1Var.set(x)
        self.Command1.text = lambda : self.Command1Var.get()
        self.Command1.place(relx=0.1, rely=0.9, relwidth=0.3, relheight=0.07)
        self.Command1.bind('<<ListboxSelect>>', self.List1_Button_1)
        
        ## 定屬性排序
        self.Command2Var = StringVar(value='取消')
        self.style.configure('TCommand2.TButton', font=('宋體',14))
        self.Command2 = Button(self.top, text='取消', textvariable=self.Command2Var, command=self.cancel, style='TCommand2.TButton')
        self.Command2.setText = lambda x: self.Command2Var.set(x)
        self.Command2.text = lambda : self.Command2Var.get()
        self.Command2.place(relx=0.6, rely=0.9, relwidth=0.3, relheight=0.07)
        
        ## 層别
        self.sb = Scrollbar(self.Frame3)    #垂直滾動條元件
        self.sb.pack(side=RIGHT,fill=Y)  #設定垂直滾動條顯示的位置
        
        self.List1Var = StringVar(value='')
        self.List1Font = Font(font=('宋體',12))
        self.List1 = Listbox(self.Frame3, listvariable=self.List1Var, font=self.List1Font,yscrollcommand=self.sb.set,selectmode=MULTIPLE)
        self.List1.place(relx=0.03, rely=0.03, relwidth=0.92, relheight=0.94)   #設定list1的位置
        self.List1.bind('<<ListboxSelect>>', self.List1_Button_1)               #綁定list1控件
        self.lyr_list = []
        self.job_name = os.environ['JOB']
        self.lyr_matrix = ezcam.DO_INFO(args = '-t matrix -e {job}/matrix -d ROW -m json'.format(job=self.job_name))  
        for item in self.lyr_matrix['gROWname']:
            if item != '':
                self.lyr_list.append(item)
                self.List1.insert(END,item)  
        #self.List1.update_idletasks()
                 
        self.sb.config(command=self.List1.yview) #設定Scrollbar元件的command選項為該元件的yview()方法




class Application(Application_ui):
    #這個類實作具體的事件處理回調函數。界面生成代碼在Application_ui中。
    def __init__(self, master=None):
        Application_ui.__init__(self, master)
        self.lyr_name = ''

    def List1_Button_1(self, event):
        print(f'self.List1.curselection()[0]={self.List1.curselection()}')
        # self.num = self.List1.curselection()[0]
        # self.lyr_name = self.lyr_list[self.num]
        # print(f'self.lyr_name={self.lyr_name}')
        
    def cancel(self, event=None):
        self.master.quit()


    def Contourse(self, step_name):   
        stp_sr = []
        print(step_name)
        stp_sr = ezcam.DO_INFO(args=f'-t step -e {job_name}/{step_name} -d SR  -m json')
        print(len(stp_sr['gSRstep']))
        if len(stp_sr['gSRstep']) == 0  and step_name not in self.AlreadDoStep:
            self.AlreadDoStep.append(step_name)
            ezcam.COM('open_entity', job=job_name, type='step', name=step_name, iconic=False)
            ezcam.COM('units', type='Inch')
            ezcam.COM('affected_layer', name='', mode='all', affected=False)
            ezcam.COM('clear_layers')
            for layIndex in self.List1.curselection():
                #ezcam.COM('display_layer', name=self.lyr_list[layIndex], display=True, number=1)
                #ezcam.COM('work_layer', name=self.lyr_list[layIndex])
                ezcam.COM('affected_layer', name=self.lyr_list[layIndex], mode='single', affected=True)
                ezcam.COM('sel_contourize', accuracy=0.1, break_to_islands=True, clean_hole_size=3, clean_hole_mode='x_and_y')
                ezcam.COM('affected_layer', name=self.lyr_list[layIndex], mode='single', affected=False)
            ezcam.COM('editor_page_close')    
            return
        for each_step in  stp_sr['gSRstep']:  
            self.Contourse(each_step)

            
    
    def OK(self, event=None):
        
       # messagebox.showinfo(title='提示', message=self.comboxlist.get())
        
        if len(self.List1.curselection()) == 0:
            messagebox.showinfo(title='提示', message='您還沒有選取層别,請至少選取一個層别!')

        step_panel=self.comboxlist.get()
        #messagebox.showinfo(title='提示', message=step_panel)
        
        self.AlreadDoStep = []
        self.Contourse(step_panel)
        
        #ezcam.COM('open_entity', job=job_name, type='step', name=step_panel, iconic=False)
        #ezcam.COM('units', type='mm')
         
            
        for layIndex in self.List1.curselection():
            #ezcam.COM('display_layer', name=self.lyr_list[layIndex], display=True, number=1)
            #ezcam.COM('work_layer', name=self.lyr_list[layIndex])
            ezcam.COM('output_layer_reset')
            ezcam.COM('output_layer_set', layer=self.lyr_list[layIndex], angle=0, mirror=False, x_scale=1, y_scale=1, comp=0, polarity='positive', setupfile='', setupfiletmp='', line_units='inch', gscl_file='', step_scale=False)
            ezcam.COM('output', job=job_name, step=step_panel, format='Gerber274x', dir_path='D:/ezcam/1.1/output/', prefix='', suffix='', x_anchor=0, y_anchor=0, x_offset=0, y_offset=0, line_units='inch', break_sr=False, break_symbols=False, break_arc=False, units='inch', scale_mode='all', surface_mode='contour', min_brush=2, surface_clean_hole_overlap=0.5, coordinates='absolute', decimal=False, zeroes='none', nf1=2, nf2=6, modal='', wheel='', film_size_cross_scan='', film_size_along_scan='', ds_model='')
        
        messagebox.showinfo(title='提示', message='所有選中層别已成功輸出!')        
        self.master.quit()

        
        


if __name__ == "__main__":
    ## 進入ezCAM定義層名
    job_name = os.environ['JOB']
    Script_path=str(os.environ['EZCAM_DIR'])
    open_step = step_open(job_name)

    tk = Tk()
    Application(tk).mainloop()