public GenericElement(Worksheet sheet, int row, int maxColumn, Dictionary <int, String> columnMap, String NameColumnName) { _tagValues = new Dictionary <string, string>(); for (int column = 1; column < maxColumn; column++) { String tag = columnMap[column]; String value = MmExcelUtil.GetCell(sheet, row, column); if (tag == NameColumnName) { Name = value; } _tagValues.Add(tag, value); } }
/// <summary> /// Read the mappings from the respective excel sheet /// </summary> /// <param name="xlWorkBook">the excel work book</param> /// <param name="mappingList">the resulting list of mappings</param> /// <param name="mappingSheetName"></param> /// <param name="elementColumnName"></param> public static void BuildMapping(Workbook xlWorkBook, List <Mapping> mappingList, String mappingSheetName, String elementColumnName, String mappingColumnName) { var xlWorkSheet = (Worksheet)xlWorkBook.Worksheets[mappingSheetName]; int column = 1; int elementColumnNumber = 0; int mappingColumnNumber = 0; String cellValue; do { cellValue = MmExcelUtil.GetCell(xlWorkSheet, 1, column); if (cellValue == elementColumnName) { elementColumnNumber = column; } if (cellValue == mappingColumnName) { mappingColumnNumber = column; } column++; } while (!String.IsNullOrEmpty(cellValue)); Trace.WriteLine("Mapping element column " + elementColumnNumber + " mapping column " + mappingColumnNumber); int row = 2; if (mappingColumnNumber != 0 && elementColumnNumber != 0) { while (MmExcelUtil.GetCell(xlWorkSheet, row, 1) != "") { var mapping = new Mapping(MmExcelUtil.GetCell(xlWorkSheet, row, elementColumnNumber), MmExcelUtil.GetCell(xlWorkSheet, row, mappingColumnNumber)); // Trace.WriteLine(" adding mapping " + mapping.ElementName + " to " + mapping.MapName); mappingList.Add(mapping); row++; } } Trace.WriteLine("List built " + mappingSheetName + " " + mappingList.Count + " entries"); }
private void decorate_Click(object sender, EventArgs e) { // open log if not yet open var tr2 = new TextWriterTraceListener(File.CreateText(selectedLogFileName.Text)); Trace.Listeners.Add(tr2); // do it with Excel .... // all the excel stuff var connectionList = new List <ShapeConnection>(); // Excel related aspects var xlApp = new Microsoft.Office.Interop.Excel.Application { Visible = true }; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); // open the excel file String excelFileName = connectionsFileName.Text; Workbook xlWorkBook = xlApp.Workbooks.Open(excelFileName, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing); if (excelFileName != "" && File.Exists(excelFileName)) { xlWorkBook = xlApp.Workbooks.Open(excelFileName, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing, _missing); var xlWorkSheet = (Worksheet)xlWorkBook.Sheets[connectionsSheetName.Text]; int sourceElementNameCol = MmExcelUtil.ColumnNumber(xlWorkSheet, sourceElementColumnName.Text); int targetElementNameCol = MmExcelUtil.ColumnNumber(xlWorkSheet, targetElementColumnName.Text); Trace.WriteLine(" source element name column " + sourceElementNameCol); Trace.WriteLine(" target element name column " + targetElementNameCol); int row = 2; bool finished = false; while (!finished) { String sourceElement = MmExcelUtil.GetCell(xlWorkSheet, row, sourceElementNameCol); String targetElement = MmExcelUtil.GetCell(xlWorkSheet, row, targetElementNameCol); finished = sourceElement == "" && targetElement == ""; if (!finished) { connectionList.Add( new ShapeConnection(sourceElement, targetElement)); } row++; } Trace.WriteLine(" connections read from Excel " + connectionList.Count); } // Visio related aspects var visapp = new Microsoft.Office.Interop.Visio.Application(); Document landscape = visapp.Documents.Open(visioFileName.Text); Page page = visapp.ActivePage; Trace.WriteLine("Landscape Masters " + landscape.Masters.Count); foreach (Master m in landscape.Masters) { Trace.WriteLine("VisioStencil Master " + m.Name); } Master connectorMaster = landscape.Masters[connectorStencilName.Text]; var placedMapShapes = new Dictionary <String, ShapeRef>(); MmVisioUtil.GetShapeList(shapeName.Text, page, elementTagName.Text, placedMapShapes); Trace.WriteLine(" shapes in Visio " + placedMapShapes.Count); // filter internal flows for (int con = connectionList.Count - 1; con >= 1; con--) { if (connectionList[con].SourceElement == connectionList[con].TargetElement) { connectionList.RemoveAt(con); } } Trace.WriteLine(" number of cross system connections " + connectionList.Count); if (summarizeCheckBox.Checked) { connectionList.Sort((c1, c2) => String.CompareOrdinal(c1.ConnectionName(), c2.ConnectionName())); for (int con = connectionList.Count - 1; con >= 1; con--) { if (connectionList[con - 1].ConnectionName() == connectionList[con].ConnectionName()) { connectionList[con - 1].Weight = (connectionList[con].Weight + 1); connectionList.RemoveAt(con); } else { Trace.WriteLine(connectionList[con].ConnectionName() + " " + connectionList[con].Weight); } } Trace.WriteLine(" summarized to " + connectionList.Count); } int progressValue = 0; decorationProgressBar.Minimum = 0; decorationProgressBar.Maximum = connectionList.Count - 1; // tweak performance bool pInhibitSelectChange = visapp.InhibitSelectChange; short pDeferRecalc = visapp.DeferRecalc; bool pShowChanges = visapp.ShowChanges; short pScreenUpdating = visapp.ScreenUpdating; int pOnDataChangeDelay = visapp.OnDataChangeDelay; visapp.InhibitSelectChange = true; visapp.DeferRecalc = 1; visapp.ShowChanges = false; visapp.ScreenUpdating = 0; visapp.OnDataChangeDelay = -2; foreach (ShapeConnection con in connectionList) { decorationProgressBar.Value = progressValue++; if (progressValue % 50 == 0) { Trace.Flush(); } ShapeRef source = null; ShapeRef target = null; if (placedMapShapes.ContainsKey(con.SourceElement)) { source = placedMapShapes[con.SourceElement]; } if (placedMapShapes.ContainsKey(con.TargetElement)) { target = placedMapShapes[con.TargetElement]; } if (source != null && target != null) { Shape connector = page.Drop(connectorMaster, 4.50, 4.50); MmVisioUtil.ConnectShapes(source.Shape, target.Shape, connector); // connector.Cells["EndArrow"].Formula = "=5"; } else { Trace.WriteLine(" cannot add connector between " + con.SourceElement + " to " + con.TargetElement); } } Trace.WriteLine(" decorations read from Excel " + connectionList.Count); // Reset Visio properties visapp.InhibitSelectChange = pInhibitSelectChange; visapp.DeferRecalc = pDeferRecalc; visapp.ShowChanges = pShowChanges; visapp.ScreenUpdating = pScreenUpdating; visapp.OnDataChangeDelay = pOnDataChangeDelay; Trace.Flush(); Trace.Close(); }