static void Main(string[] args) { /* * */ CommandLine options = new CommandLine(); if (options.Parse(args) == false) { Environment.Exit(1001); return; } if (options.Help == true) { options.HelpShow(); Environment.Exit(1002); return; } _options = options; /* * */ string[] files = Yttrium.Glob.Do(options.FilePatterns.ToArray()); if (files == null || files.Length == 0) { Console.WriteLine("error: no matching files."); Environment.Exit(2); } /* * */ Console.WriteLine("~ mode: '{0}'", _options.Mode); /* * */ Microsoft.Office.Interop.Visio.Application visio = null; bool anyError = false; try { /* * */ ModelExporter exporter = new ModelExporter(); exporter.PageStart += new EventHandler <PageEventArgs>(OnPageStart); exporter.PageEnd += new EventHandler <PageEventArgs>(OnPageEnd); exporter.StepStart += new EventHandler <PageStepEventArgs>(OnStepStart); exporter.StepEnd += new EventHandler <PageStepEventArgs>(OnStepEnd); /* * */ visio = new Microsoft.Office.Interop.Visio.Application(); visio.AlertResponse = 1; visio.Visible = false; foreach (string file in files) { /* * */ Console.Write("+ opening '{0}'...", Path2.RelativePath(file)); FileInfo fileInfo = new FileInfo(file); if (fileInfo.Exists == false) { ConsoleFail(); Console.WriteLine(" - file does not exist"); anyError = true; continue; } /* * Open the document: if the document is not a valid/compatible Visio * document, this will fail. */ Microsoft.Office.Interop.Visio.Document document; try { document = visio.Documents.Open(fileInfo.FullName); ConsoleOk(); } catch (Exception ex) { ConsoleFail(); if (options.Verbose == true) { ConsoleDump(ex); } anyError = true; continue; } /* * Settings */ ModelExportSettings exportSettings = new ModelExportSettings(); exportSettings.Program = "ModelExport"; exportSettings.Mode = options.Mode; exportSettings.Path = options.OutputDirectory ?? fileInfo.DirectoryName; exporter.Settings = exportSettings; /* * Export. */ ModelCommandResult result = null; ModelCommand command = new ModelCommand(); command.Document = document; command.Operation = ModelOperation.ExportAll; if (options.ValidateOnly == true) { command.Operation = ModelOperation.ValidateAll; } try { result = exporter.Execute(command); } catch (Exception ex) { anyError = true; if (options.Verbose == true) { ConsoleDump(ex); } } finally { document.Close(); } if (result == null) { continue; } /* * */ Console.WriteLine(""); Console.WriteLine("execution summary"); Console.WriteLine("-------------------------------------------------------------------------"); foreach (ModelCommandPageResult pageResult in result.Pages) { Console.Write(pageResult.Name); if (pageResult.Success == true) { ConsoleOk(); } else { ConsoleFail(); } foreach (ModelResultItem item in pageResult.Items) { string marker; switch (item.ItemType) { case ModelResultItemType.Error: Console.ForegroundColor = ConsoleColor.DarkRed; marker = "!"; break; case ModelResultItemType.Warning: Console.ForegroundColor = ConsoleColor.DarkYellow; marker = "W"; break; default: marker = " "; break; } if (item.VisioShapeId == null) { Console.WriteLine("{0} [page] {1}#{2}: {3}", marker, item.Actor, item.Code, item.Description); } else { Console.WriteLine("{0} [{1}] {2}#{3}: {4}", marker, item.VisioShapeId, item.Actor, item.Code, item.Description); } Console.ResetColor(); } } } } catch (Exception ex) { ConsoleDump(ex); } finally { // This _needs_ to be performed here: in case an exception is caught, // make sure that Visio is closed or otherwise a zombie process will // be left running on the machine. if (visio != null) { visio.Quit(); } } /* * */ if (anyError == true) { Environment.Exit(3); } else { Environment.Exit(0); } }
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Workers * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ /// <summary> /// Executes a document/page command. /// </summary> /// <param name="command">Which command to execute.</param> /// <param name="mode">Which validation mode to use.</param> private static void CommandExecute(ModelCommand command, ValidationMode mode) { #region Validations if (command == null) { throw new ArgumentNullException(nameof(command)); } #endregion /* * #0. Settings! */ ModelExportSettings exportSettings = new ModelExportSettings(); exportSettings.Program = "VisioAddIn"; exportSettings.Mode = mode; if (string.IsNullOrEmpty(command.Document.Path) == false) { exportSettings.Path = System.IO.Path.GetDirectoryName(command.Document.Path); } /* * #1. Run the exporter */ ProgressForm pform = new ProgressForm(); pform.Command = command; pform.Settings = exportSettings; pform.ShowDialog(); /* * #2. Repaint everything to show that it's ok. * Paint all objects which have errors to red. * * TODO: This should probably be done in an undo unit, so that * the repainting of all of the shapes can be done atomically. */ foreach (ModelCommandPageResult pageResult in pform.CommandResult.Pages) { if (pageResult.Processed == false) { continue; } /* * Paint all black. */ Visio.IVPage page = command.Document.Pages[pageResult.Name]; if (page == null) { continue; } foreach (Visio.IVShape shape in page.Shapes) { Visio.VisDefaultColors orig = VU.ShapeColorGet(shape); if (orig != Visio.VisDefaultColors.visBlack) { VU.ShapeColorSet(shape, Visio.VisDefaultColors.visBlack); } } if (pageResult.Success == true) { continue; } /* * Paint red, only shapes with errors. */ foreach (ModelResultItem item in pageResult.Items) { if (item.ItemType != ModelResultItemType.Error) { continue; } if (item.VisioShapeId == null) { continue; } Visio.IVShape shape = page.Shapes[item.VisioShapeId]; if (shape == null) { continue; } VU.ShapeColorSet(shape, Visio.VisDefaultColors.visRed); } } /* * #3. */ ResultForm rform = new ResultForm(); rform.CommandResult = pform.CommandResult; rform.ShowDialog(); }