Files
Integration/IntegrationEgaltech/BTL.cs
T
Renzo Lanza 54249c1766 Integration 2.1a1 :
- primo rilascio.
2019-01-17 15:54:00 +00:00

339 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ib.essetre.integration.egaltech
{
// La classe BTL rappresenta un singolo file BTL e come esso può contenere una o più Part (ovvero le travi di legno)
// ed ogni Part può contenere una o più Feature (ovvero i tagli da effettuare)
public class BTL
{
public string projectNumber;
public string scaleUnit;
public string barLength;
public List<Part> parts;
public List<Feature> features;
//I seguenti Id servono per il nome del file BTL
public int productionId;
public int patternId;
public string fileName;
public int projectId;
public int elementId;
public BTL()
{
}
public BTL(string projectNumber, string scaleUnit, List<Part> parts, List<Feature> features)
{
this.projectNumber = projectNumber;
this.scaleUnit = scaleUnit;
this.parts = parts;
this.features = features;
}
public string writeIntoFile(bool isFromProject)
{
// La directory in cui salvare il file viene qui inizializzata
DirectoryInfo di = new DirectoryInfo(Path.GetTempPath() + @"FileBTL\");
// Se la directory non esiste viene creata
if (!di.Exists)
{
di.Create();
}
//Il nome del file cambia a seconda che si tratti del caso Produzione o caso Ordine
string typeName = null;
if (isFromProject)
typeName = "part";
else
typeName = "bar";
// Si combina in un'unica stringa il percorso della directory e il nome del file da salvare
fileName = String.Format("{0}.btl", typeName + "_" + this.productionId + "_" + this.patternId); // DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")));
string filePath = Path.Combine(di.FullName, fileName); // DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")));
// Se un file txt (contenente messaggio di errore) con lo stesso nome esiste già viene cancellato
if (File.Exists(filePath.Replace(".btl", ".txt")))
{
File.Delete(filePath.Replace(".btl", ".txt"));
}
// Il seguente blocco 'using' si occupa della scrittura del file di testo, poi salvato in .btl
using (var tw = new StreamWriter(filePath, false))
{
tw.WriteLine(Constants.VERSION);
tw.WriteLine(Constants.BUILD);
tw.WriteLine(Constants.GENERAL);
tw.WriteLine(Constants.PROJECT_NUMBER + this.projectNumber);
tw.WriteLine(Constants.SCALE_UNIT + this.scaleUnit);
if (isFromProject)
tw.WriteLine(Constants.USERATTRIBUTE + "\"BARLEN\":" + "\"N/A\""); // Nel caso Ordine non c'è il BarLength (colonna "l" nel DB)
else
tw.WriteLine(Constants.USERATTRIBUTE + "\"BARLEN\":" + "\"" + this.barLength + "\"");
foreach (Part singlePart in parts)
{
tw.WriteLine(Constants.PART);
tw.WriteLine(Constants.SINGLE_MEMBER_NUMBER + singlePart.singleMemberNumber);
tw.WriteLine(Constants.COUNT + singlePart.count);
double len = (Convert.ToDouble(singlePart.length)) * Math.Pow(10, Constants.scaleUnit);
int intLen = (Convert.ToInt32(len));
tw.WriteLine(Constants.LENGTH + intLen.ToString("D8"));
double hei = (Convert.ToDouble(singlePart.height)) * Math.Pow(10, Constants.scaleUnit);
int intHei = (Convert.ToInt32(hei));
tw.WriteLine(Constants.HEIGHT + intHei.ToString("D8"));
double wid = (Convert.ToDouble(singlePart.width)) * Math.Pow(10, Constants.scaleUnit);
int intWid = (Convert.ToInt32(wid));
tw.WriteLine(Constants.WIDTH + intWid.ToString("D8"));
string posX = singlePart.x.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
tw.WriteLine(Constants.USERATTRIBUTE + "\"POSX\":" + "\"" + posX + "\"");
string inv = singlePart.inverted.ToString(); // "0.00", System.Globalization.CultureInfo.InvariantCulture);
tw.WriteLine(Constants.USERATTRIBUTE + "\"INVERTED\":" + "\"" + inv + "\"");
string rot = singlePart.rotated.ToString(); // "0.00", System.Globalization.CultureInfo.InvariantCulture);
tw.WriteLine(Constants.USERATTRIBUTE + "\"ROTATED\":" + "\"" + rot + "\"");
if (isFromProject)
tw.WriteLine(Constants.USERATTRIBUTE + "\"ELEMENTID\":" + "\"" + singlePart.elementId + "\"");
else
tw.WriteLine(Constants.USERATTRIBUTE + "\"CUTID\":" + "\"" + singlePart.cutId + "\"");
foreach (Feature singleFeature in singlePart.features)
{
// se il processKey contiene "250" allora si tratta di un Contorno Libero e viene analizzato come tale
if (singleFeature.processKey.Contains("250"))
{
FreeContourAnalyzer(singleFeature.sag1, tw, singleFeature);
}
else
{
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
int i = 1;
foreach (string singleParameter in singleFeature.processParameters)
{
if (i < 27) // I parametri Pnn arrivano al 26, dopo arrivano i parametri Qnn
{
// Stampo i vari parametri Pnn
if (i != 15)
tw.Write("P" + i.ToString("D2") + ":" + MultAndConvertTo8(singleParameter) + " ");
else
tw.Write("P" + i.ToString("D2") + ":" + "\"\" "); // P15: ""
}
else
{
// Stampo i vari parametri Qnn preceduti da "COMMENT:"
if (i == 27)
{
tw.WriteLine(" "); // Vado a capo
tw.Write(Constants.COMMENT);
}
tw.Write("Q" + (i - 26).ToString("D2") + ":" + MultAndConvertTo8(singleParameter) + " ");
}
//Convert.ToInt16(i);
i++;
}
tw.WriteLine(" "); // Vado a capo
tw.WriteLine(Constants.PROCESS_IDENT + singleFeature.processIdent);
tw.WriteLine(Constants.PROCESS + singleFeature.process);
}
if (isFromProject)
tw.WriteLine(Constants.USERATTRIBUTE + "\"PROCESSID\":" + "\"" + singleFeature.processId + "\"");
else
tw.WriteLine(Constants.USERATTRIBUTE + "\"TASKID\":" + "\"" + singleFeature.taskId + "\"");
}
}
}
return filePath;
}
// Il seguente metodo analizza il campo sag1 della singleFeature passata: se non è vuoto allora la stringa
// viene splittata nei parametri P di POLY ed analizzata
public void FreeContourAnalyzer(string sag1, StreamWriter tw, Feature singleFeature)
{
if (sag1 != "")
{
string[] arrParam = sag1.Split(new string[] { "POLY([P(", ")],'P','C')", "),P(" }, StringSplitOptions.RemoveEmptyEntries);
int count = 0; // Nel caso in cui ci troviamo alla fine di un Arco questa viene incrmentata di 1, in modo da scrivere il numero parametro corretto nei seguenti cicli
Boolean flagArco = false; // Nel caso in cui ci troviamo all'inizio di un Arco questa viene posta a TRUE, in modo da passare nel percorso dedicato nel prossimo ciclo
for (int i = 0; i < arrParam.Length; i++)
{
string[] parts = arrParam[i].Split(','); // ogni parametro P di POLY viene splittato in più parti
switch (parts[0]) // il primo elemento di parts[] indica se ci troviamo all'inizio di un Arco o no
{
case "0":
if (i == 0) // Caso StartPoint
{
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
tw.Write("P01:" + MultAndConvertTo8(parts[1]) + " "); // x
tw.Write("P02:" + MultAndConvertTo8(parts[2]) + " "); // y
tw.Write("P03:" + MultAndConvertTo8(parts[3]) + " "); // z
tw.Write("P05:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P06:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P07:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P08:" + Constants.EIGHT_ZEROS + " ");
if (i == arrParam.Length - 1) // Verifica se ci troviamo all'ultimo parametro di POLY
tw.Write("P09:" + Constants.EIGHT_ZEROS + " "); // L'ultimo elemento di POLY deve avere "P09: 00000000"
else
tw.Write("P09:" + MultAndConvertTo8((Convert.ToDouble(singleFeature.processParameters[8]) + i - count).ToString()) + " ");
tw.Write("P10:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P11:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P12:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P13:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P14:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P15:" + Constants.EIGHT_ZEROS + " ");
tw.WriteLine(" ");
tw.WriteLine(Constants.PROCESS_IDENT + (Convert.ToInt16(singleFeature.processIdent) + i - count));
tw.WriteLine(Constants.PROCESS + singleFeature.process);
//tw.WriteLine(Constants.USERATTRIBUTE + "\"TASKID\":" + "\"" + singleFeature.taskId + "\"");
}
else if (flagArco) // Caso FineArco
{
count++;
flagArco = false;
}
else // Caso StraightLine
{
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
tw.Write("P01:" + MultAndConvertTo8(parts[1]) + " "); // x
tw.Write("P02:" + MultAndConvertTo8(parts[2]) + " "); // y
tw.Write("P03:" + MultAndConvertTo8(parts[3]) + " "); // z
tw.Write("P05:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P06:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P07:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P08:" + "00000100" + " ");
if (i == arrParam.Length - 1)
tw.Write("P09:" + Constants.EIGHT_ZEROS + " ");
else
tw.Write("P09:" + MultAndConvertTo8((Convert.ToDouble(singleFeature.processParameters[8]) + i - count).ToString()) + " ");
tw.Write("P10:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P11:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P12:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P13:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P14:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P15:" + Constants.EIGHT_ZEROS + " ");
tw.WriteLine(" ");
tw.WriteLine(Constants.PROCESS_IDENT + (Convert.ToInt16(singleFeature.processIdent) + i - count));
tw.WriteLine(Constants.PROCESS + singleFeature.process);
//tw.WriteLine(Constants.USERATTRIBUTE + "\"TASK\":" + "\"" + singleFeature.taskId + "\"");
}
break;
case "1": // Caso InizioArco
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
string[] nextParts = arrParam[i + 1].Split(',');
tw.Write("P01:" + MultAndConvertTo8(nextParts[1]) + " "); // x (EndPoint Arc)
tw.Write("P02:" + MultAndConvertTo8(nextParts[2]) + " "); // y (EndPoint Arc)
tw.Write("P03:" + MultAndConvertTo8(nextParts[3]) + " "); // z (Endpoint Arc)
tw.Write("P05:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P06:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P07:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P08:" + "00000200" + " ");
if (i == arrParam.Length - 1)
tw.Write("P09:" + Constants.EIGHT_ZEROS + " ");
else
tw.Write("P09:" + MultAndConvertTo8((Convert.ToDouble(singleFeature.processParameters[8]) + i - count).ToString()) + " ");
tw.Write("P10:" + MultAndConvertTo8(parts[1]) + " "); // x (Point on Arc)
tw.Write("P11:" + MultAndConvertTo8(parts[2]) + " "); // y (Point on Arc)
tw.Write("P12:" + MultAndConvertTo8(parts[3]) + " "); // z (Point on Arc)
tw.Write("P13:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P14:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P15:" + Constants.EIGHT_ZEROS + " ");
tw.WriteLine(" ");
tw.WriteLine(Constants.PROCESS_IDENT + (Convert.ToInt16(singleFeature.processIdent) + i - count));
tw.WriteLine(Constants.PROCESS + singleFeature.process);
//tw.WriteLine(Constants.USERATTRIBUTE + "\"TASK\":" + "\"" + singleFeature.taskId + "\"");
flagArco = true;
break;
default:
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
tw.Write("P01:" + MultAndConvertTo8(parts[1]) + " ");
tw.Write("P02:" + MultAndConvertTo8(parts[2]) + " ");
tw.Write("P03:" + MultAndConvertTo8(parts[3]) + " ");
tw.Write("P05:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P06:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P07:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P08:" + Constants.EIGHT_ZEROS + " ");
if (i == arrParam.Length - 1)
tw.Write("P09:" + Constants.EIGHT_ZEROS + " ");
else
tw.Write("P09:" + MultAndConvertTo8((Convert.ToDouble(singleFeature.processParameters[8]) + i - count).ToString()) + " ");
tw.Write("P10:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P11:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P12:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P13:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P14:" + Constants.EIGHT_ZEROS + " ");
tw.Write("P15:" + Constants.EIGHT_ZEROS + " ");
tw.WriteLine(" ");
tw.WriteLine(Constants.PROCESS_IDENT + (Convert.ToInt16(singleFeature.processIdent) + i - count));
tw.WriteLine(Constants.PROCESS + singleFeature.process);
//tw.WriteLine(Constants.USERATTRIBUTE + "\"TASK\":" + "\"" + singleFeature.taskId + "\"");
break;
}
}
}
}
// Il seguente metodo prende il valore di un process parameter, lo moltiplica per 10^scaleUnit
// e lo converte in una stringa di 8 caratteri (i caratteri mancanti sono degli 0)
public string MultAndConvertTo8(string processParameter)
{
double sp = 0;
string rp = processParameter.Replace(",", ".");
sp = (Convert.ToDouble(rp, CultureInfo.InvariantCulture)) * Math.Pow(10, Constants.scaleUnit);
int intSp = (Convert.ToInt32(sp));
string s8 = intSp.ToString("D8");
return s8;
}
// Le 2 seguenti struct dichiarano i campi necessari alla costruzione di una Part e di una Feature
public struct Part
{
public string singleMemberNumber;
public string count;
public string length;
public string height;
public string width;
public List<Feature> features;
public double x;
public string inverted;
public string rotated;
public int elementId;
public int cutId;
}
public struct Feature
{
public string taskId;
public string processKey;
public string designation;
public List<string> processParameters;
public string processIdent;
public string process;
public string sag1;
public string processId;
}
}
}