diff --git a/CMS_CORE_Library/Demo/Nc_Demo.cs b/CMS_CORE_Library/Demo/Nc_Demo.cs index 788c44f..c7ae126 100644 --- a/CMS_CORE_Library/Demo/Nc_Demo.cs +++ b/CMS_CORE_Library/Demo/Nc_Demo.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.ServiceModel; using System.ServiceModel.Channels; @@ -687,6 +688,31 @@ namespace CMS_CORE.Demo } #endregion + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + #region File Management + + public override void R_NCReadPartProgramToFile(string partProgramPath, string partProgramLocalName, ref FileStream localFile) + { + throw new NotImplementedException(); + } + + public override void W_NCWritePartProgramFromFile(string partProgramPath, FileStream localFile) + { + throw new NotImplementedException(); + } + + public override void NCCopyPartProgram(string partProgramPath, string newPartProgramPath, bool failIfExist) + { + throw new NotImplementedException(); + } + + public override void NCDeletePartProgram(string partProgramPath, string partProgramName) + { + throw new NotImplementedException(); + } + #endregion + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region Subordinate Private Functions diff --git a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs index 9c16ebb..4aa5131 100644 --- a/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs +++ b/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.IO; +using CMS_CORE.Utils; #pragma warning disable 1591 @@ -128,7 +130,7 @@ namespace CMS_CORE.Fanuc //Check if the NC is Connected CheckConnection(); - readStaticNCData(); + ReadStaticNCData(); Language = ConverToSTEPLanguage(FanucLanguage); } @@ -140,7 +142,7 @@ namespace CMS_CORE.Fanuc //Check if the NC is Connected CheckConnection(); - readStaticNCData(); + ReadStaticNCData(); SN = Cnc_SeriesNum; } @@ -152,7 +154,7 @@ namespace CMS_CORE.Fanuc //Check if the NC is Connected CheckConnection(); - readStaticNCData(); + ReadStaticNCData(); SWV = Cnc_SftVersion; } @@ -164,7 +166,7 @@ namespace CMS_CORE.Fanuc //Check if the NC is Connected CheckConnection(); - readStaticNCData(); + ReadStaticNCData(); ModelName = Cnc_name; } @@ -176,7 +178,7 @@ namespace CMS_CORE.Fanuc //Check if the NC is Connected CheckConnection(); - readStaticNCData(); + ReadStaticNCData(); ProcNumber = (ushort) Cnc_NumPath; } @@ -1140,6 +1142,165 @@ namespace CMS_CORE.Fanuc + #endregion + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + #region File Management + + public override void R_NCReadPartProgramToFile(string partProgramPath, string partProgramLocalName, ref FileStream localFile) + { + string partProgramContent = ""; + + // Read NC part program content + R_NCReadPartProgram(partProgramPath, ref partProgramContent); + try + { + // Write content into local file + Nc_Utils.WriteLocalFile(partProgramLocalName, partProgramContent, ref localFile); + } + catch (Exception ex) + { + ThrowNCException(INTERNAL_ERROR, 0); + } + } + + private void R_NCReadPartProgram(string partProgramPath, ref string partProgramContent) + { + //Check if the NC is Connected + CheckConnection(); + + bool fileEnded = false; + short nReturn; + int lenght = 256; + + // Start read process + nReturn = Focas1.cnc_upstart4(nLibHandle[0], 0, partProgramPath); + + //Throw Exception if there's an error + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + + do + { + // Local buffer + char[] tmpContentBuffer = new char[256];// TODO find function to read size file + + // Read file + nReturn = Focas1.cnc_upload4(nLibHandle[0], ref lenght, tmpContentBuffer); + + // If File ended + if (nReturn == (short)Focas1.focas_ret.EW_RESET) + { + fileEnded = true; + } + else if (nReturn == (short)Focas1.focas_ret.EW_OK) + { + // Add the characters into string with all the content + partProgramContent += new string(tmpContentBuffer); + } + // TODO + //else if (nReturn != (short)Focas1.focas_ret.EW_BUFFER && nReturn != Focas1.focas_ret.EW_LENGTH) // case 10 = EW_BUFFER = wait until the buffer contains data + //{ + // ThrowNCException(NC_PROD_ERROR, nReturn); + //} + } while ((nReturn == (short)Focas1.focas_ret.EW_OK || nReturn == (short)Focas1.focas_ret.EW_BUFFER) && !fileEnded); + + // End read + nReturn = Focas1.cnc_upend4(nLibHandle[0]); + + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + } + + public override void W_NCWritePartProgramFromFile(string partProgramPath, FileStream localFile) + { + string partProgramContent = ""; + + try + { + // Read local file content + partProgramContent = Nc_Utils.ReadLocalFile(localFile); + } + catch (Exception ex) + { + ThrowNCException(INTERNAL_ERROR, 0); + } + + W_NCWritePartProgram(partProgramPath, partProgramContent); + } + + private void W_NCWritePartProgram(string partProgramPath, string partProgramContent) + { + //Check if the NC is Connected + CheckConnection(); + + short nReturn; + + // Start write + nReturn = Focas1.cnc_dwnstart4(nLibHandle[0], 0, partProgramPath); + + //Throw Exception if there's an error + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + + //int lenghtMax = fileData.Length; + System.Threading.Thread.Sleep(1000); + int lenght = partProgramContent.Length; + // Write program + nReturn = Focas1.cnc_download4(nLibHandle[0], ref lenght, partProgramContent); + + //Throw Exception if there's an error + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + + nReturn = Focas1.cnc_dwnend4(nLibHandle[0]); + + //Throw Exception if there's an error + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + } + + public override void NCCopyPartProgram(string partProgramPath, string newPartProgramPath, bool failIfExist) + { + //Check if the NC is Connected + CheckConnection(); + + short nReturn; + + // If the 2 path are the same + if(partProgramPath == newPartProgramPath) + ThrowNCException(NC_PROD_ERROR, 0); // TODO FIX + + if (failIfExist) + { + // Copy Nc part program to the new path + nReturn = Focas1.cnc_pdf_copy(nLibHandle[0], partProgramPath, newPartProgramPath); + + //Throw Exception if there's an error + if (nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + } + else + { + // Try a copy and if error is 4 delete file and retry + // Find a function to check if file exist, delete and copy. + // Read and put file + // TODO + } + } + + public override void NCDeletePartProgram(string partProgramPath, string partProgramName) + { + //Check if the NC is Connected + CheckConnection(); + + short nReturn; + + nReturn = Focas1.cnc_pdf_del(nLibHandle[0], partProgramPath + "/" + partProgramName); + + if(nReturn != 0) + ThrowNCException(NC_PROD_ERROR, nReturn); + } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1263,7 +1424,7 @@ namespace CMS_CORE.Fanuc //Read Static Data - private void readStaticNCData() + private void ReadStaticNCData() { short nReturn; Focas1.ODBSYS node = new Focas1.ODBSYS(); diff --git a/CMS_CORE_Library/Nc.cs b/CMS_CORE_Library/Nc.cs index 61e95ea..314a9c2 100644 --- a/CMS_CORE_Library/Nc.cs +++ b/CMS_CORE_Library/Nc.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -564,7 +565,48 @@ namespace CMS_CORE * Reference of a variable with Param values * */ public abstract void R_NCParam(short Index, ref double Value); - + + #endregion + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + #region File Management (To override) + + /** + * Read a Part Program by path and name + * + * Thrown when an internal or a library error occours + * Path of the Part Program stored in the Nc + * Name of the Part Program and name of new local file + * Reference to the new local file where the NC Part Program data will be saved + * */ + public abstract void R_NCReadPartProgramToFile(string partProgramPath, string partProgramLocalName, ref FileStream localFile); + + /** + * Write/Overwrite a Part Program by path and name + * + * Thrown when an internal or a library error occours + * Path of the Part Program stored in the Nc + * Reference to the local Part Program file + * */ + public abstract void W_NCWritePartProgramFromFile(string partProgramPath, FileStream localFile); + + /** + * Copy a Part Program into another path + * + * Thrown when an internal or a library error occours + * Path where the Part Program is saved in the NC + * Path of the copy Part Program destination + * + * */ + public abstract void NCCopyPartProgram(string partProgramPath, string newPartProgramPath, bool failIfExist); + + /** + * Delete a Nc Part Program + * Thrown when an internal or a library error occours + * Path where the Nc Part Program is saved in the NC + * Name of the Part Program file + * */ + public abstract void NCDeletePartProgram(string partProgramPath, string partProgramName); #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/CMS_CORE_Library/Osai/Nc_Osai.cs b/CMS_CORE_Library/Osai/Nc_Osai.cs index dec2cd5..aa4b9dc 100644 --- a/CMS_CORE_Library/Osai/Nc_Osai.cs +++ b/CMS_CORE_Library/Osai/Nc_Osai.cs @@ -14,7 +14,6 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using CMS_CORE_Library.OPENControl; -using System.ComponentModel; namespace CMS_CORE.Osai { @@ -1031,6 +1030,146 @@ namespace CMS_CORE.Osai + #endregion + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + #region File Management + + public override void R_NCReadPartProgramToFile(string partProgramPath, string partProgramLocalName, ref FileStream localFile) + { + string partProgramContent = ""; + // Read NC part program content + R_NCReadPartProgram(partProgramPath, ref partProgramContent); ; + + try + { + // Write content into local file + Nc_Utils.WriteLocalFile(partProgramLocalName, partProgramContent, ref localFile); + } + catch (Exception ex) + { + ThrowNCException(ex); + } + } + + private void R_NCReadPartProgram(string partProgramPath, ref string partProgramContent) + { + //Check if the NC is Connected + CheckConnection(); + + ushort nReturn; + uint errorClass, errorNum; + uint size = 0; + + try + { + // Get part program file size + nReturn = OpenNC.LogFSGetFileSize(partProgramPath, out size, out errorClass, out errorNum); + // Read part program content + nReturn = OpenNC.GetFile(partProgramPath, ref size, out partProgramContent, out errorClass, out errorNum); + + //If there's an error launch exception + if (errorClass != 0 || errorNum != 0 || nReturn == 0) + throw new Nc_Exception(getError(NC_PROD_ERROR, errorClass, errorNum)); + } + catch (Exception ex) + { + ThrowNCException(ex); + } + } + + public override void W_NCWritePartProgramFromFile(string partProgramPath, FileStream localFile) + { + string partProgramContent = ""; + + try + { + // Read local file content + partProgramContent = Nc_Utils.ReadLocalFile(localFile); + } + catch (Exception ex) + { + ThrowNCException(ex); + } + // Write/Overwrite Nc file + W_NCWritePartProgram(partProgramPath, partProgramContent); + } + + private void W_NCWritePartProgram(string partProgramPath, string partProgramContent) + { + //Check if the NC is Connected + CheckConnection(); + + ushort nReturn; + uint errorClass, errorNum; + + try + { + // Write the local file NC + nReturn = OpenNC.PutFile(partProgramContent, (uint)partProgramContent.Length, partProgramPath, out errorClass, out errorNum); + + //If there's an error launch exception + if (errorClass != 0 || errorNum != 0 || nReturn == 0) + throw new Nc_Exception(getError(NC_PROD_ERROR, errorClass, errorNum)); + } + catch(Exception ex) + { + ThrowNCException(ex); + } + } + + public override void NCCopyPartProgram(string partProgramPath, string newPartProgramPath, bool failIfExist) + { + //Check if the NC is Connected + CheckConnection(); + + uint errorClass, errorNum; + ushort nReturn; + + // If the 2 path are the same + if (partProgramPath == newPartProgramPath) + throw new Nc_Exception(getError(NC_PROD_ERROR, 0, 0)); // TODO FIX + + try + { + // Copy Nc part program to the new path + nReturn = OpenNC.LogFSCopyFile(partProgramPath, newPartProgramPath, failIfExist, out errorClass, out errorNum); + + //If there's an error launch exception + if (errorClass != 0 || errorNum != 0 || nReturn == 0) + throw new Nc_Exception(getError(NC_PROD_ERROR, errorClass, errorNum)); + + } + catch (Exception ex) + { + ThrowNCException(ex); + } + } + + public override void NCDeletePartProgram(string partProgramPath, string partProgramName) + { + //Check if the NC is Connected + CheckConnection(); + + uint errorClass, errorNum; + ushort nReturn; + + try + { + // Delete part program + nReturn = OpenNC.LogFSRemoveFile(partProgramPath, partProgramName, out errorClass, out errorNum); + + //If there's an error launch exception + if (errorClass != 0 || errorNum != 0 || nReturn == 0) + throw new Nc_Exception(getError(NC_PROD_ERROR, errorClass, errorNum)); + } + catch (Exception ex) + { + ThrowNCException(ex); + } + } + + #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1422,37 +1561,6 @@ namespace CMS_CORE.Osai return ErrororOwner + szErrorClassDesc + ": " + szErrorDesc + " (" + Class + "-" + Num + ")"; - case 5: - szErrorClassDesc = "FILESYSTEM error class"; - switch (Num) - { - case 0x001: szErrorDesc = "An error has occurred while opening a transaction with the target for file transfer."; break; - case 0x002: szErrorDesc = "An error has occurred while closing a transaction with the target for file transfer."; break; - case 0x003: szErrorDesc = "Invalid file ID."; break; - case 0x004: szErrorDesc = "The number of transaction is not valid."; break; - case 0x005: szErrorDesc = "Error opening file."; break; - case 0x006: szErrorDesc = "Error closing file."; break; - case 0x007: szErrorDesc = "A file path containing wildcards was passed to a function that does not support wildcards."; break; - case 0x008: szErrorDesc = "Error reading data from file."; break; - case 0x009: szErrorDesc = "Error writing data to file."; break; - case 0x00A: szErrorDesc = "The logical drive was not found on the target."; break; - case 0x00C: szErrorDesc = "The specified file path was not valid."; break; - case 0x00D: szErrorDesc = "An attempt was made to create a logical drive on the target with the same name as an existing logical drive."; break; - case 0x00E: szErrorDesc = "The invoked function requires a different security level on the target."; break; - case 0x00F: szErrorDesc = "A logical drive cannot be created on a remote PC."; break; - case 0x010: szErrorDesc = "Insufficient disk space to complete the requested operation."; break; - case 0x011: szErrorDesc = "The requested file was not found."; break; - } - return ErrororOwner + szErrorClassDesc + ": " + szErrorDesc + " (" + Class + "-" + Num + ")"; - - - case 6: - szErrorClassDesc = "WINDOWS error class"; - - szErrorDesc = new Win32Exception((int) Num).Message; - return ErrororOwner + szErrorClassDesc + ": " + szErrorDesc + " (" + Class + "-" + Num + ")"; - - case 10: szErrorClassDesc = "DLL_INTERFACE error class"; switch (Num) diff --git a/CMS_CORE_Library/Utils/Nc_Utils.cs b/CMS_CORE_Library/Utils/Nc_Utils.cs index 83b4a65..7f7e006 100644 --- a/CMS_CORE_Library/Utils/Nc_Utils.cs +++ b/CMS_CORE_Library/Utils/Nc_Utils.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -142,5 +143,29 @@ namespace CMS_CORE.Utils return Uint; } + + public static void WriteLocalFile(string fileName, string fileContent, ref FileStream fileReference) + { + // Create new local Part Program + fileReference = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); + + // Write Part Program + StreamWriter writer = new StreamWriter(fileReference); + writer.WriteLine(fileContent); + // Close file Writer + writer.Close(); + } + + public static string ReadLocalFile(FileStream fileReference) + { + // Get File Content + StreamReader reader = new StreamReader(fileReference); + + string fileContent = reader.ReadToEnd(); + + reader.Close(); + + return fileContent; + } } } diff --git a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.dll b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.dll index 4194a8f..699a948 100644 Binary files a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.dll and b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.dll differ diff --git a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.pdb b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.pdb index c3e5a47..8325bf8 100644 Binary files a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.pdb and b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.pdb differ diff --git a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.xml b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.xml index 35035d6..b708520 100644 --- a/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.xml +++ b/CMS_CORE_Library/bin/Debug2/CMS_CORE_Library.xml @@ -463,6 +463,39 @@ Index of the parameter Reference of a variable with Param values + + + Read a Part Program by path and name + + Thrown when an internal or a library error occours + Path of the Part Program stored in the Nc + Name of the Part Program and name of new local file + Reference to the new local file where the NC Part Program data will be saved + + + + Write/Overwrite a Part Program by path and name + + Thrown when an internal or a library error occours + Path of the Part Program stored in the Nc + Reference to the local Part Program file + + + + Copy a Part Program into another path + + Thrown when an internal or a library error occours + Path where the Part Program is saved in the NC + Path of the copy Part Program destination + + + + + Delete a Nc Part Program + Thrown when an internal or a library error occours + Path where the Nc Part Program is saved in the NC + Name of the Part Program file + diff --git a/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.dll b/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.dll index 4194a8f..699a948 100644 Binary files a/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.dll and b/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.dll differ diff --git a/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.pdb b/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.pdb index c3e5a47..8325bf8 100644 Binary files a/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.pdb and b/CMS_CORE_Library/obj/Debug2/CMS_CORE_Library.pdb differ diff --git a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.exe b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.exe index ae5b4bc..9cb8b54 100644 Binary files a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.exe and b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.exe differ diff --git a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.pdb b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.pdb index 924c20d..1f75809 100644 Binary files a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.pdb and b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/bin/Debug/Nc_Demo_Application.pdb differ diff --git a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/CMS_Core_Nc_Demo_Application.csprojResolveAssemblyReference.cache b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/CMS_Core_Nc_Demo_Application.csprojResolveAssemblyReference.cache index 2a6a395..2c7870a 100644 Binary files a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/CMS_Core_Nc_Demo_Application.csprojResolveAssemblyReference.cache and b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/CMS_Core_Nc_Demo_Application.csprojResolveAssemblyReference.cache differ diff --git a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.exe b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.exe index ae5b4bc..9cb8b54 100644 Binary files a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.exe and b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.exe differ diff --git a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.pdb b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.pdb index 924c20d..1f75809 100644 Binary files a/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.pdb and b/CMS_CORE_Nc_Demo_Application/Nc_Demo_Application/obj/Debug/Nc_Demo_Application.pdb differ