Eliminazione NC adapter
This commit is contained in:
@@ -1,911 +0,0 @@
|
||||
using CMS_CORE_Library.Models;
|
||||
using Thermo.Active.Database.Controllers;
|
||||
using Thermo.Active.Model.DatabaseModels;
|
||||
using Thermo.Active.Model.DTOModels.ToolModels;
|
||||
using Thermo.Active.Utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
using static CMS_CORE_Library.Models.DataStructures;
|
||||
using static Thermo.Active.Config.ServerConfig;
|
||||
using static Thermo.Active.Model.Constants;
|
||||
|
||||
namespace Thermo.Active.NC
|
||||
{
|
||||
public class NcToolTableAdapter : NcAdapter
|
||||
{
|
||||
public CmsError GetToolsData(out List<DTONcToolModel> dtoTools)
|
||||
{
|
||||
dtoTools = new List<DTONcToolModel>();
|
||||
|
||||
CmsError cmsError = NO_ERROR;
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
List<DbNcToolModel> tools = toolsManager.FindToolsWithDependencies();
|
||||
|
||||
foreach (DbNcToolModel tool in tools)
|
||||
{
|
||||
DTONcToolModel dtoTool = (DTONcToolModel)tool;
|
||||
|
||||
cmsError = GetToolData(tool, ref dtoTool);
|
||||
|
||||
dtoTools.Add(dtoTool);
|
||||
}
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError GetToolData(DbNcToolModel tool, ref DTONcToolModel dtoTool)
|
||||
{
|
||||
dtoTool = (DTONcToolModel)tool;
|
||||
|
||||
OffsetModel offset = new OffsetModel();
|
||||
CmsError cmsError = NO_ERROR;
|
||||
|
||||
if (tool.OffsetId1 != null && tool.OffsetId1 != 0)
|
||||
{
|
||||
// Read first offset data
|
||||
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId1, ref offset);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
dtoTool.Offset1 = offset;
|
||||
}
|
||||
if (tool.OffsetId2 != null && tool.OffsetId2 != 0)
|
||||
{
|
||||
// Read second offset data
|
||||
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId2, ref offset);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
dtoTool.Offset2 = offset;
|
||||
}
|
||||
if (tool.OffsetId3 != null && tool.OffsetId3 != 0)
|
||||
{
|
||||
// Read third offset data
|
||||
cmsError = numericalControl.TOOLS_ROffset((short)tool.OffsetId3, ref offset);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
dtoTool.Offset3 = offset;
|
||||
}
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
public CmsError AddTool(DTONewNcToolModel tool, out DTONcToolModel dtoTool, short toolId)
|
||||
{
|
||||
dtoTool = new DTONcToolModel();
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
DbNcToolModel ncTool = toolsManager.AddTool(tool, toolId);
|
||||
|
||||
if (!ToolManagerConfig.OffsetOpt)
|
||||
{
|
||||
CmsError cmsError = UpdateToolOffsetId(ncTool.ToolId, 1, ncTool.ToolId, out DTONcToolModel toolWithOffset);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
ncTool.OffsetId1 = ncTool.ToolId;
|
||||
}
|
||||
|
||||
GetToolData(ncTool, ref dtoTool);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError UpdateTool(int toolId, DTONewNcToolModel newTool, out DTONcToolModel toolWithOffsets, DTONewNcToolWithFamilyModel dtoToolWithFamily)
|
||||
{
|
||||
toolWithOffsets = new DTONcToolModel();
|
||||
CmsError cmsError = NO_ERROR;
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// If family option is not active, update family data
|
||||
if (!ToolManagerConfig.FamilyOpt)
|
||||
{
|
||||
// Copy only family
|
||||
DTONewNcFamilyModel newFamily = new DTONewNcFamilyModel();
|
||||
SupportFunctions.CopyProperties(dtoToolWithFamily, newFamily);
|
||||
// Update only family data
|
||||
newFamily = (DTONcFamilyModel)toolsManager.UpdateFamily(dtoToolWithFamily.FamilyId, newFamily);
|
||||
}
|
||||
|
||||
// Update database tool Tool
|
||||
DbNcToolModel dbTool = toolsManager.UpdateTool(toolId, newTool);
|
||||
|
||||
// Populate updated tool with offset data
|
||||
cmsError = GetToolData(dbTool, ref toolWithOffsets);
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
dbContextTransaction.Rollback();
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
bool startIsActive = false;
|
||||
|
||||
// Check if tool is loaded into a shank
|
||||
if (dbTool.ShankId != null)
|
||||
{
|
||||
// Check if shank is loaded into magazine
|
||||
DbNcShankModel shank = toolsManager.FindShankWithTools(dbTool.ShankId.Value);
|
||||
if (shank.MagazineId != null)
|
||||
{
|
||||
startIsActive = true;
|
||||
// Create backup of data stored on the NC and block memory/files accesses
|
||||
cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// If family option is not active, update NC data
|
||||
//if (!ToolManagerConfig.FamilyOpt)
|
||||
//{
|
||||
// Update Nc families
|
||||
cmsError = UpdateNcFamily(toolsManager);
|
||||
if (cmsError.IsError())
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
//}
|
||||
|
||||
// Update tool
|
||||
cmsError = UpdateNcTools(toolsManager);
|
||||
}
|
||||
}
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError UpdateNcTools(NcToolManagerController toolsManager = null)
|
||||
{
|
||||
if (toolsManager == null)
|
||||
toolsManager = new NcToolManagerController();
|
||||
|
||||
List<NcShankModel> shanks = toolsManager
|
||||
.FindShanks()
|
||||
.Where(x => x.MagazineId != null)
|
||||
.Select(x => (NcShankModel)x)
|
||||
.ToList();
|
||||
|
||||
// Get tools
|
||||
List<NcToolModel> tools = toolsManager
|
||||
.FindTools()
|
||||
.Select(x => (NcToolModel)x)
|
||||
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
||||
.ToList();
|
||||
|
||||
// Update tools
|
||||
return numericalControl.TOOLS_WUpdateTools(tools);
|
||||
}
|
||||
|
||||
public CmsError UpdateOffset(short offsetId, OffsetModel offsetData)
|
||||
{
|
||||
return numericalControl.TOOLS_WOffset(offsetId, ref offsetData);
|
||||
}
|
||||
|
||||
public CmsError UpdateToolOffsetId(int toolId, int positionId, short offsetId, out DTONcToolModel toolWithOffsets)
|
||||
{
|
||||
toolWithOffsets = new DTONcToolModel();
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// Update database tool Tool
|
||||
DbNcToolModel dbTool = toolsManager.UpdateToolOffset(toolId, positionId, offsetId);
|
||||
|
||||
// Populate updated tool with offset data
|
||||
CmsError cmsError = GetToolData(dbTool, ref toolWithOffsets);
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
dbContextTransaction.Rollback();
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
bool startIsActive = false;
|
||||
|
||||
// Check if tool is loaded into a shank
|
||||
if (dbTool.ShankId != null)
|
||||
{
|
||||
// Check if shank is loaded into magazine
|
||||
DbNcShankModel shank = toolsManager.FindShankWithTools(dbTool.ShankId.Value);
|
||||
if (shank.MagazineId != null)
|
||||
{
|
||||
startIsActive = true;
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// R tool
|
||||
cmsError = UpdateNcTools(toolsManager);
|
||||
}
|
||||
}
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError DeleteNcTool(DbNcToolModel tool)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
if (!ToolManagerConfig.FamilyOpt)
|
||||
{
|
||||
DbNcFamilyModel fam = toolsManager.FindFamily(tool.FamilyId);
|
||||
// Check if family is connected to other tools
|
||||
if (fam != null && fam.Tools.Count() == 1)
|
||||
{
|
||||
toolsManager.DeleteFamily(fam.FamilyId);
|
||||
}
|
||||
}
|
||||
|
||||
DbNcShankModel shank = null;
|
||||
if (!ToolManagerConfig.ShankOpt && tool.ShankId != null)
|
||||
{
|
||||
// If option is active find & delete tool shank
|
||||
shank = toolsManager.FindShankWithTools(tool.ShankId.Value);
|
||||
|
||||
toolsManager.DeleteNcShank(shank.ShankId);
|
||||
}
|
||||
|
||||
// Delete db tool
|
||||
toolsManager.DeleteTool(tool.ToolId);
|
||||
|
||||
bool startIsActive = false;
|
||||
CmsError cmsError = NO_ERROR;
|
||||
|
||||
if (tool.ShankId != null && shank != null)
|
||||
{
|
||||
if (shank.MagazineId != null)
|
||||
{
|
||||
startIsActive = true;
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// Update Nc data
|
||||
cmsError = UpdateNcTools(toolsManager);
|
||||
}
|
||||
}
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError UpdateShank(int shankId, DTONewNcShankModel dtoShank, out DTONcShankModel shank)
|
||||
{
|
||||
shank = new DTONcShankModel();
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
bool startIsActive = false;
|
||||
CmsError cmsError = NO_ERROR;
|
||||
|
||||
// Update db data
|
||||
shank = (DTONcShankModel)toolsManager.UpdateShank(shankId, dtoShank);
|
||||
if (shank.MagazineId != null)
|
||||
{
|
||||
startIsActive = true;
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// Update nc data
|
||||
cmsError = UpdateNcShanks(toolsManager);
|
||||
}
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError DeleteNcShank(int shankId)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
CmsError cmsError = NO_ERROR;
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// Delete database shank
|
||||
DbNcShankModel deletedShank = toolsManager.DeleteNcShank(shankId);
|
||||
bool startIsActive = false;
|
||||
if (deletedShank.MagazineId != null)
|
||||
{
|
||||
startIsActive = true;
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// Update nc data
|
||||
cmsError = UpdateNcShanks(toolsManager);
|
||||
}
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, startIsActive);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CmsError UpdateNcShanks(NcToolManagerController toolsManager)
|
||||
{
|
||||
// Get mounted shanks
|
||||
List<NcShankModel> shanks = toolsManager
|
||||
.FindShanks()
|
||||
.Where(x => x.MagazineId != null)
|
||||
.Select(x => (NcShankModel)x)
|
||||
.ToList();
|
||||
|
||||
// Update nc data
|
||||
return numericalControl.TOOLS_WUpdateShanks(shanks);
|
||||
}
|
||||
|
||||
public DTONcFamilyModel AddFamily(DTONcFamilyModel family)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
return (DTONcFamilyModel)toolsManager.AddFamily(family);
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError UpdateFamily(int familyId, DTONewNcFamilyModel family, out DTONcFamilyModel newFamily)
|
||||
{
|
||||
newFamily = new DTONcFamilyModel();
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// Update db family
|
||||
newFamily = (DTONcFamilyModel)toolsManager.UpdateFamily(familyId, family);
|
||||
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
CmsError cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// Update Nc families
|
||||
cmsError = UpdateNcFamily(toolsManager);
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError DeleteNcFamily(int famId)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (var dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
CmsError cmsError = numericalControl.TOOLS_WStartEditData();
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Check if shank opt is disabled
|
||||
if (!ToolManagerConfig.ShankOpt)
|
||||
{
|
||||
// If the relationship is 1:1 (tool:shank) i have to remove shanks manually
|
||||
// Tools are deleted thanks to foreign keys constraint
|
||||
List<DbNcShankModel> shanks = toolsManager.FindShanksByFamilyId(famId);
|
||||
foreach (var shank in shanks)
|
||||
{
|
||||
if (shank != null)
|
||||
toolsManager.DeleteNcShank(shank.ShankId);
|
||||
}
|
||||
}
|
||||
|
||||
toolsManager.DeleteFamily(famId);
|
||||
|
||||
// Update Nc families
|
||||
cmsError = UpdateNcFamily(toolsManager);
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private CmsError UpdateNcFamily(NcToolManagerController toolsManager)
|
||||
{
|
||||
// Get mounted shanks
|
||||
List<NcShankModel> shanks = toolsManager
|
||||
.FindShanks()
|
||||
.Where(x => x.MagazineId != null)
|
||||
.Select(x => (NcShankModel)x)
|
||||
.ToList();
|
||||
|
||||
// Get tools
|
||||
List<NcToolModel> tools = toolsManager
|
||||
.FindTools()
|
||||
.Select(x => (NcToolModel)x)
|
||||
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
||||
.ToList();
|
||||
|
||||
List<NcFamilyModel> families = toolsManager
|
||||
.FindFamilies()
|
||||
.Select(x => (NcFamilyModel)x)
|
||||
.Where(x => tools.Any(y => y.FamilyId == x.FamilyId)) // Find only families of mounted tools
|
||||
.ToList();
|
||||
|
||||
// Update nc families
|
||||
return numericalControl.TOOLS_WUpdateFamilies(families);
|
||||
}
|
||||
|
||||
public CmsError UpdateMagazinePosition(DbNcMagazinePositionModel dbPos, DTONcMagazinePositionModel dtoPos, out DTONcMagazinePositionModel magPos)
|
||||
{
|
||||
magPos = new DTONcMagazinePositionModel();
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
using (DbContextTransaction dbContextTransaction = toolsManager.dbCtx.Database.BeginTransaction())
|
||||
{
|
||||
// Create backup of data stored on the NC and block file accesses
|
||||
CmsError cmsError = StartEditData();
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
// Update database data
|
||||
magPos = (DTONcMagazinePositionModel)toolsManager.UpdatePosition(dbPos, dtoPos);
|
||||
|
||||
// Get magazines positions
|
||||
List<NcMagazinePositionModel> positions = toolsManager.FindMagazinesPositions()
|
||||
.Select(x => (NcMagazinePositionModel)x)
|
||||
.ToList();
|
||||
// Update Nc data
|
||||
cmsError = numericalControl.TOOLS_WUpdateMagazinePositions(positions);
|
||||
|
||||
ManageErrorAndTransaction(cmsError, dbContextTransaction, true);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ManageErrorAndTransaction(CmsError cmsError, DbContextTransaction dbContextTransaction, bool ncStartIsActive)
|
||||
{
|
||||
// Check cmsError, manage transaction and if startIsActive = true manage Nc backup created with TOOLS_WStartEditData()
|
||||
if (cmsError.IsError())
|
||||
{
|
||||
// Close transaction
|
||||
dbContextTransaction.Rollback();
|
||||
// Restore backup
|
||||
if (ncStartIsActive)
|
||||
numericalControl.TOOLS_WRestoreBackup();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Close transaction
|
||||
dbContextTransaction.Commit();
|
||||
// Stop editing ncData
|
||||
if (ncStartIsActive)
|
||||
numericalControl.TOOLS_WStopEditData();
|
||||
}
|
||||
}
|
||||
|
||||
public DTONcShankModel LoadIntoShank(DbNcToolModel tool, short shankId)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
return toolsManager.LoadToolIntoShank(tool, shankId);
|
||||
}
|
||||
}
|
||||
|
||||
public DTONcShankModel UnloadFromShank(DbNcToolModel tool)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
return toolsManager.UnloadToolFromShank(tool);
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError SetupNcToolManager()
|
||||
{
|
||||
CmsError cmsError = UpdateMagazinePositionConf(out bool newDb);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
cmsError = WriteOption();
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
using (NcToolManagerController toolManager = new NcToolManagerController())
|
||||
{
|
||||
// If database was empty read data from NC and update database
|
||||
if (newDb)
|
||||
{
|
||||
List<NcToolModel> tools = new List<NcToolModel>();
|
||||
List<NcFamilyModel> families = new List<NcFamilyModel>();
|
||||
List<NcShankModel> shanks = new List<NcShankModel>();
|
||||
|
||||
// Read data from Nc memory
|
||||
cmsError = numericalControl.TOOLS_RStoredData(ref tools, ref families, ref shanks);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Check if there are data
|
||||
if (tools.Count() > 0 || families.Count() > 0 || shanks.Count() > 0)
|
||||
{
|
||||
// Populate import object
|
||||
DTOExportToolTableModel exp = new DTOExportToolTableModel()
|
||||
{
|
||||
Tools = tools.Select(x => (DbNcToolModel)x).ToList(),
|
||||
Families = families.Select(x =>
|
||||
{
|
||||
var fam = (DbNcFamilyModel)x;
|
||||
fam.Name = "Fam" + fam.FamilyId.ToString();
|
||||
return fam;
|
||||
}).ToList(),
|
||||
Shanks = shanks.Select(x => (DbNcShankModel)x).ToList()
|
||||
};
|
||||
|
||||
// Import
|
||||
toolManager.ImportData(exp);
|
||||
}
|
||||
}
|
||||
|
||||
List<DTONcToolModel> databaseTool = toolManager.GetTools();
|
||||
bool exist = false;
|
||||
if (databaseTool != null && databaseTool.Count() > 0)
|
||||
// Check options consistency with database data
|
||||
exist = databaseTool.Exists(x =>
|
||||
(!ToolManagerConfig.FamilyOpt && x.FamilyId != x.Id) || // Check family
|
||||
(!ToolManagerConfig.ShankOpt && x.ShankId != x.Id) || // Check shanks
|
||||
(!ToolManagerConfig.OffsetOpt && x.OffsetId1 != x.Id && x.OffsetId2 != null && x.OffsetId3 != null) // Check offsets
|
||||
);
|
||||
|
||||
if (exist)
|
||||
return OPTION_NOT_CONSISTENT_ERROR;
|
||||
|
||||
// Check if there are blocked magazines, and release them
|
||||
cmsError = RestoreTableLock();
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
public CmsError UpdateMagazinePositionConf(out bool newDb)
|
||||
{
|
||||
newDb = false;
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
// Get database configured positions
|
||||
List<DbNcMagazinePositionModel> dbPositions = toolsManager.FindMagazinesPositions();
|
||||
if (dbPositions.Count <= 0)
|
||||
{
|
||||
// THe software is using a new database
|
||||
newDb = true;
|
||||
|
||||
CmsError cmsError = GetMagazineConfiguration(out List<NcMagazineConfigModel> config);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Setup new positions list in order to be saved in the database
|
||||
List<DbNcMagazinePositionModel> fromConfigPosition = new List<DbNcMagazinePositionModel>();
|
||||
foreach (var conf in config)
|
||||
{
|
||||
for (byte i = 1; i <= conf.MaxPositions; i++)
|
||||
{
|
||||
// Create new position
|
||||
fromConfigPosition.Add(new DbNcMagazinePositionModel()
|
||||
{
|
||||
MagazineId = conf.Id,
|
||||
PositionId = i,
|
||||
Disabled = false,
|
||||
Type = 0
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Update database
|
||||
toolsManager.SetupMagazinePositions(fromConfigPosition);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError WriteOption()
|
||||
{
|
||||
ToolManagerOptionsModel options = new ToolManagerOptionsModel();
|
||||
SupportFunctions.CopyProperties(ToolManagerConfig, options);
|
||||
|
||||
return numericalControl.TOOLS_WOptions(options);
|
||||
}
|
||||
|
||||
public CmsError GetMagazineConfiguration(out List<NcMagazineConfigModel> config)
|
||||
{
|
||||
config = new List<NcMagazineConfigModel>();
|
||||
// Read configuration
|
||||
return numericalControl.TOOLS_RMagazineConfig(ref config);
|
||||
}
|
||||
|
||||
public CmsError StartEditData()
|
||||
{
|
||||
return numericalControl.TOOLS_WStartEditData();
|
||||
}
|
||||
|
||||
public CmsError StopEditData()
|
||||
{
|
||||
return numericalControl.TOOLS_WStopEditData();
|
||||
}
|
||||
|
||||
public CmsError StartEditTooling(int magazineId)
|
||||
{
|
||||
return numericalControl.TOOLS_WStartEditTooling(magazineId);
|
||||
}
|
||||
|
||||
public CmsError StopEditTooling(int magazineId)
|
||||
{
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
// Get mounted shanks
|
||||
List<NcShankModel> shanks = toolsManager
|
||||
.FindShanks()
|
||||
.Where(x => x.MagazineId != null)
|
||||
.Select(x => (NcShankModel)x)
|
||||
.ToList();
|
||||
// Update shanks
|
||||
CmsError cmsError = numericalControl.TOOLS_WUpdateShanks(shanks);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Get magazine positions
|
||||
List<NcMagazinePositionModel> positions = toolsManager.FindMagazinesPositions()
|
||||
.Select(x => (NcMagazinePositionModel)x)
|
||||
.ToList();
|
||||
// Update positions
|
||||
cmsError = numericalControl.TOOLS_WUpdateMagazinePositions(positions);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Get tools
|
||||
List<NcToolModel> tools = toolsManager
|
||||
.FindTools()
|
||||
.Select(x => (NcToolModel)x)
|
||||
.Where(x => shanks.Any(y => y.ShankId == x.ShankId)) // Find only mounted tools
|
||||
.ToList();
|
||||
|
||||
// Update tools
|
||||
cmsError = numericalControl.TOOLS_WUpdateTools(tools);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
// Get families
|
||||
List<NcFamilyModel> families = toolsManager
|
||||
.FindFamilies()
|
||||
.Select(x => (NcFamilyModel)x)
|
||||
.Where(x => tools.Any(y => y.FamilyId == x.FamilyId)) // Find only families of mounted tools
|
||||
.ToList();
|
||||
|
||||
// Update families
|
||||
cmsError = numericalControl.TOOLS_WUpdateFamilies(families);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
return numericalControl.TOOLS_WStopEditTooling(magazineId);
|
||||
}
|
||||
|
||||
public CmsError GetUpdatedToolsData(out Dictionary<int, byte> updatedStatus, out Dictionary<int, uint> updatedLives)
|
||||
{
|
||||
updatedStatus = new Dictionary<int, byte>();
|
||||
updatedLives = new Dictionary<int, uint>();
|
||||
return numericalControl.TOOLS_RUpdatedToolsData(ref updatedStatus, ref updatedLives);
|
||||
}
|
||||
|
||||
public CmsError CheckIfShankCanFit(int magazineId, int positionId, DbNcShankModel shankToBeLoaded)
|
||||
{
|
||||
CmsError cmsError = GetMagazineConfiguration(out List<NcMagazineConfigModel> magazinesConfig);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
NcMagazineConfigModel magConfig = magazinesConfig.FirstOrDefault(x => x.Id == magazineId);// Get only current magazine config
|
||||
if (magConfig == null)
|
||||
return INCORRECT_PARAMETERS_ERROR;
|
||||
|
||||
using (NcToolManagerController toolsManager = new NcToolManagerController())
|
||||
{
|
||||
List<DbNcToolModel> magazineMountedTools = toolsManager.GetMountedTools();
|
||||
|
||||
// Check if there is a tool already mounted the selected position (magazineId, positionId)
|
||||
var toolAlreadyMounted = magazineMountedTools.FirstOrDefault(x => x.Shank.MagazineId == magazineId && x.Shank.PositionId == positionId);
|
||||
if (toolAlreadyMounted != null)
|
||||
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
||||
|
||||
// Get shank occupied space
|
||||
toolsManager.GetShankMaxSpaceOccupied(shankToBeLoaded.ShankId, out int maxRight, out int maxLeft);
|
||||
|
||||
// Check if tool can fit in the position considering other tools
|
||||
foreach (var mountedTool in magazineMountedTools)
|
||||
{
|
||||
// Check if is not the same position && if is different check if the position is on the same magazine
|
||||
if (mountedTool.Shank.PositionId != positionId && mountedTool.Shank.MagazineId == magazineId)
|
||||
{
|
||||
int decPosition, incPosition;
|
||||
decPosition = incPosition = mountedTool.Shank.PositionId.Value;
|
||||
// if it is a circular magazine move position before ( for left case ) or after ( right case ) the location you want to mount the tool
|
||||
if (magConfig.Type != NC_MAGAZINE_TYPE.BOX_MAGAZINE)
|
||||
{
|
||||
decPosition = decPosition < positionId ? decPosition + magConfig.MaxPositions : decPosition;
|
||||
incPosition = incPosition > positionId ? incPosition - magConfig.MaxPositions : incPosition;
|
||||
}
|
||||
|
||||
// Right check
|
||||
if (positionId < decPosition)
|
||||
{
|
||||
if (!(magConfig.Type == NC_MAGAZINE_TYPE.BOX_MAGAZINE && mountedTool.Shank.PositionId == 1))
|
||||
{
|
||||
// Calculate left space occupied (with mounted shank)
|
||||
double leftMounted = decPosition - (mountedTool.Family.LeftSize / 2.0);
|
||||
// Calculate right space needed (with shank to be loaded)
|
||||
double rightToBeMount = positionId + (maxRight / 2.0);
|
||||
if (leftMounted < rightToBeMount)
|
||||
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Left check
|
||||
if (positionId > incPosition)
|
||||
{
|
||||
if (!(magConfig.Type == NC_MAGAZINE_TYPE.BOX_MAGAZINE && mountedTool.Shank.PositionId == magConfig.MaxPositions))
|
||||
{
|
||||
// Calculate right space occupied (with mounted shank)
|
||||
double rightMounted = incPosition + (mountedTool.Family.RightSize / 2.0);
|
||||
// Calculate left space needed (with shank to be loaded)
|
||||
double leftToBeMounted = positionId - (maxLeft / 2.0);
|
||||
if (rightMounted > leftToBeMounted)
|
||||
return MAGAZINE_POSITION_OCCUPIED_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
public CmsError GetNcMagazineStatus(out Dictionary<int, bool> status)
|
||||
{
|
||||
status = new Dictionary<int, bool>();
|
||||
return numericalControl.TOOLS_RMagazineStatus(ref status);
|
||||
}
|
||||
|
||||
public CmsError StartAssistedToolingProcedure(ushort toolId, ushort familyId, ushort shankId, ushort magazineId, ushort positionId, ASSISTED_TOOLING_ACTION action)
|
||||
{
|
||||
return numericalControl.PLC_WAssistedToolingCmd(toolId, familyId, shankId, magazineId, positionId, action);
|
||||
}
|
||||
|
||||
public CmsError ReadAssistedToolingProcedure(out DTOAssistedToolingEndValueModel data)
|
||||
{
|
||||
data = new DTOAssistedToolingEndValueModel();
|
||||
AssistedToolingModel tmpData = new AssistedToolingModel();
|
||||
CmsError cmsError = numericalControl.PLC_RAssistedToolingData(ref tmpData);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
data = new DTOAssistedToolingEndValueModel(tmpData);
|
||||
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
public CmsError TerminateAssistedProcedure(int magazineId, bool writeData)
|
||||
{
|
||||
if (writeData)
|
||||
{
|
||||
CmsError cmsError = StartEditTooling(magazineId);
|
||||
if (!cmsError.IsError())
|
||||
{
|
||||
// Write tool table new data
|
||||
cmsError = StopEditTooling(magazineId);
|
||||
if (!cmsError.IsError())
|
||||
{
|
||||
// Close procedure
|
||||
return numericalControl.PLC_WTerminateAssistedToolingProcedure();
|
||||
}
|
||||
}
|
||||
return cmsError;
|
||||
}
|
||||
else
|
||||
{
|
||||
return numericalControl.PLC_WTerminateAssistedToolingProcedure();
|
||||
}
|
||||
}
|
||||
|
||||
public CmsError GetSelfAdaptiveStep(out DTOSelfAdaptiveModel step)
|
||||
{
|
||||
step = new DTOSelfAdaptiveModel();
|
||||
byte intStep = 0;
|
||||
|
||||
//Get the value
|
||||
CmsError cmsError = numericalControl.TOOLS_RAdatpivePathStep(ref intStep);
|
||||
step.Step = intStep;
|
||||
return cmsError;
|
||||
}
|
||||
|
||||
public CmsError SetSelfAdaptiveStep(DTOSelfAdaptiveModel step)
|
||||
{
|
||||
//Limit Value
|
||||
if (step.Step > 100)
|
||||
step.Step = 100;
|
||||
if (step.Step < 0)
|
||||
step.Step = 0;
|
||||
|
||||
//Set the value
|
||||
return numericalControl.TOOLS_WAdatpivePathStep(step.Step);
|
||||
}
|
||||
|
||||
public CmsError RestoreTableLock()
|
||||
{
|
||||
List<int> ids = new List<int>();
|
||||
// check if there are magazines blocked by Active previously
|
||||
CmsError cmsError = numericalControl.TOOLS_RMagazineBlock(ref ids);
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
|
||||
if (ids.Count() > 0)
|
||||
{
|
||||
// Update tables
|
||||
cmsError = StopEditTooling(ids.FirstOrDefault());
|
||||
if (cmsError.IsError())
|
||||
return cmsError;
|
||||
// Free magazines
|
||||
return numericalControl.TOOLS_WFreeMagazines();
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user