using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.InteropServices; using static CMS_CORE_Library.Models.DataStructures; namespace CMS_CORE_Library.Utils { internal static class Nc_Utils { //------------------------------------------------------------------------------------------------------------------------------------------------- // NC <-> .NET Conversions // INT -> 2 WORD internal static void IntTOWord(int IntVal, out ushort LowWord, out ushort HighWord) { byte[] bytes = BitConverter.GetBytes(IntVal); //Split into 2 word LowWord = BitConverter.ToUInt16(bytes, 0); HighWord = BitConverter.ToUInt16(bytes, 2); } // 2 WORD -> INT internal static void WordTOInt(out int IntVal, ushort LowWord, ushort HighWord) { byte[] bytes = new byte[4]; //fill the bytes from 2 word bytes[0] = BitConverter.GetBytes(LowWord)[0]; bytes[1] = BitConverter.GetBytes(LowWord)[1]; bytes[2] = BitConverter.GetBytes(HighWord)[0]; bytes[3] = BitConverter.GetBytes(HighWord)[1]; //Create the value IntVal = BitConverter.ToInt32(bytes, 0); } // INT -> 2 WORD (LIST) internal static List IntTOWordList(List UintVal) { // Initialize the new List List Word = new List(); // Fill the List foreach (int val in UintVal) { IntTOWord(val, out ushort low, out ushort high); Word.Add(low); Word.Add(high); } return Word; } // 2 WORD -> INT (LIST) internal static List WordTOIntList(List UintVal) { // Initialize the new List List Int = new List(); // Fill the List for (int i = 0; i < UintVal.Count; i = i + 2) { WordTOInt(out int value, UintVal[i], UintVal[i + 1]); Int.Add(value); } return Int; } //------------------------------------------------------------------------------------------------------------------------------------------------- // NC <-> .NET Conversions // UINT -> 2 WORD internal static void UIntTOWord(uint UintVal, out ushort LowWord, out ushort HighWord) { byte[] bytes = BitConverter.GetBytes(UintVal); // Split into 2 word LowWord = BitConverter.ToUInt16(bytes, 0); HighWord = BitConverter.ToUInt16(bytes, 2); } // 2 WORD -> UINT internal static void WordTOUint(out uint UintVal, ushort LowWord, ushort HighWord) { byte[] bytes = new byte[4]; // Fill the bytes from 2 word bytes[0] = BitConverter.GetBytes(LowWord)[0]; bytes[1] = BitConverter.GetBytes(LowWord)[1]; bytes[2] = BitConverter.GetBytes(HighWord)[0]; bytes[3] = BitConverter.GetBytes(HighWord)[1]; // Create the value UintVal = BitConverter.ToUInt32(bytes, 0); } // UINT -> 2 WORD (LIST) internal static List UIntTOWordList(List UintVal) { // Initialize the new List List Word = new List(); // Fill the List foreach (uint val in UintVal) { UIntTOWord(val, out ushort low, out ushort high); Word.Add(low); Word.Add(high); } return Word; } // 2 WORD -> UINT (LIST) internal static List WordTOUintList(List UintVal) { // Initialize the new List List uInt = new List(); // Fill the List for (int i = 0; i < (UintVal.Count); i = i + 2) { WordTOUint(out uint value, UintVal[i], UintVal[i + 1]); uInt.Add(value); } return uInt; } internal static void WriteLocalFile(string fileContent, ref FileStream fileReference) { // Write Part Program using (StreamWriter writer = new StreamWriter(fileReference)) writer.Write(fileContent); } internal static string ReadLocalFile(FileStream fileReference) { // Get File Content StreamReader reader = new StreamReader(fileReference); string fileContent = reader.ReadToEnd(); return fileContent; } // Get a substring from the beginning until the specified character -> defautl "[" internal static string GetStringUntilOrEmpty(string text, string stopAt = "[") { // Get string unti if (!String.IsNullOrWhiteSpace(text)) { // Find character index int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal); if (charLocation > 0) return text.Substring(0, charLocation); } return String.Empty; } internal static Dictionary convToDictIntBol(List origVal) { Byte[] byteArray = null; // Convert List into Byte Array byteArray = origVal.SelectMany(j => BitConverter.GetBytes(j)).ToArray(); // converto a bit BitArray bitArr = new BitArray(byteArray); Dictionary answ = new Dictionary(); for (int i = 0; i < bitArr.Count; i++) { answ.Add(i, bitArr[i]); } return answ; } internal static Dictionary convToDictIntInt(List origVal) { Dictionary answ = new Dictionary(); for (int i = 0; i < origVal.Count; i++) { answ.Add(i, origVal[i]); } return answ; } // Convert one integer in array of bools internal static bool[] IntToBits(int val) { return IntToBits(new int[] { val }); } /// /// Swap byte in 32bit DWord (uint/uint) /// /// /// internal static uint SwapBytes(uint x) { // swap adjacent 16-bit blocks x = (x >> 16) | (x << 16); // swap adjacent 8-bit blocks return ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8); } /// /// Swap byte in 32bit DWord (int/int) /// /// /// internal static int SwapBytes(int x) { byte[] bytes = BitConverter.GetBytes(x); Array.Reverse(bytes); int result = BitConverter.ToInt32(bytes, 0); return result; } // Convert array of integers in an array of bools internal static bool[] IntToBits(int[] val) { return new BitArray(val.ToArray()) .Cast() .ToArray(); } // Convert a byte into an array of bools public static bool[] ByteToBits(byte val) { return new BitArray(new byte[] { val }) .Cast() .ToArray(); } // Convert a generic number into an array of byte internal static byte[] NumberToByte(object val) { return BitConverter.GetBytes(Convert.ToInt32(val)); } internal static List IntsToBytes(List ints) { return ints.SelectMany(BitConverter.GetBytes).ToList(); } internal static List ListOfByteToListOf(byte[] array, Func bitConverter) { var size = Marshal.SizeOf(typeof(T)); return Enumerable.Range(0, array.Length / size) .Select(i => bitConverter(array, i * size)) .ToList(); } internal static SIEMENS_MAGAZINE_TYPE ConvertToSiemensMagazineType(NC_MAGAZINE_TYPE type) { switch (type) { case NC_MAGAZINE_TYPE.CHAIN: return SIEMENS_MAGAZINE_TYPE.CHAIN; case NC_MAGAZINE_TYPE.BOX_MAGAZINE: return SIEMENS_MAGAZINE_TYPE.BOX_MAGAZINE; case NC_MAGAZINE_TYPE.DISK: return SIEMENS_MAGAZINE_TYPE.REVOLVER; case NC_MAGAZINE_TYPE.MAGAZINE_TOOL_BUFFER: return SIEMENS_MAGAZINE_TYPE.MAGAZINE_TOOL_BUFFER; case NC_MAGAZINE_TYPE.INVISIBLE_MAGAZINE: return SIEMENS_MAGAZINE_TYPE.INVISIBLE_MAGAZINE; } return SIEMENS_MAGAZINE_TYPE.BOX_MAGAZINE; } internal static string[] ExtractM154ParametersFromNcCodeLine(string codeLine) { string[] returnVal = new string[1]; int startPos = 0, endPos = 0; // Get first position startPos = codeLine.IndexOf('('); if (startPos == -1) startPos = codeLine.IndexOf(';'); // Get second position endPos = codeLine.IndexOf(')', startPos + 1); if (startPos != -1 && endPos == -1) endPos = codeLine.Length; // if not exists if (startPos != -1) { string tmpString = codeLine; if (endPos != -1) tmpString = codeLine.Substring(startPos + 1, endPos - startPos - 1); returnVal = tmpString.Trim(';').Split(','); } return returnVal; } internal static string[] ExtractM155ParametersFromNcCodeLine(string codeLine, char startBrackets = '[', char endBrackets = ']') { string[] returnVal = new string[1]; int startPos = 0, endPos = 0; // Get first position startPos = codeLine.IndexOf(startBrackets); if (startPos == -1) startPos = codeLine.IndexOf(';'); // Get the final parameters index endPos = codeLine.IndexOf(endBrackets, startPos + 1); if (startPos != -1 && endPos == -1) endPos = codeLine.Length; // If parameters exist if (startPos != -1) { string tmpString = codeLine; if (endPos != -1) tmpString = codeLine.Substring(startPos + 1, endPos - startPos - 1); // Remove command string ex: M155 returnVal = tmpString.Split(','); } else { returnVal = new string[0]; } return returnVal; } public static bool GetBitValue(int b, int bitNumber) { return (b & (1 << bitNumber)) != 0; } public static bool GetBitValue(byte b, int bitNumber) { return (b & (1 << bitNumber)) != 0; } public static int SetBitOne(this int value, int position) { // Set a bit at position to 1. return value |= (1 << position); } public static int SetBitZero(this int value, int position) { // Set a bit at position to 0. return value & ~(1 << position); } public static CultureInfo GetCultureFromThreeLetter(string threeLetter) { // Get culture info from three letter iso standard return CultureInfo .GetCultures(CultureTypes.NeutralCultures) .Where(ci => string.Equals(ci.ThreeLetterISOLanguageName, threeLetter, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); } } }