From 8bac6c2a637ae10f800d0243a548fb14e4835ebe Mon Sep 17 00:00:00 2001 From: Lucio Maranta Date: Fri, 27 Jul 2018 10:20:37 +0200 Subject: [PATCH] Fix right&left check --- .../Controllers/NcToolManagerController.cs | 18 +++- Step.NC/NcHandler.cs | 84 +++++++++++-------- .../WebApi/NcToolManagerController.cs | 24 +++--- 3 files changed, 73 insertions(+), 53 deletions(-) diff --git a/Step.Database/Controllers/NcToolManagerController.cs b/Step.Database/Controllers/NcToolManagerController.cs index ca2dee1e..818a2809 100644 --- a/Step.Database/Controllers/NcToolManagerController.cs +++ b/Step.Database/Controllers/NcToolManagerController.cs @@ -43,6 +43,17 @@ namespace Step.Database.Controllers .ToList(); } + public List FindFamiliesByShankId(int shankId) + { + DbNcShankModel shank = FindShankWithTools(shankId); + // Get only families id + int[] ids = shank.Tools.Select(x => x.FamilyId).ToArray(); + + return FindFamilies() // Get Families + .Where(x => ids.Contains(x.FamilyId)) // Filter by ids + .ToList(); + } + public DbNcToolModel FindTool(int toolId) { return dbCtx.Tools @@ -50,7 +61,7 @@ namespace Step.Database.Controllers .FirstOrDefault(); } - public DbNcShankModel FindShank(int shankId) + public DbNcShankModel FindShankWithTools(int shankId) { return dbCtx.Shanks .Include("Tools") @@ -123,7 +134,6 @@ namespace Step.Database.Controllers return shank; } - public List GetShanks() { // Get shank from database @@ -320,7 +330,7 @@ namespace Step.Database.Controllers public DbNcShankModel UpdateShank(int shankId, DTONewNcShankModel dtoShank) { - DbNcShankModel ncShank = FindShank(shankId); + DbNcShankModel ncShank = FindShankWithTools(shankId); return UpdateShank(ncShank, dtoShank); } @@ -338,7 +348,7 @@ namespace Step.Database.Controllers public void DeleteShank(int shankId) { - DbNcShankModel shank = FindShank(shankId); + DbNcShankModel shank = FindShankWithTools(shankId); DeleteShank(shank); } diff --git a/Step.NC/NcHandler.cs b/Step.NC/NcHandler.cs index 6924aa89..0b69affa 100644 --- a/Step.NC/NcHandler.cs +++ b/Step.NC/NcHandler.cs @@ -12,7 +12,6 @@ using Step.Utils; using System; using System.Collections.Generic; using System.Data.Entity; -using System.Diagnostics; using System.Globalization; using System.Linq; using static CMS_CORE_Library.DataStructures; @@ -1121,7 +1120,7 @@ namespace Step.NC if (dbTool.ShankId != null) { // Check if shank is loaded in magazine - DbNcShankModel shank = toolsManager.FindShank(dbTool.ShankId.Value); + DbNcShankModel shank = toolsManager.FindShankWithTools(dbTool.ShankId.Value); if (shank.MagazineId != null) { // Update tool @@ -1149,7 +1148,7 @@ namespace Step.NC if (tool.ShankId != null) { // If tool is mounted than update Nc data - DbNcShankModel shank = toolsManager.FindShank(tool.ShankId.Value); + DbNcShankModel shank = toolsManager.FindShankWithTools(tool.ShankId.Value); if (shank.MagazineId != null) { // Update Nc data @@ -1344,7 +1343,6 @@ namespace Step.NC dbContextTransaction.Commit(); numericalControl.TOOLS_WStopEditData(); } - } //public DTONcMagazinePositionModel LoadToolInMagazine(byte magazineId, byte positionId, DbNcShankModel shank) @@ -1386,7 +1384,7 @@ namespace Step.NC public CmsError SetupNcToolManager() { - if(NcConfig.NcVendor != NC_VENDOR.SIEMENS) + if (NcConfig.NcVendor != NC_VENDOR.SIEMENS) { return UpdateMagazinePositionConf(); } @@ -1409,7 +1407,7 @@ namespace Step.NC // Setup new positions list in order to be saved in the database List fromConfigPosition = new List(); foreach (var conf in config) - { + { for (byte i = 1; i <= conf.MaxPositions; i++) { // Create new position @@ -1504,45 +1502,61 @@ namespace Step.NC return numericalControl.TOOLS_WStopEditTooling(magazineId); } - public CmsError GetUpdatedToolsData(out Dictionary updatedStatus,out Dictionary updatedLives) + public CmsError GetUpdatedToolsData(out Dictionary updatedStatus, out Dictionary updatedLives) { updatedStatus = new Dictionary(); updatedLives = new Dictionary(); - return numericalControl.TOOLS_RUpdatedToolsData(ref updatedStatus,ref updatedLives); + return numericalControl.TOOLS_RUpdatedToolsData(ref updatedStatus, ref updatedLives); } - public CmsError CheckIfShankCanFit(int magazineId, int positionId, DbNcShankModel shankToBeLoaded, List magazineMountedTools, NcMagazineConfigModel magConfig) + public CmsError CheckIfShankCanFit(int magazineId, int positionId, DbNcShankModel shankToBeLoaded) { - // Check if tool can fit in the position considering other tools - foreach (var mountedTool in magazineMountedTools) + CmsError cmsError = GetMagazineConfiguration(out List magazinesConfig); + if (cmsError.IsError()) + return cmsError; + + NcMagazineConfigModel magConfig = magazinesConfig.Where(x => x.Id == magazineId).FirstOrDefault();// Get only current magazine config + + using (NcToolManagerController toolsManager = new NcToolManagerController()) { - // 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) + List magazineMountedTools = toolsManager.GetMountedTools(); + + List families = toolsManager.FindFamiliesByShankId(shankToBeLoaded.ShankId); + + int maxRight = families.Max(x => x.RightSize); + int maxLeft = families.Max(x => x.LeftSize); + + // Check if tool can fit in the position considering other tools + foreach (var mountedTool in magazineMountedTools) { - 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 != MAGAZINE_TYPE.BOX_MAGAZINE) + // 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) { - decPosition = decPosition < shankToBeLoaded.PositionId ? decPosition + magConfig.MaxPositions : decPosition; - incPosition = incPosition > shankToBeLoaded.PositionId ? incPosition - magConfig.MaxPositions : incPosition; + 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 != MAGAZINE_TYPE.BOX_MAGAZINE) + { + decPosition = decPosition < positionId ? decPosition + magConfig.MaxPositions : decPosition; + incPosition = incPosition > positionId ? incPosition - magConfig.MaxPositions : incPosition; + } + + // Right check + // 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 + // 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; } - - // Right check - // 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 + (mountedTool.Family.RightSize / 2.0); - if (leftMounted < rightToBeMount) - return MAGAZINE_POSITION_OCCUPIED_ERROR; - - // Left check - // 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 - (mountedTool.Family.LeftSize / 2.0); - if (rightMounted > leftToBeMounted) - return MAGAZINE_POSITION_OCCUPIED_ERROR; } } diff --git a/Step/Controllers/WebApi/NcToolManagerController.cs b/Step/Controllers/WebApi/NcToolManagerController.cs index c4ccdfa3..b48bb968 100644 --- a/Step/Controllers/WebApi/NcToolManagerController.cs +++ b/Step/Controllers/WebApi/NcToolManagerController.cs @@ -47,7 +47,7 @@ namespace Step.Controllers.WebApi // If option is active check if shank exists if (ToolManagerConfig.ShankOptIsActive) { - DbNcShankModel shank = toolsManager.FindShank(dtoTool.ShankId.Value); + DbNcShankModel shank = toolsManager.FindShankWithTools(dtoTool.ShankId.Value); if (shank == null) return BadRequest(API_ERROR_KEYS.INCORRECT_PARAMETERS); } @@ -128,7 +128,7 @@ namespace Step.Controllers.WebApi if (!ToolManagerConfig.ShankOptIsActive && tool.ShankId != null) { // If option is active find & delete tool shank - DbNcShankModel shank = toolsManager.FindShank(tool.ShankId.Value); + DbNcShankModel shank = toolsManager.FindShankWithTools(tool.ShankId.Value); ncHandler.DeleteNcShank(shank.ShankId); } @@ -179,7 +179,7 @@ namespace Step.Controllers.WebApi using (NcToolManagerController toolsManager = new NcToolManagerController()) { // Check if shank exists - DbNcShankModel shank = toolsManager.FindShank(shankId); + DbNcShankModel shank = toolsManager.FindShankWithTools(shankId); if (shank == null) return NotFound(); @@ -202,7 +202,7 @@ namespace Step.Controllers.WebApi using (NcToolManagerController toolsManager = new NcToolManagerController()) { // Check if shank exists - DbNcShankModel shank = toolsManager.FindShank(shankId); + DbNcShankModel shank = toolsManager.FindShankWithTools(shankId); if (shank == null) return NotFound(); @@ -386,7 +386,7 @@ namespace Step.Controllers.WebApi using (NcToolManagerController toolsManager = new NcToolManagerController()) { // Check if shank exists - shank = toolsManager.FindShank(shankId.ToolId); + shank = toolsManager.FindShankWithTools(shankId.ToolId); if (shank == null) return BadRequest(API_ERROR_KEYS.INCORRECT_PARAMETERS); @@ -406,17 +406,13 @@ namespace Step.Controllers.WebApi { // Get Configuration ncHandler.Connect(); - CmsError cmsError = ncHandler.GetMagazineConfiguration(out List magConfig); - if (cmsError.IsError()) - return BadRequest(cmsError.localizationKey); + // Check if shank can fit - cmsError = ncHandler.CheckIfShankCanFit( + CmsError cmsError = ncHandler.CheckIfShankCanFit( magazineId, positionId, - shank, - toolsManager.GetMountedTools(), // Get mounted tools - magConfig.Where(x => x.Id == magazineId).FirstOrDefault() // Get only current magazine config + shank // Get mounted tools ); if (cmsError.IsError()) @@ -447,7 +443,7 @@ namespace Step.Controllers.WebApi using (NcToolManagerController toolsManager = new NcToolManagerController()) { // Check if shank exists - shank = toolsManager.FindShank(shankId.ToolId); + shank = toolsManager.FindShankWithTools(shankId.ToolId); if (shank == null) return BadRequest(API_ERROR_KEYS.INCORRECT_PARAMETERS); @@ -481,7 +477,7 @@ namespace Step.Controllers.WebApi using (NcToolManagerController toolsManager = new NcToolManagerController()) { // Check if shank exists - DbNcShankModel shank = toolsManager.FindShank(shankId); + DbNcShankModel shank = toolsManager.FindShankWithTools(shankId); if (shank == null) return BadRequest(API_ERROR_KEYS.INCORRECT_PARAMETERS);