Files
Integration/IntegrationEgaltech/BTL.cs
T
Dario Sassi d1a9adef64 Integration 2.1b3 :
- correzioni e modifiche durante incontro con ib.
2019-02-08 08:06:17 +00:00

322 lines
17 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 int projectId;
public int elementId;
public string fileName;
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, string dirPath)
{
// Se la directory in cui salvare il file non esiste viene creata
DirectoryInfo di = new DirectoryInfo( dirPath) ;
if ( ! di.Exists)
di.Create() ;
// Il nome del file cambia a seconda che si tratti del caso Produzione o caso Ordine
if ( isFromProject)
fileName = "Part_" + projectId.ToString() + "_" + elementId.ToString() + ".btl" ;
else
fileName = "Bar_" + productionId.ToString() + "_" + patternId.ToString() + ".btl";
// Path completa del nome del file da salvare
string filePath = Path.Combine( di.FullName, fileName) ;
// Se un file txt (contenente messaggio di errore) con lo stesso nome esiste già viene cancellato
if ( File.Exists( Path.ChangeExtension( filePath, ".txt")))
File.Delete( Path.ChangeExtension( filePath, ".txt")) ;
// Se non esistono pezzi, non faccio alcunché
if ( parts == null)
return filePath ;
// 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);
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();
tw.WriteLine(Constants.USERATTRIBUTE + "\"INVERTED\":" + "\"" + inv + "\"");
string rot = singlePart.rotated.ToString();
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) {
// I parametri Pnn arrivano al 26, dopo arrivano i parametri Qnn
if ( i < 27) {
// Stampo i vari parametri Pnn
if ( i != 15 || ! (singleFeature.processKey.Contains("060") || singleFeature.processKey.Contains("061")))
tw.Write("P" + i.ToString("D2") + ":" + MultAndConvertTo8(singleParameter) + " ");
else
tw.Write("P" + i.ToString("D2") + ":" + "\"" + singleParameter + "\" ");
if ( i == 26)
tw.WriteLine(" "); // Vado a capo
}
else {
// Stampo i vari parametri Qnn diversi da 0, preceduti da "USERATTRIBUTE:"
if ( ! singleParameter.Equals( "0")) {
string rp = singleParameter.Replace(",", ".");
tw.WriteLine(Constants.USERATTRIBUTE + "\"Q" + (i - 26).ToString("D2") + "\":\"" + singleParameter + "\" ");
}
}
i++;
}
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 == "")
return ;
string[] arrParam = sag1.Split(new string[] { "POLY([", "),", ")],'P','O')" }, StringSplitOptions.RemoveEmptyEntries);
// Se il parametro Q10 è 1, l'ordine dell'array viene invertito
if ( singleFeature.processParameters[35] == "1")
Array.Reverse( arrParam) ;
int count = 0; // Nel caso in cui ci troviamo alla fine di un Arco questa viene incrementata 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++) {
char[] separators = { ',', '(' };
string[] parts = arrParam[i].Split(separators); // ogni parametro P di POLY viene splittato in più parti
// il primo elemento di parts[] indica se ci troviamo all'inizio di un Arco o no
switch (parts[0]) {
case "P":
// Se StartPoint
if ( i == 0) {
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);
}
// altrimenti se FineArco
else if ( flagArco) {
count++;
flagArco = false ;
}
// altrimenti FineSegmento
else {
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);
}
break;
case "A": // Caso InizioArco
tw.WriteLine(Constants.PROCESS_KEY + singleFeature.processKey + " " + singleFeature.designation);
tw.Write(Constants.PROCESS_PARAMETERS);
string[] nextParts = arrParam[i + 1].Split(separators);
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);
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);
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;
}
}
}