Esempio n. 1
0
        private string CreateVSDFile(string filename, ref string visioInstall)
        {
            Microsoft.Office.Interop.Visio.Document     doc;
            Microsoft.Office.Interop.Visio.Page         page;
            Microsoft.Office.Interop.Visio.InvisibleApp app = null;
            string extension = ".vsd";
            string filewoext = "";

            try
            {
                if (filename.Length > 0)
                {
                    visioInstall = "";
                    if (CheckVisioIsInstalled() == false)
                    {
                        //ShowInformationMessage("Please install Visio and try again!");
                        visioInstall = "Please install Visio and try again!";
                        return("");
                    }
                    filewoext         = Path.GetFileNameWithoutExtension(filename);
                    ToolAdmin.picture = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(filename));
                    File.Copy(filename, System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext, true);
                    app = new Microsoft.Office.Interop.Visio.InvisibleApp();
                    //if (app == null)
                    //{
                    //    visioInstall = "Please install Visio and try again!";
                    //    return "";
                    //}
                    doc  = app.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext);
                    page = app.ActivePage;
                    page.Export(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext + ".bmp");
                    app.Quit();
                }
                return(System.AppDomain.CurrentDomain.BaseDirectory + "" + filewoext + ".bmp");
            }
            catch (Exception ex)
            {
                try
                {
                    if (app != null)
                    {
                        app.Quit();
                    }
                    throw ex.LogException();
                }
                catch (Exception ex1)
                {
                    return("");

                    throw ex1.LogException();
                }
                throw ex.LogException();
            }
        }
Esempio n. 2
0
        private Boolean SaveFile()
        {
            Microsoft.Office.Interop.Visio.Document     doc;
            Microsoft.Office.Interop.Visio.Page         page;
            Microsoft.Office.Interop.Visio.InvisibleApp app = null;
            try
            {
                if (ToolAdmin.FileType.ToUpper() == "VSD")
                {
                    if (CheckVisioIsInstalled() == false)
                    {
                        Progress.End();
                        ShowInformationMessage("Please install Visio and try again!");
                        return(false);
                    }
                    if (ToolAdmin.MimeType.ToString().IndexOf("application") >= 0)
                    {
                        app = new Microsoft.Office.Interop.Visio.InvisibleApp();
                        //if (app == null)
                        //{
                        //    ShowInformationMessage("Please install Visio and try again!");
                        //    return false;
                        //}
                        doc  = app.Documents.Open(ToolAdmin.File_Name);
                        page = app.ActivePage;
                        page.Export(ToolAdmin.DisplayFile_Name);
                        app.Quit();
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                try
                {
                    app.Quit();
                    return(false);
                }
                catch (Exception ex1)
                {
                    return(false);

                    throw ex1.LogException();
                }
                throw ex.LogException();
            }
        }
Esempio n. 3
0
        private static void ProcessRequest(TcpClient client, int clientNum)
        {
            Microsoft.Office.Interop.Visio.InvisibleApp VisioInst = null;
            try
            {
                // Buffer for reading data
                Byte[] bytes = new Byte[4096];

                StringBuilder data = new StringBuilder();

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while (stream.DataAvailable)
                {
                    i = stream.Read(bytes, 0, bytes.Length);
                    // Translate data bytes to a ASCII string.
                    String str = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    data.Append(str);
                }


                String allData = data.ToString();
                Console.WriteLine("{0} - Received: {1} at {2}", clientNum, allData, DateTime.Now);

                //Now convert these vsd file pathes to vsdx
                Console.WriteLine("{0} - Converting ...", clientNum);

                String[] info = allData.Split('\n');

                if (info.Length < 3)
                {
                    throw new Exception("Invalid number of arguments");
                }

                String[] files = new String[info.Length - 2];
                for (var o = 2; o < info.Length; o++)
                {
                    files[o - 2] = info[o];
                }

                String srcExt = info[0];
                String dstExt = info[1];

                //Using COM to call visio to convert the files
                Type VisioType = Type.GetTypeFromProgID("Visio.InvisibleApp");

                //TODO create a pool of visio instances
                VisioInst = (Microsoft.Office.Interop.Visio.InvisibleApp)Activator.CreateInstance(VisioType);

                foreach (String file in files)
                {
                    if (file.Length > 0)
                    {
                        var doc = VisioInst.Documents.Open(file + srcExt);
                        doc.SaveAs(file + dstExt);
                        doc.Close();
                    }
                }

                VisioInst.Quit();
                VisioInst = null;

                byte[] msg = System.Text.Encoding.ASCII.GetBytes("Done");

                // Send back a response.
                stream.Write(msg, 0, msg.Length);
                Console.WriteLine("{0} - Sent: Done at {1}", clientNum, DateTime.Now);
            }
            catch (SocketException e)
            {
                Console.WriteLine("{0} - {1}: SocketException: {2}", clientNum, DateTime.Now, e);
                //the socket has errors so most probably we cannot send back to the client
            }
            catch (Exception any)
            {
                Console.WriteLine("{0} - {1}: Exception: {2}", clientNum, DateTime.Now, any);

                byte[] msg = System.Text.Encoding.ASCII.GetBytes("Error");

                NetworkStream stream = client.GetStream();
                // Send back error response.
                stream.Write(msg, 0, msg.Length);
            }
            finally
            {
                //Close visio if an unexpected error occured
                if (VisioInst != null)
                {
                    VisioInst.Quit();
                }

                // Shutdown and end connection
                client.Close();
            }
        }
Esempio n. 4
0
        // Start the file conversion
        private void Convert(object sender, DoWorkEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                infoBox.AppendText(Environment.NewLine);
                infoBox.AppendText("------------------------------");
                infoBox.AppendText(Environment.NewLine);
                infoBox.AppendText("Starting conversion process");
                infoBox.AppendText(Environment.NewLine);
                infoBox.AppendText(Environment.NewLine);
            });

            Microsoft.Office.Interop.Visio.InvisibleApp VisioInst = null;

            try
            {
                // Using COM to call visio to convert the files
                Type VisioType = Type.GetTypeFromProgID("Visio.InvisibleApp");

                // Create visio instance to open all the files
                VisioInst = (Microsoft.Office.Interop.Visio.InvisibleApp)Activator.CreateInstance(VisioType);

                // If an Output Folder was selected, the save to that folder
                // Otherwise, save in the same location of the source file
                Func <string, string> SaveLocation = null;
                if (OUTPUT_FILES_PATH == null)
                {
                    SaveLocation = (filePath) => string.Format("{0}x", filePath);
                }
                else
                {
                    SaveLocation = (filePath) => string.Format("{0}\\{1}x", OUTPUT_FILES_PATH, Path.GetFileName(filePath));
                }

                //foreach (String file in VSD_FILES)
                for (int i = 0; i < VSD_FILES.Length; i++)
                {
                    var file = VSD_FILES[i];

                    try
                    {
                        if (file.Length > 0)
                        {
                            string openLocation = Path.GetFullPath(file);

                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                infoBox.AppendText(string.Format(openingText, openLocation));
                                infoBox.AppendText(Environment.NewLine);
                            });

                            // Open .vsd file
                            var    doc          = VisioInst.Documents.Open(openLocation);
                            string saveLocation = SaveLocation(openLocation);

                            // Add wait time to avoid the "The RPC server is unavailable" exception
                            Thread.Sleep(1000);

                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                infoBox.AppendText(string.Format(savingText, saveLocation));
                                infoBox.AppendText(Environment.NewLine);
                            });

                            // Save .vsdx file
                            doc.SaveAs(saveLocation);

                            // Close document
                            doc.Close();

                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                infoBox.AppendText("Done!");
                                infoBox.AppendText(Environment.NewLine);
                                infoBox.AppendText(Environment.NewLine);
                            });
                        }
                    }
                    catch (Exception loopException)
                    {
                        CreateLog(loopException.ToString());
                        String logPath = AppDomain.CurrentDomain.BaseDirectory + "error.log";

                        ShowMessage("An error occurred while while converting this file\nA log file has been created in " + logPath + "\nClick OK to continue.",
                                    "Error",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);

                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            infoBox.AppendText(Environment.NewLine);
                            infoBox.AppendText(string.Format("An error occurred while converting this file, please check the logs."));;
                            infoBox.AppendText(Environment.NewLine);
                            infoBox.AppendText(Environment.NewLine);
                        });
                    }

                    (sender as BackgroundWorker).ReportProgress((i + 1) * 100 / VSD_FILES.Length);
                }

                Application.Current.Dispatcher.Invoke(() =>
                {
                    infoBox.AppendText("All the files were converted!");
                    infoBox.AppendText(Environment.NewLine);
                    infoBox.AppendText("The application can now be closed.");
                    infoBox.AppendText(Environment.NewLine);
                });
            }
            catch (ArgumentNullException)
            {
                ShowMessage("This application requires Visio to make the conversion.",
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);

                Application.Current.Dispatcher.Invoke(() =>
                {
                    infoBox.AppendText(Environment.NewLine);
                    infoBox.AppendText("------------------------------");;
                    infoBox.AppendText(Environment.NewLine);
                    infoBox.AppendText("The conversion process failed.");;
                    infoBox.AppendText(Environment.NewLine);
                });
            }
            catch (Exception exception)
            {
                CreateLog(exception.ToString());
                String logPath = AppDomain.CurrentDomain.BaseDirectory + "error.log";
                ShowMessage("An error occurred while checking the processes\nA log file has been created in " + logPath,
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);

                Application.Current.Dispatcher.Invoke(() =>
                {
                    infoBox.AppendText(Environment.NewLine);
                    infoBox.AppendText("------------------------------");;
                    infoBox.AppendText(Environment.NewLine);
                    infoBox.AppendText("The conversion process failed.");;
                    infoBox.AppendText(Environment.NewLine);
                });
            }
            finally
            {
                // Close visio if an unexpected error occured
                if (VisioInst != null)
                {
                    // Close visio instance
                    VisioInst.Quit();
                    VisioInst = null;
                }
            }

            Application.Current.Dispatcher.Invoke(() =>
            {
                infoBox.AppendText(Environment.NewLine);
                infoBox.AppendText("------------------------------");;
                infoBox.AppendText(Environment.NewLine);
                infoBox.AppendText("All tasks finished");;
                infoBox.AppendText(Environment.NewLine);
            });
        }