Beispiel #1
0
        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            bool streamToOutput = !string.IsNullOrEmpty(Options.OutputFile) || Options.OutputOnStdOut;
            if (Options.NoGUI || Options.RequestHelp || streamToOutput)
            {
                //if(!Options.IsRunningOnMono)
                //{
                //    if (GetConsoleWindow() == IntPtr.Zero)
                //        ; // AllocConsole();
                //}

                if (Options.RequestHelp || string.IsNullOrEmpty(Options.InputFile) || !streamToOutput ||
                    Options.RootElements.Count == 0 || Options.ExpandLevel < 0 || Options.Zoom < 10.0 || Options.Zoom > 1000.0)
                {
                    string version = typeof(Program).Assembly.GetName().Version.ToString();
                    Log(usage, version, Path.GetFileName(Environment.GetCommandLineArgs()[0]));

                    return;
                }

                Log("Loading the file: {0}\n", Options.InputFile);

                Schema schema = new Schema();
                schema.RequestCredential += delegate(string url, string realm, int attemptCount, out string username, out string password)
                {
                    username = password = "";
                    if(!string.IsNullOrEmpty(Options.Username))
                    {
                        if (attemptCount > 1)
                            return false;
                        username = Options.Username;
                        password = Options.Password;
                        return true;
                    }
                    return false;
                };

                schema.LoadSchema(Options.InputFile);

                if (schema.LoadError.Count > 0)
                {
                    LogError("There are errors while loading:\n");
                    foreach (var error in schema.LoadError)
                    {
                        LogError(error);
                    }
                    LogError("\r\n");
                }

                Diagram diagram = new Diagram();
                diagram.ShowDocumentation = Options.ShowDocumentation;
                diagram.ElementsByName = schema.ElementsByName;
                diagram.Scale = Options.Zoom / 100.0f;

                foreach (var rootElement in Options.RootElements)
                {
                    string elementName = rootElement;
                    string elementNamespace = null;
                    if(!string.IsNullOrEmpty(elementName))
                    {
                        var pos = rootElement.IndexOf("@");
                        if(pos != -1)
                        {
                            elementName = rootElement.Substring(0, pos);
                            elementNamespace = rootElement.Substring(pos + 1);
                        }
                    }

                    foreach (var element in schema.Elements)
                    {
                        if ((elementNamespace != null && elementNamespace == element.NameSpace && element.Name == elementName) ||
                            (elementNamespace == null && element.Name == elementName))
                        {
                            Log("Adding '{0}' element to the diagram...\n", rootElement);
                            diagram.Add(element.Tag, element.NameSpace);
                        }
                    }
                }
                Form form = new Form();
                Graphics graphics = form.CreateGraphics();
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                for (int i = 0; i < Options.ExpandLevel; i++)
                {
                    Log("Expanding to level {0}...\n", i + 1);
                    diagram.ExpandOneLevel();
                }
                diagram.Layout(graphics);
                Log("Saving image...\n");
                try
                {
                    bool result = false;

                    DiagramExporter exporter = new DiagramExporter(diagram);
                    IDictionary<string, object> specificRendererParameters = new Dictionary<string, object>()
                            {
                                { "TextOutputFields", Options.TextOutputFields }
                                //For future parameters, {}
                            };
                    if (Options.OutputOnStdOut)
                    {
                        Stream stream = Console.OpenStandardOutput();
                        result = exporter.Export(stream, "." + Options.OutputOnStdOutExtension.ToLower(), graphics, new DiagramAlertHandler(ByPassSaveAlert), specificRendererParameters);
                        stream.Flush();
                    }
                    else
                    {
                        result = exporter.Export(Options.OutputFile, graphics, new DiagramAlertHandler(SaveAlert), specificRendererParameters);
                    }

                    if (result)
                        Log("The diagram is now saved in the file: {0}\n", Options.OutputFile);
                    else
                        Log("ERROR: The diagram has not been saved!\n");
                }
                catch (Exception ex)
                {
                    Log("ERROR: The diagram has not been saved. {0}\n", ex.Message);
                }

                graphics.Dispose();
                form.Dispose();
            }
            else
            {
                if (Options.RequestHelp)
                {
                    string version = typeof(Program).Assembly.GetName().Version.ToString();
                    MessageBox.Show(string.Format(usage, version, Environment.GetCommandLineArgs()[0]));
                }

                Application.ThreadException += HandleThreadException;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
Beispiel #2
0
 private void saveDiagramToolStripMenuItem_Click(object sender, EventArgs e)
 {
     SaveFileDialog saveFileDialog = new SaveFileDialog();
     saveFileDialog.Filter = "SVG files (*.svg)|*.svg" + (Options.IsRunningOnMono ? "" : "|EMF files (*.emf)|*.emf") + "|PNG files (*.png)|*.png|JPG files (*.jpg)|*.jpg|TXT files (*.txt)|*.txt|CSV files (*.csv)|*.csv|All files (*.*)|*.*";
     saveFileDialog.FilterIndex = 1;
     saveFileDialog.RestoreDirectory = true;
     if (saveFileDialog.ShowDialog() == DialogResult.OK)
     {
         string outputFilename = saveFileDialog.FileName;
         try
         {
             DiagramExporter exporter = new DiagramExporter(diagram);
             Graphics g1 = this.panelDiagram.DiagramControl.CreateGraphics();
             exporter.Export(outputFilename, g1, new DiagramAlertHandler(SaveAlert), new Dictionary<string, object>()
                     {
                         { "TextOutputFields", Options.TextOutputFields }
                         //For future parameters, {}
                     });
             g1.Dispose();
         }
         catch (System.ArgumentException ex)
         {
             MessageBox.Show("You have reach the system limit.\r\nPlease remove some element from the diagram to make it smaller.");
             System.Diagnostics.Trace.WriteLine(ex.ToString());
         }
         catch (System.Runtime.InteropServices.ExternalException ex)
         {
             MessageBox.Show("You have reach the system limit.\r\nPlease remove some element from the diagram to make it smaller.");
             System.Diagnostics.Trace.WriteLine(ex.ToString());
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
             System.Diagnostics.Trace.WriteLine(ex.ToString());
         }
     }
 }