public void OpenCellLib() { OpenFileDialog cellFileDialog = new OpenFileDialog() { Filter = Resources.CellLibraryFilter, Title = "选择Cell库文件" }; if (cellFileDialog.ShowDialog() == DialogResult.OK) { BD.DgnDocument cellFileDocument = BD.DgnDocument.CreateForLocalFile(cellFileDialog.FileName); BD.DgnFile cellDgnFile = BD.DgnFile.Create(cellFileDocument, BD.DgnFileOpenMode.ReadOnly).DgnFile; if (cellDgnFile == null) { Prompt = Resources.PromptHeader + $"无法读取{cellFileDialog.FileName}的DgnDocument对象"; Status = Resources.StatusHeader + Resources.ErrorString; return; } BD.StatusInt loadStatusInt; if (BD.DgnFileStatus.Success != cellDgnFile.LoadDgnFile(out loadStatusInt)) { Prompt = Resources.PromptHeader + "无法载入文件"; Status = Resources.StatusHeader + Resources.ErrorString; return; } if (cellDgnFile.FillDictionaryModel() != BD.StatusInt.Success) { Prompt = Resources.PromptHeader + "填充模型失败"; Status = Resources.StatusHeader + Resources.ErrorString; return; } CellNameTypes.Clear(); ElementProps.Clear(); int index = 0; foreach (var modelindex in cellDgnFile.GetModelIndexCollection()) { BD.DgnModel model = cellDgnFile.LoadRootModelById(out loadStatusInt, modelindex.Id); if (model != null && modelindex.CellPlacementOptions == BD.CellPlacementOptions.CanBePlacedAsCell) { CellNameTypes.Add(model.ModelName + "(" + model.GetModelInfo().CellType.ToString() + ")"); index++; } } string filename; if (CellFunction.AttachLibrary(out filename, cellFileDialog.FileName, "") != BD.StatusInt.Success) { Prompt = Resources.PromptHeader + "附加模型失败"; Status = Resources.StatusHeader + Resources.ErrorString; return; } Prompt = Resources.PromptHeader + $"{cellFileDialog.SafeFileName}已载入!"; Status = Resources.StatusHeader + Resources.SuccessString; } }
public void ImportFromFile() { OpenFileDialog excelOpenFileDialog = new OpenFileDialog() { Filter = Resources.ExcelFilter, Title = "选择输入Excel文件" }; if (excelOpenFileDialog.ShowDialog() == DialogResult.OK) { try { using (var package = new ExcelPackage(new FileInfo(excelOpenFileDialog.FileName))) { var sheet = package.Workbook.Worksheets[1]; var data = sheet .Extract <ElementProp>() .WithProperty(p => p.CellName, "A") .WithProperty(p => p.X, "B") .WithProperty(p => p.Y, "C") .WithProperty(p => p.Z, "D") .WithProperty(p => p.AngelX, "E") .WithProperty(p => p.AngelY, "F") .WithProperty(p => p.AngelZ, "G") .GetData(2, row => row != sheet.Dimension.Rows + 1) .ToList(); if (data.Count == 0) { Prompt = Resources.PromptHeader + "文件内容为空"; Status = Resources.StatusHeader + Resources.ErrorString; return; } var cellNameDictonary = CellNameTypes.ToDictionary(p => p.Split('(').First()); ElementProps.Clear(); PutCellProgress = 0; double uor = Program.GetActiveDgnModel().GetModelInfo().UorPerMaster; foreach (var elementProp in data) { if (cellNameDictonary.ContainsKey(elementProp.CellName)) { ElementProps.Add(new ElementProp() { CellName = cellNameDictonary[elementProp.CellName], X = elementProp.X * uor, Y = elementProp.Y * uor, Z = elementProp.Z * uor, AngelX = elementProp.AngelX, AngelY = elementProp.AngelY, AngelZ = elementProp.AngelZ }); } } } Prompt = Resources.PromptHeader + $"{excelOpenFileDialog.FileName}载入成功"; Status = Resources.StatusHeader + Resources.SuccessString; } catch (Exception e) { Prompt = Resources.PromptHeader + e.ToString(); Status = Resources.StatusHeader + "文件解析出错"; ElementProps.Clear(); } } }