public List <Model.LoopModel> CreateLoops <T>(int loopsAmount, int deviceAmount, Model.ControllerModel controller, IDeviceService <T> deviceService) { string strMachineNumber = controller.MachineNumber; ControllerOperationCommon controllerBase = new ControllerOperationCommon(); int loopID = controllerBase.GetMaxLoopID(); string strLoopLengthFormat = "#"; for (int i = 0; i < controller.LoopAddressLength; i++) { strLoopLengthFormat += "0"; } List <Model.LoopModel> lstLoopModel = new List <LoopModel>(); for (int i = 0; i < loopsAmount; i++) //创建回路 { LoopModel loop = new LoopModel(); loop.Controller = controller; loop.ID = loopID++; loop.Code = strMachineNumber + i.ToString(strLoopLengthFormat); loop.Name = strMachineNumber + i.ToString(strLoopLengthFormat); loop.DeviceAmount = deviceAmount; loop.SetDevices <T>(deviceService.InitializeDevices(deviceAmount)); lstLoopModel.Add(loop); loop.IsLoopDataDirty = true; // SetDataDirty(); } return(lstLoopModel); }
public bool AddLoops(LoopModel loop, string machineNumber, int loopsAmount) { try { ControllerOperationCommon controllerBase = new ControllerOperationCommon(); int loopID = controllerBase.GetMaxLoopID(); //ControllerModel controller = ProjectManager.GetInstance.GetPrimaryController(); int[] loopsCode = GetAllLoopCode(_controller, null); int currentMaxLoopCode = loopsCode.Max(); IControllerConfig config = ControllerConfigManager.GetConfigObject(_controller.Type); short allowMaxLoopValue = config.GetMaxLoopAmountValue(); if (loopsAmount > allowMaxLoopValue) { //超出最大数回数 return(false); } if ((allowMaxLoopValue - currentMaxLoopCode) < loopsAmount) { //剩余回路空间无法创建指定回路数量的回路 return(false); } string strLoopLengthFormat = "#"; for (int i = 0; i < _controller.LoopAddressLength; i++) { strLoopLengthFormat += "0"; } for (int i = 1; i <= loopsAmount; i++) { LoopModel l = new LoopModel(); l.ID = ++loopID; l.Code = _controller.MachineNumber + (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); l.Name = _controller.MachineNumber + (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); //l.Code = (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); //l.Name = loop.Name+i.ToString(); l.DeviceAmount = loop.DeviceAmount; l.Controller = _controller; _controller.Loops.Add(l); // SetDataDirty(); } } catch { return(false); } return(true); }
/// <summary> /// 生成EXCEL文件摘要信息页 /// </summary> /// <param name="excelService"></param> /// <param name="sheetNames"></param> /// <returns></returns> protected bool GenerateExcelSummarySheet(ProjectModel project, ref IExcelService excelService, out List <string> sheetNames) { List <string> lstSheetNames = new List <string>(); try { List <MergeCellRange> lstMergeCellRange = new List <MergeCellRange>(); MergeCellRange mergeCellRange = new MergeCellRange(); mergeCellRange.FirstRowIndex = 0; mergeCellRange.LastRowIndex = 0; mergeCellRange.FirstColumnIndex = 0; mergeCellRange.LastColumnIndex = 2; lstMergeCellRange.Add(mergeCellRange); const int Loop_Amount_Per_Sheet = 8; ControllerManager controllerManager = new ControllerManager(); controllerManager.InitializeAllControllerOperation(null); List <SummaryInfo> lstSummaryInfo = new List <SummaryInfo>(); lstSheetNames.Add("项目摘要"); foreach (var controller in project.Controllers) { IControllerOperation controllerOperation = controllerManager.GetController(controller.Type); //取控制器摘要信息逻辑 SummaryInfo controllerSummary = controllerOperation.GetSummaryNodes(controller, 2); controllerSummary.Name += "-机号:" + controller.MachineNumber; lstSummaryInfo.Add(controllerSummary); int loopSheetAmount = Convert.ToInt32(Math.Ceiling((float)controller.Loops.Count / Loop_Amount_Per_Sheet)); for (int i = 0; i < loopSheetAmount; i++) { lstSheetNames.Add(controller.Name + "-回路分组" + (i + 1).ToString()); } if (controller.StandardConfig.Count > 0) { lstSheetNames.Add(controller.Name + "-标准组态"); } if (controller.MixedConfig.Count > 0) { lstSheetNames.Add(controller.Name + "-混合组态"); } if (controller.GeneralConfig.Count > 0) { lstSheetNames.Add(controller.Name + "-通用组态"); } if (controller.ControlBoard.Count > 0) { lstSheetNames.Add(controller.Name + "-网络手动盘"); } } excelService.CreateExcelSheets(lstSheetNames); ControllerOperationCommon controllerOperator = new ControllerOperationCommon(); controllerOperator.SetDefaultExcelStyle(ref excelService); //取得默认EXCEL样式 excelService.RowHeight = (short)20; //到下一个高度设置前,使用该高度 excelService.SetCellValue(lstSheetNames[0], 0, 0, project.Name, CellStyleType.Caption); excelService.RowHeight = (short)15; int startRowIndex = 1; //开始行 int endRowIndex = 0; //结束行 foreach (var info in lstSummaryInfo) { endRowIndex++; //info.name格式:summary.Name = "控制器:控制器名(控制器类型,器件长度)-机号:001"; startRowIndex = endRowIndex; string strControllerName = ""; string strControllerType = ""; string strControllerDeviceAddressLength = ""; string strControllerMachineNumber = ""; int startIndex = info.Name.IndexOf(':') + 1; int endIndex = info.Name.IndexOf('['); if (startIndex < endIndex) { strControllerName = info.Name.Substring(startIndex, endIndex - startIndex); } startIndex = info.Name.IndexOf('[') + 1; endIndex = info.Name.IndexOf(','); if (startIndex < endIndex) { strControllerType = info.Name.Substring(startIndex, endIndex - startIndex); } startIndex = info.Name.IndexOf(',') + 1; endIndex = info.Name.IndexOf(']'); if (startIndex < endIndex) { strControllerDeviceAddressLength = info.Name.Substring(startIndex, endIndex - startIndex); } startIndex = info.Name.LastIndexOf(':') + 1; if (startIndex != -1) { strControllerMachineNumber = info.Name.Substring(startIndex); } excelService.SetCellValue(lstSheetNames[0], endRowIndex, 0, strControllerName, CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 1, "类型", CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 2, strControllerType, CellStyleType.Data); endRowIndex++; excelService.SetCellValue(lstSheetNames[0], endRowIndex, 0, null, CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 1, "机号", CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 2, strControllerMachineNumber, CellStyleType.Data); endRowIndex++; excelService.SetCellValue(lstSheetNames[0], endRowIndex, 0, null, CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 1, "器件长度", CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 2, strControllerDeviceAddressLength + "位", CellStyleType.Data); for (int i = 0; i < info.ChildNodes.Count; i++) { if (info.ChildNodes[i].Name != "设备类型") { endRowIndex++; excelService.SetCellValue(lstSheetNames[0], endRowIndex, 0, null, CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 1, info.ChildNodes[i].Name + "数量", CellStyleType.Data); excelService.SetCellValue(lstSheetNames[0], endRowIndex, 2, info.ChildNodes[i].Number.ToString(), CellStyleType.Data); } } mergeCellRange = new MergeCellRange(); mergeCellRange.FirstRowIndex = startRowIndex; mergeCellRange.LastRowIndex = endRowIndex; mergeCellRange.FirstColumnIndex = 0; mergeCellRange.LastColumnIndex = 0; lstMergeCellRange.Add(mergeCellRange); } excelService.SetColumnWidth(lstSheetNames[0], 1, 15f); excelService.SetMergeCells(lstSheetNames[0], lstMergeCellRange);//设置"摘要信息"合并单元格 sheetNames = lstSheetNames; } catch (Exception ex) { sheetNames = lstSheetNames; return(false); } return(true); }
public bool AddLoops(LoopModel loop, string machineNumber, int loopsAmount) { try { ControllerOperationCommon controllerBase = new ControllerOperationCommon(); int loopID = controllerBase.GetMaxLoopID(); //ControllerModel controller = ProjectManager.GetInstance.GetPrimaryController(); int[] loopsCode = GetAllLoopCode(_controller, null); int currentMaxLoopCode = loopsCode.Max(); int specifiedLoopCode = 0; //指定的回路号 bool loopCodeExistFlag = false; //回路号已经存在标志 //if (loop.Code != "") //{ // specifiedLoopCode=Convert.ToInt32(loop.Code); // if (specifiedLoopCode > currentMaxLoopCode) // { // currentMaxLoopCode = specifiedLoopCode; // } //} for (int i = 0; i < loopsCode.Length; i++) { if (loopsCode[i] == specifiedLoopCode) { loopCodeExistFlag = true; break; } } IControllerConfig config = ControllerConfigManager.GetConfigObject(_controller.Type); short allowMaxLoopValue = config.GetMaxLoopAmountValue(); if (loopsAmount == 0) { return(false); } if (loopsAmount > allowMaxLoopValue) { //超出最大数回数 return(false); } if ((allowMaxLoopValue - currentMaxLoopCode) < loopsAmount) { //剩余回路空间无法创建指定回路数量的回路 return(false); } string strLoopLengthFormat = "#"; for (int i = 0; i < _controller.LoopAddressLength; i++) { strLoopLengthFormat += "0"; } for (int i = 1; i <= loopsAmount; i++) { LoopModel l = new LoopModel(); l.ID = ++loopID; if (!loopCodeExistFlag) { if (i == 1) //减少复杂性,只对指定的回路号使用一次 { l.Code = _controller.MachineNumber + loop.Code.ToString().PadLeft(_controller.LoopAddressLength, '0'); } else { l.Code = _controller.MachineNumber + (currentMaxLoopCode + i - 1).ToString().PadLeft(_controller.LoopAddressLength, '0'); } } else { l.Code = _controller.MachineNumber + (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); } l.Name = loop.Name + "(" + l.Code + ")";// _controller.MachineNumber + (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); //l.Code = (currentMaxLoopCode + i).ToString().PadLeft(_controller.LoopAddressLength, '0'); //l.Name = loop.Name+i.ToString(); l.DeviceAmount = loop.DeviceAmount; l.Controller = _controller; l.ControllerID = _controller.ID; InitializeDevicesToLoop(_controller.Type, l); _controller.Loops.Add(l); // SetDataDirty(); } } catch { return(false); } return(true); }