using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using ib.essetre.integration; using ib.essetre.integration.egaltech; namespace ib.essetre.integration.egaltech { public class Generator : ib.essetre.integration.IGenerator { public System.Threading.Thread _thread; public static DataSet ds = new DataSet(); //public static BTL btlObject = new BTL(); public Generator() { Console.WriteLine("Inizializzato"); } public void Run(InOutParameters parameters, ICallBack callback) { _thread = new Thread(() => DoJob(parameters, callback)); _thread.Start(); } private delegate void dDoJob( InOutParameters parameters, ICallBack callback) ; // campo data ultima modifica nel DATABASE [lastModified] private static void DoJob( InOutParameters parameters, ICallBack callback) { callback.Progress( 0, "Init") ; String ExePath = parameters.Read( "EGALTECH", "PATH_EXE", "") ; String BtlDir = parameters.Read( "EGALTECH", "DIR_BTL", "") ; int numBars = parameters.Patterns.Length ; for ( int i = 0; i < numBars; i++) { BTL btlObject = new BTL() ; // Se il parametro IsFromProject è TRUE viene eseguita la query sulle tabelle Project, // se invece è FALSE viene eseguita la query sulle tabelle Production string sql = "" ; if ( parameters.Patterns[i].IsFromProject) { sql = "SELECT P.projectId, " + "P.elementId, 0 cutId, processId, info1, info2, enabled, level, " + "inTools, outTools, isChecked, P.done, isoType, ax, ay, az, P.x, P.y, P.z, " + "P.flagDeleted, P.[group], [key], face, edge, des, " + "p01, p02, p03, p04, p05, p06, p07, p08, p09, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, " + "q01, q02, q03, q04, q05, q06, q07, q08, q09, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19, q20, " + "priority, processIdent, processingQuality, sag1, P.projectId, P.elementId, P.processId, 10 cutStart, 0 cutEnd, " + "E.inverted, E.rotated, 0 doneTime, 0 startAngle, 0 endAngle, -1 referenceCutId, E.length, E.width, E.height " + "FROM dbo.vw_Process P " + "INNER JOIN dbo.vw_Element E ON P.projectId=E.projectId and P.elementId=E.elementId " + "WHERE P.projectId = @firstId" + " and E.elementId = @secondId"; } else { sql = "SELECT vw_Task.productionId, " + "vw_Task.patternId, vw_Task.cutId ,taskId, info1, info2, enabled, level, " + "inTools,outTools,isChecked,vw_Task.done,isoType,ax,ay,az, vw_Cut.x, vw_Cut.y, vw_Cut.z, " + "vw_Task.flagDeleted, [group], [key], face, edge, des, " + "p01, p02, p03, p04, p05, p06, p07, p08, p09, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, " + "q01, q02, q03, q04, q05, q06, q07, q08, q09, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19, q20, " + "priority, processIdent, processingQuality, sag1, projectId, elementId, cutStart, cutEnd, " + "inverted, rotated, doneTime, startAngle, endAngle, referenceCutId, length, width, height " + "FROM ESSETRE.dbo.vw_Task " + "INNER JOIN vw_Cut ON " + "(vw_Task.cutId = vw_Cut.cutId AND vw_Task.patternId = vw_Cut.patternId AND vw_Task.productionId = vw_Cut.productionId) " + "WHERE vw_Task.patternId = @firstId" + " and vw_Task.productionId = @secondId"; } // Connessione al DB ed esecuzione query using ( SqlConnection cn = new SqlConnection( parameters.ConnectionString)) { cn.Open() ; using (SqlCommand cmd = new SqlCommand( sql, cn)) { cmd.Parameters.Add( "@firstId", SqlDbType.Int) ; cmd.Parameters.Add( "@secondId", SqlDbType.Int) ; if ( parameters.Patterns[i].IsFromProject) { cmd.Parameters["@firstId"].Value = (Int32)parameters.Patterns[i].ProjectId ; cmd.Parameters["@secondId"].Value = (Int32)parameters.Patterns[i].ElementId ; } else { cmd.Parameters["@firstId"].Value = (Int32)parameters.Patterns[i].PatternId ; cmd.Parameters["@secondId"].Value = (Int32)parameters.Patterns[i].ProductionId ; } var dataAdapter = new SqlDataAdapter( cmd) ; // Lettura DB e riempimento DataSet ds.Clear() ; dataAdapter.Fill( ds) ; } } // Verifica parametro isFromProject: nel caso Ordine viene ottenuta la lista dei Process, // nel caso Produzione viene ottenuta la lista dei Task if ( parameters.Patterns[i].IsFromProject) getListaProcesses( btlObject, 0) ; else getListaTasks( btlObject, 0) ; // Dialog con Progress Bar float progValue = (float) 1 / numBars ; callback.Progress( progValue * (i + 1), " Progresso: " + (progValue * (i + 1) * 100) + "%" + "\n" + " BTL generati: " + (i + 1) + " su " + numBars); // Si ottiene il percorso delle cartelle in cui salvare i FileBTL e si // esegue EgtCAM5 in base ad alcuni parametri passati nella string path string BtlPath = btlObject.writeIntoFile(parameters.Patterns[i].IsFromProject, BtlDir); string path = "\"" + BtlPath + "\" " + parameters.MachineName + " " + (int)parameters.UIMode + " " + parameters.Patterns[i].OutputFilename ; Process process = Process.Start( ExePath, path) ; // Attendo il termine del processo while ( ! process.HasExited) { process.Refresh() ; System.Threading.Thread.Sleep( 50) ; } // Recupero il risultato int cutId = 0 ; int taskId = 0 ; PatternInfo.Results resExe = PatternInfo.Results.NONE ; string textErr = "" ; if ( File.Exists( Path.ChangeExtension( BtlPath, ".txt"))) { string[] lines = System.IO.File.ReadAllLines( Path.ChangeExtension( BtlPath, ".txt")) ; foreach (string line in lines) { if ( line.StartsWith( "ERR=")) { int? nVal = GetVal(line, "ERR") ; if ( nVal == 0) resExe = PatternInfo.Results.OK ; else resExe = PatternInfo.Results.ERROR; } else if ( line.StartsWith( "CUTID=")) { int? nVal = GetVal( line, "CUTID") ; if ( nVal != null) cutId = nVal.Value ; } else if ( line.StartsWith( "TASKID=")) { int? nVal = GetVal(line, "TASKID") ; if ( nVal != null) taskId = nVal.Value; } else if (!string.IsNullOrWhiteSpace(line)) textErr = line; } } parameters.Patterns[i].SetTaskState( cutId, taskId, resExe, textErr) ; //parameters.Patterns[i].GetTaskState( cutId, taskId, out PatternInfo.Results res, out string msg) ; //System.Windows.Forms.MessageBox.Show( msg, res.ToString()) ; // Lettura OutputFilename e scrittura nel parametro stringa Iso (solo nel caso UIModes.HIDDEN) if ( parameters.UIMode == InOutParameters.UIModes.HIDDEN) { string CncPath = Path.ChangeExtension( BtlPath, ".cnc") ; if ( File.Exists( CncPath)) { string lines = System.IO.File.ReadAllText(CncPath) ; parameters.Patterns[i].Iso = lines ; parameters.Patterns[i].Result = PatternInfo.Results.OK ; parameters.Patterns[i].DetailResult = PatternInfo.DetailResults.ALL_OK ; } else { parameters.Patterns[i].Iso = "" ; parameters.Patterns[i].Result = PatternInfo.Results.ERROR ; parameters.Patterns[i].DetailResult = PatternInfo.DetailResults.ALL_KO ; } ////imposta le modifiche ////solo se parameters.Patterns[i].Transformable //parameters.Patterns[i].AddCutChange(2, "inverted", 180); //parameters.Patterns[i].AddPatternChange("StartX", 20); //public enum Results //{ // NONE = 0, // OK = 1, // ERROR = 2 //} ////fattibile o non fattibile ////parameters.Patterns[i].Iso;=.... //stringa iso ////parameters.Patterns[i].OutputFilename; //nome file da generare ////parameters.Patterns[i].SetTaskState(cutId, taskId, value, message); //Cutid del db //parameters.Patterns[i].Result = PatternInfo.Results.OK; //parameters.Patterns[i].Message = ""; //eventuale errore messaggio // } //esempio di apertura db, usare solo le viste (iniziano per vw_) // chiavi //parameters.Patterns(0).ProductionId //cn.Close(); //} } } callback.Progress( 1, "End") ; callback.Done( parameters) ; //fine } private static int? GetVal( string sText, string sKey) { string[] sParts = sText.Split( new Char[] { '=' }) ; if ( string.Compare( sParts[0], sKey) != 0) return null ; if ( ! int.TryParse( sParts[1], out int nVal)) return null ; else return nVal ; } public void Abort() { //non implementata al momento } // Il seguente metodo prende i record visualizzati in ds (Caso: Produzione) e li carica in un oggetto BTL public static void getListaTasks( BTL btlObject, int i) { DataTable dTab = ds.Tables[i] ; Int32 listaTasksCount = dTab.Rows.Count ; if ( listaTasksCount == 0) return ; DataRow dRow0 = dTab.Rows[0] ; btlObject.projectNumber = dRow0["projectId"].ToString() ; btlObject.scaleUnit = "2" ; btlObject.barLength = Convert.ToString( getBarLengthP( (int)dRow0["patternId"], (int)dRow0["productionId"])) ; btlObject.productionId = (int)dRow0["productionId"] ; btlObject.patternId = (int)dRow0["patternId"] ; btlObject.parts = new List() ; List ListaCutParts = getListaCutIdP( (int)dRow0["patternId"], (int)dTab.Rows[i]["productionId"]) ; for ( int c = 0 ; c < ListaCutParts.Count ; c++) { BTL.Part readPart = new BTL.Part() ; bool bFirst = true ; for ( int f = 0; f < listaTasksCount; f++) { DataRow dRowF = dTab.Rows[f] ; if ( (int)dRowF["cutId"] != ListaCutParts[c]) continue ; if ( bFirst) { readPart.singleMemberNumber = dRowF["cutId"].ToString() ; readPart.count = "1" ; readPart.length = dRowF["length"].ToString() ; readPart.height = dRowF["height"].ToString() ; readPart.width = dRowF["width"].ToString() ; readPart.x = (double)dRowF["x"] ; readPart.inverted = dRowF["inverted"].ToString() ; readPart.rotated = dRowF["rotated"].ToString() ; readPart.cutId = (int)dRowF["cutId"] ; bFirst = false ; readPart.features = new List() ; } BTL.Feature readFeature = new BTL.Feature() ; readFeature.taskId = dRowF["taskId"].ToString() ; readFeature.processKey = dRowF["group"].ToString() + "-" + dRowF["key"].ToString().Substring( 1, 3) + "-" + dRowF["face"].ToString() ; readFeature.designation = dRowF["des"].ToString() ; readFeature.processIdent = dRowF["processIdent"].ToString() ; readFeature.sag1 = dRowF["sag1"].ToString() ; if ( Convert.ToInt16( dRowF["enabled"]) == 1) readFeature.process = "YES" ; else readFeature.process = "NO" ; readFeature.processParameters = new List( 46) ; readFeature.processParameters.Add( dRowF["p01"].ToString()) ; readFeature.processParameters.Add( dRowF["p02"].ToString()) ; readFeature.processParameters.Add( dRowF["p03"].ToString()) ; readFeature.processParameters.Add( dRowF["p04"].ToString()) ; readFeature.processParameters.Add( dRowF["p05"].ToString()) ; readFeature.processParameters.Add( dRowF["p06"].ToString()) ; readFeature.processParameters.Add( dRowF["p07"].ToString()) ; readFeature.processParameters.Add( dRowF["p08"].ToString()) ; readFeature.processParameters.Add( dRowF["p09"].ToString()) ; readFeature.processParameters.Add( dRowF["p10"].ToString()) ; readFeature.processParameters.Add( dRowF["p11"].ToString()) ; readFeature.processParameters.Add( dRowF["p12"].ToString()) ; readFeature.processParameters.Add( dRowF["p13"].ToString()) ; readFeature.processParameters.Add( dRowF["p14"].ToString()) ; readFeature.processParameters.Add( dRowF["p15"].ToString()) ; readFeature.processParameters.Add( dRowF["p16"].ToString()) ; readFeature.processParameters.Add( dRowF["p17"].ToString()) ; readFeature.processParameters.Add( dRowF["p18"].ToString()) ; readFeature.processParameters.Add( dRowF["p19"].ToString()) ; readFeature.processParameters.Add( dRowF["p20"].ToString()) ; readFeature.processParameters.Add( dRowF["p21"].ToString()) ; readFeature.processParameters.Add( dRowF["p22"].ToString()) ; readFeature.processParameters.Add( dRowF["p23"].ToString()) ; readFeature.processParameters.Add( dRowF["p24"].ToString()) ; readFeature.processParameters.Add( dRowF["p25"].ToString()) ; readFeature.processParameters.Add( dRowF["p26"].ToString()) ; readFeature.processParameters.Add( dRowF["q01"].ToString()) ; readFeature.processParameters.Add( dRowF["q02"].ToString()) ; readFeature.processParameters.Add( dRowF["q03"].ToString()) ; readFeature.processParameters.Add( dRowF["q04"].ToString()) ; readFeature.processParameters.Add( dRowF["q05"].ToString()) ; readFeature.processParameters.Add( dRowF["q06"].ToString()) ; readFeature.processParameters.Add( dRowF["q07"].ToString()) ; readFeature.processParameters.Add( dRowF["q08"].ToString()) ; readFeature.processParameters.Add( dRowF["q09"].ToString()) ; readFeature.processParameters.Add( dRowF["q10"].ToString()) ; readFeature.processParameters.Add( dRowF["q11"].ToString()) ; readFeature.processParameters.Add( dRowF["q12"].ToString()) ; readFeature.processParameters.Add( dRowF["q13"].ToString()) ; readFeature.processParameters.Add( dRowF["q14"].ToString()) ; readFeature.processParameters.Add( dRowF["q15"].ToString()) ; readFeature.processParameters.Add( dRowF["q16"].ToString()) ; readFeature.processParameters.Add( dRowF["q17"].ToString()) ; readFeature.processParameters.Add( dRowF["q18"].ToString()) ; readFeature.processParameters.Add( dRowF["q19"].ToString()) ; readFeature.processParameters.Add( dRowF["q20"].ToString()) ; readPart.features.Add( readFeature) ; } btlObject.parts.Add( readPart) ; } // L'ordinamento delle Part in un oggetto BTL avviene col confronto del parametro x, estratto dalla tabella vw_Cut btlObject.parts.Sort( (p, q) => p.x.CompareTo( q.x)) ; } // Il seguente metodo prende i record visualizzati in ds (Caso: Ordine) e li carica in un oggetto BTL public static void getListaProcesses( BTL btlObject, int i) { DataTable dTab = ds.Tables[i] ; Int32 listaProcessesCount = dTab.Rows.Count ; if ( listaProcessesCount == 0) return ; DataRow dRow0 = dTab.Rows[0] ; btlObject.projectNumber = dRow0["projectId"].ToString() ; btlObject.scaleUnit = " 2" ; btlObject.barLength = Convert.ToString( getBarLengthO( (int)dRow0["elementId"], (int)dRow0["projectId"])) ; btlObject.projectId = (int)dRow0["projectId"] ; btlObject.elementId = (int)dRow0["elementId"] ; btlObject.parts = new List() ; List ListaCutParts = new List( 1) ; ListaCutParts.Add( (int)dRow0["elementId"]) ; for ( int c = 0 ; c < ListaCutParts.Count ; c++) { BTL.Part readPart = new BTL.Part() ; bool bFirst = true ; for ( int f = 0 ; f < listaProcessesCount ; f++) { DataRow dRowF = dTab.Rows[f] ; if ( (int)dRowF["elementId"] != ListaCutParts[c]) continue ; if ( bFirst) { readPart.singleMemberNumber = dRowF["elementId"].ToString() ; readPart.count = "1" ; readPart.length = dRowF["length"].ToString() ; readPart.height = dRowF["height"].ToString() ; readPart.width = dRowF["width"].ToString() ; readPart.x = (double)dRowF["x"] ; readPart.inverted = dRowF["inverted"].ToString() ; readPart.rotated = dRowF["rotated"].ToString() ; readPart.elementId = (int)dRowF["elementId"] ; bFirst = false ; readPart.features = new List() ; } BTL.Feature readFeature = new BTL.Feature() ; readFeature.processId = dRowF["processId"].ToString() ; readFeature.processKey = dRowF["group"].ToString() + "-" + dRowF["key"].ToString().Substring( 1, 3) + "-" + dRowF["face"].ToString() ; readFeature.designation = dRowF["des"].ToString() ; readFeature.processIdent = dRowF["processIdent"].ToString() ; readFeature.sag1 = dRowF["sag1"].ToString() ; if ( Convert.ToInt16( dRowF["enabled"]) == 1) readFeature.process = "YES" ; else readFeature.process = "NO" ; readFeature.processParameters = new List( 46) ; readFeature.processParameters.Add( dRowF["p01"].ToString()) ; readFeature.processParameters.Add( dRowF["p02"].ToString()) ; readFeature.processParameters.Add( dRowF["p03"].ToString()) ; readFeature.processParameters.Add( dRowF["p04"].ToString()) ; readFeature.processParameters.Add( dRowF["p05"].ToString()) ; readFeature.processParameters.Add( dRowF["p06"].ToString()) ; readFeature.processParameters.Add( dRowF["p07"].ToString()) ; readFeature.processParameters.Add( dRowF["p08"].ToString()) ; readFeature.processParameters.Add( dRowF["p09"].ToString()) ; readFeature.processParameters.Add( dRowF["p10"].ToString()) ; readFeature.processParameters.Add( dRowF["p11"].ToString()) ; readFeature.processParameters.Add( dRowF["p12"].ToString()) ; readFeature.processParameters.Add( dRowF["p13"].ToString()) ; readFeature.processParameters.Add( dRowF["p14"].ToString()) ; readFeature.processParameters.Add( dRowF["p15"].ToString()) ; readFeature.processParameters.Add( dRowF["p16"].ToString()) ; readFeature.processParameters.Add( dRowF["p17"].ToString()) ; readFeature.processParameters.Add( dRowF["p18"].ToString()) ; readFeature.processParameters.Add( dRowF["p19"].ToString()) ; readFeature.processParameters.Add( dRowF["p20"].ToString()) ; readFeature.processParameters.Add( dRowF["p21"].ToString()) ; readFeature.processParameters.Add( dRowF["p22"].ToString()) ; readFeature.processParameters.Add( dRowF["p23"].ToString()) ; readFeature.processParameters.Add( dRowF["p24"].ToString()) ; readFeature.processParameters.Add( dRowF["p25"].ToString()) ; readFeature.processParameters.Add( dRowF["p26"].ToString()) ; readFeature.processParameters.Add( dRowF["q01"].ToString()) ; readFeature.processParameters.Add( dRowF["q02"].ToString()) ; readFeature.processParameters.Add( dRowF["q03"].ToString()) ; readFeature.processParameters.Add( dRowF["q04"].ToString()) ; readFeature.processParameters.Add( dRowF["q05"].ToString()) ; readFeature.processParameters.Add( dRowF["q06"].ToString()) ; readFeature.processParameters.Add( dRowF["q07"].ToString()) ; readFeature.processParameters.Add( dRowF["q08"].ToString()) ; readFeature.processParameters.Add( dRowF["q09"].ToString()) ; readFeature.processParameters.Add( dRowF["q10"].ToString()) ; readFeature.processParameters.Add( dRowF["q11"].ToString()) ; readFeature.processParameters.Add( dRowF["q12"].ToString()) ; readFeature.processParameters.Add( dRowF["q13"].ToString()) ; readFeature.processParameters.Add( dRowF["q14"].ToString()) ; readFeature.processParameters.Add( dRowF["q15"].ToString()) ; readFeature.processParameters.Add( dRowF["q16"].ToString()) ; readFeature.processParameters.Add( dRowF["q17"].ToString()) ; readFeature.processParameters.Add( dRowF["q18"].ToString()) ; readFeature.processParameters.Add( dRowF["q19"].ToString()) ; readFeature.processParameters.Add( dRowF["q20"].ToString()) ; readPart.features.Add( readFeature) ; } btlObject.parts.Add( readPart) ; } // L'ordinamento delle Part in un oggetto BTL avviene col confronto del parametro x, estratto dalla tabella vw_Cut btlObject.parts.Sort( (p, q) => p.x.CompareTo( q.x)) ; } // Il seguente metodo si connette al DB per estrarre tramite una query i cutId della tabella vw_Cut // aventi patternId e ProductionId che corrispondono a quelli passati come argomento private static List getListaCutIdP(int patternId, int productionId) { List ListaCutId = new List() ; string sqlCutId = "SELECT cutId FROM dbo.vw_Cut WHERE patternId = @firstId " + "AND productionId = @secondId"; // Connessione al DB ed esecuzione query using ( SqlConnection cn = new SqlConnection( Constants.ConnectionString)) { cn.Open() ; using ( SqlCommand sqlCommand = new SqlCommand( sqlCutId, cn)) { sqlCommand.Parameters.Add( "@firstId", SqlDbType.Int) ; sqlCommand.Parameters.Add( "@secondId", SqlDbType.Int) ; sqlCommand.Parameters["@firstId"].Value = patternId ; sqlCommand.Parameters["@secondId"].Value = productionId ; SqlDataReader reader = sqlCommand.ExecuteReader() ; while ( reader.Read()) { ListaCutId.Add( (int)reader["cutId"]) ; } } } return ListaCutId ; } // Recupera la lunghezza della barra nel caso Produzione (colonna "l") private static double getBarLengthP( int patternId, int productionId) { double barLength = 0 ; string sqlBarLength = "SELECT l FROM dbo.vw_Pattern WHERE patternId = @firstId " + "AND productionId = @secondId" ; // Connessione al DB ed esecuzione query using ( SqlConnection cn = new SqlConnection( Constants.ConnectionString)) { cn.Open() ; using ( SqlCommand sqlCommand = new SqlCommand( sqlBarLength, cn)) { sqlCommand.Parameters.Add( "@firstId", SqlDbType.Int) ; sqlCommand.Parameters.Add( "@secondId", SqlDbType.Int) ; sqlCommand.Parameters["@firstId"].Value = patternId ; sqlCommand.Parameters["@secondId"].Value = productionId ; SqlDataReader reader = sqlCommand.ExecuteReader() ; while ( reader.Read()) { barLength = Convert.ToDouble( reader["l"]) ; } } } return barLength ; } // Recupera la lunghezza della barra nel caso Ordine (colonna "length", ovvero la lunghezza della singola trave) // a cui aggiungiamo un valore fisso (es. 2000) private static double getBarLengthO( int elementId, int projectId) { double barLength = 0 ; string sqlBarLength = "SELECT length FROM dbo.vw_Element WHERE elementId = @firstId " + "AND projectId = @secondId"; // Connessione al DB ed esecuzione query using ( SqlConnection cn = new SqlConnection( Constants.ConnectionString)) { cn.Open() ; using ( SqlCommand sqlCommand = new SqlCommand( sqlBarLength, cn)) { sqlCommand.Parameters.Add( "@firstId", SqlDbType.Int) ; sqlCommand.Parameters.Add( "@secondId", SqlDbType.Int) ; sqlCommand.Parameters["@firstId"].Value = elementId ; sqlCommand.Parameters["@secondId"].Value = projectId ; SqlDataReader reader = sqlCommand.ExecuteReader() ; while ( reader.Read()) { barLength = Convert.ToDouble( reader["length"]) ; } } } return ( barLength + 2000) ; } } }